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 && !β¦