我正在重新测试和RSI(6)策略。我希望它在30超卖线的第二个交叉点买入(当它回升时),在70超卖线的第二个交叉点卖出(当它下跌时)。只有当它立即突破超买/超卖线时,我才能让它买入和卖出,这并不是我试图这样做的原因。我也在尝试增加10%的止损。有没有人知道如何执行这些命令?到目前为止,这是我的代码。
strategy("RSI", overlay = true)
longcon = rsi(close, 6) < 30
closecon = rsi(close,6) > 70
//backtest from 2015
_year = 2015
strategy.entry("long", strategy.long, when = longcon and year >= _year)
strategy.close("long", when = closecon)
plot(close)发布于 2021-05-11 16:35:35
这显示了调试图,因此您可以看到逻辑的展开:
//@version=4
strategy("RSI")
r = rsi(close, 6)
_year = 2015
bool xUp = crossover(r, 30)
bool xDn = crossunder(r, 70)
var bool firstXUp = false
var bool firstXDn = false
var bool inLong = false
var bool inShort = false
var float longStop = na
if xUp
firstXDn := false
if firstXUp
inLong := true
inShort := false
firstXUp := false
else if not inLong
firstXUp := true
else if xDn
firstXUp := false
if firstXDn
inLong := false
inShort := true
firstXDn := false
else if inLong
firstXDn := true
enterLong = inLong and not inLong[1]
exitLong = not inLong and inLong[1]
longStop := enterLong ? close * 0.9 : exitLong ? na : longStop
strategy.entry("long", strategy.long, when = enterLong and year >= _year)
strategy.close("long", when = exitLong, comment = "xDn")
strategy.close("long", when = close < longStop, comment = "Stop")
plot(r)
hline(30)
hline(70)
// Debugging
plotchar(xUp, "xUp", "•", location.bottom, size = size.tiny)
plotchar(xDn, "xDn", "•", location.top, size = size.tiny)
plotchar(firstXUp, "firstXUp", "1", location.bottom, size = size.tiny)
plotchar(firstXDn, "firstXDn", "1", location.top, size = size.tiny)
plotchar(longStop, "longStop", "", location.top, size = size.tiny)
bgcolor(inLong ? color.green : an)

https://stackoverflow.com/questions/67478594
复制相似问题