MACD crossover strategy

On ES futures, the MACD crossover lost $99,518 after costs and ran a $100,000 account down to its margin on November 24, 2017. The run stops there.

TrendIntraday5-minute barsBacktested Nov 6, 2026

MACD crossover backtest, the result

Contract

E-mini S&P 500

  • −$99.5K

    Net after costs

    After $85,443 in fees and slippage

  • 0 of 2

    Years that made money

    January 2016 to November 2017

  • −$104K

    Largest drawdown

    Peak to trough, 1 contract

  • −$33

    Average trade

    3,012 trades

MACD crossover on ES, net after costs by month, from January 2016 to November 2017: −$99,518; buying and holding one ES contract: +$236,212

Net after costs by month, January 2016 to November 2017.

MACD crossover rules

Buy when the MACD line crosses above its signal line, and sell when it crosses back below, on 5-minute bars.

A drawing of the rules on a made-up day, not a result.

  1. The MACD line is EMA(12) - EMA(26) of the five-minute close and the signal line its EMA(9), on every close of the Globex session.
  2. When the line crosses above the signal the strategy goes long, and when it crosses below it goes short, turning round on the opposite cross.
  3. New positions are taken only on bars that close in the day session before its exit minute.
  4. Whatever is open at the exit minute is closed: 15:45 New York on ES (day session 9:30-16:00), 14:30 on CL (day session 9:00-14:30).
Decides on
5-minute bars
Holds
Nothing overnight
Parameters
warmup 35, open minute 570, exit minute 945, bar minutes 5

Results by year

It made money in 0 of 2 years on ES. Its best year was 2016, −$46,596, and its worst 2016, −$46,596. The account was closed out on November 24, 2017, so the years stop there.

Net after costs by year on ES, with trades, win rate, largest drawdown and buying and holding the same contract
YearNetTradesWin rateDrawdownBuy and hold
2016−$46,5961,55432%−$50,667+$11,497
2017, Jan to Nov−$52,9221,45825%−$53,271+$21,936

MACD crossover backtest results

On ES it lost $14,075 before costs, then paid $85,443 in fees and slippage.

Net = before costs − fees − slippage
ES: −$14,075 − $10,143 − $75,300 = −$99,518
Every figure of the MACD crossover backtest, by contract
FigureESCL
Net after costs−$99,518−$99,021
Before costs−$14,075−$12,940
Fees$10,143$14,301
Slippage$75,300$71,780
Trades3,0123,589
Trades a year2,0081,436
Win rate28%32%
Profit factor0.610.76
Average trade−$33−$28
Average trade, 95% interval−$40 to −$26−$37 to −$18
Sharpe ratio-6.36-3.47
Largest drawdown−$103,589−$103,777
Time in the market17%15%
Buying and holding the contract+$236,212+$52,267

The whole interval is under zero: after costs, the average trade lost money.

MACD crossover code and Pine Script

The strategy as the backtest ran it, and the Pine Script our compiler wrote from it for TradingView.

MACD crossover
macd_crossover.py
1from atlantive import BUY, EMA, NEW_YORK, SELL, Bars, Session, Strategy2 3 4class MacdCrossover(Strategy):5    """MACD (12, 26, 9) crossover on five-minute bars, traded in the contract's day session.6 7    The MACD line is EMA(12) - EMA(26) of the five-minute close and the signal line its EMA(9), all8    on every close of the Globex session, alpha 2 / (n + 1). When the line crosses above the signal9    the strategy goes long, and when it crosses below it goes short, turning round on the opposite10    cross. New positions are taken only on bars that close in the day session before its exit11    minute, and whatever is open then is closed: 9:30 to 15:45 New York on the equity index, 9:0012    to 14:30 on crude oil. No cross is read before 35 bars (26 + 9) have closed on the contract.13    """14 15    data = "bars"16 17    class Params:18        warmup: int = 3519        open_minute: int = 57020        exit_minute: int = 94521        bar_minutes: int = 522        clip: int = 123 24    class State:25        clock: Session(zone=NEW_YORK)26        bars: Bars(interval_ns=300_000_000_000)27        fast: EMA(alpha=0.15384615384615385)28        slow: EMA(alpha=0.07407407407407407)29        signal: EMA(alpha=0.2)30        last_diff: float31        seen: int32 33    def on_event(self, view, ev, orders):34        self.clock.update(ev)35        if not self.bars.update(ev):36            return37        minute = self.clock.prev_minute38        close = float(self.bars.prev_close)39        self.fast.update(close)40        self.slow.update(close)41        line = self.fast.value - self.slow.value42        self.signal.update(line)43        diff = line - self.signal.value44        self.seen += 145        ends = minute + self.p.bar_minutes46        in_day = minute >= self.p.open_minute and ends < self.p.exit_minute47        crossed_up = self.seen > self.p.warmup and self.last_diff <= 0 and diff > 048        crossed_down = self.seen > self.p.warmup and self.last_diff >= 0 and diff < 049        self.last_diff = diff50        if view.position != 0 and not in_day:51            if view.position > 0:52                orders.ioc(side=SELL, qty=abs(view.position))53            else:54                orders.ioc(side=BUY, qty=abs(view.position))55            return56        if not in_day:57            return58        if crossed_up:59            if view.position < 0:60                orders.ioc(side=BUY, qty=abs(view.position) + self.p.clip)61            elif view.position == 0:62                orders.ioc(side=BUY, qty=self.p.clip)63        elif crossed_down:64            if view.position > 0:65                orders.ioc(side=SELL, qty=abs(view.position) + self.p.clip)66            elif view.position == 0:67                orders.ioc(side=SELL, qty=self.p.clip)
1//@version=62// MacdCrossover, 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("MacdCrossover", 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("MacdCrossover was exported for the 5 minute timeframe; set the chart to 5.")12 13warmup = input.int(35, "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 = 050var float _q_signal_value = 0.051var bool _q_signal_seeded = false52var int _q_signal_skipped = 053var int _q_signal_frozen = 054int minute_l = na55float close_l = na56float line_l = na57float diff = na58int ends = na59bool in_day = false60bool crossed_up = false61bool crossed_down = false62 63bool _q_running = true64minute_l := _q_int(_q_clock_prev_minute)65close_l := _q_close * 1.066_q_sample1 = close_l67if na(_q_sample1) or math.abs(_q_sample1) > 1.7976931348623157e30868    _q_fast_skipped := _q_fast_skipped + 169else70    if not _q_fast_seeded71        _q_fast_value := _q_sample172        _q_fast_seeded := true73    else74        _q_moved2 = _q_fast_value + 0.15384615384615385 * (_q_sample1 - _q_fast_value)75        if _q_moved2 == _q_fast_value and _q_sample1 != _q_fast_value76            _q_fast_frozen := _q_fast_frozen + 177        _q_fast_value := _q_moved278_q_sample3 = close_l79if na(_q_sample3) or math.abs(_q_sample3) > 1.7976931348623157e30880    _q_slow_skipped := _q_slow_skipped + 181else82    if not _q_slow_seeded83        _q_slow_value := _q_sample384        _q_slow_seeded := true85    else86        _q_moved4 = _q_slow_value + 0.07407407407407407 * (_q_sample3 - _q_slow_value)87        if _q_moved4 == _q_slow_value and _q_sample3 != _q_slow_value88            _q_slow_frozen := _q_slow_frozen + 189        _q_slow_value := _q_moved490line_l := _q_fast_value - _q_slow_value91_q_sample5 = line_l92if na(_q_sample5) or math.abs(_q_sample5) > 1.7976931348623157e30893    _q_signal_skipped := _q_signal_skipped + 194else95    if not _q_signal_seeded96        _q_signal_value := _q_sample597        _q_signal_seeded := true98    else99        _q_moved6 = _q_signal_value + 0.2 * (_q_sample5 - _q_signal_value)100        if _q_moved6 == _q_signal_value and _q_sample5 != _q_signal_value101            _q_signal_frozen := _q_signal_frozen + 1102        _q_signal_value := _q_moved6103diff := line_l - _q_signal_value104seen := _q_int(_q_iadd(seen, 1))105ends := _q_int(_q_iadd(minute_l, bar_minutes))106in_day := minute_l >= open_minute and ends < exit_minute107crossed_up := seen > warmup and last_diff <= 0 and diff > 0108crossed_down := seen > warmup and last_diff >= 0 and diff < 0109last_diff := diff110if _q_int(strategy.position_size) != 0 and not in_day111    if _q_int(strategy.position_size) > 0112        _q_qty7 = _q_int(math.abs(_q_int(strategy.position_size)))113        if _q_qty7 <= 0114            runtime.error("market order quantity must be a positive whole unit")115        _q_pos7 = _q_int(strategy.position_size)116        if _q_pos7 < 0 or _q_pos7 > 0 and _q_qty7 < _q_pos7117            runtime.error("this order would add to or partially reduce a position, so its trade ledger would not be faithful")118        strategy.order("q1", strategy.short, _q_qty7)119    else120        _q_qty8 = _q_int(math.abs(_q_int(strategy.position_size)))121        if _q_qty8 <= 0122            runtime.error("market order quantity must be a positive whole unit")123        _q_pos8 = _q_int(strategy.position_size)124        if _q_pos8 > 0 or _q_pos8 < 0 and _q_qty8 < -_q_pos8125            runtime.error("this order would add to or partially reduce a position, so its trade ledger would not be faithful")126        strategy.order("q2", strategy.long, _q_qty8)127    _q_running := false128if _q_running129    if not (in_day)130        _q_running := false131if _q_running132    if crossed_up133        if _q_int(strategy.position_size) < 0134            _q_qty9 = _q_int(_q_iadd(math.abs(_q_int(strategy.position_size)), clip))135            if _q_qty9 <= 0136                runtime.error("market order quantity must be a positive whole unit")137            _q_pos9 = _q_int(strategy.position_size)138            if _q_pos9 > 0 or _q_pos9 < 0 and _q_qty9 < -_q_pos9139                runtime.error("this order would add to or partially reduce a position, so its trade ledger would not be faithful")140            strategy.order("q3", strategy.long, _q_qty9)141        else if _q_int(strategy.position_size) == 0142            _q_qty10 = _q_int(clip)143            if _q_qty10 <= 0144                runtime.error("market order quantity must be a positive whole unit")145            _q_pos10 = _q_int(strategy.position_size)146            if _q_pos10 > 0 or _q_pos10 < 0 and _q_qty10 < -_q_pos10147                runtime.error("this order would add to or partially reduce a position, so its trade ledger would not be faithful")148            strategy.order("q4", strategy.long, _q_qty10)149    else if crossed_down150        if _q_int(strategy.position_size) > 0151            _q_qty11 = _q_int(_q_iadd(math.abs(_q_int(strategy.position_size)), clip))152            if _q_qty11 <= 0153                runtime.error("market order quantity must be a positive whole unit")154            _q_pos11 = _q_int(strategy.position_size)155            if _q_pos11 < 0 or _q_pos11 > 0 and _q_qty11 < _q_pos11156                runtime.error("this order would add to or partially reduce a position, so its trade ledger would not be faithful")157            strategy.order("q5", strategy.short, _q_qty11)158        else if _q_int(strategy.position_size) == 0159            _q_qty12 = _q_int(clip)160            if _q_qty12 <= 0161                runtime.error("market order quantity must be a positive whole unit")162            _q_pos12 = _q_int(strategy.position_size)163            if _q_pos12 < 0 or _q_pos12 > 0 and _q_qty12 < _q_pos12164                runtime.error("this order would add to or partially reduce a position, so its trade ledger would not be faithful")165            strategy.order("q6", strategy.short, _q_qty12)

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 ESU6CL: 128 contracts, CLG6 to CLU6
Costs paid
ES: $10,143 in fees, $75,300 in slippageCL: $14,301 in fees, $71,780 in slippage
Warm-up
None: each contract reads only its own bars
Flagged days
ES: NoneCL: None Days the data holds too few bars for. Flagged, not removed.

Could you pass
a prop firm challenge
with the MACD 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
Blue Guardian Futures Direct 50K
Most money

$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

Best 5%$13,143Worst 5%−$412
Day 060120180
Example: ES opening range breakout

Questions about the MACD crossover

What is the MACD crossover?

Buy when the MACD line crosses above its signal line, and sell when it crosses back below, on 5-minute bars. The rules follow Gerald Appel, The Moving Average Convergence-Divergence Trading Method (1979). Atlantive backtested it on CME futures from January 2016 to July 2026 after costs.

Is the MACD crossover profitable on ES futures?

Not in this backtest: on ES futures from January 2016 to November 2017 it lost $99,518 after costs, over 3,012 trades, positive in 0 of 2 years; on CL it lost $99,021. Buying and holding one ES contract over the same window made $236,212. The average trade was −$33, its 95% interval −$40 to −$26. These are hypothetical results of a backtest, not of trading.

What are the MACD crossover's rules?

The MACD line is EMA(12) - EMA(26) of the five-minute close and the signal line its EMA(9), on every close of the Globex session. When the line crosses above the signal 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 its exit minute. Whatever is open at the exit minute is closed: 15:45 New York on ES (day session 9:30-16:00), 14:30 on CL (day session 9:00-14:30). The rules follow Gerald Appel, The Moving Average Convergence-Divergence Trading Method (1979). Its parameters are the published ones, never tuned on the data it was tested on.

How was the MACD 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 MACD 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 MACD 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.

Rerun it in Atlantive. Change the market, the dates or a rule, then send it.

A new chat opens with this in the box

Backtest a MACD (12, 26, 9) crossover on five-minute bars on ES, 2016-01-02 to 2026-07-31, one contract on a $100,000 account with default costs: MACD on the whole Globex session, go long when the line crosses above the signal and short when it crosses below, entries only in the day session, flat at the session's exit.

Never sent for you

May we measure this visit?

With your consent, Google Analytics and PostHog count visits to these pages. You can separately allow Google Ads to measure visits from our ads. Visit measurement starts only after you choose. Cookie policy