松树脚本中的RSI乒乓球指示器

时间:2020-05-21 13:12:06

标签: pine-script

我想使用“ RSI-Ping-Pong-Indicator”作为在其他条件下进行回测的先决条件。 为此,我定义了一个RSI和一个RSI的长和短区域,例如rsi(25)=长,而rsi(75)=空。

rsiShortLevel = input(75, title="RSI Short Level")
rsiLongLevel = input(25, title="RSI Long Level")

如果RSI(crossunder())落在长区域(rsi <25)以下,则指示器应切换为“长”。指示器在下一次短区域(rsi> 75)之前一直保持“多头”,直到RSI达到(crossover()),然后指示器才切换到“短”。反之亦然,例如“ RSI乒乓球”。在一次长穿越与下一次短穿越之间,RSI可以多次穿越长水平,并且可以在此“中间区域”内移动很长时间,但是它将保持“长线”,直到RSI穿越为止短级别。

然后,我可以将带有pingpong条件的交叉条件绘制为蓝线。为了在Tradingview中更好地视觉理解,以下long-short条件显示了“ long”的长期价值(rsiLongLevel = 25)和短期价值(rsiShortLevel = 75)的“空头”:

///RSI Crossings
rsiLongCondition = crossunder(rsi, rsiLongLevel)
rsiShortCondition = crossover(rsi, rsiShortLevel)

///RSI Ping-Pong
pingpong = if(rsiLongCondition)
    rsiLongLevel
else
    if(rsiShortCondition)
        rsiShortLevel

plot(showPingpong ? pingpong : na, color=color.blue, style=plot.style_line, linewidth=2, title='RSI Ping-Pong')

但是,很明显25或75的值仅在实际交叉口输出,而不在交叉口之间输出:在交叉口之间,值为na

现在,我要实现长距离交叉之后的值继续输出值(此处为25,又称“长”),直到交叉了短区域,然后值变为75(又称为“短”)。

如果我使用技巧并且将“ strategy.entry”和“ strategy.position_size”带入游戏并将它们链接到iff条件,则可以显示此行为:

if(rsiLongCondition)
    strategy.entry(id="LE-RSI-Cross", long=true, qty=1)
if(rsiShortCondition)
    strategy.entry(id="SE-RSI-Cross", long=false, qty=1)

pingpongPositionSize = iff(strategy.position_size > 0, rsiLongLevel, iff(strategy.position_size < 0, rsiShortLevel, na))

如果我绘制变量pingpongPositionSize(黑线)并设置background-color-condition(红色=短,绿色=长),则指示器会正确显示相应的值和背景色:

plot(showPingpongSize ? pingpongPositionSize : na, color=color.black, style=plot.style_line, linewidth=4, title='RSI-Position-Size Ping-Pong')
bgcolor(showBGSize and strategy.position_size > 0 ? color.green : showBGSize and strategy.position_size < 0 ? color.red : na, transp=70)



我的问题:如何构建所需的指标,使其行为与“ strategy.position_size”技巧相同?

完整脚本

/@version=4
strategy("RSI Ping-Pong", overlay=false)

// GENERAL SETTINGS

rsiLenght = input(14, title="RSI Lenght")
rsiShortLevel = input(75, title="RSI Short Level")
rsiLongLevel = input(25, title="RSI Long Level")
showBGCrossings = input(false, title="Show RSI-Crossings with bgcolor?")
showPingpong = input(true, title="Show RSI Ping-Pong?")
showPingpongSize = input(true, title="Show RSI-Position-Size Ping-Pong?")
showBGSize = input(true, title="Show RSI Position Size Changes with bgcolor?")


// CALCULATIONS
rsi = rsi(close, rsiLenght)

///RSI Crossings
rsiLongCondition = crossunder(rsi, rsiLongLevel)
rsiShortCondition = crossover(rsi, rsiShortLevel)

///RSI Ping-Pong
pingpong = if(rsiLongCondition)
    rsiLongLevel
else
    if(rsiShortCondition)
        rsiShortLevel

/// RSI Ping-Pong with Strategy Position Size
if(rsiLongCondition)
    strategy.entry(id="LE-RSI-Cross", long=true, qty=1)
if(rsiShortCondition)
    strategy.entry(id="SE-RSI-Cross", long=false, qty=1)

pingpongPositionSize = iff(strategy.position_size > 0, rsiLongLevel, iff(strategy.position_size < 0, rsiShortLevel, na))

// PLOTTING
plot(rsi, color=color.purple, style=plot.style_line, linewidth=2, title='RSI')
plot(showPingpong ? pingpong : na, color=color.blue, style=plot.style_line, linewidth=2, title='RSI Ping-Pong')
bgcolor(showBGCrossings and rsiLongCondition ? color.green : rsiShortCondition ? color.red : na, transp=0)
plot(showPingpongSize ? pingpongPositionSize : na, color=color.black, style=plot.style_line, linewidth=4, title='RSI-Position-Size Ping-Pong')
bgcolor(showBGSize and strategy.position_size > 0  ? color.green : showBGSize and strategy.position_size < 0 ? color.red : na, transp=70)
hlong = hline(rsiLongLevel, title="RSI Long Level")
hshort = hline(rsiShortLevel, title="RSI Short Level")

1 个答案:

答案 0 :(得分:0)

使用:

///RSI Ping-Pong
var pingpong = 0.
if(rsiLongCondition)
    pingpong := rsiLongLevel
else
    if(rsiShortCondition)
        pingpong := rsiShortLevel

var初始化仅在第一个小节上初始化变量,并且其值仅在交叉时更改,因此其值在更改后在小节中传播。 参见the usrman on the subject

enter image description here