MACD trading strategy: signals and filters

A MACD trading strategy needs an entry trigger, optional filters and an exit that works even when the entry filters fail. The example below uses a bullish signal-line cross, an optional positive-MACD filter and an optional price-above-EMA filter. Each module has a separate switch, so you can test what it changes. These are illustrative rules, not a backtested recommendation.

Draft the MACD strategy

Key Takeaways

  • A signal-line crossover and a MACD zero-line crossover are different events.
  • Turning off the fresh-cross requirement permits later entries while MACD remains above its signal line.
  • Entry filters must not prevent a stop or an opposite-cross exit from closing a position.
  • Compare turnover and drawdown under identical costs before attributing any benefit to a filter.

Fix the calculation before testing the rules

For this example, use closing prices, a 12-period fast EMA, a 26-period slow EMA and a 9-period EMA signal line on the chart timeframe. MACD is fast EMA minus slow EMA; the histogram is MACD minus signal. A positive histogram means MACD is above signal, even if both lines are below zero. TradingView also offers different average types, so record those choices with the periods. The MACD indicator guide linked below covers the calculation in more detail.

  • Use standard candlesticks and record the symbol, exchange, timeframe, session and test dates.
  • Use a 200-period EMA of close for the optional trend filter, on the same timeframe as MACD.
  • Load history before the test start for indicator warm-up; skip bars with unavailable values. Keep that history consistent across runs.
  • Keep the fast period below the slow period when comparing settings. The numbers here define a test baseline, not a universal optimum.

Make the three entry modules explicit

The signal module always requires MACD above its signal line. Its fresh-cross switch controls whether that state must have begun on the current bar. The other switches control optional zero-line and trend filters. Evaluate every gate at a confirmed bar close, while flat and with no pending entry.

Entry modules and their switch behavior
ModuleSwitch onSwitch off
C: fresh-cross requirementMACD is above signal now and was at or below signal on the previous barMACD is above signal now; a fresh cross is not required
Z: zero-line filterMACD is strictly above zeroPass regardless of the MACD sign
T: trend filterClose is strictly above the 200-period EMAPass regardless of price relative to the EMA
  • With C on, a cross rejected by Z or T expires. Do not enter later unless another bullish cross occurs.
  • With C off, an entry can occur later when the filters pass, including after a previous stop while MACD is still above signal. This changes the strategy, not just its display.
  • MACD equal to signal fails C in either mode. MACD equal to zero or close equal to the EMA fails the corresponding enabled filter.

Use a truth table to audit entries

C, Z and T below mean the evaluated gate results after applying the switches, not the switch positions. A disabled Z or T returns true. Submit an entry only when all three results are true and the position, warm-up and confirmed-close requirements are satisfied.

Long-entry truth table after switch evaluation
C passesZ passesT passesEntry permitted
TrueTrueTrueYes
TrueTrueFalseNo
TrueFalseTrueNo
TrueFalseFalseNo
FalseTrueTrueNo
FalseTrueFalseNo
FalseFalseTrueNo
FalseFalseFalseNo

Define the position, stop and exit before entry

This illustrative baseline is long-only, holds at most one position and uses a fixed quantity of one tradable unit with sufficient test capital. It does not add to positions or open shorts. Choose the instrument and its contract value before interpreting money risk. A short strategy needs a separate specification rather than an implicit reversal.

  • Submit a market entry after an eligible close. Under default TradingView calculation and fill settings, it normally fills at the next bar open.
  • At the signal close, freeze a stop distance D equal to twice the 14-period ATR, rounded up to a whole number of minimum ticks. Skip the entry if D is unavailable or nonpositive.
  • Attach an entry-linked bracket using that fixed tick distance: stop D below the actual fill and target 2D above it. Keep both distances unchanged throughout the trade. Check that protection is attached to the entry rather than first created at the following close.
  • Close the entire position on a confirmed bearish signal-line cross, independently of Z and T. Do not wait for MACD to cross zero. Cancel any remaining bracket orders once the position is closed.
  • A stop or target can fill before a close-based exit. If both prices occur within one bar, review the emulator assumptions and available intrabar data. Gaps and slippage can produce losses larger than D.
  • Do not re-enter on a bar where a position has exited. Allow no entries after the chosen test cutoff, then flatten any remaining position on the next available tick and include that exit in the results.

Work through a rejected cross and a filled trade

Hypothetical example: MACD moves from -0.4 to -0.2 while its signal moves from -0.3 to -0.25. This is a bullish signal-line cross below zero. With all switches on, C passes but Z fails, so there is no entry. If Z is off and close is above the trend EMA, the entry is eligible. With C on, a later move above zero does not revive the rejected cross.

  • For a separate hypothetical filled trade, suppose the frozen distance D is 2.00 and the actual entry fill is 100.50. The stop is 98.50 and the target is 104.50, before costs.
  • If a bearish cross occurs while neither bracket order has filled, request a market exit. A failed trend filter does not block it.
  • The 2-to-1 target-to-stop distance is not a measured reward-to-risk result. Early signal exits, gaps and trading costs change realized outcomes.

Keep signal confirmation separate from order fills

MACD values based on an open bar can change as price changes. Waiting for the close avoids acting on a temporary chart-timeframe cross, but it delays the decision. It does not guarantee a profitable signal or remove every source of repainting. This baseline uses no higher-timeframe request, pivot confirmation or backdated divergence marker.

  • Leave intrabar signal recalculation and same-close order processing off for the baseline. Record any later change to these settings.
  • Inspect signal bars and fill bars separately in the trade list. A close-based market exit also normally fills on the next available tick.
  • Keep alerts consistent with the confirmed-close rules. A strategy simulation does not itself place orders in a brokerage account.

Compare filters by turnover and drawdown

First keep C on and compare four configurations: no optional filters, Z only, T only, then Z and T together. Repeat those four with C off to isolate the cost of requiring a fresh cross. Hold periods, exits, quantity, dates and execution assumptions fixed across all eight runs. No results for these configurations have been measured for this page.

  • Record closed-trade count, total traded notional, time in market, net profit, average net trade and maximum drawdown. For this comparison, define turnover as total entry-plus-exit notional divided by the same initial capital.
  • Apply commission and realistic slippage assumptions to every run. State how spread is represented, and avoid counting it twice.
  • An added entry filter reduces the set of eligible bars, but does not guarantee fewer completed trades or lower drawdown. Changed entry timing affects which later signals arrive while flat.
  • Look for rejected early trends as well as avoided whipsaws. MACD and the trend EMA both use price averages, so their agreement is not independent evidence.
  • After choosing the module configuration, compare a small nearby parameter range on development data. Freeze the choice before using an untouched later period; changing it after that test makes the period part of development.

Pineify next step

Turn the rule specification into a strategy draft

Give AI Coding Agent the three gate definitions, long-only sizing and independent exit rules above. Ask for separate inputs for the switches and periods, then inspect the generated script and simulated trades in TradingView. Visual Pine Script Editor offers a visual construction path. Once the rules match, Strategy Optimizer can compare exposed inputs; a parameter ranking does not establish future returns.

This page is educational and does not provide investment advice. Its rule set and numerical examples are hypothetical and have not been backtested for this page. Trading can lose money, stops do not guarantee an execution price, and simulated or historical performance does not guarantee future results.

Frequently Asked Questions