3

How to calculate the average true range (ATR) by only using numpy? I prefer the fastest way possible.

Atr calculation:

enter image description here

Pandas & Numpy:

import numpy as np
import pandas as pd

high_low = data['High'] - data['Low']
high_close = np.abs(data['High'] - data['Close'].shift())
low_close = np.abs(data['Low'] - data['Close'].shift())

ranges = pd.concat([high_low, high_close, low_close], axis=1)
true_range = np.max(ranges, axis=1)

atr = true_range.rolling(14).sum()/14
AlgoQ
  • 117
  • 2
  • 11
  • Are you able to provide some sample/dummy values for the `data` dataframe so we can run your code? See this [link on reproducible examples](https://stackoverflow.com/help/minimal-reproducible-example). – TC Arlen Aug 09 '21 at 16:02

0 Answers0