Nexo-S commited on
Commit
67cc9b7
·
verified ·
1 Parent(s): 52f5775

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -8
app.py CHANGED
@@ -18,7 +18,7 @@ from datetime import datetime, timezone
18
  from huggingface_hub import HfApi, hf_hub_download
19
  import gradio as gr
20
  import ccxt
21
-
22
  import sys
23
  from types import ModuleType
24
  import pandas as pd
@@ -194,18 +194,33 @@ try:
194
  except Exception as e: print(f"⚠️ Erreur IA : {e}")
195
 
196
  # --- 📊 FEATURES ENGINE ---
197
- def prepare_features_sync(symbol, timeframe='1h'):
198
- """Version Synchrone ultra-stable pour l'Auto-Pilote et le Training"""
 
 
199
  try:
200
  now = datetime.now().timestamp()
201
  cache_key = f"{symbol}_{timeframe}"
202
 
203
- # 1. Gestion du Cache (Synchrone)
204
  if cache_key in market_cache and cache_key in last_fetch_time and now - last_fetch_time[cache_key] < CACHE_DURATION:
205
  df = market_cache[cache_key].copy()
206
  else:
207
- # On utilise l'instance synchrone de CCXT définie plus haut (exchange_sync)
208
- bars = exchange_sync.fetch_ohlcv(symbol, timeframe=timeframe, limit=700)
 
 
 
 
 
 
 
 
 
 
 
 
 
209
  df = pd.DataFrame(bars, columns=['ts', 'open', 'high', 'low', 'close', 'vol'])
210
  market_cache[cache_key] = df.copy()
211
  last_fetch_time[cache_key] = now
@@ -213,7 +228,7 @@ def prepare_features_sync(symbol, timeframe='1h'):
213
  if len(df) < 250:
214
  return pd.DataFrame()
215
 
216
- # --- CALCULS TECHNIQUES (VECTORISÉS) ---
217
  df["RSI"] = get_rsi(df["close"])
218
  df["EMA50"] = get_ema(df["close"], 50)
219
  df["EMA200"] = get_ema(df["close"], 200)
@@ -251,7 +266,7 @@ def prepare_features_sync(symbol, timeframe='1h'):
251
  return df.dropna().copy()
252
 
253
  except Exception as e:
254
- print(f"❌ Sync Features Error ({symbol}): {e}")
255
  return pd.DataFrame()
256
  # --- 🎯 API PRÉDICTION ---
257
  async def predict_signal(symbol, timeframe="1h"):
 
18
  from huggingface_hub import HfApi, hf_hub_download
19
  import gradio as gr
20
  import ccxt
21
+ import time
22
  import sys
23
  from types import ModuleType
24
  import pandas as pd
 
194
  except Exception as e: print(f"⚠️ Erreur IA : {e}")
195
 
196
  # --- 📊 FEATURES ENGINE ---
197
+ # N'oublie pas d'ajouter ça en haut de ton fichier si ce n'est pas fait !
198
+
199
+ def prepare_features_sync(symbol, timeframe='1h', limit_bars=600):
200
+ """Version Synchrone avec Bouclier Anti-Blocage Kucoin (3 essais)"""
201
  try:
202
  now = datetime.now().timestamp()
203
  cache_key = f"{symbol}_{timeframe}"
204
 
205
+ # 1. Gestion du Cache
206
  if cache_key in market_cache and cache_key in last_fetch_time and now - last_fetch_time[cache_key] < CACHE_DURATION:
207
  df = market_cache[cache_key].copy()
208
  else:
209
+ # --- 🛡️ SYSTÈME DE RETRY (3 TENTATIVES) ---
210
+ bars = None
211
+ for attempt in range(3):
212
+ try:
213
+ bars = exchange_sync.fetch_ohlcv(symbol, timeframe=timeframe, limit=limit_bars)
214
+ break # Si ça passe, on sort de la boucle de retry
215
+ except Exception as e:
216
+ print(f"⚠️ Kucoin bloque {symbol} (Essai {attempt+1}/3). Attente 2s...")
217
+ time.sleep(2) # Pause tactique
218
+
219
+ # Si après 3 essais on n'a toujours rien, on abandonne proprement
220
+ if not bars:
221
+ print(f"❌ Échec total de la récupération pour {symbol}.")
222
+ return pd.DataFrame()
223
+
224
  df = pd.DataFrame(bars, columns=['ts', 'open', 'high', 'low', 'close', 'vol'])
225
  market_cache[cache_key] = df.copy()
226
  last_fetch_time[cache_key] = now
 
228
  if len(df) < 250:
229
  return pd.DataFrame()
230
 
231
+ # --- CALCULS TECHNIQUES ---
232
  df["RSI"] = get_rsi(df["close"])
233
  df["EMA50"] = get_ema(df["close"], 50)
234
  df["EMA200"] = get_ema(df["close"], 200)
 
266
  return df.dropna().copy()
267
 
268
  except Exception as e:
269
+ print(f"❌ Erreur critique calculs ({symbol}): {e}")
270
  return pd.DataFrame()
271
  # --- 🎯 API PRÉDICTION ---
272
  async def predict_signal(symbol, timeframe="1h"):