This is a trading strategy based on double moving averages crossover. It generates buy and sell signals when two moving averages of different lengths cross over. Specifically, it goes long when the faster MA crosses above the slower MA, and goes short when the faster MA crosses below the slower MA.
The core logic of this strategy lies in the crossover principles between two moving averages. A moving average is the arithmetic average price over a specified time period. It helps filter out market noise and reveal clearer price trends.
In this strategy, the shorter-term MA captures short-term trends while the longer-term MA captures long-term trends. As the short-term MA is more sensitive in responding to the latest price changes, crossing over the long-term MA signals a trend reversal ahead.
Specifically, the strategy calculates the MAs using ta.sma over the long_period and short_period defined by users. It then uses ta.crossover and ta.crossunder to detect the golden crossover and death crossover between the two MAs. When the short MA crosses above the long MA, go long. When the short MA crosses below, go short.
The main advantages of this strategy include:
There are also several risks:
To mitigate the risks, parameters can be tuned, stop loss and take profit can be incorporated, or other technical indicators can be added.
There is room for further optimization:
In conclusion, this is an ideal starter strategy for algorithmic trading, thanks to its simplicity in logic and parameters while still able to effectively capture market reversals. At the same time, it has great potential for optimizations to fit various trading needs.
/*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=5 strategy("Cross 2 Moving Average Strategy", shorttitle="2MA Cross", overlay=true) // User-defined input for moving averages long_period = input(20, title="Long Period") short_period = input(5, title="Short Period") type_ma = input.string("SMA", title = "MA type", options = ["SMA", "EMA"]) // Calculating moving averages long_ma = ta.sma(close, long_period) short_ma = ta.sma(close, short_period) // Plot moving averages plot(long_ma, title="Long Moving Average", color=color.red) plot(short_ma, title="Short Moving Average", color=color.green) // Strategy logic for crossing of moving averages longCondition = ta.crossover(short_ma, long_ma) shortCondition = ta.crossunder(short_ma, long_ma) // Entry orders if (longCondition) strategy.entry("Long", strategy.long) if (shortCondition) strategy.entry("Short", strategy.short) // Optional: Add stop loss and take profit stop_loss_perc = input(1, title="Stop Loss (%)") / 100 take_profit_perc = input(2, title="Take Profit (%)") / 100 strategy.exit("Exit Long", from_entry="Long", stop=close*(1-stop_loss_perc), limit=close*(1+take_profit_perc)) strategy.exit("Exit Short", from_entry="Short", stop=close*(1+stop_loss_perc), limit=close*(1-take_profit_perc))