PineScript 'ta.lowest' 和 'ta.highest' 在 'if' 条件下显示错误值

问题描述 投票:0回答:2

我有以下 PineScript 来检查 2 个或更多连续蜡烛的 RSI 值是否在

if
条件中指定的范围内

//@version=5
indicator("RSICondition", overlay=true, max_bars_back = 20)

RSI = ta.rsi(close, 14)

varip int ToPlot = 0                            //0 means nothing to plot, 1 means to plot RED-BOX
varip int left = 0                              //Pointer for the left edge of the box
varip float highestHigh = na                    //Declaring Global Variable
varip float lowestLow = na                      //Declaring Global Variable

if (RSI >= 58 and RSI <= 62)
    left := left + 1
    if (left > 1)                               //If we have consecutive candles within 58 & 62, eg. lets take 3 consecutive candles
        highestHigh := ta.highest(high, left)   //Calculating the Highest High from the 3 candles
        lowestLow := ta.lowest(low, left)       //Calculating the Lowest Low from the 3 candles
        ToPlot := 1                             //Specifying RED color box to plot

else
    if (ToPlot == 1)                            //When the candle after 3 consecutive candles within specified range comes, time to plot RED box for those 3 candles
        box.new(bar_index[left], highestHigh, bar_index[1], lowestLow, border_color=color.red, bgcolor=color.new(color.red, 80))
    ToPlot := 0                                 //Reset ToPlot to 0 after plotting specifying there is nothing to plot now
    left := 0                                   //Reset the pointer after box plotted

编译器对

ta.lowest
ta.highest
都显示以下警告:
The function '<function_name>' should be called on each calculation for consistency. It is recommended to extract the call from this scope
并且创建的框显示
highestHigh
lowestLow
变量的不正确值,因为它们是为对象的顶部和底部边缘指定的。分别为盒子。

我可以在

if
语句之外指定这些函数,但有时
left
变量的值也会在抛出错误的地方变成 0
length for ta.highest and ta.lowest cannot be 0

pine-script pine-script-v5 trading tradingview-api
2个回答
1
投票

您必须在 if 条件之外评估

ta.highest()

highestHighTmp = ta.highest(high, math.max(1, left)) // should "left" be 0 you don't use it anyway
lowestLowTmp = ta.lowest(low, math.max(1, left))
if (RSI >= 58 and RSI <= 62)
    left := left + 1
    if (left > 1)
        highestHigh := highestHighTmp
        lowestLow := lowestLowTmp
        ToPlot := 1                     
...

0
投票

我也有同样的问题。我的 ta.high/lowest 函数不符合条件。长度是一个变量,并且值是错误的。当 length 是静态时,这些函数是可以的,但当它是一个变量时,这些函数就不行了......:/

© www.soinside.com 2019 - 2024. All rights reserved.