How RSI and MACD Are Calculated

RSI and MACD calculation formulas parameters and data requirements.

Direct answer

RSI (Relative Strength Index) and MACD (Moving Average Convergence Divergence) are calculated from a historical price series and a small set of settings.

  • RSI converts recent price momentum into a 0–100 scale using average gains and average losses over a chosen lookback window.
  • MACD uses exponential moving averages (EMAs) to measure the difference between a faster and a slower trend estimate, then optionally smooths that difference with another EMA.

To explain and verify either indicator, you only need (1) a time-ordered list of prices, (2) the indicator’s parameters (such as lookback length or EMA spans), and (3) the exact calculation method (especially for RSI’s averaging). Because the method depends on these choices, results can differ across charting tools and settings.

Mechanism and definitions

RSI: definition of the moving momentum inputs

RSI starts from price changes between consecutive data points.

  1. Compute the change:
    • (\Delta_t = \text{Price}t - \text{Price}{t-1})
  2. Split changes into gains and losses:
    • (\text{Gain}_t = \max(\Delta_t, 0))
    • (\text{Loss}_t = \max(-\Delta_t, 0))
  3. Over a chosen lookback window of length (N), compute average gains and average losses.

A common RSI formulation uses Wilder-style smoothing. With Wilder smoothing, you first compute the initial averages using the first (N) gains and losses, then update recursively:

  • Initial (for the first RSI value):
    • (\text{AvgGain}N = \frac{1}{N}\sum{i=1}^{N}\text{Gain}_i)
    • (\text{AvgLoss}N = \frac{1}{N}\sum{i=1}^{N}\text{Loss}_i)
  • Update (for (t > N)):
    • (\text{AvgGain}t = \frac{(\text{AvgGain}{t-1}\cdot (N-1)) + \text{Gain}_t}{N})
    • (\text{AvgLoss}t = \frac{(\text{AvgLoss}{t-1}\cdot (N-1)) + \text{Loss}_t}{N})

Then compute the relative strength ratio:

  • (\text{RS}_t = \frac{\text{AvgGain}_t}{\text{AvgLoss}_t})

Finally convert to the RSI scale:

  • (\text{RSI}_t = 100 - \frac{100}{1 + \text{RS}_t})

Important edge case: if (\text{AvgLoss}_t = 0), then (\text{RS}_t) is undefined by direct division. Many implementations treat this situation as RSI approaching 100 when losses are effectively zero over the averaging window. When verifying results, you must match the same convention used by your data tool.

MACD: definition of the EMA difference

MACD is based on exponential moving averages (EMAs), which are weighted moving averages that respond more quickly to recent prices.

The core inputs are:

  • A fast EMA span (a)
  • A slow EMA span (b)
  • A signal line span (c) (often (c) is 9 in many common defaults, but you should verify your exact settings)
  1. Compute fast and slow EMAs on the chosen price series:

    • (\text{EMA}_{a}(t))
    • (\text{EMA}_{b}(t))
  2. Compute the MACD line as the difference:

    • (\text{MACD}(t) = \text{EMA}{a}(t) - \text{EMA}{b}(t))
  3. Optionally compute the signal line as an EMA of the MACD line:

    • (\text{Signal}(t) = \text{EMA}_{c} \big(\text{MACD}(t)\big))
  4. Often a histogram is also shown (again, depends on the charting convention):

    • (\text{Histogram}(t) = \text{MACD}(t) - \text{Signal}(t))

Key detail for verification: EMA initialization and the exact formula used by your implementation can differ (for example, how the first EMA value is seeded). If your computed MACD does not match a chart, check whether both sides use the same EMA initialization and spans.

Evidence or example (self-checkable)

Below are simple, verifiable ways to check your understanding using any fixed historical price series you already have.

Example setup for RSI

Assumptions for this example:

  • You have prices (\text{Price}_0, \text{Price}_1, …)
  • You choose lookback (N)
  • You use the Wilder-style smoothing method described above

Steps you can replicate:

  1. Build (\Delta_t) for each step.
  2. Convert to (\text{Gain}_t) and (\text{Loss}_t).
  3. For the first RSI value, average the first (N) gains and losses.
  4. For subsequent points, update averages with the Wilder recursion.
  5. Apply (\text{RSI}_t = 100 - \frac{100}{1 + \text{AvgGain}_t/\text{AvgLoss}_t}).

A reliable self-test is to compute RSI from scratch on the same input and compare the value at the same time index. If you only want to validate the mechanics, focus on the early RSI output where the initial averages matter most.

Example setup for MACD

Assumptions:

  • You have a time-ordered price series
  • You choose spans (a) (fast), (b) (slow), and (c) (signal)
  • You use the same EMA method (including initialization) for both EMAs and the signal EMA

Steps:

  1. Compute (\text{EMA}{a}(t)) and (\text{EMA}{b}(t)) across the series.
  2. Subtract to get (\text{MACD}(t)).
  3. Compute (\text{Signal}(t)) as an EMA of (\text{MACD}(t)).
  4. If needed, compute (\text{Histogram}(t) = \text{MACD}(t) - \text{Signal}(t)).

A self-check is to verify that your MACD line equals the fast EMA minus the slow EMA at each index. If the MACD line matches but the signal line does not, the difference is almost always due to a mismatch in signal span or EMA initialization.

Limitations and failure modes

1) Different “standard” settings produce different numbers

Both RSI and MACD outputs depend on parameters (RSI lookback (N); MACD EMA spans (a), (b), and optional signal span (c)). Even when users think they are using the same indicator, the settings can differ across platforms.

2) Data alignment and missing points change calculations

These indicators are built from consecutive data points. If your input series has missing candles, irregular timestamps, or a different definition of “Price” (such as close versus another derived value), the computed gains/losses for RSI and the EMA sequence for MACD can change.

3) EMA and RSI averaging initialization affects early values

RSI’s first computed values rely on initial averaging across the first (N) periods. MACD’s early values depend on how EMAs are seeded before the algorithm “locks in.” This can cause visible differences near the start of a chart history.

4) Edge cases can yield undefined ratios

In RSI, when average loss becomes zero (or effectively zero), (\text{RS}) involves division by zero. Implementations handle this differently (e.g., treating RSI as 100). If you verify independently, you must adopt the same edge-case convention.

5) Indicators are not standalone guarantees of outcomes

RSI and MACD describe patterns in price and momentum (transformed by math), not future behavior.

Trading foreign exchange and CFDs involves substantial risk. Information on FoxiForex is educational and is not personal financial advice. Sponsored placements are labelled clearly.