@@ -146,10 +146,11 @@ def get_rsi_graph(self, rsi_data):
146146 plt .suptitle ('Price and Relative Strength Index' , color = 'red' , fontsize = 24 )
147147 plt .savefig ('rsi.png' , bbox_inches = 'tight' , facecolor = 'orange' )
148148 return plt .show ()
149+
149150 except Exception as e :
150151 return e
151152
152- def get_bollinger_bands (df , days = 20 ):
153+ def get_bollinger_bands (df , days = 20 , plot = None ):
153154 """
154155 Type:
155156 Trend, volatility, momentum indicator
@@ -178,25 +179,27 @@ def get_bollinger_bands(df, days=20):
178179 data ['plus' ] = data ['SMA' ] + data ['SD' ]* 2
179180 data ['minus' ] = data ['SMA' ] - data ['SMA' ]* 2
180181 data1 = data .sort_values (by = 'date' , ascending = False ).reset_index (drop = True )
181- fig , ax = plt .subplots (figsize = (16 , 12 ))
182- plt .plot (data1 ['date' ], data1 ['plus' ], color = 'g' )
183- plt .plot (data1 ['date' ], data1 ['minus' ], color = 'g' )
184- plt .plot (data1 ['date' ], data1 ['price' ], color = 'orange' )
185- plt .legend ()
186- plt .xlabel ('Time' , color = 'b' , fontsize = 22 )
187- plt .ylabel ('Price' , color = 'b' , fontsize = 22 )
188- plt .title ('Bollinger Bands' , color = 'b' , fontsize = 27 )
189- plt .tick_params (labelsize = 17 )
190- fig .set_facecolor ('yellow' )
191- plt .grid ()
192- plt .savefig ('bollinger_bands.png' , bbox_inches = 'tight' , facecolor = 'orange' )
193- plt .show ()
182+ while plot :
183+ fig , ax = plt .subplots (figsize = (16 , 12 ))
184+ plt .plot (data1 ['date' ], data1 ['plus' ], color = 'g' )
185+ plt .plot (data1 ['date' ], data1 ['minus' ], color = 'g' )
186+ plt .plot (data1 ['date' ], data1 ['price' ], color = 'orange' )
187+ plt .legend ()
188+ plt .xlabel ('Time' , color = 'b' , fontsize = 22 )
189+ plt .ylabel ('Price' , color = 'b' , fontsize = 22 )
190+ plt .title ('Bollinger Bands' , color = 'b' , fontsize = 27 )
191+ plt .tick_params (labelsize = 17 )
192+ fig .set_facecolor ('yellow' )
193+ plt .grid ()
194+ plt .savefig ('bollinger_bands.png' , bbox_inches = 'tight' , facecolor = 'orange' )
195+ plt .show ()
196+ break
194197 return data1
195198
196199 except Exception as e :
197200 return e
198201
199- def get_moving_average_convergence_divergence (df ):
202+ def get_moving_average_convergence_divergence (df , plot = None ):
200203 """
201204 Type
202205 Trend and momentum indicator
@@ -220,23 +223,24 @@ def get_moving_average_convergence_divergence(df):
220223 data ['MACD' ] = data ['EMA_12' ] - data ['EMA_26' ]
221224 data1 = data .dropna ()
222225
223- fig , ax = plt .subplots (figsize = (14 , 9 ))
224- plt .plot (data1 ['date' ], data1 ['price' ], color = 'r' , label = 'Price' )
225- plt .plot (data1 ['date' ], data1 ['MACD' ], color = 'b' , label = 'MACD' )
226- plt .legend ()
227- plt .title ('Price and MACD Plot' , fontsize = 28 , color = 'b' )
228- plt .xlabel ('Time' , color = 'b' , fontsize = 19 )
229- plt .ylabel ('Price' , color = 'b' , fontsize = 19 )
230- plt .savefig ('macd.png' , bbox_inches = 'tight' , facecolor = 'orange' )
231- fig .set_facecolor ('orange' )
232- plt .show ()
233-
226+ while plot :
227+ fig , ax = plt .subplots (figsize = (14 , 9 ))
228+ plt .plot (data1 ['date' ], data1 ['price' ], color = 'r' , label = 'Price' )
229+ plt .plot (data1 ['date' ], data1 ['MACD' ], color = 'b' , label = 'MACD' )
230+ plt .legend ()
231+ plt .title ('Price and MACD Plot' , fontsize = 28 , color = 'b' )
232+ plt .xlabel ('Time' , color = 'b' , fontsize = 19 )
233+ plt .ylabel ('Price' , color = 'b' , fontsize = 19 )
234+ plt .savefig ('macd.png' , bbox_inches = 'tight' , facecolor = 'orange' )
235+ fig .set_facecolor ('orange' )
236+ plt .show ()
237+ break
234238 return data1
235239
236240 except Exception as e :
237241 return print ('MACD Error - {}' .format (e ))
238242
239- def get_simple_moving_average (df , days = 15 ):
243+ def get_simple_moving_average (df , days = 15 , plot = None ):
240244 """
241245 Simple moving average of given days
242246 :param price_data: pandas DataFrame
@@ -249,22 +253,24 @@ def get_simple_moving_average(df, days=15):
249253 data ['SMA' ] = data ['price' ].rolling (days ).mean ()
250254 data1 = data .dropna ()
251255 data1 = data1 .sort_values (by = 'date' , ascending = False ).reset_index (drop = True )
252- fig , ax = plt .subplots (figsize = (14 , 9 ))
253- plt .plot (data1 ['date' ], data1 ['price' ], color = 'r' , label = 'Price' )
254- plt .plot (data1 ['date' ], data1 ['SMA' ], color = 'b' , label = 'SMA' )
255- plt .legend ()
256- plt .title ('Price and SMA Plot' , fontsize = 28 , color = 'b' )
257- plt .xlabel ('Time' , color = 'b' , fontsize = 19 )
258- plt .ylabel ('Price' , color = 'b' , fontsize = 19 )
259- plt .savefig ('sma.png' , bbox_inches = 'tight' , facecolor = 'orange' )
260- fig .set_facecolor ('orange' )
261- plt .show ()
256+ while plot :
257+ fig , ax = plt .subplots (figsize = (14 , 9 ))
258+ plt .plot (data1 ['date' ], data1 ['price' ], color = 'r' , label = 'Price' )
259+ plt .plot (data1 ['date' ], data1 ['SMA' ], color = 'b' , label = 'SMA' )
260+ plt .legend ()
261+ plt .title ('Price and SMA Plot' , fontsize = 28 , color = 'b' )
262+ plt .xlabel ('Time' , color = 'b' , fontsize = 19 )
263+ plt .ylabel ('Price' , color = 'b' , fontsize = 19 )
264+ plt .savefig ('sma.png' , bbox_inches = 'tight' , facecolor = 'orange' )
265+ fig .set_facecolor ('orange' )
266+ plt .show ()
267+ break
262268 return data1
263269
264270 except Exception as e :
265271 return print ('SMA Error - {}' .format (e ))
266272
267- def get_exponential_moving_average (df , periods = [20 ]):
273+ def get_exponential_moving_average (df , periods = [20 ], plot = None ):
268274 """
269275 The EMA is a moving average that places a greater weight and significance on the most recent data points.
270276 Like all moving averages, this technical indicator is used to produce buy and sell signals based on crossovers and divergences from the historical average.
@@ -280,17 +286,19 @@ def get_exponential_moving_average(df, periods=[20]):
280286 data ['EMA_{}' .format (period )] = data ['price' ].ewm (span = period , adjust = False ).mean ()
281287 data = data .dropna ()
282288 data1 = data
283- fig , ax = plt .subplots (figsize = (14 , 9 ))
284- plt .plot (data1 ['date' ], data1 ['price' ], color = 'r' , label = 'Price' )
285- for period in periods :
286- plt .plot (data1 ['date' ], data1 ['EMA_{}' .format (period )], label = 'EMA_{}' .format (period ))
287- plt .legend ()
288- plt .title ('Price and EMA Plot' , fontsize = 28 , color = 'b' )
289- plt .xlabel ('Time' , color = 'b' , fontsize = 19 )
290- plt .ylabel ('Price/EMA' , color = 'b' , fontsize = 19 )
291- plt .savefig ('ema.png' , bbox_inches = 'tight' , facecolor = 'orange' )
292- fig .set_facecolor ('orange' )
293- plt .show ()
289+ while plot :
290+ fig , ax = plt .subplots (figsize = (14 , 9 ))
291+ plt .plot (data1 ['date' ], data1 ['price' ], color = 'r' , label = 'Price' )
292+ for period in periods :
293+ plt .plot (data1 ['date' ], data1 ['EMA_{}' .format (period )], label = 'EMA_{}' .format (period ))
294+ plt .legend ()
295+ plt .title ('Price and EMA Plot' , fontsize = 28 , color = 'b' )
296+ plt .xlabel ('Time' , color = 'b' , fontsize = 19 )
297+ plt .ylabel ('Price/EMA' , color = 'b' , fontsize = 19 )
298+ plt .savefig ('ema.png' , bbox_inches = 'tight' , facecolor = 'orange' )
299+ fig .set_facecolor ('orange' )
300+ plt .show ()
301+ break
294302 return data1
295303
296304 except Exception as e :
0 commit comments