1. Notation
Time is split into the price steps of the data, , each of length hours (1 h or 0.25 h; DST days simply have 23 or 25 hours of steps). All times are Europe/Berlin.
| Symbol | Meaning | Unit |
|---|---|---|
| Day-ahead spot price | €/MWh | |
| Charge power drawn from the grid, discharge power delivered (grid side) | kW | |
| Stored energy at the end of step | kWh | |
| Minimum and maximum stored energy (usable capacity ) | kWh | |
| Charge and discharge power limits | kW | |
| Charge and discharge efficiency, with round-trip efficiency | – | |
| Wear cost per kWh delivered | €/kWh | |
| Household load (household mode only) | kW | |
| Buy adder, sell deduction, retail fees, VAT rate | €/MWh, – |
2. Prices seen by the battery
Money is in €, so prices are converted to €/kWh. In wholesale mode the battery buys and sells at spot with adders:
In household mode the battery sits behind the meter. Every kWh charged is bought at the retail price, every kWh discharged replaces a kWh the household would otherwise buy at the same retail price, and nothing is exported:
Because , the fixed part of the price only acts on the energy lost in a round trip, per kWh cycled. That is why the grid fee barely moves the household savings.
3. The planning problem for one window
Every plan is an optimisation over a window of steps , starting from the energy actually stored at . As a maximisation of net savings (the code passes the equivalent minimisation to HiGHS):
The charge efficiency acts on the grid-side energy going in, the discharge efficiency on the energy taken out of the cells, so a full round trip returns of what was bought.
Terminal condition
The plan must end with at least stored. The requested minimum is clipped to what can be reached at all, so the LP is never infeasible:
Terminal value
The last group of constraints is the hypograph of a concave piecewise-linear function on the grid with values and slopes . Since is maximised, at the optimum
which is exact only because is concave (Section 6). Without a value function the terms are dropped, i.e. .
MILP where needed
To rule out charging and discharging in the same step, the rolling and 24-hour plans add a binary per step:
The big-M constants are the step's own bounds, so the relaxation is as tight as this formulation allows. Each window is first solved as an LP. Only if that LP charges and discharges in the same step is the window re-solved as the MILP, to a relative gap of with a 60 s limit. An LP optimum without simultaneous flows is feasible for the MILP, and the LP is a relaxation of the MILP, so it is also the MILP optimum. The result is exactly the MILP's (unless the 60 s limit is hit, when the best solution found so far is used), and the slow solve only runs on the rare negative-price windows.
When the LP charges and discharges at once
Running both directions in the same step, with so the stored energy is unchanged, pays
This only matters when the battery has no room left to absorb more energy, and it is positive only for deeply negative prices. In wholesale mode:
e.g. below −270 €/MWh at , €/kWh and zero adders (checked numerically with HiGHS). The LP is then dumping energy as heat, which a real battery cannot do. The MILP rules it out. In household mode the retail price would have to fall below , which essentially never happens.
4. The sequential decision problem (MDP)
The backtest is a sequential decision problem under price uncertainty. The battery is a price taker: its actions do not move prices, and only the stored energy carries over from one step to the next.
Information
Day-ahead prices for day become known once the auction clears, at decision time on day (13:00 by default). The natural stage is therefore the interval between two decision times, . At the information state is
Elements
The energy transition is deterministic; only the price information is random, and it evolves independently of the actions.
Bellman equation
The optimal non-anticipative policy satisfies
Solving it exactly needs a model of the joint price distribution and a value function over energy and the price state. The backtest does not do that. It compares policies that approximate this recursion in different ways (Section 5).
5. Policies compared
Perfect foresight (upper bound)
One LP over the whole period, , starting at and ending at or above like the other policies, with no terminal value:
It knows all prices in advance, so no non-anticipative policy can beat it. It is always solved as an LP, even when the MILP option is on. That keeps it a valid upper bound, since the LP relaxation is at least as good as the MILP.
24-hour plan at midnight
Each day at 00:00, one window covering that day only, ending at or above , with . This is a myopic policy: energy left at midnight is worth nothing to the plan.
Rolling plan
Day 1 is planned at 00:00 for day 1. From then on, at the window runs to the end of day , i.e. all prices currently known. Only the part up to is executed, and the next plan starts from the energy actually reached. In MDP terms it is a deterministic lookahead policy:
The prices inside the window are known, so the only approximation is the continuation after the window: for "rolling", and the fitted of the month after the window for "rolling with value function". The window that reaches the end of the backtest gets no terminal value, because energy left then scores zero for every policy. Compared with the exact Bellman equation, the plan also treats the stretch as fixed, although the next stage will re-plan it after seeing day . It therefore slightly undervalues flexibility, a certainty-equivalent step.
6. Fitting the value of stored energy
estimates how much more the following days are worth when a plan ends with instead of stored. It should be fitted on history strictly before the backtest. By default the value period is all loaded prices, and the page warns when it overlaps the backtest.
For every historical day and every grid point , with , solve the LP of Section 3 over the days starting at midnight of , from , with and no terminal value:
Group the days by calendar month and average the differences:
Concavity and monotonicity
The optimal value of a linear maximisation is concave in its right-hand side, and enters only there. So every is concave in , and so is their average. This is why the fit always uses the LP, never the MILP. To remove solver noise, each slope, taken from the already rebuilt point to the next raw point, is clipped to be non-negative and no larger than the previous one, and the curve is rebuilt from :
with . The result is concave, so the hypograph constraints of Section 3 represent it exactly, and the rolling plan stays an LP. Months with no history get .
Bias
Each uses perfect foresight over its two days, so estimates rather than the value a non-anticipative policy can achieve (). The fitted value of stored energy therefore tends to be optimistic. It also ignores the price state: a month has one curve, whatever tomorrow's prices look like.
Calibration
To correct the optimism, the curve is shrunk by a single factor chosen on the same history. For each the rolling plan with terminal value is run over the history period, and the factor that earns most is kept:
Scaling by keeps concave and non-decreasing, so the plan stays an LP. is the plain rolling plan, so on the history the calibrated plan can never do worse than ignoring leftover energy. Months without a curve behave the same for every and do not affect the choice. The factor is chosen on the history used for the fit, so it avoids the backtest period only when the fit does. The cost is extra rolling runs over the history.
7. Evaluation
The executed schedules of each policy are scored on the realised prices:
In household mode, with the bill without battery, the bill with battery is , and is the saving net of wear. Full cycles are counted on the cell side, where the cells deliver :
8. Why a plan charges or discharges
The backtest page's chart "Why it charges or discharges" draws a hold band for each strategy: below it the plan charges, above it the plan discharges, inside it the plan holds. The band is read off the window LP of Section 3; nothing extra is fitted.
Value of stored energy
Let be the dual (shadow price) of the energy balance at step : the increase in the window's optimal savings per extra kWh stored at the end of , in €/kWh. It contains everything the plan sees: the prices in the window, losses, wear, the power and capacity limits, the terminal condition and, for the rolling plan with value function, . HiGHS returns it with the solution (the code negates the dual of its minimisation).
Thresholds
At the optimum no step can be improved on its own. Charging 1 kWh more costs and stores kWh worth ; discharging 1 kWh more earns and uses kWh worth . So
The gap between the two thresholds is the cost of losses and wear. Inverting the prices of Section 2 puts them on the spot axis (, in €/MWh):
Example with the household defaults ( = 13.67 ct/kWh, = 19 %, = 0.9, = 3 ct/kWh): a charge edge at 0 €/MWh spot means ct/kWh, and the discharge edge is then about 40 €/MWh.
Which LP
Each step takes from the LP whose decision was executed there: the single LP for perfect foresight, that day's LP for the 24-hour plan, and the covering 13:00 solve (00:00 on day 1) for the rolling plans.
Limits
- Degenerate steps. When the battery is empty or full and idle, the dual is not unique: any in an interval gives the same plan and HiGHS returns one of them. The band stays consistent with the decisions, but its edges can jump.
- MILP windows have no duals, so those steps have no band.
- How much, not whether. The band says whether the plan charges or discharges, not how much. Above the band the plan discharges as much as it can in that step: up to the battery's power, but in household mode never more than the household uses (), and not when the battery is empty. Likewise below the band it charges up to the charge power, unless the battery is full.
9. Review notes and limitations
- Checked and consistent: units (€/MWh → €/kWh, kW·h → kWh), the energy balance with on the way in and on the way out, the hypograph form of the terminal value, the MILP big-M bounds, the 13:00 information set, and the absence of look-ahead in the value fit when the periods do not overlap.
- Same end condition for every policy. All policies start at , must end at or above , and get nothing for energy left at the end. The rolling plan with value function drops on its final window, so it does not hold back energy that would score zero.
- LP energy dumping. Below the threshold of Section 3 the LP would charge and discharge at once. This affects only deeply negative wholesale prices. The rolling and 24-hour plans always use the LP-then-MILP fallback, which removes it at almost no cost. Perfect foresight and the value fit are always LPs, so they may still dump energy. That keeps perfect foresight an upper bound, but a slightly looser one.
- Cycles are counted on the cell side (), while wear is charged per kWh delivered to the grid or household, as its label says.
- Value function: the raw fit is optimistic (perfect foresight over two days). The calibration factor removes the average bias, but not its dependence on energy level or season. There is one curve per month, independent of the price state; alternatives are in
docs/method.md. The concavity projection is a greedy slope clip, not a least-squares fit, which only matters when the averages are noisy. - Data gaps longer than one hour are closed: the step before the gap keeps the normal step length, and the battery state carries straight across the gap. Shorter gaps in 15-minute data are not detected: the step before simply lasts until the next price.