A restart silently disabled our trailing stop — and the crash fix is what hid it
Описание и применение
При перезапуске торговый бот не передавал расстояние до стоп‑лосса, из‑за чего трейлинг‑стопы никогда не активировались, а правило «лестницы» ошибочно заканчивало позиции. Исправление включило поиск исходного расстояния в журнале сделок и добавление счётчиков срабатываний.
Задача
When the bot restarts, it rebuilds positions without the stop‑loss distance, causing trailing stops never to arm and ladder rules to incorrectly cut positions.
Результат
The bot now correctly arms trailing stops after 2R profit and no longer cuts recovered positions as dead weight; the silent failure is eliminated.
Рабочий процесс
- 1. Bot restarts and runs recovery code.
- 2. Recovery code builds position objects from broker data, missing sl_dist field.
- 3. Default sl_dist becomes 0, leading to divide‑by‑zero guard skipping trailing stop and ladder rule.
- 4. Guard silently suppresses errors, no log output.
- 5. Issue discovered when analyzing trade performance and noticing missing 2R gains.
- 6. Fix implemented: lookup sl_dist from ledger and pass it during recovery.
- 7. Additional safeguards added: safe defaults, hit counters, and audit logs.
Доказательства
When our bot restarts, it takes back ownership of positions that are already open. To do that it rebuilds its internal picture of each position — and it forgot to carry over one number: the distance to the stop‑loss.
The guard is the interesting bit. Someone — correctly — noticed that dividing by zero would crash the bot, so they wrote: if the distance is zero or less, skip the trailing stop.
On restart the bot now looks the original stop distance up in its own trade ledger by ticket number and passes it in, along with the position size.
- Дата записи: 2026-10-09T22:55:50.315Z