Menu

The Bug That Cost Me Three Weeks: Why Your SL/TP Logic Is Probably Wrong
πŸ“°

The Bug That Cost Me Three Weeks: Why Your SL/TP Logic Is Probably Wrong

DEV CommunityΒ·Vuk StanicΒ·6 months ago
#GSbJ2BH9
#engineering#failure#algorithmictrading#position#price
Reading 0:00
15s threshold

This is the story of a production bug I fixed, turned into a book. It's also why most algorithmic traders fail.

Every algorithmic trader thinks they understand stop-loss and take-profit (SL/TP). Most are wrong. Not subtly wrong β€” catastrophically wrong in ways that don't show up in backtesting but destroy live systems.

This is the opening chapter of my second book , and it's the reason I wrote the whole series.

The Naive Implementation

Here's what the original code looked like in our production system:

struct Position { entry_price : Decimal , sl_price : Decimal , tp_price : Decimal , sl_triggered : bool , tp_triggered : bool , }

fn check_sl_tp ( position : & mut Position , current_price : Decimal ) { if current_price <= position .sl_price && ! position .sl_triggered { position .sl_triggered = true ; close_position ( position ); }

if current_price >= position .tp_price && !…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.