首页 > 解决方案 > 在两个先前计算的指标之间找到最高的 bar_index

问题描述

我正在尝试实施分形以找到支撑位和阻力位。我可以使用以下代码找到“支持”分形:

//@version=4
study("Fractals v1", 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)

//======================= Support levels =========================================================================================================================================================================

var previous_lows = array.new_int(3)

support_at_depth_2() =>
    a = low[0]
    b = low[1]
    c = low[2] // mid point - we are checking if this low is the lowest in the set 0-4
    d = low[3] 
    e = low[4]
    
    leftSide = c <= b and c <= a ? true : false
    rightSide = c <= d and c <= e ? true : false
    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] // mid point - we are checking if this low is the lowest in the set 0-6
    e = low[4]
    f = low[5]
    g = low[6]
    leftSide = d <= c and d <= b and d <= a ? true : false
    rightSide = d <= e and d <= f and d <= g ? true : false
    result = leftSide and rightSide
    add_to_array(previous_lows, bar_index[3])
    result // return bool


support = offset == 2 ? support_at_depth_2() : support_at_depth_3()

plotshape(support, style=shape.triangleup, location=location.belowbar, color=color.olive, size = size.small, offset=-offset)

绘制在交易视图图表上: 在此处输入图像描述


我已经为“阻力”分形实现了相同的逻辑,但这有时会产生我不想要的结果——例如两个“阻力分形”并排出现。

我想要实现的是一个模式

...并不是:

所以添加的代码是:

var previous_highs = array.new_int(3)

resistance_at_depth_2() =>
    a = high[0]
    b = high[1]
    c = high[2] // mid point - we are checking if this low is the lowest in the set 0-4
    d = high[3] 
    e = high[4]
    
    leftSide = c >= b and c >= a ? true : false
    rightSide = c >= d and c >= e ? true : false
    result = leftSide and rightSide
    add_to_array(previous_highs, bar_index[2])
    result


resistance_at_depth_3() =>
    a = high[0]
    b = high[1]
    c = high[2]
    d = high[3] // mid point - we are checking if this low is the lowest in the set 0-6
    e = high[4]
    f = high[5]
    g = high[6]
    leftSide = d >= c and d >= b and d >= a ? true : false
    rightSide = d >= e and d >= f and d >= g ? true : false
    result = leftSide and rightSide
    add_to_array(previous_highs, bar_index[3])
    result // return bool


resistance = offset == 2 ? resistance_at_depth_2() : resistance_at_depth_3()

plotshape(resistance, style=shape.triangledown, location=location.abovebar, color=color.red, size = size.small, offset=-offset)

正如我所说,这有时会导致我正在寻找的模式不正确,如下图所示:
在此处输入图像描述


我对如何解决这个模式问题的想法是简单地在它的 bar_index 处找到两个“支撑”分形(绿色向上箭头)和 plotshape() 之间的最高点。我曾尝试(但收效甚微)实现类似于我的想法但无济于事的东西。

?-我可以抓住当前支撑和先前支撑之间的时间段,并在该范围内的柱上循环,然后尝试抓住最高点highbar_index

我是 pine 脚本的新手,并且在每个栏上使用其他编程语言(Python/JavaScript)我会将栏的详细信息保存到 dict/object 中,然后将它们推送到数组中。然后,我将遍历数组以查找每个支持级别(使用上述分形方法)并在适用时将 support = true 添加到每个字典/对象,然后我可以再次遍历列表/数组以填充每个支持之间的最高点 = =真。使用 pine 脚本,我觉得这些选项(目前)有点有限,我必须找到 pine 脚本的“细微差别”并解决问题。

我很感激对此(以及一般的 pine 脚本)的任何指示或帮助,或者即使这可以使用 pine 脚本中更简单的方法来实现。

提前致谢。

标签: pine-scriptalgorithmic-trading

解决方案


我认为您需要使用一个额外的浮点数组来存储枢轴价格以及柱索引,并使用 avar bool来跟踪发生的最后一个枢轴是“阻力”还是“支撑”。

例如,如果您获得连续的阻力支点,则可以将添加到阵列的最后阻力与新阻力支点进行比较,如果新阻力支点更高,则使用array.set修改最近的阵列元素而不是向阵列添加新支点。

如果新的阻力支点较低,您将不会向阵列添加任何内容,只保留前一个支点。

如果之前的事件是支撑支点,则无论先前存储的阻力值如何,您都将向阻力阵列添加一个新的阻力支点。

您还需要使用标签数组代替,plotshape()以便您可以删除或移动先前的阻力枢轴标记标签,然后在较旧的阻力枢轴后面跟着一个连续的更高枢轴。


推荐阅读