The moving average crossover trading strategy generates buy and sell signals by calculating the crossover of the fast EMA (fastLength) and slow EMA (slowLength) lines. When the fast line crosses above the slow line, a buy signal is generated. When the fast line crosses below the slow line, a sell signal is generated. This strategy is simple and practical, suitable for medium and short term trading.
The strategy uses two moving average lines, fast line and slow line. The fast line parameter EMAfastLength defaults to 9-day line, and the slow line parameter EMAslowLength defaults to 26-day line. Calculate the crossover of the two EMA lines to determine market buy and sell signals:
The specific trading signals and strategy rules are as follows:
So this strategy trades based on the golden cross and dead cross of the two moving average lines.
To address the risks, parameters that can be optimized include moving average cycle, trading variety, profit taking and stop loss ratio, etc. Extensive testing is required to reduce risks.
The moving average crossover idea of this strategy is simple and practical. It can be optimized in the following ways:
Through these optimization tests, the strategy’s practical effect and stability can be greatly improved.
The moving average crossover strategy idea is simple, but practical application requires continuous optimization. This strategy gives the logic of generating trading signals and basic trading rules. On this basis, it can be greatly optimized to become a usable quantitative strategy. The application of moving average also provides us with ideas for strategies, based on which we can innovate and improve.
/*backtest start: 2023-12-01 00:00:00 end: 2023-12-31 23:59:59 period: 1h basePeriod: 15m exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}] */ //@version=4 strategy("EMA Cross by MarketAlpha", overlay=true) EMAfastLength = input(defval = 9, minval = 2) EMAslowLength = input(defval = 26, minval = 2) Targetpercentage = input(defval = 0.15, title = "Profit Target in percentage", minval = 0.05) StopLosspercentage = input(defval = 0.20, title = "Stop Loss in percentage", minval = 0.05) profitpoints = close*Targetpercentage stoplosspoints = close*StopLosspercentage price = close FromMonth = input(defval = 1, title = "From Month", minval = 1, maxval = 12) FromDay = input(defval = 1, title = "From Day", minval = 1, maxval = 31) FromYear = input(defval = 2018, title = "From Year", minval = 2000) ToMonth = input(defval = 1, title = "To Month", minval = 1, maxval = 12) ToDay = input(defval = 1, title = "To Day", minval = 1, maxval = 31) ToYear = input(defval = 9999, title = "To Year", minval = 2017) start = timestamp(FromYear, FromMonth, FromDay, 00, 00) // backtest start window finish = timestamp(ToYear, ToMonth, ToDay, 23, 59) // backtest finish window window() => true // create function "within window of time" emafast = ema(price, EMAfastLength) emaslow = sma(price, EMAslowLength) plot(emafast,color=green) plot(emaslow,color=red) enterLong() => crossover(emafast, emaslow) strategy.entry(id = "MarketAlpha Long", long = true, when = window() and enterLong()) strategy.exit("Exit Long", from_entry = "MarketAlpha Long", profit = profitpoints,loss = stoplosspoints) enterShort() => crossunder(emafast, emaslow) strategy.entry(id = "MarketAlpha Short", long = false, when = window() and enterShort()) strategy.exit("Exit Short", from_entry = "MarketAlpha Short", profit = profitpoints,loss = stoplosspoints)