-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathatr.py
32 lines (23 loc) · 876 Bytes
/
atr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import pandas as pd
import pandas_datareader.data as web
import numpy as np
import datetime
def getATR(ticker, N=20, precision=3, source='yahoo'):
'''Calculates the average true range for a given stock symbol and a period (in days).'''
start=datetime.date.today()-datetime.timedelta(int(N*10))
end=datetime.date.today()
df = web.DataReader(ticker, source, start, end)
df = df.round(2)
df['TR1'] = df.High - df.Low
df['TR2'] = abs(df.High - df.Close.shift())
df['TR3'] = abs(df.Low - df.Close.shift())
df['TR'] = df[['TR1', 'TR2', 'TR3']].max(axis=1)
trs = np.array(df.TR)
atrs = np.zeros(len(trs))
atrs[N-1] = np.mean(trs[:N])
for i in range(N, len(trs)):
atrs[i] = ((N-1) * atrs[i-1] + trs[i])/N
atrs = atrs.round(precision)
return atrs[-1]
if __name__ == '__main__':
print(getATR('MSFT'))