我想使用 Pinescript 在 Tradingview 上设置回测策略。我想使用 SMA 和 RSI 作为指标。如何编写脚本?
Posted
技术标签:
【中文标题】我想使用 Pinescript 在 Tradingview 上设置回测策略。我想使用 SMA 和 RSI 作为指标。如何编写脚本?【英文标题】:I would like to set up a backtest strategy on Tradingview using Pinescript. I would like to use SMA and RSI as indicators. How to write the script? 【发布时间】:2021-06-21 23:25:12 【问题描述】:我想使用 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)
【问题讨论】:
使用这个例子pinecoders.com/faq_and_code/… 【参考方案1】://@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", type = input.integer, minval = 1, maxval = 31)
fromYear = input(defval = 2007, title = "From Year", type = input.integer, minval = 1970)
thruMonth = input(defval = 3, title = "Thru Month", type = input.integer, minval = 1, maxval = 12)
thruDay = input(defval = 31, title = "Thru Day", type = input.integer, minval = 1, maxval = 31)
thruYear = input(defval = 2009, title = "Thru Year", type = input.integer, 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/
【讨论】:
请描述您面临错误的地方!以上是关于我想使用 Pinescript 在 Tradingview 上设置回测策略。我想使用 SMA 和 RSI 作为指标。如何编写脚本?的主要内容,如果未能解决你的问题,请参考以下文章