-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTrueRange.py
More file actions
78 lines (46 loc) · 1.59 KB
/
Copy pathTrueRange.py
File metadata and controls
78 lines (46 loc) · 1.59 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
'''
Created on 04/09/2017
@author: michaelnew
'''
# 3rd parties
import pandas as pd
import numpy as np
# Our Library
from Skyze_Indicators_Library.IndicatorAbstract import IndicatorAbstract
class TrueRange(IndicatorAbstract):
'''
classdocs
TR =max[high-low),abs(high-close{prev}),abs(low-close{prev})]
'''
# Static Variables
name = "Average True Range v01"
version = 1.0
def __init__(self):
''' Constructor '''
# raise exceptionality
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 # pd dataframe series
):
''' Calculations '''
p_data = self.initial(p_data)
p_data['H-L'] = abs(p_data['High']-p_data['Low'])
p_data['H-PC'] = abs(p_data['High']-p_data['Close'].shift(1))
p_data['L-PC'] = abs(p_data['Low']-p_data['Close'].shift(1))
p_data['True_Range'] = p_data[['H-L','H-PC','L-PC']].max(axis=1).round(6)
p_data['True_Range'] = np.where(p_data['H-PC'].notnull(), p_data['True_Range'], 'NaN')
# p_data['True Range'] = if p_data['H-PC'] == "NaN" or p_data['L-PC'] == "NaN" :
# del p_data['H-L']
# del p_data['H-PC']
# del p_data['L-PC']
return p_data
def getResult (self):
''' Getter '''
return self.result
def getName(self):
''' Getter '''
return self.name