The moving average tracking strategy is a trend-following strategy based on simple moving average. It uses a 200-day simple moving average to determine the price trend direction. When the price crosses above the moving average, it goes long. When the price crosses below the moving average, it goes short. This strategy tracks the trend to profit.
The strategy is based on the following principles:
The strategy tracks the trend by moving average direction and makes reverse trades when the MA crossover happens, to profit from the trend.
The strategy has the following advantages:
There are also some risks:
The risks can be addressed through the following optimizations:
The strategy can be further optimized in the following aspects:
Optimize the MA period parameter using methods like Walk Forward Analysis to find the optimal parameters.
Add a short-period MA to track both long and short-term trends.
Incorporate trend indicators like MACD to improve trend reversal identification.
Add stop loss mechanisms like trailing stop loss to control single trade loss.
Robustness test on different products and time periods.
Use machine learning for parameter adaptive optimization.
The moving average tracking strategy is a simple and practical trend-following strategy. It has a clear logic and is easy to implement for capturing trends. But it also has some weaknesses like being insensitive to short-term corrections and weak risk control. We can optimize the strategy from multiple aspects to make it more robust, better parameterized and with stronger risk management. Overall, the moving average tracking strategy has good application value and is an important trend trading concept in quantitative trading.
/*backtest start: 2023-09-19 00:00:00 end: 2023-10-19 00:00:00 period: 1h basePeriod: 15m exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}] */ //@version=4 strategy("MA X 200 BF", overlay=true, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=100, commission_type=strategy.commission.percent, commission_value=0.0) /////////////// Time Frame /////////////// testStartYear = input(2012, "Backtest Start Year") testStartMonth = input(1, "Backtest Start Month") testStartDay = input(1, "Backtest Start Day") testPeriodStart = timestamp(testStartYear,testStartMonth,testStartDay, 0, 0) testStopYear = input(2019, "Backtest Stop Year") testStopMonth = input(12, "Backtest Stop Month") testStopDay = input(31, "Backtest Stop Day") testPeriodStop = timestamp(testStopYear,testStopMonth,testStopDay, 0, 0) testPeriod() => true ///////////// MA 200 ///////////// slowMA = sma(close, input(200)) /////////////// Strategy /////////////// long = close > slowMA short = close < slowMA last_long = 0.0 last_short = 0.0 last_long := long ? time : nz(last_long[1]) last_short := short ? time : nz(last_short[1]) long_signal = crossover(last_long, last_short) short_signal = crossover(last_short, last_long) /////////////// Execution /////////////// if testPeriod() strategy.entry("Long Entry", strategy.long, when=long_signal) strategy.entry("Short Entry", strategy.short, when=short_signal) strategy.exit("Long Ex", "Long Entry") strategy.exit("Short Ex", "Short Entry") /////////////// Plotting /////////////// plot(slowMA, color = long ? color.lime : color.red, linewidth=2) bgcolor(strategy.position_size > 0 ? color.lime : strategy.position_size < 0 ? color.red : color.white, transp=80) bgcolor(long_signal ? color.lime : short_signal ? color.red : na, transp=30)template: strategy.tpl:40:21: executing "strategy.tpl" at <.api.GetStrategyListByName>: wrong number of args for GetStrategyListByName: want 7 got 6