Spaces:
Running
on
Zero
Running
on
Zero
adds padding, time windows based on yfinance
Browse files
app.py
CHANGED
@@ -57,7 +57,7 @@ def get_historical_data(symbol: str, timeframe: str = "1d", lookback_days: int =
|
|
57 |
pd.DataFrame: Historical data with OHLCV and technical indicators
|
58 |
"""
|
59 |
try:
|
60 |
-
# Map timeframe to yfinance interval
|
61 |
tf_map = {
|
62 |
"1d": "1d",
|
63 |
"1h": "1h",
|
@@ -65,6 +65,12 @@ def get_historical_data(symbol: str, timeframe: str = "1d", lookback_days: int =
|
|
65 |
}
|
66 |
interval = tf_map.get(timeframe, "1d")
|
67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
# Calculate date range
|
69 |
end_date = datetime.now()
|
70 |
start_date = end_date - timedelta(days=lookback_days)
|
@@ -73,6 +79,9 @@ def get_historical_data(symbol: str, timeframe: str = "1d", lookback_days: int =
|
|
73 |
ticker = yf.Ticker(symbol)
|
74 |
df = ticker.history(start=start_date, end=end_date, interval=interval)
|
75 |
|
|
|
|
|
|
|
76 |
# Get additional info for structured products
|
77 |
info = ticker.info
|
78 |
df['Market_Cap'] = info.get('marketCap', None)
|
@@ -80,31 +89,50 @@ def get_historical_data(symbol: str, timeframe: str = "1d", lookback_days: int =
|
|
80 |
df['Industry'] = info.get('industry', None)
|
81 |
df['Dividend_Yield'] = info.get('dividendYield', None)
|
82 |
|
83 |
-
# Calculate technical indicators
|
84 |
-
|
85 |
-
|
86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
df['RSI'] = calculate_rsi(df['Close'])
|
88 |
df['MACD'], df['MACD_Signal'] = calculate_macd(df['Close'])
|
89 |
df['BB_Upper'], df['BB_Middle'], df['BB_Lower'] = calculate_bollinger_bands(df['Close'])
|
90 |
|
91 |
# Calculate returns and volatility
|
92 |
df['Returns'] = df['Close'].pct_change()
|
93 |
-
df['Volatility'] = df['Returns'].rolling(window=
|
94 |
-
df['Annualized_Vol'] = df['Volatility'] * np.sqrt(252)
|
95 |
|
96 |
# Calculate drawdown metrics
|
97 |
-
df['Rolling_Max'] = df['Close'].rolling(window=
|
98 |
df['Drawdown'] = (df['Close'] - df['Rolling_Max']) / df['Rolling_Max']
|
99 |
-
df['Max_Drawdown'] = df['Drawdown'].rolling(window=
|
100 |
|
101 |
# Calculate liquidity metrics
|
102 |
-
df['Avg_Daily_Volume'] = df['Volume'].rolling(window=
|
103 |
-
df['Volume_Volatility'] = df['Volume'].rolling(window=
|
104 |
|
105 |
# Drop NaN values
|
106 |
df = df.dropna()
|
107 |
|
|
|
|
|
|
|
108 |
return df
|
109 |
|
110 |
except Exception as e:
|
@@ -157,6 +185,14 @@ def make_prediction(symbol: str, timeframe: str = "1d", prediction_days: int = 5
|
|
157 |
# Prepare data for Chronos
|
158 |
returns = df['Returns'].values
|
159 |
normalized_returns = (returns - returns.mean()) / returns.std()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
160 |
context = torch.tensor(normalized_returns.reshape(-1, 1), dtype=torch.float32)
|
161 |
|
162 |
# Make prediction with GPU acceleration
|
@@ -176,7 +212,10 @@ def make_prediction(symbol: str, timeframe: str = "1d", prediction_days: int = 5
|
|
176 |
elif timeframe == "1h":
|
177 |
actual_prediction_length = min(prediction_days * 24, max_prediction_length)
|
178 |
else: # 15m
|
179 |
-
actual_prediction_length = min(prediction_days * 96, max_prediction_length)
|
|
|
|
|
|
|
180 |
|
181 |
with torch.inference_mode():
|
182 |
prediction = pipe.predict(
|
|
|
57 |
pd.DataFrame: Historical data with OHLCV and technical indicators
|
58 |
"""
|
59 |
try:
|
60 |
+
# Map timeframe to yfinance interval and adjust lookback period
|
61 |
tf_map = {
|
62 |
"1d": "1d",
|
63 |
"1h": "1h",
|
|
|
65 |
}
|
66 |
interval = tf_map.get(timeframe, "1d")
|
67 |
|
68 |
+
# Adjust lookback period based on timeframe
|
69 |
+
if timeframe == "1h":
|
70 |
+
lookback_days = min(lookback_days, 30) # Yahoo limits hourly data to 30 days
|
71 |
+
elif timeframe == "15m":
|
72 |
+
lookback_days = min(lookback_days, 5) # Yahoo limits 15m data to 5 days
|
73 |
+
|
74 |
# Calculate date range
|
75 |
end_date = datetime.now()
|
76 |
start_date = end_date - timedelta(days=lookback_days)
|
|
|
79 |
ticker = yf.Ticker(symbol)
|
80 |
df = ticker.history(start=start_date, end=end_date, interval=interval)
|
81 |
|
82 |
+
if df.empty:
|
83 |
+
raise Exception(f"No data available for {symbol} in {timeframe} timeframe")
|
84 |
+
|
85 |
# Get additional info for structured products
|
86 |
info = ticker.info
|
87 |
df['Market_Cap'] = info.get('marketCap', None)
|
|
|
89 |
df['Industry'] = info.get('industry', None)
|
90 |
df['Dividend_Yield'] = info.get('dividendYield', None)
|
91 |
|
92 |
+
# Calculate technical indicators with adjusted windows based on timeframe
|
93 |
+
if timeframe == "1d":
|
94 |
+
sma_window_20 = 20
|
95 |
+
sma_window_50 = 50
|
96 |
+
sma_window_200 = 200
|
97 |
+
vol_window = 20
|
98 |
+
elif timeframe == "1h":
|
99 |
+
sma_window_20 = 20 * 6 # 5 trading days
|
100 |
+
sma_window_50 = 50 * 6 # ~10 trading days
|
101 |
+
sma_window_200 = 200 * 6 # ~40 trading days
|
102 |
+
vol_window = 20 * 6
|
103 |
+
else: # 15m
|
104 |
+
sma_window_20 = 20 * 24 # 5 trading days
|
105 |
+
sma_window_50 = 50 * 24 # ~10 trading days
|
106 |
+
sma_window_200 = 200 * 24 # ~40 trading days
|
107 |
+
vol_window = 20 * 24
|
108 |
+
|
109 |
+
df['SMA_20'] = df['Close'].rolling(window=sma_window_20).mean()
|
110 |
+
df['SMA_50'] = df['Close'].rolling(window=sma_window_50).mean()
|
111 |
+
df['SMA_200'] = df['Close'].rolling(window=sma_window_200).mean()
|
112 |
df['RSI'] = calculate_rsi(df['Close'])
|
113 |
df['MACD'], df['MACD_Signal'] = calculate_macd(df['Close'])
|
114 |
df['BB_Upper'], df['BB_Middle'], df['BB_Lower'] = calculate_bollinger_bands(df['Close'])
|
115 |
|
116 |
# Calculate returns and volatility
|
117 |
df['Returns'] = df['Close'].pct_change()
|
118 |
+
df['Volatility'] = df['Returns'].rolling(window=vol_window).std()
|
119 |
+
df['Annualized_Vol'] = df['Volatility'] * np.sqrt(252)
|
120 |
|
121 |
# Calculate drawdown metrics
|
122 |
+
df['Rolling_Max'] = df['Close'].rolling(window=len(df), min_periods=1).max()
|
123 |
df['Drawdown'] = (df['Close'] - df['Rolling_Max']) / df['Rolling_Max']
|
124 |
+
df['Max_Drawdown'] = df['Drawdown'].rolling(window=len(df), min_periods=1).min()
|
125 |
|
126 |
# Calculate liquidity metrics
|
127 |
+
df['Avg_Daily_Volume'] = df['Volume'].rolling(window=vol_window).mean()
|
128 |
+
df['Volume_Volatility'] = df['Volume'].rolling(window=vol_window).std()
|
129 |
|
130 |
# Drop NaN values
|
131 |
df = df.dropna()
|
132 |
|
133 |
+
if len(df) < 2:
|
134 |
+
raise Exception(f"Insufficient data points for {symbol} in {timeframe} timeframe")
|
135 |
+
|
136 |
return df
|
137 |
|
138 |
except Exception as e:
|
|
|
185 |
# Prepare data for Chronos
|
186 |
returns = df['Returns'].values
|
187 |
normalized_returns = (returns - returns.mean()) / returns.std()
|
188 |
+
|
189 |
+
# Ensure we have enough data points
|
190 |
+
min_data_points = 64 # Minimum required by Chronos
|
191 |
+
if len(normalized_returns) < min_data_points:
|
192 |
+
# Pad the data with the last value
|
193 |
+
padding = np.full(min_data_points - len(normalized_returns), normalized_returns[-1])
|
194 |
+
normalized_returns = np.concatenate([padding, normalized_returns])
|
195 |
+
|
196 |
context = torch.tensor(normalized_returns.reshape(-1, 1), dtype=torch.float32)
|
197 |
|
198 |
# Make prediction with GPU acceleration
|
|
|
212 |
elif timeframe == "1h":
|
213 |
actual_prediction_length = min(prediction_days * 24, max_prediction_length)
|
214 |
else: # 15m
|
215 |
+
actual_prediction_length = min(prediction_days * 96, max_prediction_length)
|
216 |
+
|
217 |
+
# Ensure prediction length is at least 1
|
218 |
+
actual_prediction_length = max(1, actual_prediction_length)
|
219 |
|
220 |
with torch.inference_mode():
|
221 |
prediction = pipe.predict(
|