Debugging a 200x Leverage Bot When the Numbers Do Not Add Up
How I spent 3 days debugging PnL discrepancies in a MEXC futures bot. Real money, real trades, and a single wrong assumption that broke everything.
· DEV
I fired up the bot with $50 on PEPE_USDT futures, 200x leverage. The strategy was simple: catch micro-moves, collect the spread, get out fast. Backtests looked great. First hour looked fine. Then I checked the exchange balance.
What Was the Problem?
The bot reported a small profit. MEXC showed a loss. Not a big one — a few dollars — but the direction was wrong. That should not happen with a deterministic PnL formula. I checked the code. I checked it again. Everything looked correct.
The issue was subtle. The bot calculated drawdown from a hardcoded `dry_run_balance` of 100.0 USDT. My real balance was 50.0 USDT. So when the bot hit a 25% drawdown, it thought I still had 75 USDT. In reality I had 37.50 USDT. The numbers were off by half.
What Did the Debug Loop Look Like?
What followed was three days of tight iterations. Change a parameter. Watch it trade. Compare bot logs against exchange history. Rinse and repeat. I stopped counting after 68 edits to bot.py.
# Before: drawdown calculated from wrong baseline
self._peak_balance = 100.0 # hardcoded
# After: reset peak to actual balance on start
self._peak_balance = stats.balance # real
def reset_peak_balance(self) -> None:
"""Reset peak to current balance after exchange sync."""
self._peak_balance = self._stats.balance
That one-line fix — `self._peak_balance = self._stats.balance` — took two days to find. The bug was not in the math. It was in the assumption that the default balance was the real balance.
What Actually Fixed It
- Reset peak_balance to actual exchange balance on startup, not the dry_run default
- Removed deal_flow_threshold filter that was blocking valid entry signals
- Lowered min_deal_flow_count from 3 to 2 to catch more micro-moves
- Dropped entry_confirm_delay from 0.1s to 0.0s — speed matters at 200x
- Added real-time PnL audit logging that prints every close fill
# Real-time audit log for every close
print(f"[WS_CLOSE] side={side} dealAvg={deal_avg_price} "
f"profit={realized} fee={fee} net_pnl={net_pnl}")
The audit log was the real breakthrough. Before that, the bot silently swallowed mismatches. After adding the print, I watched the numbers diverge in real time. The bot thought a trade was +$0.80. The exchange recorded -$0.15. The gap was the fee.
What Were the Numbers?
- 68 edits to bot.py across 3 sessions
- 11 config parameters changed, 3 removed entirely
- 200x leverage = 0.5% move is a 100% balance swing
- 1 wrong assumption about initial balance broke the whole risk model
- Fix: 1 line of code + 6 lines of logging