Strategy vault / Trend following
Short Term Medium Term and Long Term EMA Crossover Trading Strategy
This strategy generates trading signals based on three exponential moving average lines (EMA) with different periods: short-term EMA with 5-day period, medium-term EMA with 8-day period and long-term EMA with 13-day…

This strategy generates trading signals based on three exponential moving average lines (EMA) with different periods: short-term EMA with 5-day period, medium-term EMA with 8-day period and long-term EMA with 13-day period. It goes long when the short-term EMA crosses over the medium-term and long-term EMAs, and goes short when the short-term EMA crosses under the medium-term and long-term EMAs.
Strategy Logic
This strategy judges the market trend by calculating EMAs of different periods. The short-term EMA reflects the average price of the recent few days while the medium- and long-term EMAs reflect the average price over longer timeframes. The crossover of short-term EMA over medium- and long-term EMAs signals an upward breakout of the price, so a long position is taken. Conversely, when the short-term EMA crosses under the other two, it signals a downward price breakout so a short position is taken.
Specifically, this strategy concurrently computes 5-day, 8-day and 13-day EMAs. It generates long signals when the 5-day EMA crosses over the 8-day and 13-day ones; it generates short signals when the 5-day EMA crosses under the other two. After going long, the position is closed once the 5-day EMA crosses back under the 13-day EMA. Likewise for the short position.
Advantages of the Strategy
- Using multiple-period EMAs avoids missing key trend reversal points that could occur with overly short or long single EMA periods
- Combining three EMAs of short, medium and long terms enhances reliability of trading signals
- Smoothed pricing via EMAs filters out some market noise and prevents unnecessary entries
Risks of the Strategy
- All three EMAs are lagging trend indicators, inherently containing some time delays before actual price breakouts, risking late signals
- EMAs cannot effectively distinguish real trends versus short-term corrections, apt to yield false signals
- Fixed EMA periods cannot adapt to varying market regimes across different timeframes
Improvement ideas:
- Adding other indicators like MACD to better gauge real trend, avoiding false signals
- Flexibly tuning EMA period parameters for different products and market environments
- Adding moving stop loss to lock in profits and control risks
Summary
This is a typical breakout system that judges trend reversals by comparing crossovers between short, medium and long-period EMAs. Its simplicity in signaling facilitates ease of trading, but also suffers from EMAs' inherent lagging and inability to filter real trends from temporary corrections. Future enhancements may integrate other technical indicators or adaptive parameter tuning to optimize it.
Parameters
| Name | Default | What it controls |
|---|---|---|
| v_input_1 | 2020 | Backtest Start Year |
| v_input_2 | 5 | Length |
| v_input_3 | 8 | Length |
| v_input_4 | 13 | Length |
| v_input_5 | false | Long Only |
Source Pine Script
/*backtest
start: 2023-11-16 00:00:00
end: 2023-11-23 00:00:00
period: 30m
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=4
//
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © gregoirejohnb
// @It is modified by ttsaadet.
// Moving average crossover systems measure drift in the market. They are great strategies for time-limited people.
// So, why don't more people use them?
//
//
strategy(title="EMA Crossover Strategy", shorttitle="EMA-5-8-13 COS by TTS", overlay=true, pyramiding=0, default_qty_type=strategy.percent_of_equity, default_qty_value=100, currency=currency.TRY,commission_type=strategy.commission.percent,commission_value=0.04, process_orders_on_close = true, initial_capital = 100000)
// === GENERAL INPUTS ===
//strategy start date
start_year = input(defval=2020, title="Backtest Start Year")
// === LOGIC ===
short_period = input(type=input.integer,defval=5,minval=1,title="Length")
mid_period = input(type=input.integer,defval=8,minval=1,title="Length")
long_period = input(type=input.integer,defval=13,minval=1,title="Length")
longOnly = input(type=input.bool,defval=false,title="Long Only")
shortEma = ema(hl2,short_period)
midEma = ema(hl2,mid_period)
longEma = ema(hl2,long_period)
plot(shortEma,linewidth=2,color=color.red,title="Fast")
plot(midEma,linewidth=2,color=color.orange,title="Fast")
plot(longEma,linewidth=2,color=color.blue,title="Slow")
longEntry = ((shortEma > midEma) and crossover(shortEma,longEma)) or ((shortEma > longEma) and crossover(shortEma,midEma))
shortEntry =((shortEma < midEma) and crossunder(shortEma,longEma)) or ((shortEma < longEma) and crossunder(shortEma,midEma))
plotshape(longEntry ? close : na,style=shape.triangleup,color=color.green,location=location.belowbar,size=size.small,title="Long Triangle")
plotshape(shortEntry and not longOnly ? close : na,style=shape.triangledown,color=color.red,location=location.abovebar,size=size.small,title="Short Triangle")
plotshape(shortEntry and longOnly ? close : na,style=shape.xcross,color=color.black,location=location.abovebar,size=size.small,title="Exit Sign")
// === STRATEGY - LONG POSITION EXECUTION ===
enterLong() =>
longEntry
exitLong() =>
crossunder(shortEma,longEma)
strategy.entry(id="Long", long=strategy.long, when=enterLong())
strategy.close(id="Long", when=exitLong())
// === STRATEGY - SHORT POSITION EXECUTION ===
enterShort() =>
not longOnly and shortEntry
exitShort() =>
crossover(shortEma,longEma)
strategy.entry(id="Short", long=strategy.short, when=enterShort())
strategy.close(id="Short", when=exitShort())Published by ChaoZhang on the FMZ strategy square · last modified 2023-11-24. 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/Short-Term-Medium-Term-and-Long-Term-EMA-Crossover-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
- Crossover EMA Short term Trading StrategyPine Script · Trend following · 1h
- Dual ATR Channel Trend Following StrategyPine Script · Trend following · 15m
- Dual EMA Crossover Oscillation Tracking StrategyPine Script · Trend following · 5m