LIVE·WAITING…
connecting to the live quote stream…
--:--:--UTC
#46
Drawdown-aware kill switch
mediumTradingacceptance —~20 min
Implement kill_switch(pnl, limit) — the daily loss limit as a pure function. pnl is the sequence of per-trade profits through the day, limit a positive number. Walk the cumulative profit; the moment it is at or below -limit, trading halts — and it STAYS halted for the rest of the day, even if later fills bring the total back up. Return a list of 0/1 flags, one per trade, 1 meaning halted after that trade.
This is the kill switch in this site's safety settings, reduced to twelve lines. Re-arming on a bounce is how a bad day becomes a terrible one.
example 1
input
pnl=[-100.0, -150.0, -300.0, -50.0], limit=500.0
output
[0, 0, 1, 1]
example 2
input
pnl=[-300.0, -250.0, 400.0, 100.0], limit=500.0
output
[0, 1, 1, 1]
note
The limit is hit on trade 2; the 400 win on trade 3 does not un-halt the day.
constraints
- 1 ≤ len(pnl) ≤ 500
- limit > 0
teacher · 2 hints · the first is free
↗ read the theory
the chapter this problem is cut from
●solution.py· utf-8 · spaces:4
ln 1, col 1tab indent · esc leave editor
python 3.13 · pyodide
+ 6 hidden on submit
input · keyword arguments to kill_switch()
pnl=[-100.0, -150.0, -300.0, -50.0], limit=500.0
expected
[0, 0, 1, 1]
the judge calls kill_switch() with exactly these inputs · outputs are compared to 6 decimal places
● standing bypython 3.13 in your browser · pyodide 0.28.3 · judged on the server to 6 dp⌘↵ run · ⌘⇧↵ submit
paper only · educational · not advice