Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -594,6 +594,10 @@ async def predict_signal(symbol, timeframe="1h"):
|
|
| 594 |
if veto:
|
| 595 |
return {"symbol": symbol, "timeframe": timeframe, "status": "veto", "message": veto_reason, "scenario": final_scenario}
|
| 596 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 597 |
# 💾 9. ENREGISTREMENT DB
|
| 598 |
db_task = (datetime.now(timezone.utc).isoformat(), symbol, timeframe, 'HAUSSIER' if final_p > 0.5 else 'BAISSIER', final_p, prix, tp, sl, 'EN_COURS', final_scenario, time_prob, ml_prob, dino_prob, p_sent, prix)
|
| 599 |
await save_to_db(db_task)
|
|
@@ -605,55 +609,64 @@ async def predict_signal(symbol, timeframe="1h"):
|
|
| 605 |
}
|
| 606 |
except Exception as e: return {"status": "error", "message": str(e)}
|
| 607 |
|
| 608 |
-
# =============================
|
| 609 |
# =============================
|
| 610 |
def mutate_agent(symbol, timeframe, regime, success=True):
|
| 611 |
-
|
| 612 |
-
|
| 613 |
-
|
| 614 |
-
|
| 615 |
-
|
| 616 |
-
|
| 617 |
-
|
| 618 |
-
|
| 619 |
-
|
| 620 |
-
if not row:
|
| 621 |
-
res_def = conn.execute(
|
| 622 |
-
"SELECT * FROM agent_logic WHERE symbol = 'ALL' AND timeframe = ?",
|
| 623 |
-
(timeframe,)
|
| 624 |
).fetchone()
|
| 625 |
-
tp, sl, prob = (res_def['tp_mult'], res_def['sl_mult'], res_def['min_prob']) if res_def else (1.5, 1.0, 0.60)
|
| 626 |
-
else:
|
| 627 |
-
tp, sl, prob = row['tp_mult'], row['sl_mult'], row['min_prob']
|
| 628 |
|
| 629 |
-
|
| 630 |
-
|
| 631 |
-
|
| 632 |
-
|
| 633 |
-
|
| 634 |
-
|
| 635 |
-
|
| 636 |
-
|
| 637 |
-
|
| 638 |
-
|
| 639 |
-
|
| 640 |
-
|
| 641 |
-
|
| 642 |
-
|
| 643 |
-
|
| 644 |
-
|
| 645 |
-
|
| 646 |
-
|
| 647 |
-
|
| 648 |
-
|
| 649 |
-
|
| 650 |
-
|
| 651 |
-
|
| 652 |
-
|
| 653 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 654 |
|
| 655 |
-
|
| 656 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 657 |
|
| 658 |
|
| 659 |
# =============================
|
|
@@ -727,15 +740,14 @@ async def dream_simulation_loop():
|
|
| 727 |
await asyncio.sleep(60)
|
| 728 |
|
| 729 |
def should_send_signal(symbol, timeframe, proba):
|
|
|
|
| 730 |
key = (symbol, timeframe)
|
| 731 |
now = time.time()
|
| 732 |
|
| 733 |
if key in last_signals_sent:
|
| 734 |
last_proba, last_time = last_signals_sent[key]
|
| 735 |
-
#
|
| 736 |
-
|
| 737 |
-
# 🔥 filtre proba (Tolérance augmentée à 10%)
|
| 738 |
-
if abs(last_proba - proba) < 0.10 and (now - last_time) < 900:
|
| 739 |
return False
|
| 740 |
|
| 741 |
last_signals_sent[key] = (proba, now)
|
|
|
|
| 594 |
if veto:
|
| 595 |
return {"symbol": symbol, "timeframe": timeframe, "status": "veto", "message": veto_reason, "scenario": final_scenario}
|
| 596 |
|
| 597 |
+
# 🛑 8.5 ANTI-SPAM (On bloque ici si le signal a déjà été envoyé)
|
| 598 |
+
if not should_send_signal(symbol, timeframe, final_p):
|
| 599 |
+
return {"symbol": symbol, "timeframe": timeframe, "status": "veto", "message": "Anti-Spam Actif (Déjà envoyé)", "scenario": final_scenario}
|
| 600 |
+
|
| 601 |
# 💾 9. ENREGISTREMENT DB
|
| 602 |
db_task = (datetime.now(timezone.utc).isoformat(), symbol, timeframe, 'HAUSSIER' if final_p > 0.5 else 'BAISSIER', final_p, prix, tp, sl, 'EN_COURS', final_scenario, time_prob, ml_prob, dino_prob, p_sent, prix)
|
| 603 |
await save_to_db(db_task)
|
|
|
|
| 609 |
}
|
| 610 |
except Exception as e: return {"status": "error", "message": str(e)}
|
| 611 |
|
|
|
|
| 612 |
# =============================
|
| 613 |
def mutate_agent(symbol, timeframe, regime, success=True):
|
| 614 |
+
import sqlite3, random, numpy as np, time
|
| 615 |
+
|
| 616 |
+
for tentative in range(5): # 🔄 Tente 5 fois si la base est verrouillée
|
| 617 |
+
try:
|
| 618 |
+
with sqlite3.connect(DB_NAME, timeout=20) as conn:
|
| 619 |
+
conn.row_factory = sqlite3.Row
|
| 620 |
+
row = conn.execute(
|
| 621 |
+
"SELECT * FROM agent_logic WHERE symbol = ? AND timeframe = ? AND regime = ?",
|
| 622 |
+
(symbol, timeframe, regime)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 623 |
).fetchone()
|
|
|
|
|
|
|
|
|
|
| 624 |
|
| 625 |
+
if not row:
|
| 626 |
+
res_def = conn.execute(
|
| 627 |
+
"SELECT * FROM agent_logic WHERE symbol = 'ALL' AND timeframe = ?",
|
| 628 |
+
(timeframe,)
|
| 629 |
+
).fetchone()
|
| 630 |
+
tp, sl, prob = (res_def['tp_mult'], res_def['sl_mult'], res_def['min_prob']) if res_def else (1.5, 1.0, 0.60)
|
| 631 |
+
else:
|
| 632 |
+
tp, sl, prob = row['tp_mult'], row['sl_mult'], row['min_prob']
|
| 633 |
+
|
| 634 |
+
# 🧠 MUTATION DOUCE
|
| 635 |
+
if success:
|
| 636 |
+
new_tp = tp * random.uniform(1.01, 1.05)
|
| 637 |
+
new_sl = sl * random.uniform(0.98, 1.00)
|
| 638 |
+
new_prob = max(0.55, prob - 0.003)
|
| 639 |
+
else:
|
| 640 |
+
new_tp = tp * random.uniform(0.95, 0.99)
|
| 641 |
+
new_sl = sl * random.uniform(1.01, 1.05)
|
| 642 |
+
new_prob = min(0.85, prob + 0.005)
|
| 643 |
+
|
| 644 |
+
# 🛡️ HARD CLAMP (ANTI-EXPLOSION)
|
| 645 |
+
new_tp = float(np.clip(new_tp, 0.5, 3.0))
|
| 646 |
+
new_sl = float(np.clip(new_sl, 0.5, 2.0))
|
| 647 |
+
|
| 648 |
+
conn.execute('''
|
| 649 |
+
INSERT OR REPLACE INTO agent_logic
|
| 650 |
+
(symbol, timeframe, regime, tp_mult, sl_mult, min_prob, min_tp_dist, generation)
|
| 651 |
+
VALUES (?, ?, ?, ?, ?, ?, ?,
|
| 652 |
+
COALESCE((SELECT generation FROM agent_logic WHERE symbol=? AND timeframe=? AND regime=?)+1, 1))
|
| 653 |
+
''', (
|
| 654 |
+
symbol, timeframe, regime,
|
| 655 |
+
new_tp, new_sl, new_prob, 0.001,
|
| 656 |
+
symbol, timeframe, regime
|
| 657 |
+
))
|
| 658 |
+
conn.commit()
|
| 659 |
+
return # ✅ Succès, on sort de la fonction
|
| 660 |
|
| 661 |
+
except sqlite3.OperationalError as e:
|
| 662 |
+
if "locked" in str(e):
|
| 663 |
+
time.sleep(1) # La porte est bloquée, on attend 1 seconde
|
| 664 |
+
else:
|
| 665 |
+
print(f"❌ [ERREUR DB] Mutation échouée : {e}")
|
| 666 |
+
break
|
| 667 |
+
except Exception as e:
|
| 668 |
+
print(f"🧬 Erreur Mutation V32: {e}")
|
| 669 |
+
break
|
| 670 |
|
| 671 |
|
| 672 |
# =============================
|
|
|
|
| 740 |
await asyncio.sleep(60)
|
| 741 |
|
| 742 |
def should_send_signal(symbol, timeframe, proba):
|
| 743 |
+
global last_signals_sent
|
| 744 |
key = (symbol, timeframe)
|
| 745 |
now = time.time()
|
| 746 |
|
| 747 |
if key in last_signals_sent:
|
| 748 |
last_proba, last_time = last_signals_sent[key]
|
| 749 |
+
# 🛡️ Tolérance stricte : 5% d'écart max, et blocage pendant 1 heure (3600s)
|
| 750 |
+
if abs(last_proba - proba) < 0.05 and (now - last_time) < 3600:
|
|
|
|
|
|
|
| 751 |
return False
|
| 752 |
|
| 753 |
last_signals_sent[key] = (proba, now)
|