获取低于当前条低的第一个栏的bar_index

发布于 2025-01-29 10:37:45 字数 955 浏览 0 评论 0原文

为了找到分歧,我正在尝试获得第一个栏的bar_index,该栏的低点低于当前栏的低点。

我希望我可以将当​​前的条值注入ta.valuewhen()的条件字段,但这是不可能的。

我可以为循环使用,但这是地狱般的慢速,在所有蜡烛上迭代,这不是必需的,因为我想检查第一个较低的枢轴,而不是所有蜡烛都是枢轴。

使用ta.valuewhen(条件,源,出现)在循环内部,并在出现字段中迭代脚本本身。

例如:

prevPivLL  = na
prevPivLLI = na

// Looking back 5 pivot lows for a lower low than current low
for i = 0 to 5  
    prevPivotLo = ta.valuewhen(na(pivotLo), pivotLo, i)
    prevPivLL  := pivotLo > prevPivotLo ? pivotLo : na
    prevPivLLI := bar_index - ta.valuewhen(na(pivotLo), bar_index, i)

那么,还有什么呢?有更好的方法吗?

我可能已经看到所有其他寻求差异的脚本,看看当前的低点是否高于前面的脚本。问题是,当前低点可以是先前的枢轴,但仍然是较高的枢轴的枢轴。我想检查第一个下部枢轴,即使两者之间有更高的枢轴。之后,我停止看(因为那时它变得无关紧要)。

顺便说一句,也是如此。

这与大多数脚本所做的事情有点倒退,但可能更准确。

To find divergences I'm trying to get the bar_index of the first bar that has a lower low than the low of the current bar.

enter image description here

I wish I could inject current bar values into the condition field of ta.valuewhen() but this is not possible.

I could use a for loop, but this is slow as hell and iterating over ALL candles, which is not necessary because I want to check the first lower pivot and not all candles are pivots.

Using ta.valuewhen(condition, source, occurrence) inside a for loop and iterate with the occurrence field is discouraged by the script itself.

Eg:

prevPivLL  = na
prevPivLLI = na

// Looking back 5 pivot lows for a lower low than current low
for i = 0 to 5  
    prevPivotLo = ta.valuewhen(na(pivotLo), pivotLo, i)
    prevPivLL  := pivotLo > prevPivotLo ? pivotLo : na
    prevPivLLI := bar_index - ta.valuewhen(na(pivotLo), bar_index, i)

So, what else then? Is there a better approach to this?

I have seen probably all other scripts that look for divergence, look if the current low is higher than the previous one. The thing is that the current low can be a lower one with the previous pivot, but still a higher one with a earlier pivot. I want to check against the first lower pivot, even if there are higher ones in between. After that, i stop looking (because it becomes irrelevant then).

Same goes for close btw.

This is kinda backwards from what most scripts do, but probably way more accurate.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

将军与妓 2025-02-05 10:37:45

您可以在发生时使用数组来存储枢轴及其相应的条形指数。然后,您只需要迭代一小部分枢轴值即可进行评估。

但是,您需要限制历史搜索,枢轴的数量和/或通过条形回溯限制。

//@version=5
indicator("Last pivot low index", overlay = true)

lb_limit = input.int(100, title = "bar lookback limit")

piv_limit = input.int(5, title = "number of pivots limit")

var float[] piv_l_price = array.new_float()
var int[] piv_l_index = array.new_int()

bool pivl = low > low[1] and low[1] < low[2]

if pivl
    array.unshift(piv_l_price, low[1])
    array.unshift(piv_l_index, bar_index[1])

if array.size(piv_l_price) > piv_limit
    array.pop(piv_l_price)
    array.pop(piv_l_index)

size = array.size(piv_l_price)

bool found_piv_l = false
float last_piv_l_price = na
int last_piv_l_index = na

if size > 0
    for i = 0 to size - 1
        temp_piv_l_price = array.get(piv_l_price, i)
        temp_piv_l_index = array.get(piv_l_index, i)
        if temp_piv_l_price < low and temp_piv_l_index > bar_index - lb_limit
            found_piv_l := true
            last_piv_l_price := temp_piv_l_price
            last_piv_l_index := temp_piv_l_index
            break

var line piv_l_line = line.new(x1 = na, y1 = na, x2 = na, y2 = na, color = color.teal)

if found_piv_l
    line.set_xy1(piv_l_line, x = last_piv_l_index, y = last_piv_l_price)
    line.set_xy2(piv_l_line, x = bar_index, y = low)

You can use arrays to store the pivots and their corresponding bar indices as they occur. Then you only need to iterate through a small array of pivot values to do the evaluation.

You'll need to limit the historical search though, by the number of pivots and/or by a bar lookback limit.

//@version=5
indicator("Last pivot low index", overlay = true)

lb_limit = input.int(100, title = "bar lookback limit")

piv_limit = input.int(5, title = "number of pivots limit")

var float[] piv_l_price = array.new_float()
var int[] piv_l_index = array.new_int()

bool pivl = low > low[1] and low[1] < low[2]

if pivl
    array.unshift(piv_l_price, low[1])
    array.unshift(piv_l_index, bar_index[1])

if array.size(piv_l_price) > piv_limit
    array.pop(piv_l_price)
    array.pop(piv_l_index)

size = array.size(piv_l_price)

bool found_piv_l = false
float last_piv_l_price = na
int last_piv_l_index = na

if size > 0
    for i = 0 to size - 1
        temp_piv_l_price = array.get(piv_l_price, i)
        temp_piv_l_index = array.get(piv_l_index, i)
        if temp_piv_l_price < low and temp_piv_l_index > bar_index - lb_limit
            found_piv_l := true
            last_piv_l_price := temp_piv_l_price
            last_piv_l_index := temp_piv_l_index
            break

var line piv_l_line = line.new(x1 = na, y1 = na, x2 = na, y2 = na, color = color.teal)

if found_piv_l
    line.set_xy1(piv_l_line, x = last_piv_l_index, y = last_piv_l_price)
    line.set_xy2(piv_l_line, x = bar_index, y = low)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文