MagicTradeBot provides detailed tracking of why trades are closed, which makes debugging unexpected exits straightforward.
Steps to Debug
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
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.
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_tradestable (or similar) to inspect the following fields:- Trade ID
- Symbol
- Entry Price / Quantity
- Exit Price / Quantity
- Close Reason
- Timestamp
Analyze the Close Reason
- Compare the reason with your configured rules in
decision_engine.yaml,market_data.yaml, orrisk_limits.yaml. - Example: If
Close Reason = "EmergencyClose", check yourAverageMovementEmergencyClosethreshold orEmergencyCloseMinHoldTime.
- Compare the reason with your configured rules in
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.