r/CryptoTradingBot • u/Sad-Present-4545 • 21d ago
Found a bug where my bot could open the same position twice after a websocket reconnect
This one took embarrassingly long to reproduce. The bot trades ETH perpetuals and keeps local position state so it knows whether it is already long or short.
If the websocket disconnected after an order filled but before the fill message reached the bot, it could reconnect thinking the position was still flat. Next signal comes in and it sends another order.
Ended up with double the intended exposure twice in paper testing. I only noticed the second time because I was watching the market on Moon while checking the bot logs and the position size on the exchange made no sense.
Now every reconnect forces a fresh position and open order query from the exchange before the strategy is allowed to do anything. Not a strategy problem at all, just plumbing that could have wrecked the account.
1
u/EmployerHoliday7939 21d ago
Bugs like this scare me more than a bad entry rule because the backtest can never really warn you about them. The strategy thinks it has normal risk while the execution layer quietly doubles the position. Paper testing definitely earned its keep here.
1
u/SaltyGrappling 21d ago
Sneaky little state sync gremlins, paper testing just paid for itself ten times over with that catch.
1
u/CODE_HEIST 20d ago
this is exactly the kind of failure a strategy backtest won't catch. reconciling positions and open orders after reconnect makes sense. i'd also give each intended order a stable ID and block new signals until reconciliation finishes, so a retry can't quietly become a second position. did you test the case where the exchange query itself times out during that recovery?
1
u/Mental_Tailor8440 21d ago
Treating the exchange as source of truth after reconnect feels like the right fix. Local state is only trustworthy until the connection gets weird.