Skip to content
⚠️ Not financial advice. For educational use only.
BotsTrendFollowingBot

TrendFollowingBot

A pragmatic trend-following strategy that pairs an EMA trend filter with two complementary entry patterns:

  1. a crossback into the trend (price crosses the EMA and the very next bar opens on the trend side), and
  2. a volatility-scaled breakout away from the EMA (the current open is beyond the EMA by k × ATR).

Stops, trailing logic, and position sizing come from your configured ExitStrategy (e.g., ATR-trailing), so this bot focuses purely on when to enter. Signals are read from the previous closed bar, and orders are placed at the current bar open to avoid look-ahead.


Overview

  • What it does: Uses an EMA to define trend and ATR to demand “enough distance” before entering.
  • Two entry modes:
    • Crossback confirms that price actually crossed the EMA and opened on the trend side.
    • ATR breakout requires the open to be at least k × ATR beyond the EMA (default k = 1.5).
  • Risk first: Exits and sizing are handled by your exit module (e.g., ATR-trailing, fixed-ratio).
  • Timing: Compute on bar t-1; place at the open of bar t (no look-ahead).
  • Use cases: Clean template for experimenting with EMA spans, ATR multipliers, and different exit policies.

How it works (at a glance)

Let:

  • ema_prev = EMA value on the previous (closed) bar
  • prev_close = previous bar’s close
  • entry_open = current bar open (execution price)
  • atr_prev = previous bar’s ATR
  • k = breakout_atr_mult (default 1.5)

1) Crossback into trend

  • Long: prev_close ≤ ema_prev and entry_open > ema_prevbuy
  • Short: prev_close ≥ ema_prev and entry_open < ema_prevsell

2) EMA ± (k × ATR) breakout

  • Long: entry_open > ema_prev + k × atr_prevbuy
  • Short: entry_open < ema_prev − k × atr_prevsell

If neither rule fires, the bot stays flat for that bar.


Key parameters

ParameterDefaultDescription
trend_ema_span50EMA length for the trend filter (expects a column like ema_50).
breakout_atr_mult1.5ATR multiplier for the EMA-distance breakout threshold.

Open the notebook

View Notebook →