Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -88,55 +88,72 @@ DB_NAME = "alphatrade_v9.db"
|
|
| 88 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 89 |
|
| 90 |
def init_db():
|
| 91 |
-
|
|
|
|
|
|
|
| 92 |
if os.path.exists(DB_NAME):
|
| 93 |
-
|
|
|
|
| 94 |
cursor = conn.execute("PRAGMA table_info(signals)")
|
| 95 |
columns = [col[1] for col in cursor.fetchall()]
|
| 96 |
-
#
|
|
|
|
| 97 |
if 'date' not in columns or 'timeframe' not in columns:
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
os.remove(DB_NAME)
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
async def save_to_db(data):
|
| 142 |
try:
|
|
|
|
| 88 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 89 |
|
| 90 |
def init_db():
|
| 91 |
+
reset_needed = False
|
| 92 |
+
|
| 93 |
+
# 1. Vérification (On ouvre et on ferme proprement)
|
| 94 |
if os.path.exists(DB_NAME):
|
| 95 |
+
try:
|
| 96 |
+
conn = sqlite3.connect(DB_NAME)
|
| 97 |
cursor = conn.execute("PRAGMA table_info(signals)")
|
| 98 |
columns = [col[1] for col in cursor.fetchall()]
|
| 99 |
+
conn.close() # On ferme avant de tester la condition
|
| 100 |
+
|
| 101 |
if 'date' not in columns or 'timeframe' not in columns:
|
| 102 |
+
reset_needed = True
|
| 103 |
+
except Exception as e:
|
| 104 |
+
print(f"⚠️ Erreur check DB: {e}")
|
| 105 |
+
reset_needed = True
|
| 106 |
+
|
| 107 |
+
# 2. Reset si nécessaire
|
| 108 |
+
if reset_needed:
|
| 109 |
+
print("⚠️ Structure obsolète détectée. Reset de la base de données...")
|
| 110 |
+
try:
|
| 111 |
+
if os.path.exists(DB_NAME):
|
| 112 |
os.remove(DB_NAME)
|
| 113 |
+
print("✅ Ancienne base supprimée.")
|
| 114 |
+
except Exception as e:
|
| 115 |
+
print(f"❌ Impossible de supprimer la DB: {e}")
|
| 116 |
+
|
| 117 |
+
# 3. Création de la nouvelle structure
|
| 118 |
+
try:
|
| 119 |
+
with sqlite3.connect(DB_NAME) as conn:
|
| 120 |
+
# Table des Signaux V21
|
| 121 |
+
conn.execute('''CREATE TABLE IF NOT EXISTS signals (
|
| 122 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 123 |
+
date TEXT,
|
| 124 |
+
symbol TEXT,
|
| 125 |
+
timeframe TEXT,
|
| 126 |
+
direction TEXT,
|
| 127 |
+
prob REAL,
|
| 128 |
+
price REAL,
|
| 129 |
+
tp REAL,
|
| 130 |
+
sl REAL,
|
| 131 |
+
status TEXT,
|
| 132 |
+
regime INTEGER,
|
| 133 |
+
prob_time REAL,
|
| 134 |
+
prob_ml REAL,
|
| 135 |
+
prob_lstm REAL,
|
| 136 |
+
prob_sent REAL)''')
|
| 137 |
+
|
| 138 |
+
# Table de Renforcement
|
| 139 |
+
conn.execute('''CREATE TABLE IF NOT EXISTS agent_logic (
|
| 140 |
+
timeframe TEXT PRIMARY KEY,
|
| 141 |
+
tp_mult REAL,
|
| 142 |
+
sl_mult REAL,
|
| 143 |
+
score REAL,
|
| 144 |
+
last_pnl REAL)''')
|
| 145 |
+
|
| 146 |
+
# Initialisation des multiplicateurs
|
| 147 |
+
cursor = conn.cursor()
|
| 148 |
+
cursor.execute("SELECT COUNT(*) FROM agent_logic")
|
| 149 |
+
if cursor.fetchone()[0] == 0:
|
| 150 |
+
defaults = [('5m', 1.5, 1.0, 0, 0), ('15m', 2.0, 1.2, 0, 0),
|
| 151 |
+
('1h', 3.0, 1.5, 0, 0), ('4h', 4.5, 2.0, 0, 0), ('1d', 6.0, 2.5, 0, 0)]
|
| 152 |
+
conn.executemany("INSERT INTO agent_logic VALUES (?, ?, ?, ?, ?)", defaults)
|
| 153 |
+
print("✅ Mémoire RL initialisée.")
|
| 154 |
+
print("✅ Base de données V21 Synchronisée.")
|
| 155 |
+
except Exception as e:
|
| 156 |
+
print(f"❌ Erreur critique création DB: {e}")
|
| 157 |
|
| 158 |
async def save_to_db(data):
|
| 159 |
try:
|