简体   繁体   English

Tradingview 松脚本回测 rsi

[英]Tradingview pine-script backtesting rsi

I'm back-testing and RSI(6) strategy.我正在回测和 RSI(6) 策略。 I want it to buy on the SECOND CROSSING of the 30 oversold line(When it is going back up) and Sell on the SECOND CROSSING of the 70 oversold line(when it is going down).我希望它在 30 超卖线的第二个交叉点(当它回升时)买入并在 70 超卖线的第二个交叉点(当它下降时)卖出。 I can only get it to buy and sell when it immediately crosses the overbought/oversold line which is not why I'm trying to do.只有当它立即越过超买/超卖线时,我才能让它买卖,这不是我试图这样做的原因。 I'm also trying to add a 10% stop loss.我也在尝试添加 10% 的止损。 Does anyone have any ideas how to implement those orders?有谁知道如何执行这些命令? This is my code so far.到目前为止,这是我的代码。

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)

This shows debugging plots so you can see the logic unfolding:这显示了调试图,因此您可以看到逻辑展开:

//@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)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM