0

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

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

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

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 回撤工具及其完整源代码可在此处找到。上面的代码片段是我到目前为止添加的。

4

1 回答 1

1

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

您可以轻松检查修改代码以processLevel()返回值r并绘制返回值。

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)

在此处输入图像描述

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

crossFibGoldenLvls = low < pl and barstate.isconfirmed
于 2021-11-15T09:15:55.940 回答