0bps

Strategy vault / Trend following

An Advanced EMA Trend Following Strategy with Relaxed RSI and ATR Filters

This is an EMA trend following strategy incorporating RSI and ATR filters, aiming to capture long term trends. It utilizes fast and slow EMA crossovers along with a long term EMA trend filter, relaxed RSI…

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

IMG

Overview

This is an EMA trend following strategy incorporating RSI and ATR filters, aiming to capture long term trends. It utilizes fast and slow EMA crossovers along with a long term EMA trend filter, relaxed RSI oversold/overbought levels, and ATR-based consolidation detection to reduce whipsaws.

Strategy Logic

  1. Long when fast EMA (20) crosses above slow EMA (50)
  2. Short when fast EMA crosses below slow EMA
  3. Only trade in the direction of long term trend judged by 200 EMA
  4. Require RSI to be relatively oversold for longs and overbought for shorts
  5. Use ATR to detect consolidation zones to avoid false breakouts

Advantages

  1. EMA crossover system effective for trend following
  2. Additional long term filter avoids trading counter trend
  3. Relaxed RSI thresholds reduce missed profitable trends
  4. ATR filter helps navigate choppy consolidation periods

Risks

  1. Can underperform in strongly trending bull markets
  2. Whipsaws possible with EMA crossovers during consolidation
  3. RSI and ATR parameters need individual optimization per instrument

Enhancements

  1. Optimize EMA lengths for different instruments
  2. Fine tune RSI and ATR parameters to improve consolidation detection
  3. Consider adding profit taking via trailing stops

Conclusion

Overall an effective system for long term trend following across various instruments. Optimization of parameters combined with prudent risk management can lead to positive results, especially in persistent bull market environments. The incorporation of auxiliary indicators like RSI and ATR make this an advanced EMA trend system.

Parameters

NameDefaultWhat it controls
v_input_120Short EMA
v_input_250Long EMA
v_input_3200Trend EMA
v_input_414RSI Length
v_input_514ATR Length

Source Pine Script

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

//@version=5
strategy("EMA Strategy with Trend Filter", overlay=true)

// Параметры EMA
shortEmaLength = input(20, title="Short EMA")
longEmaLength = input(50, title="Long EMA")
trendEmaLength = input(200, title="Trend EMA")
shortEma = ta.ema(close, shortEmaLength)
longEma = ta.ema(close, longEmaLength)
trendEma = ta.ema(close, trendEmaLength)

// Параметры RSI
rsiLength = input(14, title="RSI Length")
rsi = ta.rsi(close, rsiLength)

// Параметры ATR
atrLength = input(14, title="ATR Length")
atr = ta.atr(atrLength)

// Логика для сигналов покупки и продажи с фильтром по тренду
buySignal = ta.crossover(shortEma, longEma) and close > trendEma
sellSignal = ta.crossunder(shortEma, longEma) and close < trendEma

// Вход в сделки
if (buySignal)
    strategy.entry("Buy", strategy.long)

if (sellSignal)
    strategy.entry("Sell", strategy.short)

// Выход из сделок на основе EMA пересечений
exitBuySignal = ta.crossunder(shortEma, longEma)
exitSellSignal = ta.crossover(shortEma, longEma)

if (exitBuySignal)
    strategy.close("Buy")

if (exitSellSignal)
    strategy.close("Sell")

// Визуализация EMA
plot(shortEma, color=color.blue, title="Short EMA")
plot(longEma, color=color.red, title="Long EMA")
plot(trendEma, color=color.green, title="Trend EMA")

// Отдельная панель для RSI и его визуализация
plot(rsi, title="RSI", color=color.purple, linewidth=2)

// Отдельная панель для ATR и его визуализация
plot(atr, title="ATR", color=color.orange, linewidth=2)

Published by ChaoZhang on the FMZ strategy square · last modified 2024-02-29. 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/An-Advanced-EMA-Trend-Following-Strategy-with-Relaxed-RSI-and-ATR-Filters.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