-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAverageTrueRange.py
More file actions
62 lines (46 loc) · 1.61 KB
/
Copy pathAverageTrueRange.py
File metadata and controls
62 lines (46 loc) · 1.61 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""Created on 04/09/2017
@author: michaelnew"""
# 3rd parties
import pandas as pd
# Our Library
from Skyze_Indicators_Library.IndicatorAbstract import IndicatorAbstract
from Skyze_Indicators_Library.TrueRange import TrueRange
from Skyze_Indicators_Library.MovingAverage import MovingAverage
class AverageTrueRange(IndicatorAbstract):
"""classdocs
TR =max[high-low),abs(high-close{prev}),abs(low-close{prev})]
df['ATR'] = df['TR'].ewm(span = 10).mean()"""
# Static Variables
_name = "Average True Range"
_version = 1.0
def __init__(self, p_atr_period):
"""Constructor"""
# raise exceptionality
if p_atr_period < 0:
pass
self._atr_period = p_atr_period
self._result = pd.DataFrame()
self._error = []
def initial(self, p_data):
"""Calculate the first value if the calc is different to the subsequent calculations"""
return p_data
def calculate(self, p_data):
"""Calculations"""
atr_name = 'ATR_' + str(self._atr_period)
p_data = self.initial(p_data)
# Create and calculate the true range indicator
true_range = TrueRange()
p_data = true_range.calculate(p_data)
# Average True Range over the series
p_data[atr_name] = p_data["True_Range"] \
.rolling(window=self._atr_period).mean().shift(1)
return p_data
def getResult(self):
"""Getter"""
return self._result
def getName(self):
"""Getter"""
return self._name
def getVersion(self):
"""Getter"""
return self._version