Files
quantumbotx/testing/fix_bot_state.py
T
Reynov Christian bf94b22825 🚀 REVOLUTIONARY FEATURE: Indonesian AI Trading Mentor System
 CORE AI MENTOR SYSTEM:
  - Complete Indonesian language AI trading mentor
  - Real-time trading psychology analysis with cultural context
  - Emotional intelligence for Indonesian trading behavior
  - Personal feedback with Islamic context ('Alhamdulillah profit!')
  - Jakarta timezone optimization and BI rate awareness

 DATABASE INTEGRATION:
  - New trading_sessions, ai_mentor_reports, daily_trading_data tables
  - Real-time capture of trading data for AI analysis
  - Historical performance tracking and emotional state logging
  - Seamless integration with existing bot architecture

 WEB INTERFACE:
  - Beautiful Indonesian AI mentor dashboard
  - Interactive emotion selection with cultural sensitivity
  - Real-time feedback generation and instant AI consultation
  - Daily report generation with comprehensive analysis
  - Quick feedback modal for emotional check-ins

 TRADING BOT INTEGRATION:
  - Automatic trade logging for AI mentor analysis
  - Risk management scoring (1-10 scale)
  - Strategy performance correlation with emotional states
  - Stop loss and take profit usage tracking

 REVOLUTIONARY FEATURES:
  - First-ever Indonesian AI trading mentor in the world
  - Combines trading psychology with Islamic values
  - Market-specific guidance for Indonesian traders
  - Progressive learning path from beginner to expert
  - Cultural trading wisdom (Jakarta hours, Ramadan considerations)

IMPACT: This transforms QuantumBotX into the world's first culturally-aware
AI trading mentor specifically designed for Indonesian retail traders.

Indonesian beginners now have personal AI guidance in their native language
with full understanding of local market conditions and cultural context.
2025-08-26 09:02:03 +08:00

141 lines
5.0 KiB
Python

#!/usr/bin/env python3
"""
🔧 Fix Bot State Synchronization
Fixes the active_bots dictionary to match running bot threads
"""
import sys
import os
import threading
# Add the project root to the path
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
try:
from core.bots.controller import active_bots, mulai_bot, hentikan_bot
from core.db import queries
from core.bots.trading_bot import TradingBot
def diagnose_bot_state():
"""Diagnose current bot state"""
print("🔍 DIAGNOSING BOT STATE")
print("=" * 30)
# Check database bots
all_bots = queries.get_all_bots()
active_db_bots = [bot for bot in all_bots if bot['status'] == 'Aktif']
print(f"Database active bots: {len(active_db_bots)}")
for bot in active_db_bots:
print(f" - ID: {bot['id']}, Name: {bot['name']}, Market: {bot['market']}")
# Check controller active bots
print(f"\\nController active_bots: {len(active_bots)}")
for bot_id, bot_instance in active_bots.items():
print(f" - ID: {bot_id}, Alive: {bot_instance.is_alive()}, Status: {bot_instance.status}")
# Check running threads
all_threads = threading.enumerate()
trading_bot_threads = [t for t in all_threads if isinstance(t, TradingBot)]
print(f"\\nRunning TradingBot threads: {len(trading_bot_threads)}")
for thread in trading_bot_threads:
print(f" - ID: {thread.id}, Name: {thread.name}, Alive: {thread.is_alive()}")
print(f" Market: {thread.market}, Status: {thread.status}")
return active_db_bots, active_bots, trading_bot_threads
def fix_bot_state():
"""Fix bot state synchronization"""
print("\\n🔧 FIXING BOT STATE")
print("=" * 25)
# Get current state
db_bots, controller_bots, thread_bots = diagnose_bot_state()
# Find bots that are running but not in controller
orphaned_threads = []
for thread in thread_bots:
if thread.id not in controller_bots and thread.is_alive():
orphaned_threads.append(thread)
if orphaned_threads:
print(f"\\n🚨 Found {len(orphaned_threads)} orphaned bot threads:")
for thread in orphaned_threads:
print(f" - Bot {thread.id} ({thread.name}) is running but not in active_bots")
# Add to active_bots
active_bots[thread.id] = thread
print(f" ✅ Added Bot {thread.id} to active_bots")
# Find bots in controller but not alive
dead_bots = []
for bot_id, bot_instance in list(controller_bots.items()):
if not bot_instance.is_alive():
dead_bots.append(bot_id)
if dead_bots:
print(f"\\n💀 Found {len(dead_bots)} dead bots in controller:")
for bot_id in dead_bots:
print(f" - Bot {bot_id} is in active_bots but thread is dead")
del active_bots[bot_id]
queries.update_bot_status(bot_id, 'Dijeda')
print(f" ✅ Removed Bot {bot_id} from active_bots and set status to 'Dijeda'")
return len(orphaned_threads), len(dead_bots)
def test_analysis_after_fix():
"""Test analysis API after fix"""
print("\\n🧪 TESTING ANALYSIS AFTER FIX")
print("=" * 35)
from core.bots.controller import get_bot_analysis_data
bot_id = 3
analysis_data = get_bot_analysis_data(bot_id)
if analysis_data:
print(f"✅ Bot {bot_id} analysis data:")
print(f" Signal: {analysis_data.get('signal', 'N/A')}")
print(f" Price: {analysis_data.get('price', 'N/A')}")
print(f" Explanation: {analysis_data.get('explanation', 'N/A')}")
else:
print(f"❌ Bot {bot_id} analysis data is None")
def main():
print("🔧 Bot State Synchronization Fix")
print("=" * 40)
# Diagnose
diagnose_bot_state()
# Fix
orphaned, dead = fix_bot_state()
# Test
test_analysis_after_fix()
# Summary
print("\\n" + "=" * 40)
print("🎯 FIX SUMMARY")
print("=" * 40)
print(f"Orphaned threads fixed: {orphaned}")
print(f"Dead bots cleaned: {dead}")
print(f"Current active_bots: {len(active_bots)}")
if orphaned > 0:
print("\\n✅ SUCCESS: Bot state synchronized!")
print("💡 The 'Analisis Real-Time' should now work in the dashboard")
else:
print("\\n⚠️ No orphaned threads found")
print("💡 If issue persists, restart the QuantumBotX application")
if __name__ == "__main__":
main()
except ImportError as e:
print(f"❌ Import error: {e}")
except Exception as e:
print(f"❌ Error: {e}")
import traceback
traceback.print_exc()