Skip to content

Commit 1ca0823

Browse files
committed
Code format and sort imports
1 parent 99c81bf commit 1ca0823

35 files changed

+550
-428
lines changed

app/analysis.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
import math
55
from datetime import datetime
66

7-
import structlog
87
import pandas
8+
import structlog
99
from talib import abstract
1010

11+
from analyzers import *
1112
from analyzers.indicators import *
1213
from analyzers.informants import *
13-
from analyzers import *
14+
1415

1516
class StrategyAnalyzer():
1617
"""Contains all the methods required for analyzing strategies.
@@ -20,7 +21,6 @@ def __init__(self):
2021
"""Initializes StrategyAnalyzer class """
2122
self.logger = structlog.get_logger()
2223

23-
2424
def indicator_dispatcher(self):
2525
"""Returns a dictionary for dynamic anaylsis selector
2626
@@ -49,7 +49,6 @@ def indicator_dispatcher(self):
4949

5050
return dispatcher
5151

52-
5352
def informant_dispatcher(self):
5453
"""Returns a dictionary for dynamic informant selector
5554
@@ -68,7 +67,6 @@ def informant_dispatcher(self):
6867

6968
return dispatcher
7069

71-
7270
def crossover_dispatcher(self):
7371
"""Returns a pandas.DataFrame for dynamic crossover selector
7472

app/analyzers/indicators/adx.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
Average Directional Index indicator
44
"""
55

6-
import pandas
76
import numpy
7+
import pandas
88

99
from analyzers.utils import IndicatorUtils
1010

@@ -84,7 +84,6 @@ def analyze(self, historical_data, signal=["adx"], period_count=14, hot_thresh=N
8484

8585
return adx_values
8686

87-
8887
def TR(self, high, low, close, tr):
8988
"""
9089
Calculates TR (True Range)
@@ -104,7 +103,6 @@ def TR(self, high, low, close, tr):
104103

105104
return tr
106105

107-
108106
def DM(self, high, low, pdm, ndm):
109107
"""
110108
Calculates DM (Directional Movement)
@@ -130,7 +128,6 @@ def DM(self, high, low, pdm, ndm):
130128

131129
return pdm, ndm
132130

133-
134131
def DMsmooth(self, pdm, ndm, pdm_smooth, ndm_smooth, period_count):
135132
"""
136133
Smoothing positive and negative directional movement
@@ -145,12 +142,13 @@ def DMsmooth(self, pdm, ndm, pdm_smooth, ndm_smooth, period_count):
145142
pdm_smooth[period_count-1] = pdm[0:period_count].sum() / period_count
146143
ndm_smooth[period_count - 1] = ndm[0:period_count].sum() / period_count
147144
for index in range(period_count, pdm.shape[0]):
148-
pdm_smooth[index] = (pdm[index-1] - (pdm_smooth[index-1]/period_count)) + pdm_smooth[index-1]
149-
ndm_smooth[index] = (ndm[index - 1] - (ndm_smooth[index-1] / period_count)) + ndm_smooth[index-1]
145+
pdm_smooth[index] = (
146+
pdm[index-1] - (pdm_smooth[index-1]/period_count)) + pdm_smooth[index-1]
147+
ndm_smooth[index] = (
148+
ndm[index - 1] - (ndm_smooth[index-1] / period_count)) + ndm_smooth[index-1]
150149

151150
return pdm_smooth, ndm_smooth
152151

153-
154152
def DI(self, pdm_smooth, ndm_smooth, tr, pdi, ndi):
155153
"""
156154
Calculates DI (Directional Movement Indicator)
@@ -168,7 +166,6 @@ def DI(self, pdm_smooth, ndm_smooth, tr, pdi, ndi):
168166

169167
return pdi, ndi
170168

171-
172169
def ATR(self, tr, atr, period_count):
173170
"""
174171
Calculates ATR (Average True Range)
@@ -183,11 +180,11 @@ def ATR(self, tr, atr, period_count):
183180

184181
atr[period_count-1] = tr[0:period_count].sum() / period_count
185182
for index in range(period_count, tr.shape[0]):
186-
atr[index] = ((atr[index-1] * (period_count - 1)) + tr[index]) / period_count
183+
atr[index] = ((atr[index-1] * (period_count - 1)) +
184+
tr[index]) / period_count
187185

188186
return atr
189187

190-
191188
def ADX(self, pdi, ndi, dx, adx, period_count):
192189
"""
193190
Calculates DX (Directional index) and ADX (Average Directional Index)
@@ -200,11 +197,14 @@ def ADX(self, pdi, ndi, dx, adx, period_count):
200197
"""
201198

202199
for index in range(0, pdi.shape[0]):
203-
dx[index] = ((abs(pdi[index] - ndi[index])) / (abs(pdi[index] + ndi[index]))) * 100
200+
dx[index] = ((abs(pdi[index] - ndi[index])) /
201+
(abs(pdi[index] + ndi[index]))) * 100
204202

205203
period_count2 = period_count*2
206-
adx[period_count2-1] = dx[period_count:period_count2].sum() / period_count
204+
adx[period_count2-1] = dx[period_count:period_count2].sum() / \
205+
period_count
207206
for index in range(period_count2, dx.shape[0]):
208-
adx[index] = ((adx[index-1] * (period_count - 1)) + dx[index]) / period_count
207+
adx[index] = ((adx[index-1] * (period_count - 1)) +
208+
dx[index]) / period_count
209209

210210
return dx, adx

app/analyzers/indicators/aroon_oscillator.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
Aroon Oscillator
44
"""
55

6-
import pandas
76
import numpy
7+
import pandas
88

99
from analyzers.utils import IndicatorUtils
1010

@@ -36,8 +36,8 @@ def analyze(self, historical_data, sma_vol_period, period_count=25, signal=["aro
3636
}
3737

3838
aroon_values = pandas.DataFrame(aroon_columns,
39-
index=dataframe.index
40-
)
39+
index=dataframe.index
40+
)
4141

4242
for index in range(0, dataframe.shape[0]-24):
4343
id = dataframe.shape[0]-index
@@ -47,16 +47,20 @@ def analyze(self, historical_data, sma_vol_period, period_count=25, signal=["aro
4747

4848
periods_since_high = id - dataframe.index.get_loc(high_date) - 1
4949
periods_since_low = id - dataframe.index.get_loc(low_date) - 1
50-
aroon_values['aroon_up'][id-1] = 100 * ((25 - periods_since_high) / 25)
50+
aroon_values['aroon_up'][id-1] = 100 * \
51+
((25 - periods_since_high) / 25)
5152

52-
aroon_values['aroon_down'][id-1] = 100 * ((25 - periods_since_low) / 25)
53+
aroon_values['aroon_down'][id-1] = 100 * \
54+
((25 - periods_since_low) / 25)
5355

54-
aroon_values['aroon'] = aroon_values['aroon_up'] - aroon_values['aroon_down']
56+
aroon_values['aroon'] = aroon_values['aroon_up'] - \
57+
aroon_values['aroon_down']
5558

5659
aroon_values['is_hot'] = False
5760
aroon_values['is_cold'] = False
5861

59-
aroon_values['sma_volume'] = dataframe.volume.rolling(sma_vol_period).mean()
62+
aroon_values['sma_volume'] = dataframe.volume.rolling(
63+
sma_vol_period).mean()
6064
aroon = aroon_values['aroon'].iloc[-1]
6165
volume = dataframe['volume'].iloc[-1]
6266
volume_sma = aroon_values['sma_volume'].iloc[-1]

app/analyzers/indicators/bbp.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
Bollinger Bands indicator
44
"""
55

6-
from talib import BBANDS
7-
from talib import abstract
6+
import math
87

98
import pandas
10-
import math
9+
from talib import BBANDS, abstract
1110

1211
from analyzers.utils import IndicatorUtils
1312

@@ -35,20 +34,21 @@ def analyze(self, historical_data, signal=['bbp'], hot_thresh=0, cold_thresh=0.8
3534

3635
mfi = abstract.MFI(dataframe, period_count=14)
3736

38-
#Required to avoid getting same values for low, middle, up
37+
# Required to avoid getting same values for low, middle, up
3938
dataframe['close_10k'] = dataframe['close'] * 10000
40-
41-
up_band, mid_band, low_band = BBANDS(dataframe['close_10k'], timeperiod=period_count, nbdevup=std_dev, nbdevdn=std_dev, matype=0)
39+
40+
up_band, mid_band, low_band = BBANDS(
41+
dataframe['close_10k'], timeperiod=period_count, nbdevup=std_dev, nbdevdn=std_dev, matype=0)
4242

4343
bbp = (dataframe['close_10k'] - low_band) / (up_band - low_band)
4444

4545
bollinger = pandas.concat([dataframe, bbp, mfi], axis=1)
4646
bollinger.rename(columns={0: 'bbp', 1: 'mfi'}, inplace=True)
4747

48-
bollinger['is_hot'] = False
48+
bollinger['is_hot'] = False
4949
bollinger['is_cold'] = False
5050

51-
bollinger['is_hot'].iloc[-1] = bollinger['bbp'].iloc[-2] <= hot_thresh and bollinger['bbp'].iloc[-2] < bollinger['bbp'].iloc[-1]
51+
bollinger['is_hot'].iloc[-1] = bollinger['bbp'].iloc[-2] <= hot_thresh and bollinger['bbp'].iloc[-2] < bollinger['bbp'].iloc[-1]
5252
bollinger['is_cold'].iloc[-1] = bollinger['bbp'].iloc[-1] >= cold_thresh
5353

54-
return bollinger
54+
return bollinger

app/analyzers/indicators/bollinger.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
Bollinger Bands indicator
44
"""
55

6-
from talib import BBANDS
7-
import pandas
86
import math
97

8+
import pandas
9+
from talib import BBANDS
10+
1011
from analyzers.utils import IndicatorUtils
1112

1213

@@ -27,28 +28,31 @@ def analyze(self, historical_data, signal=['close'], hot_thresh=None, cold_thres
2728

2829
dataframe = self.convert_to_dataframe(historical_data)
2930

30-
#Required to avoid getting same values for low, middle, up
31+
# Required to avoid getting same values for low, middle, up
3132
dataframe['close_10k'] = dataframe['close'] * 10000
32-
33-
up_band, mid_band, low_band = BBANDS(dataframe['close_10k'], timeperiod=period_count, nbdevup=std_dev, nbdevdn=std_dev, matype=0)
3433

35-
bollinger = pandas.concat([dataframe, up_band, mid_band, low_band], axis=1)
36-
bollinger.rename(columns={0: 'up_band', 1: 'mid_band', 2: 'low_band'}, inplace=True)
34+
up_band, mid_band, low_band = BBANDS(
35+
dataframe['close_10k'], timeperiod=period_count, nbdevup=std_dev, nbdevdn=std_dev, matype=0)
36+
37+
bollinger = pandas.concat(
38+
[dataframe, up_band, mid_band, low_band], axis=1)
39+
bollinger.rename(
40+
columns={0: 'up_band', 1: 'mid_band', 2: 'low_band'}, inplace=True)
3741

38-
old_up, old_low = bollinger.iloc[-2]['up_band'] , bollinger.iloc[-2]['low_band']
39-
cur_up, cur_low = bollinger.iloc[-1]['up_band'] , bollinger.iloc[-1]['low_band']
42+
old_up, old_low = bollinger.iloc[-2]['up_band'], bollinger.iloc[-2]['low_band']
43+
cur_up, cur_low = bollinger.iloc[-1]['up_band'], bollinger.iloc[-1]['low_band']
4044

4145
old_close = bollinger.iloc[-2]['close_10k']
4246
cur_close = bollinger.iloc[-1]['close_10k']
4347

44-
bollinger['is_hot'] = False
48+
bollinger['is_hot'] = False
4549
bollinger['is_cold'] = False
4650

47-
bollinger['is_hot'].iloc[-1] = old_low < old_close and cur_low > cur_close
51+
bollinger['is_hot'].iloc[-1] = old_low < old_close and cur_low > cur_close
4852
bollinger['is_cold'].iloc[-1] = old_up > old_close and cur_up < cur_close
4953

5054
bollinger['up_band'] = bollinger['up_band'] / 10000
5155
bollinger['mid_band'] = bollinger['mid_band'] / 10000
5256
bollinger['low_band'] = bollinger['low_band'] / 10000
5357

54-
return bollinger
58+
return bollinger

app/analyzers/indicators/candle_recognition.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

2-
import talib
32
import numpy
43
import pandas
4+
import talib
55

66
from analyzers.utils import IndicatorUtils
77

@@ -28,7 +28,7 @@ def analyze(self, historical_data, signal, notification='hot', candle_check=1, h
2828
low = dataframe['low']
2929
close = dataframe['close']
3030

31-
candle_functions= {
31+
candle_functions = {
3232
'two_crows': talib.CDLTRISTAR,
3333
'three_black_crows': talib.CDL3BLACKCROWS,
3434
'three_inside_up_down': talib.CDL3INSIDE,
@@ -95,13 +95,12 @@ def analyze(self, historical_data, signal, notification='hot', candle_check=1, h
9595
candle_args = {'open': open, 'high': high, 'low': low, 'close': close}
9696
for candle in signal:
9797
if candle in candle_functions.keys():
98-
candles_values[candle] = candle_functions[candle](**candle_args)
99-
98+
candles_values[candle] = candle_functions[candle](
99+
**candle_args)
100100

101101
candles_values['is_hot'] = False
102102
candles_values['is_cold'] = False
103103

104-
105104
to_check = 0 - candle_check
106105
for candle in signal:
107106
if len(set(candles_values[candle].iloc[to_check:])) != 1:
@@ -114,4 +113,3 @@ def analyze(self, historical_data, signal, notification='hot', candle_check=1, h
114113
candles_values[candle] = candles_values[candle].astype(float)
115114

116115
return candles_values
117-

app/analyzers/indicators/ichimoku.py

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212

1313
class Ichimoku(IndicatorUtils):
14-
def analyze(self, historical_data, tenkansen_period, kijunsen_period , senkou_span_b_period,
14+
def analyze(self, historical_data, tenkansen_period, kijunsen_period, senkou_span_b_period,
1515
signal=['leading_span_a', 'leading_span_b'], hot_thresh=None, cold_thresh=None, chart=None):
1616
"""Performs an ichimoku cloud analysis on the historical data
1717
@@ -44,17 +44,21 @@ def analyze(self, historical_data, tenkansen_period, kijunsen_period , senkou_sp
4444
index=dataframe.index
4545
)
4646
# value calculations
47-
low_tenkansen = dataframe['low'].rolling(window=tenkansen_period).min()
48-
low_kijunsen = dataframe['low'].rolling(window=kijunsen_period).min()
49-
low_senkou = dataframe['low'].rolling(window=senkou_span_b_period).min()
50-
high_tenkansen = dataframe['high'].rolling(window=tenkansen_period).max()
51-
high_kijunsen = dataframe['high'].rolling(window=kijunsen_period).max()
52-
high_senkou = dataframe['high'].rolling(window=senkou_span_b_period).max()
53-
54-
ichimoku_values['tenkansen'] = (low_tenkansen + high_tenkansen) / 2
55-
ichimoku_values['kijunsen'] = (low_kijunsen + high_kijunsen) / 2
56-
ichimoku_values['leading_span_a'] = ((ichimoku_values['tenkansen'] + ichimoku_values['kijunsen']) / 2)
57-
ichimoku_values['leading_span_b'] = (high_senkou + low_senkou) / 2
47+
low_tenkansen = dataframe['low'].rolling(window=tenkansen_period).min()
48+
low_kijunsen = dataframe['low'].rolling(window=kijunsen_period).min()
49+
low_senkou = dataframe['low'].rolling(
50+
window=senkou_span_b_period).min()
51+
high_tenkansen = dataframe['high'].rolling(
52+
window=tenkansen_period).max()
53+
high_kijunsen = dataframe['high'].rolling(window=kijunsen_period).max()
54+
high_senkou = dataframe['high'].rolling(
55+
window=senkou_span_b_period).max()
56+
57+
ichimoku_values['tenkansen'] = (low_tenkansen + high_tenkansen) / 2
58+
ichimoku_values['kijunsen'] = (low_kijunsen + high_kijunsen) / 2
59+
ichimoku_values['leading_span_a'] = (
60+
(ichimoku_values['tenkansen'] + ichimoku_values['kijunsen']) / 2)
61+
ichimoku_values['leading_span_b'] = (high_senkou + low_senkou) / 2
5862

5963
# add time period for cloud offset
6064
## if cloud discplacement changed the ichimuko plot will be off ##
@@ -64,10 +68,13 @@ def analyze(self, historical_data, tenkansen_period, kijunsen_period , senkou_sp
6468
newindex = pandas.DatetimeIndex(start=last_time + timedelta,
6569
freq=timedelta,
6670
periods=cloud_displacement)
67-
ichimoku_values = ichimoku_values.append(pandas.DataFrame(index=newindex))
71+
ichimoku_values = ichimoku_values.append(
72+
pandas.DataFrame(index=newindex))
6873
# cloud offset
69-
ichimoku_values['leading_span_a'] = ichimoku_values['leading_span_a'].shift(cloud_displacement)
70-
ichimoku_values['leading_span_b'] = ichimoku_values['leading_span_b'].shift(cloud_displacement)
74+
ichimoku_values['leading_span_a'] = ichimoku_values['leading_span_a'].shift(
75+
cloud_displacement)
76+
ichimoku_values['leading_span_b'] = ichimoku_values['leading_span_b'].shift(
77+
cloud_displacement)
7178

7279
ichimoku_values['is_hot'] = False
7380
ichimoku_values['is_cold'] = False
@@ -80,11 +87,13 @@ def analyze(self, historical_data, tenkansen_period, kijunsen_period , senkou_sp
8087
span_hot = ichimoku_values['leading_span_a'][date] > ichimoku_values['leading_span_b'][date]
8188
close_hot = dataframe['close'][date] > ichimoku_values['leading_span_a'][date]
8289
if hot_thresh:
83-
ichimoku_values.at[date, 'is_hot'] = span_hot and close_hot
90+
ichimoku_values.at[date,
91+
'is_hot'] = span_hot and close_hot
8492
span_cold = ichimoku_values['leading_span_a'][date] < ichimoku_values['leading_span_b'][date]
8593
close_cold = dataframe['close'][date] < ichimoku_values['leading_span_a'][date]
8694
if cold_thresh:
87-
ichimoku_values.at[date, 'is_cold'] = span_cold and close_cold
95+
ichimoku_values.at[date,
96+
'is_cold'] = span_cold and close_cold
8897
else:
8998
pass
9099

@@ -94,4 +103,4 @@ def analyze(self, historical_data, tenkansen_period, kijunsen_period , senkou_sp
94103
if chart == None:
95104
ichimoku_values.dropna(how='any', inplace=True)
96105

97-
return ichimoku_values
106+
return ichimoku_values

0 commit comments

Comments
 (0)