9 · Was the battery well traded? A backtesting framework¶
Intermediate Production Case B Backtesting MPC
In this chapter
- Build a backtest that cannot cheat: policies see only what was known at the
time (
information_policy="historical_only"), and asking for the future raises an error - Benchmark against perfect foresight, fairly, by valuing the energy left in the battery at the end, and read the capture ratio
- Compare policies: greedy thresholds, rolling-horizon model predictive control (MPC) as a schedule or as a bid, and ten price bands set the day before
- Model network constraints that leave the battery constrained on to charge at $10,000/MWh, and the constraint management that avoids it
- Attribute the gap to its causes with counterfactuals, and see why the answer depends on the order unless you use Shapley values
- Run the same battery on a month of real AEMO prices in every region, and see what changes when the margin is thin
Chapter 2 optimised a battery with perfect knowledge of prices. Real batteries are traded through bids submitted before prices are known, behind network constraints, by software that re-plans every few minutes. The owner's question is not "what is optimal?" but "how well did we do, and where did the rest go?" A backtest answers it, if it is honest about what was known when. This chapter builds one and uses it. It is Milestone 7 of the book: the common backtesting framework later chapters reuse.
1 · The real-world problem¶
A battery operator's monthly report (the pattern here is drawn from real ones) lists revenue by market, cycles and throughput margin. Then come the questions:
- The battery was constrained on to charge at about $10,500/MWh on the same day it captured a $14,000 spike. How much did that cost, and how do we stop it?
- It was empty before the evening peak several times. Bad forecasting, bad bidding, or bad luck?
- One price band sat in the mid-$80s while the market cleared at $50–60. How much did stale bands cost?
- Is a 60 % capture ratio good?
2 · The physical system¶
The Case B battery: 10 MW / 20 MWh, 3–97 % SOC window, ≈ 88 % round-trip efficiency, and a wear cost of $40/MWh discharged (Chapter 2; Chapter 10 revisits it). It trades energy and ten FCAS markets, including the very fast 1-second services introduced in October 2023.
The market is a synthetic month, calibrated to a confidential source: a NEM battery operator's reports. It has 5-minute prices with a solar trough, morning and evening peaks and spikes of up to $15,000/MWh. LOWER1SEC is valuable through the day and collapses in the evening peak. Network constraint events cluster at 17:00–19:00. During an event the battery's local price falls below −$1,000: it cannot be dispatched to generate, and if it offers load it is constrained on, charging at the regional price whatever that is.
3 · The decision being judged¶
A trading policy: every 5 minutes, the offers the battery submits. These are a price–volume staircase for generation, another for load (as in Chapter 8), and FCAS enablement. The market then decides what is dispatched.
4 · The framework: forecast, decide, settle¶
The framework keeps the book's three-way separation explicit in code:
| Piece | Code | Rule |
|---|---|---|
| What could be known | MarketView(market, t, information) |
Past outcomes; forecasts issued at t; constraint warnings |
| What was decided | Policy.decide(view, soc) → Instruction |
Offers and FCAS for interval t |
| What happened | run_backtest(policy, market, spec, start, end) |
Dispatch at actual prices and constraints, physics, settlement |
Under historical_only, the view hands a policy past prices and forecasts issued at
decision time. These are a pre-dispatch-like price forecast whose error grows with
lead time and which sees only half the spikes, and only shortly before. It also gets
a constraint warning that catches three events in four. Asking for a future outcome
raises LookAheadError. Only a benchmark labelled perfect_foresight may read the
future, and its results carry the label.
Look-ahead bias is the backtest's original sin
Using today's price to decide today's bid, re-fitting a model on data that
includes the test period, or setting bands from a month that includes the day
being traded: each makes a strategy look better than it can be. The framework
makes the honest path the easy one. daily_bands uses only the days before, and
a test changes the future and checks that nothing a historical policy sees
changes with it.
Settlement. Energy is settled at the regional price and FCAS at the enablement price. Dispatch follows the offers, local constraints override them, and the state of charge must stay inside the window. This is a simplified dispatch, not NEMDE.
5 · The benchmark: perfect foresight, fairly¶
The ceiling is one LP over the whole window with the actual prices: Chapter 2's energy + FCAS model, now with ten markets and the constraint events.
Policies end the window with different amounts of energy stored. A policy that sells its inventory on the last day looks better than one that doesn't, without having traded better. So every result is scored on adjusted profit:
with one inventory value \(v\) for everyone (the window's mean price × η). The benchmark LP values its own end-of-window energy at the same \(v\). That makes it a true upper bound on every policy's adjusted profit (a tested property). The capture ratio is \(\Pi^\text{adj}/\Pi^\text{adj}_\text{PF}\).
6 · The policies¶
- Greedy threshold. Charge below $40, discharge above $150, offer all FCAS. This is common, and the source of the "empty before the peak" pattern.
- Rolling MPC. Every hour, re-solve the energy + FCAS LP over the next 24 hours on the forecasts, then act. The plan must end at 50 % SOC, and the controller doesn't plan to discharge, and withdraws its load offer, when a constraint is warned.
- As a schedule: deliver the planned MW at any price.
- As a bid: offer the planned volume at the price the plan's own dual says it is worth. The value of stored energy \(\lambda_t\) gives a discharge threshold \(\lambda_t/\eta_d + k_{deg}\) and a charge threshold \(\lambda_t\eta_c\). The rest of the inverter is offered at a spike price, to catch spikes the forecast missed. The real price then decides.
- Bands. Bid MPC, but each threshold snaps to one of ten band prices set the day before (Chapter 8's rule). Two variants: a stale set chosen once (with a band at $85), and bands re-calibrated daily from quantiles of the past fortnight's prices.
7 · Results¶
Fourteen synthetic days (days 8–21 of the month), the same battery and the same prices throughout:
| Policy | Adjusted profit | Capture | Cycles | Throughput margin | Forced-charge cost |
|---|---|---|---|---|---|
| Perfect foresight | $199,332 | 100 % | 22.1 | $491/MWh | $0 |
| Ideal MPC (perfect forecasts, any price) | $198,671 | 99.7 % | 21.9 | $494/MWh | $0 |
| MPC + forecasts | $181,995 | 91.3 % | 20.9 | $477/MWh | $998 |
| MPC + forecasts + daily bands | $178,146 | 89.4 % | 18.6 | $521/MWh | $998 |
| MPC + forecasts + stale bands | $173,524 | 87.1 % | 17.4 | $540/MWh | $998 |
| MPC + forecasts + stale bands, no constraint management | $169,071 | 84.8 % | 18.4 | $499/MWh | $7,884 |
| Greedy threshold | $119,830 | 60.1 % | 35.0 | $212/MWh | $8,119 |
Planning around the constraints matters even with perfect foresight. The same LP, blind to the constraint events, earns $185,610 (93.1 %). It plans to charge in intervals where it is then forced to, and to discharge where it can't, and pays $7,963 in forced charging. The $998 that even the constraint-managed controllers pay comes from the one event in four that arrives without a warning.
What the table says:
- Re-planning is nearly free; forecasting isn't. A 24-hour rolling horizon with perfect forecasts loses only 0.3 % against hindsight over the whole fortnight. The receding horizon is not the problem. Forecast error costs ≈ 8.4 points.
- Greedy trades twice as much for less. Thirty-five cycles at a $212/MWh margin, against eighteen at about $500. A fixed threshold sells into any price above $150 and has nothing left when the real spikes come. More cycling is not more value.
- Constraint management is worth real money. Withdrawing the load offer when a constraint is warned turns $7.9k of forced charging into about $1k. The warning misses a quarter of events.
- A 60 % capture ratio is not "good". It depends entirely on the benchmark and the window. Against an honest benchmark, a competent controller here captures 85–91 %, and the greedy rule captures 60 %.
FCAS earns about two-thirds of revenue under every policy that offers it, consistent with the real battery behind the calibration.
On real prices: September 2026¶
The synthetic month was built to make the chapter's problems visible. Here is the same
battery (10 MW / 20 MWh, η = 0.94 each way, $40/MWh wear) on real AEMO prices:
the regional energy price and all ten FCAS prices, every 5-minute interval of
September 2026, from the Lab 3 lake. Source: Australian Energy Market Operator
(AEMO), NEMWEB DispatchIS reports. The battery is settled at the regional price with
no local network constraints, because it is not attached to a real connection
point, so forced charging is zero throughout. The policies, the benchmark and the KPIs
are unchanged: bess_market_from_lake returns a market with the same interface as
the synthetic one.
The lake does not yet hold AEMO's own pre-dispatch forecasts, so each MPC runs with two forecasts that bracket what a trading desk would see:
- Emulated pre-dispatch: this chapter's synthetic error model applied to the real outcomes. It is like for like with the synthetic month, but it borrows from the outcome by construction, so it is optimistic.
- History only: the last price, fading into the median daily profile of the previous seven days. Nothing in it looks at the outcome, so it is a floor.
South Australia, 1–30 September 2026. The price averaged $37.71/MWh, was negative in 30 % of intervals and above $300 in only two. The mean daily spread was $209/MWh.
| Policy | Adjusted profit | Capture | Cycles | Throughput margin | FCAS share |
|---|---|---|---|---|---|
| Perfect foresight | $67,935 | 100 % | 36.0 | $135/MWh | 16 % |
| Ideal MPC (perfect forecasts) | $67,437 | 99.3 % | 36.1 | $134/MWh | 16 % |
| MPC + emulated pre-dispatch | $31,023 | 45.7 % | 25.0 | $103/MWh | 18 % |
| MPC + emulated + daily bands | $27,963 | 41.2 % | 21.1 | $107/MWh | 20 % |
| MPC + history-only + daily bands | $25,137 | 37.0 % | 20.0 | $104/MWh | 22 % |
| MPC + history-only forecast | $23,132 | 34.1 % | 21.7 | $94/MWh | 22 % |
| Greedy threshold | −$8,076 | −11.9 % | 56.6 | $33/MWh | 74 % |
What changes on real prices:
- The receding horizon is still not the problem. With perfect forecasts the rolling MPC captures 99.3 %, as on the synthetic month. Almost the whole gap is information.
- Capture halves (91 % → 46 %). The synthetic month earns most of its value from large spikes and FCAS. This September had no scarcity. Its value is a daily spread from the solar trough to the evening, earned at about $135/MWh against a $40/MWh wear cost. With a margin that thin, a forecast error of a few tens of dollars turns a profitable cycle into a losing one. The controllers react by trading less: 25 and 22 cycles, against 36 with hindsight.
- The forecast is worth real money. The gap between the two forecasts is 12 capture points in SA1, about $7,900 in the month. That is the value of a better forecast in dollars, not in MAE.
- Bands cost 4.5 points with the better forecast but gain 3 with the weaker one. With the history-only forecast, snapping thresholds to bands set from the past fortnight's prices seems to act as a guard against a poor forecast. Test band rules against the forecast you actually have.
- The greedy rule loses money. Its $40 and $150 thresholds were set for the synthetic month. In SA1 the average price was below $40, so it charges through most of the day and rarely sells. It discharges 1,132 MWh (57 cycles), and the wear on it ($45k) exceeds all its revenue ($37k). A rule's thresholds are a model too, calibrated to a market that moves.
The same battery in every region:
| Region | Mean price | Negative | Perfect foresight | FCAS share | MPC, emulated | MPC, history only | Greedy |
|---|---|---|---|---|---|---|---|
| NSW1 | $65.32 | 10 % | $42,943 | 22 % | 52.0 % | 49.3 % | −24.7 % |
| QLD1 | $57.10 | 17 % | $50,124 | 23 % | 54.5 % | 50.6 % | −14.5 % |
| SA1 | $37.71 | 30 % | $67,935 | 16 % | 45.7 % | 34.1 % | −11.9 % |
| TAS1 | $38.86 | 21 % | $182,645 | 75 % | 66.1 % | 52.4 % | 20.1 % |
| VIC1 | $36.83 | 24 % | $46,404 | 20 % | 50.9 % | 41.6 % | −22.1 % |
Tasmania stands apart. Its FCAS prices were far above the mainland's in September (RAISE6SEC averaged $6.81/MW/h against $0.05 or less, RAISEREG $8.48 against about $3.14), and FCAS is three-quarters of the ceiling there. It also has the highest capture. One caveat weighs most here: a 10 MW battery would move prices in a market that thin, and the backtest assumes it does not (section 13).
On SA1's best day (10 September) the price went negative from late morning and fell to between −$200 and −$290 in the early afternoon. Hindsight drains the battery overnight, waits for the deepest prices to fill it, and sells into the evening. Both MPCs fill early, at the first negative prices, and so miss the cheapest energy. The greedy rule holds an almost full battery all day.
What these real-price numbers are not
They come from one month and a regional price with no connection point, losses or local constraints. FCAS is enabled at the published price, with no price impact. The dispatch is simplified, not NEMDE. The forecasts are bounds, not AEMO's pre-dispatch. Read the capture ratios as a comparison of policies on the same prices, not as what any real battery earned.
8 · Where did the money go? Counterfactual attribution¶
Start from the ideal controller and switch on one imperfection at a time: forecasts instead of hindsight, stale bands, no constraint management. Three switches make \(2^3 = 8\) backtests:
| Imperfections switched on | Adjusted profit |
|---|---|
| none (ideal controller) | $198,671 |
| no constraint management | $191,609 |
| stale bands | $186,951 |
| forecasts | $181,995 |
| stale bands + no constraint management | $180,967 |
| forecasts + no constraint management | $176,329 |
| forecasts + stale bands | $173,524 |
| all three | $169,071 |
The total loss is $29,600. How much of it is "due to" each cause? The usual answer is a waterfall: add the causes in some order and record each step. But the causes interact. Stale bands cost $11,700 on top of perfect forecasts, but only $8,471 once forecasts are already wrong: the forecast error had already caused some of the same misses. So the waterfall depends on its order.
Shapley values remove the arbitrariness. Each cause gets its marginal loss averaged over every order in which the causes could be added:
| Cause | Waterfall (forecast → bands → constraints) | Shapley |
|---|---|---|
| Forecasts instead of hindsight | −$16,676 | −$14,308 |
| Stale price bands | −$8,471 | −$9,512 |
| No constraint management | −$4,453 | −$5,780 |
| Total | −$29,600 | −$29,600 |
Both add up to the same total, but they split it differently. The waterfall blames whatever comes first, and Shapley shares the interactions fairly. Report Shapley values, and say which counterfactuals defined the game.
The operator's checklist, as code
Each pattern from the operator reports becomes a check on the ledger:
- SOC at each day's peak-price interval (
soc_at_daily_peak) - MWh missed in spike intervals while energy was available (
spike_mwh_missed) - Forced-charge cost
- Revenue per cycle
Each becomes an alert in production.
Why these techniques?¶
Why these techniques? Structure → method¶
| Property of the problem | Here | So |
|---|---|---|
| Question | would a policy have made money using only what was known at the time? | a backtest that cannot look ahead: MarketView raises LookAheadError |
| Information | prices, forecasts and constraint warnings arrive over time | rolling-horizon MPC: re-solve every hour, commit only the first step |
| Per-step model | linear profit, battery energy balance, ten FCAS markets | an LP of about 3,700 variables, solved in well under a second |
| Time coupling | state of charge links every interval | plan over 24 hours, and end the plan at 50 % SOC so it stays feasible |
| Benchmark | no policy can beat hindsight, but end inventory differs between policies | one LP over the window with the actual prices, valuing end energy at the same \(v\) |
| Causes of loss | forecasts, stale bands and constraint management, which interact | \(2^3 = 8\) counterfactual backtests, then Shapley values |
| Cost | 336 LPs per backtest; 8 backtests for attribution | a few minutes in total |
Chosen. - Rolling-horizon MPC matches how the information arrives. Each hour the plan is rebuilt on the newest forecast; only the first step is acted on. The LP's dual \(\lambda_t\) on the energy balance then gives the bid thresholds. - A perfect-foresight LP as the ceiling, with a common inventory value, so adjusted profit compares like with like. - Shapley values share a total that depends on the order in which causes are added. Each cause gets its marginal loss averaged over all orders.
Not chosen. - One full-window optimisation as the policy. It uses future prices. It is the benchmark, and its results carry the label. - Dynamic programming over state of charge. It is exact for one battery, but the action space spans ten markets and gives no duals for bidding; the LP does both. - A stochastic programme at every step. Closer to reality, but scenario trees grow quickly, and the receding horizon already re-plans hourly. Chapter 15 treats the commitment decision where it matters. - A waterfall attribution. It adds causes in one order and blames whichever comes first; here stale bands cost $11,700 alone but $8,471 after forecasts.
What the theory guarantees. - Perfect foresight is an upper bound on every policy's adjusted profit, because every policy's schedule is feasible in that LP. Receding horizon has no general optimality guarantee; the fixed end SOC gives feasibility, and the 0.3 % loss against hindsight is measured, not proved. - Shapley's value is the only attribution that is efficient, symmetric, additive and gives nothing to a cause with no effect, and it sums exactly to the total loss.
References. - Rawlings, Mayne and Diehl (2017), Model Predictive Control: what is re-solved, what is committed, and when it is stable (Chapter 9). - Shapley (1953), A value for n-person games: the axioms behind the attribution (Chapter 9). - Hyndman and Athanasopoulos (2021), Forecasting: Principles and Practice: time-series cross-validation as an honest backtest (Chapter 4). - Choosing a technique for how time and information choose between a single solve and a rolling one.
9 · Implementation¶
from energy_or.backtest import (
PerfectForesight,
RollingMPC,
attribute_losses,
kpis,
run_backtest,
)
from energy_or.bess import BatterySpec
from energy_or.data.bess_market import bess_market
market = bess_market() # SYNTHETIC month
spec = BatterySpec(
energy_mwh=20.0, eta_charge=0.94, eta_discharge=0.94, degradation_cost_per_mwh=40.0
)
start, end = 7 * 288, 21 * 288
v = float(market.rrp_per_mwh[start:end].mean()) * spec.eta_discharge
pf = run_backtest(
PerfectForesight(spec, start, end, inventory_value_per_mwh=v), market, spec, start, end
)
mpc = run_backtest(RollingMPC(spec, resolve_every=12, bands="daily"), market, spec, start, end)
print(kpis(mpc, pf, v).capture_ratio)
attr = attribute_losses(market, spec, start, end, v) # 8 backtests
print(attr.shapley)
10 · Solve: what it costs to run¶
Perfect foresight is one LP with about 52,000 variables, solved in ≈ 2 s by HiGHS. Each MPC backtest is 336 LPs of about 3,700 variables, ≈ 45 s for the fortnight. The attribution's eight backtests take ≈ 6 minutes. A 5-minute production cycle has room for one MPC solve in well under a second.
The real-price run takes about 16 minutes for five regions, 2–5 minutes each, for the benchmark and 30-day MPC backtests (SA1, with seven runs, is the slowest). In SA1 the benchmark carries about 2,600 binaries, one per negative-price interval out of 8,640 (section 12).
11 · Interpret¶
For the owner:
- Of a $199k ceiling, a realistic controller earns $169k. Of the $30k gap:
- $14k is forecasting. Better forecasts help, but only so far.
- $10k is stale bands. Re-calibrate the bands deliberately, and test the change with this framework first, because re-calibration can also make things worse.
- $6k is constraint management.
- Better forecast accuracy is not the goal; decision value is. The attribution measures forecast error in dollars, not in MAE.
12 · Backtest the backtest¶
A backtest is a model too. Check it the way the tests do:
- the perfect-foresight replay must reproduce its own LP to the cent;
- no policy may beat the benchmark;
- physics and FCAS headroom must hold for any policy, however silly;
- a historical policy's view must be unchanged when the future is changed.
Real prices caught a bug these checks exist for. On SA1's September prices a policy
appeared to beat perfect foresight. The benchmark LP was charging and discharging in
the same interval when the price was negative: it was paid to consume, and burned
the surplus as conversion losses. A real inverter can't do that, and the simulator
settles only the net flow. The plan did this in 17 % of intervals, and the
benchmark's replay earned $9.7k (8 %) less than its own LP. The fix forbids
simultaneous charge and discharge only where the price is negative, with one
binary per negative-price interval, so the benchmark stays an LP everywhere else
(PerfectForesight(exclusive_at_negative_prices=True), the default). A test
reproduces the failure. On the synthetic fortnight the fix changes nothing: the
tables above are the same to the dollar.
13 · Adding realism¶
- Price impact. A 10 MW battery rarely moves the regional price, but it does set prices in thin FCAS markets. Perfect foresight overstates the ceiling there.
- Rebid timing and gate closure. Offers are locked shortly before each interval. Model the lag between decision and dispatch.
- Causer pays, regulation utilisation, FCAS trapezia. These are all simplified here.
- Telemetry vs calculated SOC. In the incident lab, the battery's reported SOC drifts from the energy balance and the controller plans on the wrong number.
14 · Exercises¶
Guided
For a two-day window, compute the capture ratio of the greedy policy with and without the inventory adjustment. Which way does the adjustment move it, and why?
Engineering
Add a "late file" incident: on one day the forecast is issued two hours stale. Measure the cost in the attribution framework as a fourth factor (16 backtests).
Market
Lower the stale band set's $85 band to $55 and re-run. How much of the band loss does one band explain?
Challenge
Prove that Shapley values are the only attribution that is efficient, symmetric, additive and gives zero to a factor that never changes the outcome.
Production challenge
Turn the ledger into a daily report: capture ratio, revenue by market, forced charging, SOC at peak, spike MWh missed. Decide the alert thresholds, and where the report runs (Cloudflare Worker + R2, later chapters).
15 · Production perspective¶
- The backtester and the live system share code. The same
Policyobject runs in both. Only the view changes: historical data replay or live feeds. - Every result carries its information policy. A dashboard must never show a perfect-foresight number without saying so.
- Attribution is the monthly conversation with the trader. It is in dollars, by cause, with the counterfactuals written down.
Run it yourself¶
| Artefact | Location |
|---|---|
| Views, instructions, simulator, ledger | src/energy_or/backtest/framework.py |
| Policies (perfect foresight, rolling MPC, bands, greedy) | src/energy_or/backtest/policies.py |
| KPIs and pattern checks | src/energy_or/backtest/metrics.py |
| Counterfactual and Shapley attribution | src/energy_or/backtest/attribution.py |
| Synthetic market month | src/energy_or/data/bess_market.py |
| Real AEMO prices in the same interface | src/energy_or/nem/markets.py |
| Real-price backtest and its figures | scripts/real_price_backtest.py (results in docs/assets/data/rp_real_backtest.json) |
| Tests | tests/test_backtest.py |
| Notebook | notebooks/09_backtesting_a_battery.ipynb |