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