0bps

Strategy vault / Trend following

Trend Following Strategy Based on Dual EMA

This strategy is named "Trend Following Strategy Based on Dual EMA". It calculates two EMA lines of different periods and judges trend direction based on their relationship, to follow trends. Specifically, the…

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

This strategy is named "Trend Following Strategy Based on Dual EMA". It calculates two EMA lines of different periods and judges trend direction based on their relationship, to follow trends.

Specifically, the trading logic is:

  1. Calculate the 50-day EMA and 200-day EMA.

  2. When the 50-day EMA crosses above the 200-day EMA, it signals an uptrend, thus going long.

  3. When the 50-day EMA crosses below the 200-day EMA, it flags a downtrend, thus going short.

  4. When trend reversal happens, existing positions are closed and direction is switched to the new trend.

The advantage of this strategy is using EMA "golden cross" and "dead cross" to determine the main trend direction. But EMA lagging requires parameter optimization, plus stop loss to manage risks.

In general, the dual EMA strategy suits mid-to-long term positioning by timely capturing major trend reversals for trend following. But traders still need to watch more indicators and maintain flexibility in strategy adjustment.

Source Pine Script

/*backtest
start: 2023-08-13 00:00:00
end: 2023-09-12 00:00:00
period: 2h
basePeriod: 15m
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/
// © Sonu1997

//@version=4
//@version=5
strategy('moving average strategy', overlay=true)

ema50 =ema(close, 50)
ema200 =ema(close, 200)



long = ema50 > ema200
short = ema50 < ema200

strategy.entry('long', strategy.long,  0, when=long)
strategy.entry('short', strategy.short,  0, when=short)

strategy.close('long', when=short)
strategy.close('short', when=long)

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