Skip to main content

What this builds

  • MACD line (fast EMA - slow EMA)
  • Signal line (EMA of MACD)
  • Histogram (MACD - signal) with green/red coloring
  • Zero line reference

Full script

With divergence coloring

Color the MACD line based on whether it is above or below the signal:

Understanding the return value

ta.macd returns an object with three fields: All three are NaN during the warmup period (first slowLen + sigLen - 2 bars approximately).

Histogram color tip

The histogram uses colorUp and colorDown based on whether the value is positive or negative:
  • hist > 0 (MACD above signal) uses colorUp
  • hist <= 0 (MACD below signal) uses colorDown

Oscillators

ta.macd function reference.

Plot styles

histogram style and colorUp/colorDown options.

NaN handling

Why na(m.macd) check is needed during warmup.