OnurKerimoglu commited on
Commit
2ac2093
·
1 Parent(s): 5df6537

technical analysis: better error handling, fetchperiodinweeks=12

Browse files
Files changed (1) hide show
  1. src/technical_analysis.py +16 -15
src/technical_analysis.py CHANGED
@@ -16,7 +16,7 @@ class TechnicalAnalysis():
16
  def __init__(
17
  self,
18
  ticker:str,
19
- fetchperiodinweeks:int=8,
20
  plot_ta:bool=True,
21
  debug=False):
22
  # input arguments
@@ -40,9 +40,11 @@ class TechnicalAnalysis():
40
  self.logger = logging.getLogger(__name__)
41
  logging.basicConfig(level=self.logger_level) # filename='TechnicalAnalysis.log',
42
 
 
43
  self.ticker = ticker
44
  self.fetchperiodinweeks = fetchperiodinweeks
45
  self.plot_ta = plot_ta
 
46
  self.logger.info(f'Initialized TechnicalAnalysis object for ticker: {ticker}')
47
 
48
  def run(
@@ -58,18 +60,19 @@ class TechnicalAnalysis():
58
  """
59
  self.df = self.fetch_data()
60
  # add the features based on technical analysis
61
- self.df = self.tech_analysis()
62
- # plot the results
63
- if self.plot_ta:
64
- os.makedirs('plots', exist_ok=True)
65
- self.plot_stock_metrics(
66
- self.df,
67
- datasets={
68
- 'Volume': ['Volume'],
69
- 'Prices': ['Close', 'VWAP'], # 'High','Low',
70
- 'Indices': ['RSI', 'StochOsc'],
71
- 'Trend': ['MACD', 'MACDsig', 'MACDdif']}
72
- )
 
73
  return self.df
74
 
75
  def fetch_data(
@@ -112,8 +115,6 @@ class TechnicalAnalysis():
112
  df.columns = df.columns.droplevel('Ticker')
113
  self.logger.debug(df.head(10))
114
  self.logger.info(f'Fetched {df.shape[0]} rows for {self.ticker}')
115
- else:
116
- raise Exception(f'Failed to fetch data for {self.ticker}')
117
 
118
  return df
119
 
 
16
  def __init__(
17
  self,
18
  ticker:str,
19
+ fetchperiodinweeks:int=12,
20
  plot_ta:bool=True,
21
  debug=False):
22
  # input arguments
 
40
  self.logger = logging.getLogger(__name__)
41
  logging.basicConfig(level=self.logger_level) # filename='TechnicalAnalysis.log',
42
 
43
+ # input arguments
44
  self.ticker = ticker
45
  self.fetchperiodinweeks = fetchperiodinweeks
46
  self.plot_ta = plot_ta
47
+ # done initializing
48
  self.logger.info(f'Initialized TechnicalAnalysis object for ticker: {ticker}')
49
 
50
  def run(
 
60
  """
61
  self.df = self.fetch_data()
62
  # add the features based on technical analysis
63
+ if self.df.shape[0] > 0:
64
+ self.df = self.tech_analysis()
65
+ # plot the results
66
+ if self.plot_ta:
67
+ os.makedirs('plots', exist_ok=True)
68
+ self.plot_stock_metrics(
69
+ self.df,
70
+ datasets={
71
+ 'Volume': ['Volume'],
72
+ 'Prices': ['Close', 'VWAP'], # 'High','Low',
73
+ 'Indices': ['RSI', 'StochOsc'],
74
+ 'Trend': ['MACD', 'MACDsig', 'MACDdif']}
75
+ )
76
  return self.df
77
 
78
  def fetch_data(
 
115
  df.columns = df.columns.droplevel('Ticker')
116
  self.logger.debug(df.head(10))
117
  self.logger.info(f'Fetched {df.shape[0]} rows for {self.ticker}')
 
 
118
 
119
  return df
120