0bps

Strategy vault / Trend following

Trend Following Strategy Based on EMA and SMA Crossover

The "Trend Following Strategy Based on EMA and SMA Crossover" is a trend-following trading strategy based on the crossover of Exponential Moving Averages (EMAs) and Simple Moving Averages (SMAs). This strategy aims…

Author
ChaoZhang
Complexity
Starter · 39 lines
Parameters
0
Backtest bar
1h · intraday
Tested on
BTC/USDT · futures

IMG

Overview

The "Trend Following Strategy Based on EMA and SMA Crossover" is a trend-following trading strategy based on the crossover of Exponential Moving Averages (EMAs) and Simple Moving Averages (SMAs). This strategy aims to identify potential buy and sell signals by capturing moments when the short-term EMA crosses above the long-term SMA.

Strategy Logic

This strategy generates trading signals based on two conditions:

  1. The latest 5-period EMA crossed above the latest 20-period SMA
  2. On the 4-hour timeframe, the latest 5-period EMA crossed above the latest 20-period SMA

When both conditions are true, a buy signal is generated. When both conditions are false, a sell signal is generated.

By comparing EMA and SMA crossovers across different timeframes, this strategy comprehensively judges the trend direction and generates trading signals. The short-term EMA reflects price changes more sensitively while the long-term SMA has better trend filtering capability. When the short-term EMA crosses above the long-term SMA, it indicates a slight trend reversal and generates a buy signal. Conversely, when the short-term EMA crosses below the long-term SMA, it indicates a trend reversal and generates a sell signal.

Adding the 4-hour EMA and SMA crossover filters out short-term noise and makes trading signals more reliable.

Advantages of the Strategy

This strategy has the following advantages:

  1. Simple and easy to understand
  2. Quick response, timely captures trend reversal
  3. Noise filtering by incorporating multiple timeframes

Risks of the Strategy

There are also some risks with this strategy:

  1. Prone to false signals, signals should be carefully validated
  2. Does not cope well with trendless markets
  3. EMA and SMA parameters need to be chosen carefully

Risks can be managed through incorporating stop loss/take profit, parameter optimization etc.

Enhancement Areas

Some ways to enhance this strategy:

  1. Test more EMA and SMA parameter combinations
  2. Add other indicators for signal validation e.g. MACD, Bollinger Bands
  3. Build a dynamic stop loss mechanism
  4. Filter by trading volume

Conclusion

In summary, this is a basic trend following strategy using simple EMA and SMA crossover rules. It can be improved via parameter optimization, signal filtering etc. to adapt better and improve strategy performance.

Source Pine Script

/*backtest
start: 2023-12-17 00:00:00
end: 2024-01-12 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=5
strategy("EMA and SMA Crossover Strategy", shorttitle="Shashank Cross", overlay=true)

// Condition 1: Latest EMA (Close, 5) crossed above Latest SMA (Close, 20)
ema5 = ta.ema(close, 5)
sma20 = ta.sma(close, 20)

condition1 = ta.crossover(ema5, sma20)

// Condition 2: [0] 4-hour EMA ([0] 4-hour Close, 5) crossed above [0] 4-hour SMA ([0] 4-hour Close, 20)
ema5_4h = request.security(syminfo.tickerid, "240", ta.ema(close, 5))
sma20_4h = request.security(syminfo.tickerid, "240", ta.sma(close, 20))

condition2 = ta.crossover(ema5_4h, sma20_4h)

// Combine both conditions for a buy signal
buy_signal = condition1 and condition2

// Plotting signals on the chart
plotshape(buy_signal, color=color.green, style=shape.labelup, location=location.belowbar, size=size.small, text="Buy Signal")

// Strategy logic
if (buy_signal)
    strategy.entry("Buy", strategy.long)

// Exit long position on the next bar at market price
if (ta.barssince(buy_signal) == 1)
    strategy.close("Exit")

// You can add more code for stop-loss, take-profit, etc., as per your strategy.

Published by ChaoZhang on the FMZ strategy square · last modified 2024-01-17. 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/Trend-Following-Strategy-Based-on-EMA-and-SMA-Crossover.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