【问题标题】:Indicator error - must use (max_bars_back) <Pinescript>指标错误 - 必须使用 (max_bars_back) <Pinescript>
【发布时间】:2021-10-25 12:20:00
【问题描述】:

//@version=4 在“for 循环”代码段 "for i = index_cross to 50"(第 22 - 24 行)中,我正在寻找 rsi[i] &lt; oversold 值。我有“max_bars_back=50”,但脚本一直给我指示错误:Pine cannot determine the referencing lenght of a series. Try using max_bars_back in the study or strategy function.

1   study("error max bars back", overlay=false, max_bars_back=50)
2
3   //input
4   oversold = input(30, title="oversold")
5   rsiLen   = input(14, title="rsi len")
6
7   //rsi and ema
8   rsi = rsi(close, rsiLen)
9   ema1 = ema(close, 21)
10  ema2 = ema(close,50)
11
12  //condition 1
13  cross=crossover(ema1, ema2)
14
15  //index of condition (used in the for loop)
16  index_cross = 0
17  if cross
18      index_cross := bar_index
19
20  //condition 2
21  bool rsi_valid = false
22  for i = index_cross to 50
23      if rsi[i] < oversold 
24          rsi_valid := true
25
26  //final condition and plot
27  a = rsi_valid == true ? 1 : 0
28  plot(a, color=color.white)

我使用此脚本的目标是确定“从交叉到 50 柱之前”rsi 是否小于 30(超卖)。 感谢您的支持。

【问题讨论】:

    标签: pine-script


    【解决方案1】:

    您实际上不需要 for 循环。您可以使用不同的算法。

    您可以只使用计数器并在 rsi 超卖时将其递增,否则将其重置。因此,对于每根 rsi 超卖的柱,您可以使用counter = counter + 1,如果在任何时候它变得超买,只需counter = 0

    当您的“交叉”发生时,查看您的计数器是否 > 50。这意味着在最后 50 根柱线中 rsi 超卖。

    算法:

    var oversoldCnt = 0     // A counter for consecutive rsi oversold bars
    oversoldCnt := iff(rsi < oversold, oversoldCnt + 1, 0)  // Increase the counter if rsi is still oversold, reset otherwise
    

    连同你的代码:

    //@version=4
    study("error max bars back", overlay=false, max_bars_back=50)
    
    //input
    oversold = input(30, title="oversold")
    rsiLen   = input(14, title="rsi len")
    
    //rsi and ema
    rsi = rsi(close, rsiLen)
    ema1 = ema(close, 21)
    ema2 = ema(close,50)
    
    var oversoldCnt = 0     // A counter for consecutive rsi oversold bars
    oversoldCnt := iff(rsi < oversold, oversoldCnt + 1, 0)  // Increase the counter if rsi is still oversold, reset otherwise
    
    //condition 1
    cross=crossover(ema1, ema2)
    bool rsi_valid = cross and (oversoldCnt > 50)
    
    //final condition and plot
    a = rsi_valid == true ? 1 : 0
    plot(a, color=color.white)
    

    我简化了你的代码,所以整个事情可能无法如你所愿,但我希望算法是清晰的。如果您有任何问题,请告诉我。

    【讨论】:

    • 非常感谢!
    • 但是,有了这个新代码,我注意到绘制我的变量“a”,它始终保持在 0 的值,它永远不会改变他的值 1 所以我认为脚本无法读取当最终条件发生时。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-21
    • 2015-06-22
    • 2018-08-07
    • 1970-01-01
    • 2018-10-03
    • 2020-02-06
    相关资源
    最近更新 更多