How do I debug unexpected trade exits caused by the decision engine?

  • Home
  • Documentation
  • How do I debug unexpected trade exits caused by the decision engine?

MagicTradeBot provides detailed tracking of why trades are closed, which makes debugging unexpected exits straightforward.


Steps to Debug

  1. Check the Close Trade Reason

    • Every trade closed by the decision engine is tagged with a reason in the database.
    • Common reasons include:

      • Threshold / Stop-Loss Rule Triggered
      • Take Profit / Smart TP Executed
      • Maximum Hold Time Exceeded
      • Emergency Close Triggered
      • MaxLossReached or MaxProfitReached Reached
  2. Locate the Database

    • MagicTradeBot uses SQLite to store trade states.
    • Database path:

      /data/trades.db
      
    • This file contains all running and closed trade records, including timestamps, quantities, and exit reasons.
  3. Open SQLite Database

    • You can use any SQLite viewer or IDE:

      • DB Browser for SQLite
      • DBeaver
      • Visual Studio Code + SQLite Extension
    • Look for the closed_trades table (or similar) to inspect the following fields:

      • Trade ID
      • Symbol
      • Entry Price / Quantity
      • Exit Price / Quantity
      • Close Reason
      • Timestamp
  4. Analyze the Close Reason

    • Compare the reason with your configured rules in decision_engine.yaml, market_data.yaml, or risk_limits.yaml.
    • Example: If Close Reason = "EmergencyClose", check your AverageMovementEmergencyClose threshold or EmergencyCloseMinHoldTime.
  5. Adjust Rules If Needed

    • If a trade exited unexpectedly, it’s often due to:

      • Misconfigured stop-loss / threshold percentages
      • Maximum hold time exceeded
      • Emergency close conditions triggered during volatile markets
    • Update the configuration and test in demo mode before applying changes to live trading.

Pro Tip: Keep a debug log enabled in the bot (logs/ folder). It often logs rule evaluations per trade every execution interval, helping you trace why a trade was closed step by step.

This structured approach ensures you can trace, understand, and safely adjust decision engine behavior without guessing.

📎 Related Topics