Noon Barbari
Registrarse
Todos los artículos
tutorialPublicado ·8 min de lectura

How to backtest a MACD strategy on crypto, the right way

MACD crossovers look great on a chart and fall apart in a backtest unless you test them properly. Here's how to define, run, and validate a MACD strategy on crypto data.

En esta página

MACD is a momentum indicator that looks tailor-made for mechanical rules: a line crosses a signal line, you buy or sell. On a chart, the crosses line up beautifully with past moves — which is exactly why eyeballing it is dangerous. Hindsight makes every crossover look obvious. A backtest is how you find out whether the crosses are tradeable in real time or just pretty after the fact.

Step 1: pin down what "a MACD signal" means

MACD gives you three things to build rules from: the MACD line (the gap between a fast and slow EMA), the signal line (a smoothing of the MACD line), and the histogram (the difference between the two). "A MACD buy" could mean the MACD line crossing above the signal line, the histogram crossing zero, or the MACD line crossing above the zero line — and these fire at different times with different results. Pick one and state it exactly. A vague signal can't be tested.

The classic version is the signal-line crossover: go long when the MACD line crosses above the signal line, flat or short when it crosses below. That is a trend-following rule, so it will whipsaw in a range — which is the first thing your backtest should reveal.

Step 2: add the parts a crossover alone is missing

A crossover is an entry trigger and nothing else. To be testable it needs an exit and a stop, and usually a filter. Because MACD is a trend tool, the most useful filter is a longer-term trend gate — only take long crossovers when price is above a slow moving average, so you are trading crosses *with* the bigger trend rather than against it. Without that gate, a MACD crossover system spends ranges getting chopped to pieces.

Step 3: run it and judge it on risk, not return

When the backtest finishes, ignore the headline return and look at how it was earned. Check maximum drawdown, the Sharpe ratio, profit factor, and the number of trades. A MACD system that made its money from two lucky trades over three years is not a strategy; the trade count tells you whether the result is statistically meaningful or a coincidence.

Here is a complete trend-filtered MACD rule you can drop into a backtest:

rules.yaml — MACD signal-line crossover, gated by a 200-EMA trend
strategy:
  name: macd_trend
  indicators:
    - { id: macd, kind: MACD, fast: 12, slow: 26, signal: 9 }
    - { id: ema,  kind: EMA, period: 200 }
  rules:
    entry:
      all:
        - { type: above, left: close, right: ema }          # uptrend only
        - { type: crosses_above, left: macd.line, right: macd.signal }
    exit:
      any:
        - { type: crosses_below, left: macd.line, right: macd.signal }
  risk:
    size_pct: 0.5
    stop_loss_atr: 2.5

Step 4: validate before you trust

The MACD periods (12, 26, 9) are conventions, not laws — which tempts people to sweep them for the combination that maximised past returns. That is the fastest route to a curve-fit system that dies live. Run walk-forward optimization: tune the periods on early data, test them on the next unseen window, and roll forward. If the "best" MACD settings only work on the slice they were chosen from, you have found overfitting, not an edge. If they hold up across windows, you have a candidate worth paper-trading.

Build and test it in Noon Barbari

MACD is built in, and the visual strategy designer lets you express the crossover, the trend gate, the exit, and the stop without writing code. Backtest it over years of crypto history in seconds, then walk-forward the periods to separate signal from noise. The free tier runs the whole thing for one strategy.

Pruébalo con tus datos

Cada concepto de arriba está implementado en la plataforma. Backtest, walk-forward, paper trading, luego live — el mismo conjunto de reglas en cada etapa.

Lecturas relacionadas

Guías relacionadas

Términos clave