Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/mplfinance/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ def _calculate_atr(atr_length, highs, lows, closes):
raise ValueError("Specified atr_length may not be less than 1")
elif atr_length >= len(closes):
raise ValueError("Specified atr_length is larger than the length of the dataset: " + str(len(closes)))
# Point & Figure passes DatetimeIndex Series. Integer keys on those
# Series are labels, not positions, so index by a plain array.
highs = np.asarray(highs)
lows = np.asarray(lows)
closes = np.asarray(closes)
atr = 0
for i in range(len(highs)-atr_length, len(highs)):
high = highs[i]
Expand Down
15 changes: 15 additions & 0 deletions tests/test_atr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import pandas as pd

from mplfinance._utils import _calculate_atr


def test_atr_indexes_datetime_series_by_position():
index = pd.date_range("2020-01-01", periods=5, freq="D")
high = pd.Series([10, 12, 11, 13, 12], index=index)
low = pd.Series([8, 9, 9, 10, 9], index=index)
close = pd.Series([9, 11, 10, 12, 11], index=index)

# Bars 4 and 5 each have a true range of 3, so a 2-bar ATR is 3.
assert _calculate_atr(2, high, low, close) == 3
assert _calculate_atr(2, high.to_numpy(), low.to_numpy(), close.to_numpy()) == 3
assert _calculate_atr(2, list(high), list(low), list(close)) == 3