9 and 21 EMA crossover strategy
On ES futures, the 9 and 21 EMA crossover lost $99,526 after costs and ran a $100,000 account down to its margin on March 3, 2020. The run stops there.
TrendIntraday5-minute barsBacktested Nov 6, 2026
9 and 21 EMA crossover backtest, the result
Contract
E-mini S&P 500
-
−$99.5K
Net after costs
After $105,276 in fees and slippage
-
1 of 5
Years that made money
January 2016 to March 2020
-
−$106K
Largest drawdown
Peak to trough, 1 contract
-
−$27
Average trade
3,709 trades
Net after costs by month, January 2016 to March 2020.
9 and 21 EMA crossover rules
Buy when the 9-bar EMA crosses above the 21-bar EMA on 5-minute bars, and sell when it crosses back below.
A drawing of the rules on a made-up day, not a result.
- The 9 and 21 bar EMAs (alpha 2 / (n + 1)) run on every five-minute close of the Globex session.
- When the 9 crosses above the 21 the strategy goes long, and when it crosses below it goes short, turning round on the opposite cross.
- New positions are taken only on bars that close in the day session before 15:45 New York.
- Whatever is open is closed at 15:45 New York.
- Decides on
- 5-minute bars
- Holds
- Nothing overnight
- Parameters
- warmup 21, open minute 570, exit minute 945, bar minutes 5
Results by year
It made money in 1 of 5 years on ES. Its best year was 2018, +$7,237, and its worst 2019, −$35,656. The account was closed out on March 3, 2020, so the years stop there.
| Year | Net | Trades | Win rate | Drawdown | Buy and hold |
|---|---|---|---|---|---|
| 2016 | −$26,434 | 877 | 30% | −$33,364 | +$11,497 |
| 2017 | −$34,027 | 926 | 25% | −$34,596 | +$21,936 |
| 2018 | +$7,237 | 898 | 32% | −$27,506 | −$8,589 |
| 2019 | −$35,656 | 862 | 30% | −$39,852 | +$35,436 |
| 2020, Jan to Mar | −$10,646 | 146 | 34% | −$18,405 | +$27,761 |
9 and 21 EMA crossover backtest results
On ES it made $5,750 before costs. Fees and slippage took $105,276, more than all of it.
- Net = before costs − fees − slippage
- ES: $5,750 − $12,551 − $92,725 = −$99,526
The whole interval is under zero: after costs, the average trade lost money.
9 and 21 EMA crossover code and Pine Script
The strategy as the backtest ran it, and the Pine Script our compiler wrote from it for TradingView.
1from atlantive import BUY, EMA, NEW_YORK, SELL, Bars, Session, Strategy2 3 4class EmaCrossover(Strategy):5 """9 and 21 EMA crossover on five-minute bars, traded in the New York day session.6 7 Both averages run on every five-minute close of the Globex session, alpha 2 / (n + 1). When the8 9 crosses above the 21 the strategy goes long, and when it crosses below it goes short, turning9 the position round on the opposite cross. New positions are taken only on bars that close in the10 day session before 15:45, and whatever is open at 15:45 is closed. No cross is read before 2111 bars have closed on the contract.12 """13 14 data = "bars"15 16 class Params:17 warmup: int = 2118 open_minute: int = 57019 exit_minute: int = 94520 bar_minutes: int = 521 clip: int = 122 23 class State:24 clock: Session(zone=NEW_YORK)25 bars: Bars(interval_ns=300_000_000_000)26 fast: EMA(alpha=0.2)27 slow: EMA(alpha=0.09090909090909091)28 last_diff: float29 seen: int30 31 def on_event(self, view, ev, orders):32 self.clock.update(ev)33 if not self.bars.update(ev):34 return35 minute = self.clock.prev_minute36 day = self.clock.prev_day37 close = float(self.bars.prev_close)38 self.fast.update(close)39 self.slow.update(close)40 diff = self.fast.value - self.slow.value41 self.seen += 142 ends = minute + self.p.bar_minutes43 in_day = minute >= self.p.open_minute and ends < self.p.exit_minute44 crossed_up = self.seen > self.p.warmup and self.last_diff <= 0 and diff > 045 crossed_down = self.seen > self.p.warmup and self.last_diff >= 0 and diff < 046 self.last_diff = diff47 if view.position != 0 and not in_day:48 if view.position > 0:49 orders.ioc(side=SELL, qty=abs(view.position))50 else:51 orders.ioc(side=BUY, qty=abs(view.position))52 return53 if not in_day:54 return55 if crossed_up:56 if view.position < 0:57 orders.ioc(side=BUY, qty=abs(view.position) + self.p.clip)58 elif view.position == 0:59 orders.ioc(side=BUY, qty=self.p.clip)60 elif crossed_down:61 if view.position > 0:62 orders.ioc(side=SELL, qty=abs(view.position) + self.p.clip)63 elif view.position == 0:64 orders.ioc(side=SELL, qty=self.p.clip) 1//@version=62// EmaCrossover, exported from Atlantive.3// Chart simulation: decisions use completed bars; market orders fill at the next bar open.4// Native fills, fees, data gaps and session boundaries can differ. Re-test on the target symbol.5// Use standard time-based candles and the strategy's timeframe, not synthetic chart prices.6// Pine v6 trims old trade records at its order limit; the strategy keeps calculating.7// Margin is explicitly disabled to retain the native signal simulation's sizing.8strategy("EmaCrossover", overlay=true, initial_capital=100000.0, commission_type=strategy.commission.cash_per_contract, commission_value=1.68, default_qty_type=strategy.fixed, default_qty_value=1, calc_on_every_tick=false, process_orders_on_close=false, slippage=0, margin_long=0, margin_short=0)9 10if timeframe.in_seconds() != 30011 runtime.error("EmaCrossover was exported for the 5 minute timeframe; set the chart to 5.")12 13warmup = input.int(21, "warmup", minval=-9007199254740991, maxval=9007199254740991)14open_minute = input.int(570, "open_minute", minval=-9007199254740991, maxval=9007199254740991)15exit_minute = input.int(945, "exit_minute", minval=-9007199254740991, maxval=9007199254740991)16bar_minutes = input.int(5, "bar_minutes", minval=-9007199254740991, maxval=9007199254740991)17clip = input.int(1, "clip", minval=-9007199254740991, maxval=9007199254740991)18 19_q_safe_int(x) =>20 if math.abs(x) > 900719925474099121 runtime.error("an integer left the exact chart range; this run cannot be compared faithfully")22 int(x)23_q_int(x) => _q_safe_int(x < 0 ? math.ceil(x) : math.floor(x))24_q_iadd(a, b) => _q_safe_int(a + b)25 26_q_close = _q_int(math.round(close * 1000000000.0))27 28var float last_diff = 0.029var int seen = 030// clock: completed chart bar's opening timestamp (America/New_York).31_q_clock_prev_minute = hour(time, "America/New_York") * 60 + minute(time, "America/New_York")32_q_clock_prev_second = _q_clock_prev_minute * 60 + second(time, "America/New_York")33_q_clock_prev_weekday = (dayofweek(time, "America/New_York") + 5) % 734_q_clock_prev_day = int(timestamp("UTC", year(time, "America/New_York"), month(time, "America/New_York"), dayofmonth(time, "America/New_York"), 0, 0) / 86400000)35// Current clock: this chart bar's scheduled close, known without a future bar.36// A native next event after a data gap can carry a later time.37_q_clock_minute = hour(time_close, "America/New_York") * 60 + minute(time_close, "America/New_York")38_q_clock_second = _q_clock_minute * 60 + second(time_close, "America/New_York")39_q_clock_weekday = (dayofweek(time_close, "America/New_York") + 5) % 740_q_clock_day = int(timestamp("UTC", year(time_close, "America/New_York"), month(time_close, "America/New_York"), dayofmonth(time_close, "America/New_York"), 0, 0) / 86400000)41_q_clock_seen = 142var float _q_fast_value = 0.043var bool _q_fast_seeded = false44var int _q_fast_skipped = 045var int _q_fast_frozen = 046var float _q_slow_value = 0.047var bool _q_slow_seeded = false48var int _q_slow_skipped = 049var int _q_slow_frozen = 050int minute_l = na51int day = na52float close_l = na53float diff = na54int ends = na55bool in_day = false56bool crossed_up = false57bool crossed_down = false58 59bool _q_running = true60minute_l := _q_int(_q_clock_prev_minute)61day := _q_int(_q_clock_prev_day)62close_l := _q_close * 1.063_q_sample1 = close_l64if na(_q_sample1) or math.abs(_q_sample1) > 1.7976931348623157e30865 _q_fast_skipped := _q_fast_skipped + 166else67 if not _q_fast_seeded68 _q_fast_value := _q_sample169 _q_fast_seeded := true70 else71 _q_moved2 = _q_fast_value + 0.2 * (_q_sample1 - _q_fast_value)72 if _q_moved2 == _q_fast_value and _q_sample1 != _q_fast_value73 _q_fast_frozen := _q_fast_frozen + 174 _q_fast_value := _q_moved275_q_sample3 = close_l76if na(_q_sample3) or math.abs(_q_sample3) > 1.7976931348623157e30877 _q_slow_skipped := _q_slow_skipped + 178else79 if not _q_slow_seeded80 _q_slow_value := _q_sample381 _q_slow_seeded := true82 else83 _q_moved4 = _q_slow_value + 0.09090909090909091 * (_q_sample3 - _q_slow_value)84 if _q_moved4 == _q_slow_value and _q_sample3 != _q_slow_value85 _q_slow_frozen := _q_slow_frozen + 186 _q_slow_value := _q_moved487diff := _q_fast_value - _q_slow_value88seen := _q_int(_q_iadd(seen, 1))89ends := _q_int(_q_iadd(minute_l, bar_minutes))90in_day := minute_l >= open_minute and ends < exit_minute91crossed_up := seen > warmup and last_diff <= 0 and diff > 092crossed_down := seen > warmup and last_diff >= 0 and diff < 093last_diff := diff94if _q_int(strategy.position_size) != 0 and not in_day95 if _q_int(strategy.position_size) > 096 _q_qty5 = _q_int(math.abs(_q_int(strategy.position_size)))97 if _q_qty5 <= 098 runtime.error("market order quantity must be a positive whole unit")99 _q_pos5 = _q_int(strategy.position_size)100 if _q_pos5 < 0 or _q_pos5 > 0 and _q_qty5 < _q_pos5101 runtime.error("this order would add to or partially reduce a position, so its trade ledger would not be faithful")102 strategy.order("q1", strategy.short, _q_qty5)103 else104 _q_qty6 = _q_int(math.abs(_q_int(strategy.position_size)))105 if _q_qty6 <= 0106 runtime.error("market order quantity must be a positive whole unit")107 _q_pos6 = _q_int(strategy.position_size)108 if _q_pos6 > 0 or _q_pos6 < 0 and _q_qty6 < -_q_pos6109 runtime.error("this order would add to or partially reduce a position, so its trade ledger would not be faithful")110 strategy.order("q2", strategy.long, _q_qty6)111 _q_running := false112if _q_running113 if not (in_day)114 _q_running := false115if _q_running116 if crossed_up117 if _q_int(strategy.position_size) < 0118 _q_qty7 = _q_int(_q_iadd(math.abs(_q_int(strategy.position_size)), clip))119 if _q_qty7 <= 0120 runtime.error("market order quantity must be a positive whole unit")121 _q_pos7 = _q_int(strategy.position_size)122 if _q_pos7 > 0 or _q_pos7 < 0 and _q_qty7 < -_q_pos7123 runtime.error("this order would add to or partially reduce a position, so its trade ledger would not be faithful")124 strategy.order("q3", strategy.long, _q_qty7)125 else if _q_int(strategy.position_size) == 0126 _q_qty8 = _q_int(clip)127 if _q_qty8 <= 0128 runtime.error("market order quantity must be a positive whole unit")129 _q_pos8 = _q_int(strategy.position_size)130 if _q_pos8 > 0 or _q_pos8 < 0 and _q_qty8 < -_q_pos8131 runtime.error("this order would add to or partially reduce a position, so its trade ledger would not be faithful")132 strategy.order("q4", strategy.long, _q_qty8)133 else if crossed_down134 if _q_int(strategy.position_size) > 0135 _q_qty9 = _q_int(_q_iadd(math.abs(_q_int(strategy.position_size)), clip))136 if _q_qty9 <= 0137 runtime.error("market order quantity must be a positive whole unit")138 _q_pos9 = _q_int(strategy.position_size)139 if _q_pos9 < 0 or _q_pos9 > 0 and _q_qty9 < _q_pos9140 runtime.error("this order would add to or partially reduce a position, so its trade ledger would not be faithful")141 strategy.order("q5", strategy.short, _q_qty9)142 else if _q_int(strategy.position_size) == 0143 _q_qty10 = _q_int(clip)144 if _q_qty10 <= 0145 runtime.error("market order quantity must be a positive whole unit")146 _q_pos10 = _q_int(strategy.position_size)147 if _q_pos10 < 0 or _q_pos10 > 0 and _q_qty10 < _q_pos10148 runtime.error("this order would add to or partially reduce a position, so its trade ledger would not be faithful")149 strategy.order("q6", strategy.short, _q_qty10) How it was tested
The published rules, never tuned on the data, with real costs on every fill. Every strategy here is tested the same way.
- Market data
- CME Globex 5-minute bars, Jan 2, 2016 to Jul 31, 2026
- Rules
- As published, with the published parameters, never tuned on this data
- Size
- 1 contract on every trade
- Contract
- The most traded one, rolled as the market moved
- Fees
- The broker's $0.50 a side, plus CME's and the NFA's fees when the window opens
- Slippage
- 1 tick a side on ES, 2 in NQ's regular session and 3 outside it, 1 on other markets, where it was not measured
- Stops and entries
- Tested on 1-second closes and filled at the next bar
- Contracts traded
- ES: 43 contracts, ESH6 to ESU6NQ: 43 contracts, NQH6 to NQU6
- Costs paid
- ES: $12,551 in fees, $92,725 in slippageNQ: $12,519 in fees, $74,165 in slippage
- Warm-up
- None: each contract reads only its own bars
- Flagged days
- ES: NoneNQ: None Days the data holds too few bars for. Flagged, not removed.
Could you pass
a prop firm challenge
with the 9 and 21 EMA crossover?
A backtest says whether a strategy makes money. Our AI runs its trades through the rules of every prop firm account, then tells you which one it would pass and at what size.
No card needed. Free credits every month.
- Most money: Blue Guardian Futures Direct 50K, $5,084
- Best payout odds: Funded Futures Family Straight to Funded 50K, 98%
- Fastest pass: Phidias Express to Live 50K, 11 days
$5,084
Median net across 2,000 simulated runs
- Evaluation
- None
- First payout
- 97%
- Fees paid
- $1,521
Median net across 2,000 simulated runs
Questions about the 9 and 21 EMA crossover
What is the 9 and 21 EMA crossover?
Buy when the 9-bar EMA crosses above the 21-bar EMA on 5-minute bars, and sell when it crosses back below. Atlantive backtested it on CME futures from January 2016 to July 2026 after costs.
Is the 9 and 21 EMA crossover profitable on ES futures?
Not in this backtest: on ES futures from January 2016 to March 2020 it lost $99,526 after costs, over 3,709 trades, positive in 1 of 5 years; on NQ it lost $99,019. Buying and holding one ES contract over the same window made $236,212. The average trade was −$27, its 95% interval −$39 to −$14. These are hypothetical results of a backtest, not of trading.
What are the 9 and 21 EMA crossover's rules?
The 9 and 21 bar EMAs (alpha 2 / (n + 1)) run on every five-minute close of the Globex session. When the 9 crosses above the 21 the strategy goes long, and when it crosses below it goes short, turning round on the opposite cross. New positions are taken only on bars that close in the day session before 15:45 New York. Whatever is open is closed at 15:45 New York. Its parameters are the published ones, never tuned on the data it was tested on.
How was the 9 and 21 EMA crossover backtested?
On CME Globex data from January 2016 to July 2026, one contract, on 5-minute bars, rolled to the most traded contract as the market moved, with the broker's and CME's fees and the slippage measured on each market. Stops and entries were tested on 1-second closes and filled at the next bar. Nothing was fitted to the data.
Can I run the 9 and 21 EMA crossover in TradingView?
Yes. The page carries its Pine Script, written by Atlantive's compiler from the same code the backtest ran, for TradingView's strategy tester. A chart's fills and fees can differ from the backtest's, so test it again on the symbol you trade.
Can I change the 9 and 21 EMA crossover and test it again?
Yes. Rerun it in Atlantive: a new chat opens with the strategy's rules in the box, for you to read, change and send. The agent writes the code, backtests it on the market and window you name, and reports the result with its costs.