松脚本FOR循环无法正常工作

时间:2019-06-28 17:06:49

标签: pine-script

我的代码旨在查找具有RSI输入宽度的峰和谷并将其连接。我知道它在FOR循环中有一些东西,因为我将所有这些都逐行地在FOR循环外运行,并且它完全按预期运行。有任何想法吗?我在做错什么吗?

    //@version=4
    study(title="RSI Peaks and Valleys FOR test",shorttitle="RSI P&V", overlay = false)
    int width1= input(6,minval=1,maxval=200,title="Width") 
    int p1 = 0
    int v1 = 0

    float rsiMain = rsi(close,14)
    float rsi_compare = 0.0
    float rsi_width1 = rsi(close[width1], 14)

    for i = 0 to ((width1 * 2))
        rsi_compare = rsi(close[i], 14)
        p1 := p1 + ((rsi_compare < rsi_width1) ? 1 : 0)
        v1 := v1 + ((rsi_compare > rsi_width1) ? 1 : 0)

    float tp1 = iff(p1 == (2 * width1), rsi_width1, na)
    float tv1 = iff(v1 == (2 * width1), rsi_width1, na)

    plot(tp1, title="Test1", color=color.yellow, offset = -width1)
    plot(tv1, title="Test2", color=color.silver, offset = -width1)

    plot(rsiMain, title="RSI", style=plot.style_line, linewidth=2, color=color.purple)

编辑:我决定添加没有FOR循环的代码。对我而言,该过程看起来相同,因此期望得到相同的结果,而令我感到惊讶的是,FOR循环的结果不符合预期。

    //@version=4
    study(title="RSI Peaks and Valleys manual FOR test",shorttitle="RSI P&V", overlay = false)
    int width1= input(6,minval=1,maxval=200,title="Width") 
    int p1 = 0
    int v1 = 0

    float rsiMain = rsi(close,14)
    float rsi_compare = 0.0
    float rsi_width1 = rsi(close[width1], 14)

    rsi0 = rsi(close[0], 14)
    rsi1 = rsi(close[1], 14)
    rsi2 = rsi(close[2], 14)
    rsi3 = rsi(close[3], 14)
    rsi4 = rsi(close[4], 14)
    rsi5 = rsi(close[5], 14)
    rsi6 = rsi(close[6], 14)

    p1 := p1 + ((rsi0 < rsi_width1) ? 1 : 0)
    p1 := p1 + ((rsi1 < rsi_width1) ? 1 : 0)
    p1 := p1 + ((rsi2 < rsi_width1) ? 1 : 0)
    p1 := p1 + ((rsi3 < rsi_width1) ? 1 : 0)
    p1 := p1 + ((rsi4 < rsi_width1) ? 1 : 0)
    p1 := p1 + ((rsi5 < rsi_width1) ? 1 : 0)
    p1 := p1 + ((rsi6 < rsi_width1) ? 1 : 0)

    v1 := v1 + ((rsi0 > rsi_width1) ? 1 : 0)
    v1 := v1 + ((rsi1 > rsi_width1) ? 1 : 0)
    v1 := v1 + ((rsi2 > rsi_width1) ? 1 : 0)
    v1 := v1 + ((rsi3 > rsi_width1) ? 1 : 0)
    v1 := v1 + ((rsi4 > rsi_width1) ? 1 : 0)
    v1 := v1 + ((rsi5 > rsi_width1) ? 1 : 0)
    v1 := v1 + ((rsi6 > rsi_width1) ? 1 : 0)

    float tp1 = iff(p1 == (2 * width1), rsi_width1, na)
    float tv1 = iff(v1 == (2 * width1), rsi_width1, na)

    plot(tp1, title="Test1", color=color.yellow, offset = -width1)
    plot(tv1, title="Test2", color=color.silver, offset = -width1)

    plot(rsiMain, title="RSI", style=plot.style_line, linewidth=2, color=color.purple)

0 个答案:

没有答案