henriquebap commited on
Commit
b40af40
·
verified ·
1 Parent(s): c8506f3

Fix: Use yfinance 0.2.28 (no websockets dependency)

Browse files
Files changed (2) hide show
  1. app.py +31 -24
  2. requirements.txt +1 -2
app.py CHANGED
@@ -158,35 +158,42 @@ class LSTMModel(nn.Module):
158
  # ============================================================================
159
 
160
  def load_stock_data(symbol: str, days: int = 400) -> pd.DataFrame:
161
- """Carrega dados com múltiplas tentativas."""
162
  import yfinance as yf
163
 
164
  end = datetime.now()
165
  start = end - timedelta(days=days)
166
 
167
- # Tentar múltiplas vezes
168
- for attempt in range(3):
169
- try:
170
- ticker = yf.Ticker(symbol)
171
- df = ticker.history(
172
- start=start.strftime('%Y-%m-%d'),
173
- end=end.strftime('%Y-%m-%d'),
174
- auto_adjust=True
175
- )
176
-
177
- if not df.empty:
178
- df = df.reset_index()
179
- df.columns = df.columns.str.lower()
180
- if 'date' in df.columns:
181
- df = df.rename(columns={'date': 'timestamp'})
182
- return df
183
-
184
- except Exception as e:
185
- if attempt == 2:
186
- raise ValueError(f"Não foi possível obter dados para {symbol}: {str(e)}")
187
- continue
188
-
189
- raise ValueError(f"Dados não encontrados para {symbol}")
 
 
 
 
 
 
 
190
 
191
 
192
  def create_features(df: pd.DataFrame) -> pd.DataFrame:
 
158
  # ============================================================================
159
 
160
  def load_stock_data(symbol: str, days: int = 400) -> pd.DataFrame:
161
+ """Carrega dados usando yfinance.download (mais estável)."""
162
  import yfinance as yf
163
 
164
  end = datetime.now()
165
  start = end - timedelta(days=days)
166
 
167
+ try:
168
+ # Usar download ao invés de Ticker.history (mais estável)
169
+ df = yf.download(
170
+ symbol,
171
+ start=start.strftime('%Y-%m-%d'),
172
+ end=end.strftime('%Y-%m-%d'),
173
+ progress=False,
174
+ auto_adjust=True
175
+ )
176
+
177
+ if df.empty:
178
+ raise ValueError(f"Dados não encontrados para {symbol}")
179
+
180
+ # Tratar MultiIndex columns
181
+ if isinstance(df.columns, pd.MultiIndex):
182
+ df.columns = df.columns.get_level_values(0)
183
+
184
+ df = df.reset_index()
185
+ df.columns = df.columns.str.lower()
186
+
187
+ # Renomear coluna de data
188
+ for col in ['date', 'Date', 'datetime', 'Datetime']:
189
+ if col in df.columns:
190
+ df = df.rename(columns={col: 'timestamp'})
191
+ break
192
+
193
+ return df
194
+
195
+ except Exception as e:
196
+ raise ValueError(f"Erro ao obter dados para {symbol}: {str(e)}")
197
 
198
 
199
  def create_features(df: pd.DataFrame) -> pd.DataFrame:
requirements.txt CHANGED
@@ -3,7 +3,6 @@ torch
3
  pandas
4
  numpy
5
  scikit-learn
6
- yfinance>=0.2.50
7
- websockets>=11.0
8
  huggingface_hub
9
  joblib
 
3
  pandas
4
  numpy
5
  scikit-learn
6
+ yfinance==0.2.28
 
7
  huggingface_hub
8
  joblib