0bps

Strategy vault / Trend following

Long Only Triple EMA Golden Cross Trading Strategy

This strategy generates trading signals based on the golden cross and death cross of three EMA lines with different periods to take advantage of each EMA's strengths and achieve better trading performance.

Author
ChaoZhang
Complexity
Intermediate · 54 lines
Parameters
10
Backtest bar
5m · intraday
Tested on
BTC/USDT · futures

Overview

This strategy generates trading signals based on the golden cross and death cross of three EMA lines with different periods to take advantage of each EMA's strengths and achieve better trading performance.

Strategy Logic

The strategy calculates three EMA lines with periods of 8, 50 and 200, and generates signals when the faster EMA crosses above or below the slower EMA. The logic is:

  1. Calculate the 8-period, 50-period and 200-period EMA lines.

  2. Go long when the 8-period EMA crosses above the 50-period EMA (golden cross), close position when the 50-period EMA crosses below the 8-period EMA (death cross).

  3. Optionally only go long when the price is above the 200-period EMA to avoid whipsaws.

  4. An optional profit-taking EMA line can be set to close positions when the price crosses below it.

The fast EMA identifies bottoms, the medium EMA determines trend, and the slow EMA filters out noise. Together they identify trend changes while maintaining decent trading frequency.

Advantages

  1. The triple EMAs effectively determine trends and capitalize on individual strengths. The 8-period EMA catches short bottoms, 50-period EMA determines mid-term trend, and 200-period EMA filters out noise.

  2. Only going long above 200-period EMA avoids whipsaws.

  3. Customizable profit-taking EMA sets reasonable profit targets.

  4. Visual customizations like bar colors and EMA plotting improve flexibility.

  5. Simple golden/death cross logic is easy to understand.

Risks and Mitigations

  1. EMA lags may cause missed entry timing. Shorten EMA periods or combine with indicators like MACD.

  2. Whipsaws may generate bad signals. Use longer EMAs to filter, or add conditions.

  3. Fixed profit target is not adaptive. Use dynamic exits based on ATR etc.

  4. No stops means unlimited risk. Add trailing or fixed-value stops.

Enhancement Opportunities

  1. Optimize EMA periods for best parameters.

  2. Add indicators like MACD for timing.

  3. Implement dynamic profit-taking based on volatility.

  4. Add stop-loss logic, trailing or fixed value.

  5. Improve entry conditions, e.g. volume filters.

Conclusion

This strategy capitalizes on EMA's trend filtering to identify high-probability moves. Optimizing exits, adding indicators and filters can improve performance. Overall it is simple and practical for EMA-based trend following.

Parameters

NameDefaultWhat it controls
v_input_1trueEnable Bar Color?
v_input_2trueShow 8 EMA
v_input_3trueShow 50 EMA
v_input_4trueShow 200 EMA
v_input_5falseShow profit level EMA
v_input_6falseLong only above EMA200
v_input_78v_input_7
v_input_850v_input_8
v_input_9200v_input_9
v_input_1050v_input_10

Source Pine Script

/*backtest
start: 2023-09-18 00:00:00
end: 2023-09-20 00:00:00
period: 5m
basePeriod: 1m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=2
strategy("Long only EMA CROSS 8/50/200 Backtest", shorttitle="Golden Cross Tri EMA", overlay=true)

// EMA 8/50/200 Cross TEST



// Input
switch1=input(true, title="Enable Bar Color?")
switch2=input(true, title="Show 8 EMA")
switch3=input(true, title="Show 50 EMA")
switch4=input(true, title="Show 200 EMA")
switch5=input(false, title="Show profit level EMA")
bool_Long_EMA200=input(false, title="Long only above EMA200")
movingaverage_8 = ema(close, input(8))
movingaverage_50 = ema(close, input(50))
movingaverage_market_signal = ema(close, input(200))
movingaverage_profitlvl = ema(close, input(50))


// Calculation
bullish_cross = if bool_Long_EMA200 == true
    crossover(movingaverage_8, movingaverage_50) and movingaverage_8 > movingaverage_market_signal
else 
    bullish_cross = crossover(movingaverage_8, movingaverage_50)
bearish_cross = crossunder(close, movingaverage_profitlvl)

// Strategy
if bullish_cross
    strategy.entry("long", strategy.long)

strategy.close("long", when = bearish_cross )

// Colors
bartrendcolor = close > movingaverage_8 and close > movingaverage_50 and change(movingaverage_50) > 0 ? green : close < movingaverage_8 and close < movingaverage_50 and change(movingaverage_50) < 0 ? red : blue
barcolor(switch1?bartrendcolor:na)

// Output
plot(switch2?movingaverage_8:na,color = change(movingaverage_8) > 0 ? green : red,linewidth=2, title="EMA8")
plot(switch3?movingaverage_50:na,color = change(movingaverage_50) > 0 ? green : red,linewidth=2,title="EMA50")
plot(switch4?movingaverage_market_signal:na,color = change(movingaverage_market_signal) > 0 ? green : red,linewidth=3,title="EMA200")
plot(switch5?movingaverage_profitlvl:na,color = change(movingaverage_profitlvl) > 0 ? green : red,linewidth=3, title="EMA Profit LVL")

//
alertcondition(bullish_cross, title='Golden Cross (bullish)', message='Bullish')
alertcondition(bearish_cross, title='Death Cross (bearish)', message='Bearish')

Published by ChaoZhang on the FMZ strategy square · last modified 2023-09-26. 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.

in Claude Code, Cursor, Codex & 4 more
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/Long-Only-Triple-EMA-Golden-Cross-Trading-Strategy.md
  1. npx agenkit install engineering-kit in your project
  2. Paste the command into your agent and approve each phase
  3. 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

esc

Try , , or .

↑↓ move↵ open