Strategy vault / Trend following
EMA Trend Following Trading Strategy
This strategy is a typical EMA trend following strategy. It uses the golden cross of a fast EMA and slow EMA to determine uptrends, and the death cross to determine downtrends, for long and short trades accordingly…
Overview
This strategy is a typical EMA trend following strategy. It uses the golden cross of a fast EMA and slow EMA to determine uptrends, and the death cross to determine downtrends, for long and short trades accordingly. The strategy reliably tracks medium- to long-term trends and is suitable for swing trading.
Strategy Logic
The core logic is:
- Calculate fast EMA, e.g. 12-period EMA
- Calculate slow EMA, e.g. 26-period EMA
- When fast EMA crosses above slow EMA, determine uptrend for long entry
- When fast EMA crosses below slow EMA, determine downtrend for short entry
- Exit current position when fast EMA crosses back below slow EMA
Using EMAs of different speeds can effectively detect trend changes. The fast EMA reacts quickly to price changes for early trend detection, while the slow EMA filters out false signals to ensure trend confirmation. Together they form a reliable trend system.
Golden crosses signal the start of an uptrend for longs, while death crosses signal the start of a downtrend for shorts. Exiting on fast EMA death crosses helps stop losses in a timely manner.
Advantages
- EMAs effectively identify medium- to long-term trends
- Fast and slow EMAs combine for reliable trend system
- Simple logic easy to implement
- Configurable EMA parameters suit different instruments
- Fast EMA crossover stop loss controls risk
Risks and Mitigations
- Unable to predict trend reversal points upfront, some losses
- Poor EMA parameter selection may miss trend change points
- EMA parameters need adjustment for market condition changes
Mitigations:
- Use range stops to limit losses
- Add other indicators to detect potential trend reversals
- Optimize parameters for better trend identification
Enhancement Opportunities
The strategy can be enhanced in areas like:
Machine learning to auto-tune EMA parameters for better adaptability
Volatility-based position sizing to adjust with market volatility
Oscillators like RSI to fine-tune entry points
Adding trailing stops, profit-taking stops for better risk management
Volume analysis to gauge fund inflows/outflows for trend verification
Portfolio combinations with non-correlated strategies to lower drawdowns and increase return stability
Conclusion
The EMA trend following strategy is a simple and practical way to track medium- to long-term trends. It uses fast and slow EMA crosses for entry timing. Easy to implement, it can also be extended in multiple dimensions for greater adaptability. A great fit for swing trading trending markets.
Parameters
| Name | Default | What it controls |
|---|---|---|
| v_input_bool_1 | true | (?Backtest Time Period)Filter Date Range of Backtest |
| v_input_1 | timestamp(1 Jan 2021) | Start Date |
| v_input_2 | timestamp(1 Jan 2022) | End Date |
Source Pine Script
/*backtest
start: 2023-09-11 00:00:00
end: 2023-09-18 00:00:00
period: 10m
basePeriod: 1m
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/
// © HomoDeus666
//@version=5
strategy("EMA12/26 with date backtest range (BTCpair)", overlay=true,initial_capital = 1,commission_type = strategy.commission.percent,currency = currency.BTC)
//input date and time
useDateFilter = input.bool(true, title="Filter Date Range of Backtest",
group="Backtest Time Period")
backtestStartDate = input(timestamp("1 Jan 2021"),
title="Start Date", group="Backtest Time Period",
tooltip="This start date is in the time zone of the exchange " +
"where the chart's instrument trades. It doesn't use the time " +
"zone of the chart or of your computer.")
backtestEndDate = input(timestamp("1 Jan 2022"),
title="End Date", group="Backtest Time Period",
tooltip="This end date is in the time zone of the exchange " +
"where the chart's instrument trades. It doesn't use the time " +
"zone of the chart or of your computer.")
//check date and time option
inTradeWindow = true
/// plot and indicator
fastEMA = ta.ema(close,12), slowEMA=ta.ema(close,26)
plot(fastEMA,color=color.green,linewidth = 2)
plot(slowEMA,color=color.red,linewidth=2)
//entry when condition
longCondition = ta.crossover(fastEMA,slowEMA)
if (longCondition) and inTradeWindow
strategy.entry("buy", strategy.long)
if ta.crossunder(ta.ema(close, 12), ta.ema(close, 26)) and inTradeWindow
strategy.close("buy")
// trades and cancel all unfilled pending orders
if not inTradeWindow and inTradeWindow[1]
strategy.cancel_all()
strategy.close_all(comment="Date Range Exit")Published by ChaoZhang on the FMZ strategy square · last modified 2023-09-19. 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-Trend-Following-Trading-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
- Adaptive ATR and RSI Trend Following Strategy with Trailing Stop LossPine Script · Trend following · 1h
- Advanced Multi Indicator Multi Dimensional Trend Cross Quantitative StrategyPine Script · Trend following · 3h
- Bollinger Bands and RSI Trend Following StrategyPine Script · Trend following · 1h
- Dual ATR Channel Trend Following StrategyPine Script · Trend following · 15m
- Dual EMA Crossover with RSI Trend Confirmation StrategyPine Script · Trend following · 3h
- Dual Mode Adaptive Trend Trading Strategy EMA Crossover with ATR Volatility Based Risk Management SystemPine Script · Trend following · 1d