Browse Source

New indicator: ATR

master
Denis Tereshkin 7 years ago
parent
commit
21094256d4
  1. 1
      src/naiback/indicators/__init__.py
  2. 20
      src/naiback/indicators/atr.py

1
src/naiback/indicators/__init__.py

@ -4,3 +4,4 @@ from .rsi import RSI @@ -4,3 +4,4 @@ from .rsi import RSI
from .intradaybarnumber import IntradayBarNumber
from .highest import Highest,HighestValue
from .lowest import Lowest,LowestValue
from .atr import ATR

20
src/naiback/indicators/atr.py

@ -0,0 +1,20 @@ @@ -0,0 +1,20 @@
import numpy as np
def ATR(bars, period):
tr = np.zeros(len(bars.close))
if len(bars.close) == 0:
return np.array([])
tr[0] = bars.high[0] - bars.low[0]
for i in range(1, len(bars.close)):
tr[i] = max(bars.high[i] - bars.low[i], abs(bars.high[i] - bars.close[i - 1]), abs(bars.low[i] - bars.close[i - 1]))
atr = np.zeros(len(bars.close))
if len(bars.close) <= period:
return atr
atr[period - 1] = sum(tr[0:period]) / period
for i in range(period, len(bars.close)):
atr[i] = (atr[i - 1] * (period - 1) + tr[i]) / period
return atr
Loading…
Cancel
Save