【问题标题】:Pine Script stateless function implementationPine Script 无状态函数实现
【发布时间】:2020-05-04 08:26:35
【问题描述】:

在 tradingview 的 pine-script 中,我试图找到高于最后收盘价的前一个分形 (HF1),并绘制 HF1 的水平

到目前为止,我的代码并不能正常工作:

//@version=4
strategy("Fractal Breakout Strategy", overlay=true)

HF = bar_index>5 and high[2]>high[1] and high[2]>high and high[2]>high[3] and high[2]>high[4] ? -1 : 0
LF = bar_index>5 and low[2]<low[1] and low[2]<low and low[2]<low[3] and low[2]<low[4] ? 1 : 0

tot = HF + LF
pl = abs(tot)>=1 ? 1 : 0

//Plot fractal arrows:
plotarrow(pl==1 ? tot : na, colorup=color.teal, colordown=color.orange,offset=-2,maxheight=15)

HF1 = 0.0

for i = 0 to 40
    HF1_temp = valuewhen(HF, high[2],i)
    if (HF1_temp > close[1])
        HF1 := HF1_temp
        break

plot(HF1, color = color.blue)

我怀疑这与文档 here 中的最后一段有关

为避免这种情况,您可以使用自己的无状态函数实现。 这是具有相同行为的内置函数列表:

sma(source, length):长度是有状态的。

ema(source, length):长度是有状态的。

sum(source, length):长度是有状态的。

valuewhen(条件、来源、发生):发生是有状态的。

rsi(x, y):当 y 是整数类型并且表现得像一个长度时,y 是有状态的。

..但无法真正弄清楚如何实现这一点...任何帮助将不胜感激,谢谢!

【问题讨论】:

    标签: pine-script


    【解决方案1】:

    你的怀疑是正确的。需要进行测试,但认为这样做可以:

    //@version=4
    strategy("Fractal Breakout Strategy", overlay=true)
    
    ofst = 2
    HF = bar_index>5 and high[2]>high[1] and high[2]>high and high[2]>high[3] and high[2]>high[4] ? -1 : 0
    LF = bar_index>5 and low[2]<low[1] and low[2]<low and low[2]<low[3] and low[2]<low[4] ? 1 : 0
    // Save fractal's hi/lo when we find one, offset `ofst` bars later.
    //      This could also be done in the 2 lines above but didn't want to change them 
    //      in case you will need their -1 and 1 values elsewhere in your code.
    HFval = HF != 0 ? high[ofst] : na
    LFval = LF != 0 ? low[ofst] : na
    
    tot = HF + LF
    pl = abs(tot)>=1 ? 1 : 0
    
    //Plot fractal arrows:
    plotarrow(pl==1 ? tot : na, colorup=color.teal, colordown=color.orange,offset=-ofst,maxheight=15)
    
    float HF1 = na
    for i = 0 to 40
        if (HFval[i] > close[1])
            HF1 := HFval[i]
            break
    
    // Plotting circles eliminates the inelegant joins between non `na` values.
    plot(HF1, "HF1", color.blue, 2, plot.style_circles)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-06
      • 2021-04-08
      • 1970-01-01
      • 2021-12-15
      • 1970-01-01
      • 1970-01-01
      • 2021-07-09
      • 2023-01-09
      相关资源
      最近更新 更多