You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pine Script计算两个支撑分形间最高K线的bar_index实现方法

Pine Script 分形交替逻辑实现方案

核心原理说明

Pine Script 内置了大量序列处理函数,不需要手动遍历存储所有K线数据,完全可以实现你需要的区间极值查找逻辑,以下提供两种可直接落地的实现方案。


方案1:轻量过滤连续同类型分形

仅需新增3行代码即可解决连续阻力/支撑的问题,逻辑是校验当前分形和上一次生效分形的类型,同类型直接拦截:

// 原来的代码不变,新增以下变量
var last_type = "" // 取值 support / resistance
valid_support = support and last_type != "support"
valid_resistance = resistance and last_type != "resistance"

// 触发有效信号时更新上一次类型
if valid_support
    last_type := "support"
if valid_resistance
    last_type := "resistance"

// 替换原来的plotshape,用valid_xxx作为触发条件
plotshape(valid_support, style=shape.triangleup, location=location.belowbar, color=color.olive, size = size.small, offset=-offset)
plotshape(valid_resistance, style=shape.triangledown, location=location.abovebar, color=color.red, size = size.small, offset=-offset)

方案2:相邻支撑间取最高点作为阻力(完全匹配你的需求)

这种方案不会依赖原生阻力分形的判定,直接取两个相邻支撑之间的最高高点作为唯一阻力,从根源上避免连续阻力的问题:

//@version=4
study("Fractals v2", overlay=true, max_bars_back=300)

var offset = input(defval=2, title='depth', type=input.integer, minval=2, maxval=3)

add_to_array(arr, val) =>
    array.shift(arr)
    array.push(arr, val)

// 支撑分形逻辑不变
var previous_lows = array.new_int(3)
support_at_depth_2() =>
    a = low[0]
    b = low[1]
    c = low[2]
    d = low[3] 
    e = low[4]
    leftSide = c <= b and c <= a
    rightSide = c <= d and c <= e
    result = leftSide and rightSide
    add_to_array(previous_lows, bar_index[2])
    result
support_at_depth_3() =>
    a = low[0]
    b = low[1]
    c = low[2]
    d = low[3]
    e = low[4]
    f = low[5]
    g = low[6]
    leftSide = d <= c and d <= b and d <= a
    rightSide = d <= e and d <= f and d <= g
    result = leftSide and rightSide
    add_to_array(previous_lows, bar_index[3])
    result
support = offset == 2 ? support_at_depth_2() : support_at_depth_3()

// ------------------- 新增相邻支撑区间找阻力逻辑 -------------------
var resistance_points = array.new_int() // 存储阻力位的bar索引
var last_support_bar = na

// 当触发支撑时计算区间最高点
if support
    current_support_bar = bar_index[offset]
    if not na(last_support_bar)
        // 计算两个支撑之间的K线数量
        interval = current_support_bar - last_support_bar
        // 找区间内最高高点的偏移量(相对于当前bar)
        highest_offset = ta.highestbars(high, interval)
        // 存储最高点的bar索引
        highest_bar = bar_index + highest_offset
        array.push(resistance_points, highest_bar)
    // 更新上一个支撑的bar索引
    last_support_bar := current_support_bar

// 判定当前bar是否是阻力位
is_resistance = array.includes(resistance_points, bar_index)

// 绘制信号
plotshape(support, style=shape.triangleup, location=location.belowbar, color=color.olive, size = size.small, offset=-offset)
plotshape(is_resistance, style=shape.triangledown, location=location.abovebar, color=color.red, size = size.small)

关键语法说明

  • ta.valuewhen(condition, source, n):取第n次满足condition时的source值,n=0是最近一次,n=1是上一次
  • ta.highestbars(source, length):返回过去length个bar中source最大值对应的偏移量,最大值在当前bar则返回0,在前一个bar返回-1,以此类推
  • array.includes(arr, val):判断数组中是否存在指定值,用来匹配当前bar是否是之前计算出的阻力位

内容的提问来源于stack exchange,提问作者DevStormUK

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.20 10:24:04