Spaces:
Paused
Paused
Tonic commited on
Commit ·
fd75294
1
Parent(s): 2665f90
adds retry mechanism
Browse files
app.py
CHANGED
|
@@ -14,12 +14,37 @@ import json
|
|
| 14 |
import spaces
|
| 15 |
import gc
|
| 16 |
import pytz
|
|
|
|
|
|
|
| 17 |
|
| 18 |
# Initialize global variables
|
| 19 |
pipeline = None
|
| 20 |
scaler = MinMaxScaler(feature_range=(-1, 1))
|
| 21 |
scaler.fit_transform([[-1, 1]])
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
def clear_gpu_memory():
|
| 24 |
"""Clear GPU memory cache"""
|
| 25 |
if torch.cuda.is_available():
|
|
@@ -106,15 +131,23 @@ def get_historical_data(symbol: str, timeframe: str = "1d", lookback_days: int =
|
|
| 106 |
end_date = datetime.now()
|
| 107 |
start_date = end_date - timedelta(days=lookback_days)
|
| 108 |
|
| 109 |
-
# Fetch data using yfinance
|
| 110 |
ticker = yf.Ticker(symbol)
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
if df.empty:
|
| 114 |
raise Exception(f"No data available for {symbol} in {timeframe} timeframe")
|
| 115 |
|
| 116 |
-
# Get additional info for structured products
|
| 117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
df['Market_Cap'] = info.get('marketCap', None)
|
| 119 |
df['Sector'] = info.get('sector', None)
|
| 120 |
df['Industry'] = info.get('industry', None)
|
|
@@ -165,9 +198,13 @@ def get_historical_data(symbol: str, timeframe: str = "1d", lookback_days: int =
|
|
| 165 |
# Ensure we have enough data points
|
| 166 |
min_required_points = 64 # Minimum required for Chronos
|
| 167 |
if len(df) < min_required_points:
|
| 168 |
-
# Try to fetch more historical data
|
| 169 |
extended_start_date = start_date - timedelta(days=min_required_points - len(df))
|
| 170 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
if not extended_df.empty:
|
| 172 |
df = pd.concat([extended_df, df])
|
| 173 |
df = df.fillna(method='ffill').fillna(method='bfill')
|
|
|
|
| 14 |
import spaces
|
| 15 |
import gc
|
| 16 |
import pytz
|
| 17 |
+
import time
|
| 18 |
+
import random
|
| 19 |
|
| 20 |
# Initialize global variables
|
| 21 |
pipeline = None
|
| 22 |
scaler = MinMaxScaler(feature_range=(-1, 1))
|
| 23 |
scaler.fit_transform([[-1, 1]])
|
| 24 |
|
| 25 |
+
def retry_yfinance_request(func, max_retries=3, initial_delay=1):
|
| 26 |
+
"""
|
| 27 |
+
Retry mechanism for yfinance requests with exponential backoff.
|
| 28 |
+
|
| 29 |
+
Args:
|
| 30 |
+
func: Function to retry
|
| 31 |
+
max_retries: Maximum number of retry attempts
|
| 32 |
+
initial_delay: Initial delay in seconds before first retry
|
| 33 |
+
|
| 34 |
+
Returns:
|
| 35 |
+
Result of the function call if successful
|
| 36 |
+
"""
|
| 37 |
+
for attempt in range(max_retries):
|
| 38 |
+
try:
|
| 39 |
+
return func()
|
| 40 |
+
except Exception as e:
|
| 41 |
+
if "401" in str(e) and attempt < max_retries - 1:
|
| 42 |
+
# Calculate delay with exponential backoff and jitter
|
| 43 |
+
delay = initial_delay * (2 ** attempt) + random.uniform(0, 1)
|
| 44 |
+
time.sleep(delay)
|
| 45 |
+
continue
|
| 46 |
+
raise e
|
| 47 |
+
|
| 48 |
def clear_gpu_memory():
|
| 49 |
"""Clear GPU memory cache"""
|
| 50 |
if torch.cuda.is_available():
|
|
|
|
| 131 |
end_date = datetime.now()
|
| 132 |
start_date = end_date - timedelta(days=lookback_days)
|
| 133 |
|
| 134 |
+
# Fetch data using yfinance with retry mechanism
|
| 135 |
ticker = yf.Ticker(symbol)
|
| 136 |
+
|
| 137 |
+
def fetch_history():
|
| 138 |
+
return ticker.history(start=start_date, end=end_date, interval=interval)
|
| 139 |
+
|
| 140 |
+
df = retry_yfinance_request(fetch_history)
|
| 141 |
|
| 142 |
if df.empty:
|
| 143 |
raise Exception(f"No data available for {symbol} in {timeframe} timeframe")
|
| 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)
|
|
|
|
| 198 |
# Ensure we have enough data points
|
| 199 |
min_required_points = 64 # Minimum required for Chronos
|
| 200 |
if len(df) < min_required_points:
|
| 201 |
+
# Try to fetch more historical data with retry mechanism
|
| 202 |
extended_start_date = start_date - timedelta(days=min_required_points - len(df))
|
| 203 |
+
|
| 204 |
+
def fetch_extended_history():
|
| 205 |
+
return ticker.history(start=extended_start_date, end=start_date, interval=interval)
|
| 206 |
+
|
| 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')
|