// Options that configure the backtest date range startDate = input(title="Start Date", type=input.time, defval=timestamp("01 Jan 1970 00:00")) endDate = input(title="End Date", type=input.time, defval=timestamp("31 Dec 2170 23:59"))
// CALCULATIONS:
// Use the built-in function to calculate two EMA lines fastEMA = ema(close, emaFast) slowEMA = ema(close, emaSlow)
// PLOT:
// Draw the EMA lines on the chart plot(series=fastEMA, color=color.orange, linewidth=2) plot(series=slowEMA, color=color.blue, linewidth=2)
// CONDITIONS:
// Check if the close time of the current bar falls inside the date range inDateRange = (time >= startDate) and (time < endDate)
// Translate input into trading conditions longOK = (tradeDirection == "Long") or (tradeDirection == "Both") shortOK = (tradeDirection == "Short") or (tradeDirection == "Both")
// Decide if we should go long or short using the built-in functions longCondition = crossover(fastEMA, slowEMA) shortCondition = crossunder(fastEMA, slowEMA)
// ORDERS:
// Submit entry (or reverse) orders if (longCondition and inDateRange) strategy.entry(id="long", long=true, when = longOK) if (shortCondition and inDateRange) strategy.entry(id="short", long=false, when = shortOK)
// Submit exit orders in the cases where we trade only long or only short if (strategy.position_size > 0 and shortCondition) strategy.exit(id="exit long", stop=close) if (strategy.position_size < 0 and longCondition) strategy.exit(id="exit short", stop=close)
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.