Skip to content

Commit e77ca9d

Browse files
author
Dayal Chand Aichara
committed
V-1.0.1
1 parent 8f65231 commit e77ca9d

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

PriceIndices/price_indicators.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77

88
class Indices:
9+
910
def get_bvol_index(df):
1011

1112
"""
@@ -88,6 +89,7 @@ def get_rsi(df):
8889
"""
8990
try:
9091
data = df
92+
data = data.sort_values(by='date').reset_index(drop=True)
9193
data['price_change'] = data['price'] - df['price'].shift(1)
9294
data.dropna(inplace=True)
9395
data['gain'] = data['price_change'].apply(lambda x: x if x >= 0 else 0)
@@ -107,8 +109,9 @@ def get_rsi(df):
107109

108110
data['RSI_2'] = 100 * (1 - (1 / (1 + data['RS_Smooth'])))
109111
data = data.fillna(0).reset_index(drop=True)
110-
data1 = data
112+
data1 = data.sort_values(by='date', ascending=False).reset_index(drop=True)
111113
return data1
114+
112115
except Exception as e:
113116
return e
114117

@@ -169,11 +172,12 @@ def get_bollinger_bands(df, days=20):
169172

170173
try:
171174
data = df
175+
data = data.sort_values(by='date').reset_index(drop=True)
172176
data['SMA'] = data['price'].rolling(days).mean()
173177
data['SD'] = data['price'].rolling(days).std()
174178
data['pluse'] = data['SMA'] + data['SD']*2
175179
data['minus'] = data['SMA'] - data['SMA']*2
176-
data1 = data
180+
data1 = data.sort_values(by='date', ascending=False).reset_index(drop=True)
177181
fig, ax = plt.subplots(figsize=(16, 12))
178182
plt.plot(data1['date'], data1['pluse'], color='g')
179183
plt.plot(data1['date'], data1['minus'], color='g')
@@ -188,6 +192,7 @@ def get_bollinger_bands(df, days=20):
188192
plt.savefig('bollinger_bands.png', bbox_inches='tight', facecolor='orange')
189193
plt.show()
190194
return data1
195+
191196
except Exception as e:
192197
return e
193198

@@ -227,6 +232,7 @@ def get_moving_average_convergence_divergence(df):
227232
plt.show()
228233

229234
return data1
235+
230236
except Exception as e:
231237
return print('MACD Error - {}'.format(e))
232238

@@ -239,8 +245,10 @@ def get_simple_moving_average(df, days=15):
239245
"""
240246
try:
241247
data = df
248+
data = data.sort_values(by='date').reset_index(drop=True)
242249
data['SMA'] = data['price'].rolling(days).mean()
243250
data1 = data.dropna()
251+
data1 = data1.sort_values(by='date', ascending=False).reset_index(drop=True)
244252
fig, ax = plt.subplots(figsize=(14, 9))
245253
plt.plot(data1['date'], data1['price'], color='r', label='Price')
246254
plt.plot(data1['date'], data1['SMA'], color='b', label='SMA')
@@ -252,6 +260,7 @@ def get_simple_moving_average(df, days=15):
252260
fig.set_facecolor('orange')
253261
plt.show()
254262
return data1
263+
255264
except Exception as e:
256265
return print('SMA Error - {}'.format(e))
257266

@@ -283,6 +292,7 @@ def get_exponential_moving_average(df, periods=[20]):
283292
fig.set_facecolor('orange')
284293
plt.show()
285294
return data1
295+
286296
except Exception as e:
287297
return print('EMA Error - {}'.format(e))
288298

0 commit comments

Comments
 (0)