Strategy vault / Trend following
Moving Average Crossover Trading Strategy 433446
The moving average crossover trading strategy generates buy and sell signals when shorter and longer term moving averages cross. It belongs to technical analysis based trading strategies. This strategy is simple…

Overview
The moving average crossover trading strategy generates buy and sell signals when shorter and longer term moving averages cross. It belongs to technical analysis based trading strategies. This strategy is simple, capital efficient with smaller drawdowns, suitable for medium-long term trading.
Strategy Logic
This strategy calculates the 20 and 50 period Exponential Moving Average (EMA). It triggers long position when the 20 EMA crosses over 50 EMA. It triggers short position when 20 EMA crosses under 50 EMA.
EMA gives more weight to recent data. The formula is:
EMAtoday = (Pricetoday * k) + EMAyesterday * (1-k)
Where k = 2/(number of periods + 1)
When shorter term EMA crosses over longer term EMA, it indicates bullish price move to LONG. When it crosses under, it indicates bearish price reversal to SHORT.
Pros
The pros of this strategy:
- Simple logic, easy to understand and execute
- Less capital required, smaller drawdowns
- Flexible parameter tuning for different markets
- Applicable to any instruments for scalping or trend trading
Risks and Enhancements
The risks include:
- Frequent trading signals during price oscillation. Filters can help.
- Stop loss needed to avoid being trapped.
- Parameter optimization requires more historical data.
Enhancements:
- Adding filters like Bollinger Bands to reduce false signals
- Adding stop loss/take profit to avoid being trapped
- Finding optimal parameter sets for different instruments
- Combining with volume to confirm signals
Conclusion
The moving average crossover strategy is a simple yet effective technical strategy that is proven by the market. Further improvements on risk control and robustness can be achieved by parameter tuning, adding filters etc. It serves as a fundamental building block for quantitative trading.
Parameters
| Name | Default | What it controls |
|---|---|---|
| v_input_1_close | 0 | Source: close | high | low | open | hl2 | hlc3 | hlcc4 | ohlc4 |
| v_input_2 | 20 | 1st MA Length |
| v_input_string_1 | 0 | 1st MA Type: EMA |
| v_input_3 | 50 | 2nd MA Length |
| v_input_string_2 | 0 | 2nd MA Type: EMA |
Source Pine Script
/*backtest
start: 2022-11-20 00:00:00
end: 2023-11-26 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/
// © brandlabng
//@version=5
//study(title="Holly Grail", overlay = true)
strategy('HG|E15m', overlay=true)
src = input(close, title='Source')
price = request.security(syminfo.tickerid, timeframe.period, src)
ma1 = input(20, title='1st MA Length')
type1 = input.string('EMA', '1st MA Type', options=['EMA'])
ma2 = input(50, title='2nd MA Length')
type2 = input.string('EMA', '2nd MA Type', options=['EMA'])
price1 = if type1 == 'EMA'
ta.ema(price, ma1)
price2 = if type2 == 'EMA'
ta.ema(price, ma2)
//plot(series=price, style=line, title="Price", color=black, linewidth=1, transp=0)
plot(series=price1, style=plot.style_line, title='1st MA', color=color.new(#219ff3, 0), linewidth=2)
plot(series=price2, style=plot.style_line, title='2nd MA', color=color.new(color.purple, 0), linewidth=2)
longCondition = ta.crossover(price1, price2)
if longCondition
strategy.entry('Long', strategy.long)
shortCondition = ta.crossunder(price1, price2)
if shortCondition
strategy.entry('Short', strategy.short)Published by ChaoZhang on the FMZ strategy square · last modified 2023-11-27. Collected in the Quant Trading Vault. Unvalidated research: re-test with your own data and costs before trusting it.
Build this with AgenKit
A spec is not a trading system. Paste this into your coding agent and AgenKit runs a specialist team — architecture, test-first build, risk gates, review — with paper trading on by default.
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/Moving-Average-Crossover-Trading-Strategy-433446.md- npx agenkit install engineering-kit in your project
- Paste the command into your agent and approve each phase
- Get tests, backtest, risk controls and a paper-trading switch
Understand it first
Before you run this, read the lesson behind the idea and how to backtest it honestly. Specs like this one are the survivors of an unknown number of trials; deflate their results accordingly.
Related specs
- ATR and Moving Average Crossover Hybrid StrategyPine Script · Trend following · 1h
- Bollinger Band Moving Average Crossover StrategyPine Script · Trend following · 1h
- Bollinger Bands and EMA Trend Following StrategyPine Script · Trend following · 1h
- Bollinger Bands and RSI Trend Following StrategyPine Script · Trend following · 1h
- Dual Moving Average Crossover Dynamic Position Optimization Strategy with EMA Cross Signal SystemPine Script · Trend following · 1h
- EMA Cross Trend Following Strategy with SMA Stop Loss and Re Entry MechanismPine Script · Trend following · 1h