Strategy vault / Trend following
Trend Following Strategy Based on EMA and MACD across Timeframes
This strategy combines EMA lines and MACD indicator across timeframes to identify trend signals and capture mid-to-long term trends. It takes trend following actions when short-term trend aligns with mid-to-long term…

Overview
This strategy combines EMA lines and MACD indicator across timeframes to identify trend signals and capture mid-to-long term trends. It takes trend following actions when short-term trend aligns with mid-to-long term trend. Meanwhile, the strategy uses ATR indicator to set stop loss and take profit to control risks from fluctuations.
Principles
The strategy uses 50-day EMA and 100-day EMA to determine mid-to-long term trend direction. When short-term trend is identified by MACD indicator, it checks if the directions align. If yes, it takes trend following actions.
Specifically, when MACD fast line crosses above slow line, and closes > 50-day EMA and closes > 100-day EMA, it goes long. When MACD fast line crosses below slow line, and closes < 50-day EMA and closes < 100-day EMA, it goes short.
Also, the strategy uses ATR indicator to calculate range of fluctuations and set stop loss and take profit prices. It sets certain multiplier of ATR based on close price as stop loss level, and certain multiplier of ATR based on close price as take profit level.
Advantage Analysis
Combining EMA lines and MACD indicator across timeframes helps identify trend signals and prevents missing mid-to-long term trends
Using ATR indicator to set stop loss and take profit based on market fluctuation effectively controls risks
Avoiding market neutral zones prevents unnecessary losses
Risk Analysis
EMA lines have lagging effect and may miss turning points
MACD indicator has multiple timeframes and parameter settings that impact results
ATR ranges cannot fully represent future price fluctuations, cannot eliminate risks
Counter measures:
Confirm signals with other indicators to avoid EMA lagging issues
Adjust MACD parameters and optimize results
Reasonably set ATR multiplier to control maximum loss
Optimization Directions
Test different combinations of EMA line periods
Optimize MACD parameter settings
Use machine learning methods to automatically find optimal ATR stop loss/take profit multipliers
Summary
The strategy combines EMA, MACD and ATR indicators to implement trend following operations across timeframes. Through parameter optimization, it has the potential to achieve good strategy return rates. Also need to prevent risks including indicator lagging, improper parameter adjustment and fluctuation control, and continue to optimize and enhance.
Parameters
| Name | Default | What it controls |
|---|---|---|
| v_input_1 | 12 | Fast Length |
| v_input_2 | 26 | Slow Length |
| v_input_3 | 9 | Signal Length |
| v_input_4 | 14 | ATR Length |
| v_input_5 | 3 | Take Profit Multiplier |
| v_input_6 | true | Stop Loss Multiplier |
Source Pine Script
/*backtest
start: 2022-12-29 00:00:00
end: 2024-01-04 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("EMA-50, EMA-100, and MACD Strategy with ATR for Stop Loss/Profit", overlay=true)
// MACD hesaplama
fastLength = input(12, title="Fast Length")
slowLength = input(26, title="Slow Length")
signalLength = input(9, title="Signal Length")
[macdLine, signalLine, _] = ta.macd(close, fastLength, slowLength, signalLength)
// EMA-50 ve EMA-100 hesaplama
ema50 = ta.ema(close, 50)
ema100 = ta.ema(close, 100)
// ATR hesaplama
atrLength = input(14, title="ATR Length")
atrValue = ta.atr(atrLength)
// Take Profit ve Stop Loss çoklayıcıları
takeProfitMultiplier = input(3.0, title="Take Profit Multiplier") // TP, 3 katı ATR
stopLossMultiplier = input(1.0, title="Stop Loss Multiplier")
// Long Pozisyon Koşulları
longCondition = ta.crossover(macdLine, signalLine) and close > ema50 and close > ema100
// Short Pozisyon Koşulları
shortCondition = ta.crossunder(macdLine, signalLine) and close < ema50 and close < ema100
// Take Profit ve Stop Loss Seviyeleri
takeProfitLevel = close + takeProfitMultiplier * atrValue
stopLossLevel = close - stopLossMultiplier * atrValue
// Long Pozisyon İşlemleri
strategy.entry("Long", strategy.long, when=longCondition)
strategy.exit("Take Profit/Stop Loss", from_entry="Long", loss=stopLossLevel, profit=takeProfitLevel)
// Short Pozisyon İşlemleri
strategy.entry("Short", strategy.short, when=shortCondition)
strategy.exit("Take Profit/Stop Loss", from_entry="Short", loss=stopLossLevel, profit=takeProfitLevel)
// Grafikte Gösterme
plot(ema50, color=color.blue, title="EMA-50")
plot(ema100, color=color.red, title="EMA-100")
hline(0, "Zero Line", color=color.gray)
Published by ChaoZhang on the FMZ strategy square · last modified 2024-01-05. 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-and-MACD-across-Timeframes.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