75 lines
2.0 KiB
Python
75 lines
2.0 KiB
Python
#!/usr/bin/env python3
|
|||
|
|
"""
|
||
|
|
🧹 Clean Invalid Exit Data
|
||
|
|
Bereinigt Trades mit ungültigen Exit-Daten (Exit vor Entry)
|
||
|
|
"""
|
||
|
|
|
||
|
|
import sqlite3
|
||
|
|
from datetime import datetime
|
||
|
|
|
||
|
|
DB_PATH = "trading_bot.db"
|
||
|
|
|
||
|
|
print("=" * 70)
|
||
|
|
print("🧹 CLEANING INVALID EXIT DATA")
|
||
|
|
print("=" * 70)
|
||
|
|
|
||
|
|
conn = sqlite3.connect(DB_PATH)
|
||
|
|
conn.row_factory = sqlite3.Row
|
||
|
|
cursor = conn.cursor()
|
||
|
|
|
||
|
|
# Find trades with exit_time before entry_time
|
||
|
|
cursor.execute("""
|
||
|
|
SELECT ticket, entry_time, exit_time, net_profit, status
|
||
|
|
FROM trades
|
||
|
|
WHERE status = 'closed'
|
||
|
|
AND exit_time IS NOT NULL
|
||
|
|
AND exit_time < entry_time
|
||
|
|
""")
|
||
|
|
|
||
|
|
invalid_trades = cursor.fetchall()
|
||
|
|
|
||
|
|
print(f"\n Found {len(invalid_trades)} trades with invalid exit data:")
|
||
|
|
print(" (Exit time is BEFORE entry time)")
|
||
|
|
|
||
|
|
if invalid_trades:
|
||
|
|
print("\n Ticket | Entry Time | Exit Time | Status")
|
||
|
|
print(" " + "-" * 65)
|
||
|
|
for trade in invalid_trades:
|
||
|
|
print(f" {trade['ticket']:<12} | {trade['entry_time']:<19} | {trade['exit_time']:<19} | {trade['status']}")
|
||
|
|
|
||
|
|
response = input("\n Reset these trades to 'open' status? (yes/no): ")
|
||
|
|
|
||
|
|
if response.lower() in ['yes', 'y']:
|
||
|
|
# Reset to open
|
||
|
|
cursor.execute("""
|
||
|
|
UPDATE trades
|
||
|
|
SET
|
||
|
|
status = 'open',
|
||
|
|
exit_time = NULL,
|
||
|
|
exit_price = NULL,
|
||
|
|
profit = NULL,
|
||
|
|
commission = NULL,
|
||
|
|
swap = NULL,
|
||
|
|
net_profit = NULL,
|
||
|
|
exit_reason = NULL,
|
||
|
|
duration_hours = NULL,
|
||
|
|
rr_ratio = NULL
|
||
|
|
WHERE status = 'closed'
|
||
|
|
AND exit_time IS NOT NULL
|
||
|
|
AND exit_time < entry_time
|
||
|
|
""")
|
||
|
|
|
||
|
|
conn.commit()
|
||
|
|
print(f"\n ✅ Reset {len(invalid_trades)} trades to 'open' status")
|
||
|
|
print(" Position Monitor will properly update them when they close")
|
||
|
|
else:
|
||
|
|
print("\n Cancelled - no changes made")
|
||
|
|
else:
|
||
|
|
print("\n ✅ No invalid trades found")
|
||
|
|
|
||
|
|
conn.close()
|
||
|
|
|
||
|
|
print("\n" + "=" * 70)
|
||
|
|
print("✅ Cleanup Complete!")
|
||
|
|
print("=" * 70)
|