Strategy vault / Trend following
EMA Crossover Trend Following Strategy
This strategy is a simple trend following strategy based on EMA crossover. It uses two EMA lines with different parameters, a short-term EMA line and a long-term EMA line. When the short-term EMA line crosses above…

Overview
This strategy is a simple trend following strategy based on EMA crossover. It uses two EMA lines with different parameters, a short-term EMA line and a long-term EMA line. When the short-term EMA line crosses above the long-term EMA line, go long. When the short-term EMA line crosses below the long-term EMA line, close position. With stop loss and take profit to manage risk.
Strategy Logic
EMA indicator is a trend following indicator which exponentially smoothes price. The short-term EMA line responds faster to price changes, reflecting recent trend. The long-term EMA line responds slower, reflecting long term trend. When short EMA crosses above long EMA, it indicates the recent upward momentum is stronger than the long term trend, can go long. Vice versa, when short EMA crosses below long EMA, it indicates the recent downward momentum is stronger, should close long position.
This strategy sets 9 period and 21 period EMA lines. Use the crossover of 9 period short EMA and 21 period long EMA as trading signals:
- When 9 EMA crosses above 21 EMA, go long
- When 9 EMA crosses below 21 EMA, close position
Advantages
- Use EMA crossover to form trading signals, avoid over-trading
- EMA smoothes price, helps identify trend direction
- Simple and easy-to-understand logic
Risks
- EMA has lagging effect during volatile markets, may cause losses
- Rely solely on single indicator, prone to false signals
Risk Solutions:
- Optimize EMA parameters for faster response
- Add other indicators for signal filtration
Optimization Directions
- Optimize EMA periods, find best combination
- Add volume or other indicators for filtration, avoid false signals
- Add dynamic stop loss and take profit
Summary
The strategy capitalizes on EMA crossover of two EMAs to follow trends. Its advantage is simple logic, medium trading frequency, catching mid-to-long term trends. However EMA has lagging effect. Adding more indicators for filtration and optimising dynamic stop loss can reduce risk further. Overall, EMA Crossover is effective from seizing mid-to-long term trends.
Parameters
| Name | Default | What it controls |
|---|---|---|
| v_input_1 | 9 | Short EMA Period |
| v_input_2 | 21 | Long EMA Period |
| v_input_3 | true | Stop Loss (%) |
| v_input_4 | 2 | Take Profit Multiplier |
Source Pine Script
/*backtest
start: 2023-01-25 00:00:00
end: 2024-01-31 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=4
strategy("EMA Crossover Strategy", overlay=true)
// Input parameters
shortPeriod = input(9, title="Short EMA Period")
longPeriod = input(21, title="Long EMA Period")
stopLossPercent = input(1, title="Stop Loss (%)") / 100
takeProfitMultiplier = input(2, title="Take Profit Multiplier")
// Calculate EMAs
emaShort = ema(close, shortPeriod)
emaLong = ema(close, longPeriod)
// Plot EMAs
plot(emaShort, color=color.blue, title="Short EMA")
plot(emaLong, color=color.red, title="Long EMA")
// Strategy logic
strategy.entry("Buy", strategy.long, when=crossover(emaShort, emaLong))
strategy.close("Buy", when=crossunder(emaShort, emaLong))
// Risk management
atrValue = atr(14)
stopLossLevel = close * (1 - stopLossPercent)
takeProfitLevel = close * takeProfitMultiplier
strategy.exit("Take Profit/Stop Loss", from_entry="Buy", loss=stopLossLevel, profit=takeProfitLevel)
Published by ChaoZhang on the FMZ strategy square · last modified 2024-02-01. 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/EMA-Crossover-Trend-Following-Strategy.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 Dynamic Trend Following and EMA Crossover Trading StrategyPine Script · Trend following · 1d
- ATR and Moving Average Crossover Hybrid StrategyPine Script · Trend following · 1h
- ATR and T3 Moving Average StrategyPine Script · Trend following · 5m
- Adaptive ATR and RSI Trend Following Strategy with Trailing Stop LossPine Script · Trend following · 1h
- Bollinger Bands and RSI Trend Following StrategyPine Script · Trend following · 1h
- Dual ATR Channel Trend Following StrategyPine Script · Trend following · 15m