Strategy vault / Trend following
Trend Following Strategy Based on EMA Crossover 442508
This strategy identifies market trend direction through the crossover of fast and slow EMA lines, and trades along the trend. It goes long when the fast EMA crosses above the slow EMA, and closes position when price…

Overview
This strategy identifies market trend direction through the crossover of fast and slow EMA lines, and trades along the trend. It goes long when the fast EMA crosses above the slow EMA, and closes position when price breaks below the fast EMA.
Strategy Logic
The strategy calculates fast EMA (i_shortTerm) and slow EMA (i_longTerm) based on input parameters. When the short term EMA crosses above the long term EMA (goLongCondition1) and price is above the short term EMA (goLongCondition2), it enters long position. When price breaks below the short term EMA (exitCondition2), it closes position.
The strategy is based on the golden cross of EMA lines to determine the major market trend, and trade along the trend. When short term EMA crosses above long term EMA, it signals an uptrend; when price is above short term EMA, it indicates the uptrend is underway, so go long. When price drops below short term EMA, it signals a trend reversal, so close position immediately.
Advantage Analysis
The main advantages of this strategy are:
Utilize EMA crossover to identify major market trend, avoid short-term fluctuations.
Adjustable sensitivity in trend detection via fast and slow EMA parameters.
Simple and clear logic, easy to understand and implement, suitable for quant trading beginners.
Customizable EMA period parameters for different products and markets.
Effective risk control by stop loss when price breaks EMA line.
Risk Analysis
There are also some risks:
Delayed EMA crossover signals may cause losses during trend reversal.
False breakout above short term EMA may cause failed entries.
Improper paramedic parameter settings may undermine strategy performance.
Performance relies heavily on market condition, not suitable for all products and periods.
The corresponding risk management measurements:
Optimize EMA parameters for better sensitivity on reversals.
Add other technical indicators to filter entry signals.
Continuously debug and optimize parameters for different markets.
Fully understand applicable market conditions before applying strategy.
Optimization Directions
The strategy can be further optimized in the following aspects:
Add other indicators like MACD and KD to filter entry signals.
Implement trailing stop loss to lock profit and better risk control.
Optimize stop loss placement with volatility indicator ATR.
Test and find better scientific methods for EMA parameter tuning.
Validate signals on multiple timeframes to improve accuracy.
Try BREAKOUT modifications to catch larger moves during trend acceleration stages.
Conclusion
This strategy effectively tracks market trend by trading on EMA crossover signals. With clear logic and controllable risks, it is suitable for quant trading beginners to practice on. Further optimizations on parameter tuning, entry filtering, stop loss placement can improve strategy performance. But all strategies have limitations, users should apply cautiously based on market conditions when live trading.
Parameters
| Name | Default | What it controls |
|---|---|---|
| v_input_1 | 21 | Fast EMA |
| v_input_2 | 55 | Slow EMA |
| v_input_3 | timestamp(01 Jan 2023 00:00) | From |
| v_input_4 | timestamp(31 Dec 2033 23:59) | To |
| v_input_5 | true | Show In-trade / Out-trade background |
Source Pine Script
/*backtest
start: 2023-02-15 00:00:00
end: 2024-02-21 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © pradhan_abhishek
//@version=5
strategy('EMA cross-over strategy by AP', overlay=true, shorttitle='EMACS-AP', initial_capital=100000, default_qty_value=100, default_qty_type=strategy.percent_of_equity, commission_value=0.025)
// inputs
i_shortTerm = input(title='Fast EMA', defval=21)
i_longTerm = input(title='Slow EMA', defval=55)
// select backtest range: if this is not given, then tradingview goes back since inception / whereever it finds data
i_from = input(defval = timestamp("01 Jan 2023 00:00"), title = "From")
i_to = input(defval = timestamp("31 Dec 2033 23:59"), title = "To")
i_showBg = input(defval = true, title = "Show In-trade / Out-trade background")
// create date function "within window of time"
date() => true
// exponential moving average (EMA) variables, derived from input parameters
shortTermEMA = ta.ema(close, i_shortTerm)
longTermEMA = ta.ema(close, i_longTerm)
atr = ta.atr(14)
// ### Trade strategy: begins ###
inTrade = strategy.position_size > 0
notInTrade = strategy.position_size <= 0
goLongCondition1 = shortTermEMA > longTermEMA
goLongCondition2 = close > shortTermEMA
// exitCondition1 = shortTermEMA < midTermEMA
exitCondition2 = close < shortTermEMA
// enter if not in trade and long conditions are met
if date() and goLongCondition1 and goLongCondition2 and notInTrade
strategy.entry('long', strategy.long)
// exit on stop-Loss hit
stopLoss = close - atr * 3
strategy.exit('exit', 'long', stop=stopLoss)
// exit if already in trade and take profit conditions are met
if date() and exitCondition2 and inTrade
strategy.close(id='long')
// ###Trade strategy: ends ###
// plot emas & background color for trade status
plot(shortTermEMA, color=color.new(color.blue, 0))
plot(longTermEMA, color=color.new(color.green, 0))
trade_bgcolor = notInTrade ? color.new(color.red, 75) : color.new(color.green, 75)
bgcolor(i_showBg ? trade_bgcolor : color.new(color.white, 75))Published by ChaoZhang on the FMZ strategy square · last modified 2024-02-22. 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/Trend-Following-Strategy-Based-on-EMA-Crossover-442508.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
- 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
- Dual MA Crossover Trend Tracking StrategyPine Script · Trend following · 1h
- Dynamic Dual EMA Crossover Strategy with Adaptive Profit Loss ControlPine Script · Trend following · 1d
- EMA and RSI Trend Trading StrategyPine Script · Trend following · 1d