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

# 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?


Pro Tip: Always log the raw exchange response before your bot processes it. If the exchange says the fee is 0.02 USDT and your bot calculates 0.015, you want to know that immediately — not after 50 trades.
Pitfall: Hardcoding defaults that look "reasonable" is a trap. 100.0 USDT is a fine default for paper trading. For live trading with 50.0 USDT, it is a disaster. Always sync state from the exchange on startup.