Skip to content

Instantly share code, notes, and snippets.

@christophercrouzet
Last active November 16, 2024 22:54
Show Gist options
  • Save christophercrouzet/c5815ffd611396ac399428bf5801f9fd to your computer and use it in GitHub Desktop.
Save christophercrouzet/c5815ffd611396ac399428bf5801f9fd to your computer and use it in GitHub Desktop.
Alternative ATR% Formula

I'm seeing websites computing ATR% with (ATR / close) * 100, and I believe this yields unintuitive results.

Let's take the popular ADR indicator as a simpler example. Its absolute value, without accounting for the averaging, is the distance between high and low high - low. Its relative value, used for computing the ADR% indicator popularized by Kristjan Kullamägi, is (high / low - 1.0) * 100.0, which essentially represents how stretched a candle is.

If we were to apply the relative formula recommended for the ATR% to compute our ADR%, we'd get (high - low) / close * 100.0. This represents an awkward distance between the high and low relative to the candle's close. For two candles having the same height, their ADR% could vastly differ depending depending on where the close is located within that candle's range. This isn't really what we want, is it?

image

Now, the same logic can be applied to computing the ATR%.

First, the official absolute formula used for computing the true range is max(high - low, abs(high - prev_close), abs(low - prev_close)), but it can be simplified to max(high, prev_close) - min(low, prev_close).

From there, the relative formula becomes max(high, prev_close) / min(low, prev_close). This handles the 3 use cases where:

  1. prev_close > high > low resolves to prev_close / low.
  2. high > prev_close > low resolves to high / low.
  3. high > low > prev_close resolves to high / prev_close.

How does this look like in practice? Let's compare the recommended formula (in red) against ours (in green).

image

Wild differences, right? The formulas match up when the close is near the low however, when it's not, the red formula fails to capture some moves and even diverges from what we'd expect.

There's more! When we plot the formula for the relative daily range in black, we can see that our formula yields similar results when there is no gap, as we'd intuitively expect.

image

Finally, the complete ATR% formula is rma(max(high, prev_close) / min(low, prev_close), length).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment