LIVE
connecting to the live quote stream…
--:--:--UTC
mediumTrading○
#44

Long-only backtest with costs

mediumTradingacceptance —~30 min

Implement backtest(closes, signals, cost_bps) — a long-only backtest with transaction costs. Start with equity 1.0. On each bar t >= 1, if signals[t - 1] == 1 the strategy was long, so multiply equity by closes[t] / closes[t - 1]. Whenever the position changes between signals[t - 1] and signals[t] (in or out), charge cost_bps / 10_000 of equity. Return the final equity.

The same signals that look brilliant at zero cost usually die at ten basis points — which is why the /learn backtest chapter has a cost slider. Return the equity rounded to 4 decimal places.

example 1
input
closes=[100.0, 102.0, 101.0, 105.0, 104.0], signals=[1, 1, 0, 1, 0], cost_bps=0.0
output
1.0004
example 2
input
closes=[100.0, 102.0, 101.0, 105.0, 104.0], signals=[1, 1, 0, 1, 0], cost_bps=10.0
output
0.9974
constraints
  • 2 ≤ len(closes) == len(signals) ≤ 10_000
  • signals ∈ {0, 1}
  • 0 ≤ cost_bps ≤ 100
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 backtest()
closes=[100.0, 102.0, 101.0, 105.0, 104.0], signals=[1, 1, 0, 1, 0], cost_bps=0.0
expected
1.0004

the judge calls backtest() 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