OnurKerimoglu commited on
Commit
3f99637
·
1 Parent(s): f9577f0

technical_analysis: refined forecast plotting

Browse files
Files changed (1) hide show
  1. src/technical_analysis.py +13 -10
src/technical_analysis.py CHANGED
@@ -5,6 +5,7 @@ import dotenv
5
  import matplotlib.pyplot as plt
6
  import matplotlib.dates as mdates
7
  from matplotlib.axes import Axes
 
8
  import os
9
  import pandas as pd
10
  from ta.volume import volume_weighted_average_price
@@ -307,7 +308,7 @@ class TechnicalAnalysis():
307
  """
308
 
309
  self.logger.info(f'plotting {colstoplot} in {dataset}')
310
- colorcycle = ['black', 'blue', 'red', 'green', 'orange']
311
  for i, col in enumerate(colstoplot):
312
  ax.plot(
313
  df.index,
@@ -324,15 +325,17 @@ class TechnicalAnalysis():
324
  # Add a transparent shaded region daily lows and highs
325
  ax.fill_between(df.index, df['Low'], df['High'], color='gray', alpha=0.3, label='Price Range')
326
  # extract the Price rows for which 'High's are NaN
327
- nanind = df.High.isna()
328
- df_fcst_close = df.loc[nanind, 'Close']
329
- if df_fcst_close.shape[0] > 0:
330
- ax.plot(
331
- df_fcst_close.index,
332
- df_fcst_close,
333
- color='red',
334
- marker='*',
335
- label='Forecast')
 
 
336
  # else:
337
  # # plot a transparent line across the full df.index just to make sure that x-axis limits are identical for all panels
338
  # ax.plot(df.index, df[col], color='gray', alpha=0.0)
 
5
  import matplotlib.pyplot as plt
6
  import matplotlib.dates as mdates
7
  from matplotlib.axes import Axes
8
+ import numpy as np
9
  import os
10
  import pandas as pd
11
  from ta.volume import volume_weighted_average_price
 
308
  """
309
 
310
  self.logger.info(f'plotting {colstoplot} in {dataset}')
311
+ colorcycle = ['black', 'blue', 'green', 'orange']
312
  for i, col in enumerate(colstoplot):
313
  ax.plot(
314
  df.index,
 
325
  # Add a transparent shaded region daily lows and highs
326
  ax.fill_between(df.index, df['Low'], df['High'], color='gray', alpha=0.3, label='Price Range')
327
  # extract the Price rows for which 'High's are NaN
328
+ nanind = np.where(df.High.isna())
329
+ df_fcst = df['Close'].iloc[nanind]
330
+ if df_fcst.shape[0] > 0:
331
+ # append the last day before the forecasts
332
+ nonnanind = np.where(df.High.notna())
333
+ df_now = df['Close'].iloc[[nonnanind[0][-1]]]
334
+ df_now_fcst = pd.concat([df_now, df_fcst])
335
+ # connect the last available day and forecasts with a red line
336
+ ax.plot(df_now_fcst.index, df_now_fcst, color='red')
337
+ # plot only the forecasts with markers
338
+ ax.plot(df_fcst.index, df_fcst, color='red', marker='*', label='Forecast')
339
  # else:
340
  # # plot a transparent line across the full df.index just to make sure that x-axis limits are identical for all panels
341
  # ax.plot(df.index, df[col], color='gray', alpha=0.0)