This strategy is based on the crossover principle of moving averages to trade. It uses two moving averages, generating buy signals when the short term moving average crosses above the long term moving average from below. Sell signals are generated when the price breaks below another moving average. This strategy is suitable for trending markets, able to effectively filter out some noise trades and capture the main trend.
This strategy uses user-customizable short term and long term moving average periods, exit moving average period, and moving average calculation methods.
When the short term moving average crosses above the long term moving average from below, a buy signal is generated. This indicates the short term trend has switched to an uptrend, and we can buy.
When the close price breaks below the exit moving average, a sell signal is generated. This indicates a trend reversal, so we should exit the position.
Therefore, the strategy’s trading signals come from the crossover of the short term and long term moving averages, and the relationship between the closing price and the exit moving average.
The advantages of this strategy are:
Simple and easy to implement.
Customizable parameters suit different market conditions.
Moving averages filter out noise and capture the main trend.
Can incorporate other technical indicators like trend, support/resistance to further optimize.
Controllable risk-reward ratio, has stop loss mechanism.
The risks are:
Prone to false signals in non-trending consolidation markets.
Improper parameter settings may cause missing trends or too many invalid trades.
Improper stop loss placement could increase losses.
Failed breakouts may cause losses.
Parameters need timely adjustment to suit market changes.
Solutions include optimizing parameters, incorporating other filters, adjusting stops, waiting for trend confirmation before trading, etc.
This strategy can be improved by:
Developing trend determination mechanisms and only trading after trend confirmation.
Adding filters like volume or volatility to filter signals.
Dynamic optimization of moving average periods.
Optimizing the stop loss mechanism to use a trailing stop.
Incorporating support/resistance and other indicators to further confirm signals.
Adjusting parameters based on different products and timeframes.
Overall this moving average crossover strategy is a simple and practical trend following system. It can be adjusted to market conditions by tweaking parameters and catch the main trend direction in trending markets. But risks like trend misidentification should be noted, and constant optimization is needed to adapt to changing markets. In general, this strategy has good viability.
/*backtest start: 2022-10-30 00:00:00 end: 2023-11-05 00:00:00 period: 1d basePeriod: 1h exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}] */ // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ // © TwoChiefs //@version=4 strategy("John EMA Crossover Strategy", overlay=true) //////////////////////////////////////////////////////////////////////////////// // BACKTESTING RANGE // From Date Inputs fromDay = input(defval = 1, title = "From Day", minval = 1, maxval = 31) fromMonth = input(defval = 1, title = "From Month", minval = 1, maxval = 12) fromYear = input(defval = 2020, title = "From Year", minval = 1970) // To Date Inputs toDay = input(defval = 1, title = "To Day", minval = 1, maxval = 31) toMonth = input(defval = 1, title = "To Month", minval = 1, maxval = 12) toYear = input(defval = 2021, title = "To Year", minval = 1970) // Calculate start/end date and time condition startDate = timestamp(fromYear, fromMonth, fromDay, 00, 00) finishDate = timestamp(toYear, toMonth, toDay, 00, 00) time_cond = true //////////////////////////////////////////////////////////////////////////////// //CREATE USER-INPUT VARIABLES periodShort = input(13, minval=1, title="Enter Period for Short Moving Average") smoothingShort = input(title="Choose Smoothing Type for Short Moving Average", defval="EMA", options=["RMA", "SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "HullMA", "LSMA"]) periodLong = input(48, minval=1, title="Enter Period for Long Moving Average") smoothingLong = input(title="Choose Smoothing Type for Long Moving Average", defval="EMA", options=["RMA", "SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "HullMA", "LSMA"]) periodExit = input(30, minval=1, title="Enter Period for Exit Moving Average") smoothingExit = input(title="Choose Smoothing Type for Exit Moving Average", defval="EMA", options=["RMA", "SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "HullMA", "LSMA"]) src1 = close pivot = (high + low + close) / 3 //MA CALCULATION FUNCTION ma(smoothing, src, length) => if smoothing == "RMA" rma(src, length) else if smoothing == "SMA" sma(src, length) else if smoothing == "EMA" ema(src, length) else if smoothing == "WMA" wma(src, length) else if smoothing == "VWMA" vwma(src, length) else if smoothing == "SMMA" na(src[1]) ? sma(src, length) : (src[1] * (length - 1) + src) / length else if smoothing == "HullMA" wma(2 * wma(src, length / 2) - wma(src, length), round(sqrt(length))) //ASSIGN A MOVING AVERAGE RESULT TO A VARIABLE shortMA=ma(smoothingShort, pivot, periodShort) longMA=ma(smoothingLong, pivot, periodLong) exitMA=ma(smoothingExit, pivot, periodExit) //PLOT THOSE VARIABLES plot(shortMA, linewidth=4, color=color.yellow,title="The Short Moving Average") plot(longMA, linewidth=4, color=color.blue,title="The Long Moving Average") plot(exitMA, linewidth=1, color=color.red,title="The Exit CrossUnder Moving Average") //BUY STRATEGY buy = crossover(shortMA,longMA) ? true : na exit = crossunder(close,exitMA) ? true : na strategy.entry("long",true,when=buy and time_cond) strategy.close("long",when=exit and time_cond) if (not time_cond) strategy.close_all()