Explorar o código

Refactor reset_data.sh and trading_bot.py for SQLite integration - Updated reset_data.sh to remove legacy JSON files and handle SQLite database cleanup. Modified trading_bot.py to check for SQLite database existence instead of JSON files, enhancing statistics persistence reporting. Adjusted management_commands.py to reflect the new SQLite file structure for trading stats. Improved trading_stats.py to rename order_id to exchange_fill_id and added linked_order_table_id for better data management.

Carles Sentis hai 3 días
pai
achega
938e56a535
Modificáronse 4 ficheiros con 28 adicións e 26 borrados
  1. 14 15
      reset_data.sh
  2. 1 2
      src/commands/management_commands.py
  3. 3 2
      src/trading/trading_stats.py
  4. 10 7
      trading_bot.py

+ 14 - 15
reset_data.sh

@@ -19,26 +19,25 @@ DATA_DIR="data"
 # mkdir -p $DATA_DIR
 
 # Remove trading statistics
-if [ -f "$DATA_DIR/trading_stats.json" ]; then
-    rm "$DATA_DIR/trading_stats.json"
-    echo "✅ Removed $DATA_DIR/trading_stats.json"
+if [ -f "$DATA_DIR/trading_stats.sqlite" ]; then
+    rm "$DATA_DIR/trading_stats.sqlite"
+    echo "✅ Removed $DATA_DIR/trading_stats.sqlite"
 fi
 
-# Remove backup stats (if any were made at root or in data)
+# Remove legacy files (if they still exist)
+rm -f "$DATA_DIR/trading_stats.json"
 rm -f trading_stats.json.backup* # Old root backups
 rm -f "$DATA_DIR/trading_stats.json.backup*"
-echo "✅ Removed stats backups (if any)"
+echo "✅ Removed legacy stats files (if any)"
 
-# Remove trading engine state persistence file
-if [ -f "$DATA_DIR/trading_engine_state.json" ]; then
-    rm "$DATA_DIR/trading_engine_state.json"
-    echo "✅ Removed $DATA_DIR/trading_engine_state.json"
-fi
-# Remove old bot_state.json if it exists at root from previous versions
-if [ -f "bot_state.json" ]; then
-    rm bot_state.json
-    echo "✅ Removed legacy bot_state.json from root"
-fi
+# Remove market monitor state (legacy - now in SQLite)
+rm -f "$DATA_DIR/market_monitor_state.json"
+echo "✅ Removed legacy market monitor state (if any)"
+
+# Remove legacy bot state files (no longer used - all state now in SQLite)
+rm -f "$DATA_DIR/trading_engine_state.json"
+rm -f "bot_state.json"
+echo "✅ Removed legacy bot state files (if any)"
 
 # Remove alarm data
 if [ -f "$DATA_DIR/price_alarms.json" ]; then

+ 1 - 2
src/commands/management_commands.py

@@ -355,9 +355,8 @@ Will trigger when {token} price moves {alarm['direction']} ${target_price:,.2f}
 • Running: {'✅ Yes' if self.market_monitor.is_running else '❌ No'}
 
 📁 <b>State Files:</b>
-• Trading Engine State: {'✅ Exists' if os.path.exists('data/trading_engine_state.json') else '❌ Missing'}
 • Price Alarms: {'✅ Exists' if os.path.exists('data/price_alarms.json') else '❌ Missing'}
-• Trading Stats: {'✅ Exists' if os.path.exists('data/trading_stats.json') else '❌ Missing'}
+• Trading Stats: {'✅ Exists' if os.path.exists('data/trading_stats.sqlite') else '❌ Missing'}
 
 🔔 <b>Alarm Manager:</b>
 • Active Alarms: {self.alarm_manager.get_statistics()['total_active']}

+ 3 - 2
src/trading/trading_stats.py

@@ -71,7 +71,7 @@ class TradingStats:
             """
             CREATE TABLE IF NOT EXISTS trades (
                 id INTEGER PRIMARY KEY AUTOINCREMENT,
-                order_id TEXT UNIQUE,
+                exchange_fill_id TEXT UNIQUE,
                 timestamp TEXT NOT NULL,
                 symbol TEXT NOT NULL,
                 side TEXT NOT NULL,
@@ -79,7 +79,8 @@ class TradingStats:
                 price REAL NOT NULL,
                 value REAL NOT NULL,
                 trade_type TEXT NOT NULL,
-                pnl REAL DEFAULT 0.0 
+                pnl REAL DEFAULT 0.0,
+                linked_order_table_id INTEGER
             )
             """,
             # pnl on trades table is for individual realized pnl if a trade closes a part of a position.

+ 10 - 7
trading_bot.py

@@ -123,20 +123,23 @@ class BotManager:
     
     def check_stats_persistence(self):
         """Check and report on statistics persistence."""
-        stats_file = "data/trading_stats.json"
-        if os.path.exists(stats_file):
+        # Check if we have persistent statistics
+        self.logger.info("📊 Checking statistics persistence...")
+        sqlite_file = "data/trading_stats.sqlite"
+        if os.path.exists(sqlite_file):
             try:
+                # Quick check to see if database has data
                 stats = TradingStats()
                 basic_stats = stats.get_basic_stats()
-                total_trades = basic_stats.get('total_trades_count', basic_stats.get('total_trades', 0))
-                start_date = basic_stats.get('start_date_formatted', basic_stats.get('start_date', 'unknown'))
-                self.logger.info(f"📊 Existing stats found - {total_trades} trades since {start_date}")
+                total_trades = basic_stats.get('total_trades', 0)
+                start_date = basic_stats.get('start_date', 'unknown')
+                self.logger.info(f"📊 Existing SQLite database found - {total_trades} trades since {start_date}")
                 return True
             except Exception as e:
-                self.logger.warning(f"⚠️ Stats file {stats_file} exists but couldn't load: {e}")
+                self.logger.warning(f"⚠️ SQLite database {sqlite_file} exists but couldn't load: {e}")
                 return False
         else:
-            self.logger.info(f"📊 No existing stats file at {stats_file} - will create new tracking from launch")
+            self.logger.info(f"📊 No existing SQLite database at {sqlite_file} - will create new tracking from launch")
             return False
     
     async def run_bot(self):