Tonic commited on
Commit
1c92324
·
1 Parent(s): fd75294

improves error handling by by passing company info

Browse files
Files changed (1) hide show
  1. app.py +20 -10
app.py CHANGED
@@ -144,14 +144,24 @@ def get_historical_data(symbol: str, timeframe: str = "1d", lookback_days: int =
144
 
145
  # Get additional info for structured products with retry mechanism
146
  def fetch_info():
147
- return ticker.info
 
 
 
148
 
149
- info = retry_yfinance_request(fetch_info)
150
-
151
- df['Market_Cap'] = info.get('marketCap', None)
152
- df['Sector'] = info.get('sector', None)
153
- df['Industry'] = info.get('industry', None)
154
- df['Dividend_Yield'] = info.get('dividendYield', None)
 
 
 
 
 
 
 
155
 
156
  # Calculate technical indicators with adjusted windows based on timeframe
157
  if timeframe == "1d":
@@ -192,8 +202,8 @@ def get_historical_data(symbol: str, timeframe: str = "1d", lookback_days: int =
192
  df['Avg_Daily_Volume'] = df['Volume'].rolling(window=vol_window, min_periods=1).mean()
193
  df['Volume_Volatility'] = df['Volume'].rolling(window=vol_window, min_periods=1).std()
194
 
195
- # Fill NaN values with forward fill, then backward fill
196
- df = df.fillna(method='ffill').fillna(method='bfill')
197
 
198
  # Ensure we have enough data points
199
  min_required_points = 64 # Minimum required for Chronos
@@ -207,7 +217,7 @@ def get_historical_data(symbol: str, timeframe: str = "1d", lookback_days: int =
207
  extended_df = retry_yfinance_request(fetch_extended_history)
208
  if not extended_df.empty:
209
  df = pd.concat([extended_df, df])
210
- df = df.fillna(method='ffill').fillna(method='bfill')
211
 
212
  if len(df) < 2:
213
  raise Exception(f"Insufficient data points for {symbol} in {timeframe} timeframe")
 
144
 
145
  # Get additional info for structured products with retry mechanism
146
  def fetch_info():
147
+ info = ticker.info
148
+ if info is None:
149
+ raise Exception(f"Could not fetch company info for {symbol}")
150
+ return info
151
 
152
+ try:
153
+ info = retry_yfinance_request(fetch_info)
154
+ df['Market_Cap'] = info.get('marketCap', None)
155
+ df['Sector'] = info.get('sector', None)
156
+ df['Industry'] = info.get('industry', None)
157
+ df['Dividend_Yield'] = info.get('dividendYield', None)
158
+ except Exception as e:
159
+ print(f"Warning: Could not fetch company info for {symbol}: {str(e)}")
160
+ # Set default values for missing info
161
+ df['Market_Cap'] = None
162
+ df['Sector'] = None
163
+ df['Industry'] = None
164
+ df['Dividend_Yield'] = None
165
 
166
  # Calculate technical indicators with adjusted windows based on timeframe
167
  if timeframe == "1d":
 
202
  df['Avg_Daily_Volume'] = df['Volume'].rolling(window=vol_window, min_periods=1).mean()
203
  df['Volume_Volatility'] = df['Volume'].rolling(window=vol_window, min_periods=1).std()
204
 
205
+ # Fill NaN values using forward fill then backward fill (updated to use ffill/bfill)
206
+ df = df.ffill().bfill()
207
 
208
  # Ensure we have enough data points
209
  min_required_points = 64 # Minimum required for Chronos
 
217
  extended_df = retry_yfinance_request(fetch_extended_history)
218
  if not extended_df.empty:
219
  df = pd.concat([extended_df, df])
220
+ df = df.ffill().bfill()
221
 
222
  if len(df) < 2:
223
  raise Exception(f"Insufficient data points for {symbol} in {timeframe} timeframe")