Nexo-S commited on
Commit
bbf43f8
·
verified ·
1 Parent(s): 054ab3a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -44
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
- # --- 🛡️ DÉTECTION DE STRUCTURE ---
 
 
92
  if os.path.exists(DB_NAME):
93
- with sqlite3.connect(DB_NAME) as conn:
 
94
  cursor = conn.execute("PRAGMA table_info(signals)")
95
  columns = [col[1] for col in cursor.fetchall()]
96
- # Si 'date' ou 'timeframe' manquent, on reset tout
 
97
  if 'date' not in columns or 'timeframe' not in columns:
98
- print("⚠️ Structure obsolète détectée. Reset de la base de données...")
99
- conn.close()
 
 
 
 
 
 
 
 
100
  os.remove(DB_NAME)
101
-
102
- # --- 🏗️ CRÉATION PROPRE ---
103
- with sqlite3.connect(DB_NAME) as conn:
104
- # Table des Signaux (Version V21 Complète)
105
- conn.execute('''CREATE TABLE IF NOT EXISTS signals (
106
- id INTEGER PRIMARY KEY AUTOINCREMENT,
107
- date TEXT,
108
- symbol TEXT,
109
- timeframe TEXT,
110
- direction TEXT,
111
- prob REAL,
112
- price REAL,
113
- tp REAL,
114
- sl REAL,
115
- status TEXT,
116
- regime INTEGER,
117
- prob_time REAL,
118
- prob_ml REAL,
119
- prob_lstm REAL,
120
- prob_sent REAL)''')
121
-
122
- # Table de Renforcement
123
- conn.execute('''CREATE TABLE IF NOT EXISTS agent_logic (
124
- timeframe TEXT PRIMARY KEY,
125
- tp_mult REAL,
126
- sl_mult REAL,
127
- score REAL,
128
- last_pnl REAL)''')
129
-
130
- # Init agent_logic si vide
131
- cursor = conn.cursor()
132
- cursor.execute("SELECT COUNT(*) FROM agent_logic")
133
- if cursor.fetchone()[0] == 0:
134
- defaults = [('5m', 1.5, 1.0, 0, 0), ('15m', 2.0, 1.2, 0, 0),
135
- ('1h', 3.0, 1.5, 0, 0), ('4h', 4.5, 2.0, 0, 0), ('1d', 6.0, 2.5, 0, 0)]
136
- conn.executemany("INSERT INTO agent_logic VALUES (?, ?, ?, ?, ?)", defaults)
137
- print("✅ Mémoire RL initialisée.")
138
-
139
- print("✅ Base de données V21 Synchronisée.")
 
 
 
 
 
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: