如何解决在两个先前计算的指标之间找到最高的 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,offset=-offset)
我对如何解决这个模式问题的想法是简单地在 bar_index 处找到两个“支撑”分形(绿色向上箭头)和 plotshape() 之间的最高点。我曾尝试(但收效甚微)实现与我的想法类似的东西,但尚未成功。
?- 我能否抓住当前支撑位和前一个支撑位之间的时间段并循环遍历该范围内的柱线,然后尝试抓住最高的 high
并且它是 bar_index
?
我是 pine 脚本和其他编程语言 (Python/JavaScript) 的新手,我会将条形图的详细信息保存到 dict/object 中,然后将它们推送到数组中。然后我将遍历数组找到每个支持级别(使用上述分形方法)并在适用时向每个字典/对象添加 support = true,然后我可以再次遍历列表/数组以填充每个支持之间的最高高度 = = 真的。对于 pine 脚本,我觉得这些选项(目前)有点有限,我必须找到 pine 脚本的“细微差别”和解决方法。
我感谢任何关于此的指针或帮助(以及一般的 pine 脚本),或者即使这可以使用 pine 脚本中更简单的方法来实现。
提前致谢。
解决方法
我认为您需要使用额外的浮点数组来存储枢轴价格以及柱线索引,并使用 var bool
来跟踪发生的最后一个枢轴是“阻力”还是“支撑”。
例如,如果您获得连续的阻力支点,则可以将添加到阵列的最后一个阻力与新的阻力支点进行比较,如果新阻力支点更高,则使用 array.set
修改最近的阵列元素而不是向数组添加一个新的枢轴。
如果新的阻力支点较低,则不会向阵列添加任何内容,仅保留先前的支点。
如果之前的事件是支撑支点,则无论之前存储的阻力值如何,您都会向阻力阵列添加一个新的阻力支点。
您还需要使用标签数组而不是 plotshape()
,以便您可以删除或移动之前的阻力轴心标记标签,当旧阻力轴心之后是连续的更高轴心时。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。