如何使用 crossunder() 方法检测价格与 pinescript 中的斐波那契水平相互作用的位置?

How to use crossunder() method to detect where price is interacting with Fibonacci levels in pinescript?

我对编写 pinescript 相当陌生,我正在尝试编写一个自定义警报,如果价格低于斐波那契黄金水平(0.382、0.50 或 0.618)并且柱状图状态得到确认,它将 return true 并触发警报。

仅供参考,我正在分析和构建 TradingView 的内置自动斐波拉契回撤指标脚本以用作警报。我在尝试检索要比较的斐波那契值时遇到困难。我注意到在使用自动斐波回撤工具时,它绘制了斐波那契水平(十进制格式)和价格,我想知道如何检索它以比较价格触及其中水平的位置(如果这是正确的方法)是吗?

代码片段(我从内置指示器添加的警报代码):

crossFibGoldenLvls = low < value_0_382 and barstate.isconfirmed // if the 0.382 level price is greater than the current bar low AND bar state is confirmed; thus returns the bar’s final price
// if price cross down thru any of these golden levels (0.382, 0.50, 0.618) AND then 2 green consecutive candles close higher than previous bar close, return true
if crossFibGoldenLvls
    alert("Price has crossed a fib golden level.", alert.freq_once_per_bar_close)
    //alert("Price has crossed a fib golden level:" + tostring(processLevel(show_1, value_1, color_1)), alert.freq_once_per_bar_close)
// else if price cross down thru any of these levels (0.786, 1), return false
// …

//plot(series=processLevel(show_0_382, value_0_382, color_0_382), title="fib level 0.382")
//plot(series=processLevel(value_0_382), title="fib level 0.382")
plotchar(show_0_382, title="show_0_382")
plotchar(value_0_382, title="value_0_382")
//plotchar(value, title="value")
//plotchar(m, title="m")
//plotchar(r, title="r")
plotchar(diff, title="diff")
plotchar(startPrice, title="startPrice")
plotchar(endPrice, title="endPrice")
//plotchar(price, title="price")
//plotchar(crossFibGoldenLvls, title="crossFibGoldenLvls")

可以找到 Tradingview 的自动 fib 回撤工具 here 及其完整源代码。上面的代码片段是我到目前为止添加的内容。

value_0_382 实际上保持斐波那契水平 (0.382)。您要查找的内容在 processLevel() 函数中计算并命名为 r.

您可以轻松地检查修改代码,使 processLevel() return 是 r 的值并绘制 returned 值。

processLevel(show, value, colorL) =>
    float m = value
    r = startPrice + diff * m
    if show
        _draw_line(r, colorL)
        _draw_label(r, _label_txt(m, r), colorL)
        if _crossing_level(close, r)
            alert("Autofib: " + syminfo.ticker + " crossing level " + tostring(value))
    r

pl = processLevel(show_0_382, value_0_382, color_0_382)
plot(pl)

然后在您的条件下使用此 return 值:

crossFibGoldenLvls = low < pl and barstate.isconfirmed