微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我想使用 Pinescript 在 Tradingview 上设置回测策略我想使用 SMA 和 RSI 作为指标剧本怎么写?

如何解决我想使用 Pinescript 在 Tradingview 上设置回测策略我想使用 SMA 和 RSI 作为指标剧本怎么写?

我想使用 Pinescript 在 Tradingview 上设置回测策略。我想使用 SMA 和 RSI 作为指标。我希望开始日期为 2007 年 1 月 1 日,结束日期为测试期的 2009 年 3 月 31 日。有人可以写一个脚本吗?这是我目前拥有的,但它似乎没有过滤掉测试期的开始和结束日期。

//@version=4
strategy("My Strategy",overlay=true)

// Indicators
SMA50 = sma(close,50)
SMA100 = sma(close,100)
rsi = rsi(close,14)
atr = atr(14)

// Crossover conditions 
longCondition = crossover(SMA50,SMA100)

if (longCondition)
    stopLoss = low - atr * 2
    takeProfit = high + atr * 6
    strategy.entry("long",strategy.long,100,when = rsi > 30)
    strategy.exit ("exit","long",stop=stopLoss,limit=takeProfit)
    
    
// Plotting SMAs in the chart.
plot(SMA50)
plot(SMA100,color=color.black)

解决方法

//@version=4
strategy("My Strategy",overlay=true)

// Indicators
SMA50 = sma(close,50)
SMA100 = sma(close,100)
rsi = rsi(close,14)
atr = atr(14)


// === BACKTEST RANGE ===
fromMonth = input(defval = 1,title = "From Month",type = input.integer,minval = 1,maxval = 12)
fromDay   = input(defval = 1,title = "From Day",maxval = 31)
fromYear  = input(defval = 2007,title = "From Year",minval = 1970)
thruMonth = input(defval = 3,title = "Thru Month",maxval = 12)
thruDay   = input(defval = 31,title = "Thru Day",maxval = 31)
thruYear  = input(defval = 2009,title = "Thru Year",minval = 1970)


// === FUNCTION ===
start     = timestamp(fromYear,fromMonth,fromDay,00,00)        // backtest start window
finish    = timestamp(thruYear,thruMonth,thruDay,23,59)        // backtest finish window
window()  => time >= start and time <= finish ? true : false  

// Crossover conditions 
longCondition = crossover(SMA50,SMA100)

if (longCondition)
    stopLoss = low - atr * 2
    takeProfit = high + atr * 6
    strategy.entry("long",strategy.long,100,when = rsi > 30 and window())
    strategy.exit ("exit","long",stop=stopLoss,limit=takeProfit)
    
if not window ()
    strategy.close ("long")
    
// Plotting SMAs in the chart.
plot(SMA50)
plot(SMA100,color=color.black)

希望对您有所帮助,在此链接中您有更多信息:

https://www.tradingview.com/script/62hUcP6O-How-To-Set-Backtest-Date-Range/

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。