The core idea of this strategy is to implement a framework that can flexibly select the backtest date range to meet different needs of users, so that they can automatically or manually set the start and end times for backtest.
The strategy provides four options for date range selection through input parameters: using all history data, recent specified days, recent specified weeks or manually specifying a date range. The strategy will dynamically set the backtest window based on the selected date range, while keeping the trading logic unchanged, so that the performance difference under different time windows can be compared.
The strategy consists of two modules: backtest date range selection and double MA trading strategy.
As a flexible and customizable framework for date range selection, the advantages are meeting different test needs of users. Combined with simple but effective double MA trading logic, it can quickly verify and compare strategies. Follow-up optimizations like adding filters or stop loss logic can make the strategy more practical for live trading. In summary, the strategy framework has good scalability and reference value.
/*backtest start: 2022-12-29 00:00:00 end: 2024-01-04 00:00:00 period: 1d basePeriod: 1h exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}] */ //@version=4 strategy(title = "How To Auto Set Date Range", shorttitle = " ", overlay = true) // Revision: 1 // Author: @allanster // === INPUT MA === fastMA = input(defval = 14, title = "FastMA", type = input.integer, minval = 1, step = 1) slowMA = input(defval = 28, title = "SlowMA", type = input.integer, minval = 1, step = 1) // === INPUT BACKTEST RANGE === useRange = input(defval = "WEEKS", title = "Date Range", type = input.string, confirm = false, options = ["ALL", "DAYS", "WEEKS", "MANUAL"]) nDaysOrWeeks = input(defval = 52, title = "# Days or Weeks", type = input.integer, minval = 1) FromMonth = input(defval = 9, title = "From Month", minval = 1, maxval = 12) FromDay = input(defval = 15, title = "From Day", minval = 1, maxval = 31) FromYear = input(defval = 2019, title = "From Year", minval = 2014) ToMonth = input(defval = 12, title = "To Month", minval = 1, maxval = 12) ToDay = input(defval = 31, title = "To Day", minval = 1, maxval = 31) ToYear = input(defval = 9999, title = "To Year", minval = 2014) // === FUNCTION EXAMPLE === window() => true // === LOGIC === buy = crossover(sma(close, fastMA), sma(close, slowMA)) // buy when fastMA crosses over slowMA sell = crossunder(sma(close, fastMA), sma(close, slowMA)) // sell when fastMA crosses under slowMA // === EXECUTION === strategy.entry("L", strategy.long, when=window() and buy) // buy long when "within window of time" AND crossover strategy.close("L", when=window() and sell) // sell long when "within window of time" AND crossunder // === PLOTTING === plot(sma(close, fastMA), title = 'FastMA', color = color.aqua, linewidth = 2, style = plot.style_line) // plot FastMA plot(sma(close, slowMA), title = 'SlowMA', color = color.yellow, linewidth = 2, style = plot.style_line) // plot SlowMA