Pine脚本使用以前的RSI交叉点作为新交叉的参考

时间:2020-05-31 18:32:59

标签: pine-script

我一般都不熟悉Pine Script和编码,所以这可能是一个新手问题:/ 有什么方法可以保存/注明过去RSI交叉点的百分比水平,以查看当前交叉点是高于还是低于以前的交叉点?

谢谢!

//@version=4


study(title="K-D Crossover30may")

Length = input(14, minval=1, title="Stochastic Length")
RSILength = input(9, minval=1, title="RSI Length")
smoothk = input(3, minval=1, title="Stochastic %K")
smoothd = input(4, minval=1, title="Stochastic %D")
RSI = rsi(close, RSILength)


BuyRangeLow = input(1, title="Buy Range Low")
BuyRangeHigh = input(34, title="Buy Range High")
SellRangeLow = input(68, title="Sell Range Low")
SellRangeHigh= input(99, title="Sell Range High")
hline(BuyRangeLow, title="Buy Range Low", color=color.green)
hline(BuyRangeHigh, title="Buy Range High", color=color.green)
hline(SellRangeLow, title="Sell Range Low", color=color.red)
hline(SellRangeHigh, title="Sell Range High", color=color.red)

stoch = stoch(RSI, RSI, RSI, Length)

K =sma(stoch, smoothk)
D =sma(K, smoothd)

plot(K, title="%K", color=color.red, linewidth=2)
plot(D, title="%D", color=color.yellow, linewidth=2)

CrossedUp = crossover(K, D) and D >6 and D <= BuyRangeHigh
CrossedDown = crossunder(K, D) and D >= SellRangeLow

bgcolor(CrossedUp ? color.green : na, transp=50)
bgcolor(CrossedDown ? color.red : na, transp=50)

var a = valuewhen(CrossedUp, D, open, 0)
var b = valuewhen(CrossedUp, D, open, 1)
var c = valuewhen(CrossedUp, D, open, 2)

if (var a > var b) 
      if (var b > var c) 
        bgcolor=color.blue 

1 个答案:

答案 0 :(得分:0)

您的脚本对我来说看起来不错,并且应该按照您的描述运行。其中只有几个小错误。

首先,valuewhen使用三个参数,而不是四个。第二个参数是一个序列,用于在条件为真时读取条上的值。我相信您的情况应该是DK

第二,bgcolor是一个函数,不要给它赋值,只需调用它即可:

a = valuewhen(CrossedUp, D, 0)
b = valuewhen(CrossedUp, D, 1)
c = valuewhen(CrossedUp, D, 2)

bgcolor(a > b and b > c ? color.blue : na, transp=50)

希望这会有所帮助。