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 or PV), 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: battery price ÷ (rated full cycles × capacity × ) | €/kWh | |
| Household load (household mode only) | kW | |
| PV output and PV exported (household mode with solar) | kW | |
| Feed-in tariff paid for exported PV | €/kWh | |
| 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.
Household with solar
With PV, a kWh no longer has one price per step. Grid imports cost , PV surplus is exported at , so the bill of a step depends on the net load :
In a step with surplus, charging costs only the forgone feed-in , not . Capping the feed-in at the retail price keeps convex in , so it fits an LP; the cap only binds at spot prices far below zero. The battery still discharges only into the household, , so it never exports grid energy.
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.
Solar
In household mode, every step with gets an export variable and a row that keeps grid import non-negative:
Together with the battery terms this is up to a constant: at the optimum is the surplus left after the load and the battery, . Steps without PV keep exactly the LP above.
Wholesale with solar
The battery shares the grid connection with a PV plant whose output sells at and is curtailed when that is below zero. A variable is the PV put into the battery, with cost : it avoids the grid purchase and forgoes the sale. Profit stays the battery's own, measured against the plant without battery.
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.
Why it can pay at all: at a negative price the grid pays for taking energy, and a battery that is already full can only take more by losing it. Per kWh charged at −790 €/MWh with and €/kWh:
| Energy | Money | |
|---|---|---|
| Charge | 1 kWh from the grid | +0.790 € (paid to take it) |
| Discharge in the same step | 0.9 kWh to the grid | −0.711 € (delivering at a negative price costs) |
| Wear on the discharge | −0.079 € (0.9 × 0.088) | |
| Net | 0.1 kWh lost as heat, stored energy unchanged | 0.000 € |
Only the lost is really sold, while wear falls on all of the discharged. That is the factor in the threshold: the price has to be about ten times the wear (in €/MWh) before the trick pays.
With the page's default battery (7 kWh, 3500 €, 6000 cycles, ), €/kWh, so the wholesale threshold is about −790 €/MWh. That is below the day-ahead price floor of −500 €/MWh, so with these defaults the LP never does it and no window is re-solved as a MILP, in wholesale mode too. A MILP shows up only with low wear and full-battery steps at deeply negative prices: at −500 €/MWh and zero adders it needs below about 0.056 €/kWh, and with any negative price on a full battery triggers it. The MILP still takes money from negative prices, but only as a real battery can: charge in one step, discharge in a later one.
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.
Number of solves and problem size
A step is one price interval (15 min, or 1 h for older data), not one solve. One window is one LP, with per step, plus (household) or (wholesale) on PV steps, on a MILP re-solve and with a terminal value. For a backtest of days and a value history of days:
| Part | LPs | Steps per LP (quarter-hours) |
|---|---|---|
| Perfect foresight | 1 | all (a year of quarter-hours: 35,040 steps, about 105k variables without PV) |
| 24-hour plan at midnight | one day (96) | |
| Rolling plan, each variant | up to about 44 h (176): rest of today and all of tomorrow | |
| Value fit | up to | two days (192); only history days in the backtest's calendar months |
| Calibration | as the rolling plan |
The rolling plan has one more solve than days because day 1 is first planned at 00:00 and then again at . With a year of history the value of stored energy costs LPs, far more than any strategy. A MILP re-solve adds to the LP count: the window was first solved as an LP. The backtest page lists these counts under "Solves".
7. Evaluation
The executed schedules of each policy are scored on the realised prices:
In household mode both bills are summed from of Section 2: without battery on with , with battery on the same load and the executed . is their difference net of wear. Without PV this is with .
Solar shares: consumption from solar is without battery; with battery, PV first serves the load and then the battery, and discharge counts as solar by the share of all charging that came from PV surplus over the period (an approximation, not tracked per stored kWh). An empty usable capacity means no battery: no power and no wear, so the page shows the bill with solar alone.
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, 8.8 ct/kWh from Section 3): a charge edge at 0 €/MWh spot means ct/kWh, and the discharge edge is then about 89 €/MWh. With = 3 ct/kWh it would be about 40 €/MWh: wear is most of the gap.
Empty or full battery
On a step where the battery sits empty (or full) and neither charges nor discharges at a rate strictly inside its limits, is not unique: any value between the neighbouring ones gives the same plan, and HiGHS returns one end. Charging and discharging there are priced from two values instead:
- Charge side : a kWh charged on an empty battery stays in it until the battery next trades, so at the first later step that is not empty and idle.
- Discharge side : a kWh taken from a full battery is missed where it next trades, so at the first later step that is not full and idle.
Past the end of the window the carried value is the slope of the terminal value: of the segment just above for , just below for , and 0 without . If the window ends full because , there is no clean discharge value and stays the dual. Everywhere else . The charge edge uses , the discharge edge . Example: a battery empty overnight before a sunny day no longer shows the evening's discharge value as its charge edge, but the lower value a stored kWh has once the sun is up.
Solar surplus
While charging stays within the PV surplus (household: ; wholesale: all of , since the plant sells everything), a charged kWh gives up the PV export instead of a purchase:
- Wholesale: the PV would have sold at , so the charge edge moves up by the sell deduction, (for ). The discharge edge is unchanged.
- Household, charging: the cost is the feed-in , which does not depend on spot, so the step has no band. With the battery soaking up PV, .
- Household, holding: if (a tie: storing pays the same as exporting, so any surplus step will do) there is no band. Otherwise the charge edge is the usual one (the battery charges once the retail price falls below ). Discharging into a surplus only pushes out PV at , so the discharge edge is the usual one if , and absent otherwise: no spot price makes it discharge.
Charging beyond the surplus imports at spot, and the plain band applies again.
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. and are carried back over that LP's whole window, including the part a rolling plan does not execute.
Limits
- Indifference. While the battery soaks up PV at the feed-in, every surplus step costs the same, so which of them it charges in is arbitrary. Gaps between the charge bars there carry no meaning. The same happens off the grid: when is set by buying at the day's lowest price (often a flat stretch at 0 €/MWh), the charge edge sits on that price, and steps a few hundredths of a €/MWh apart land on opposite sides of it. The plan then charges at full power in some of them and not in others, for almost no difference in savings. The tooltip flags steps within 1 €/MWh of an edge.
- 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. - Solar profile. PVGIS (EU JRC) gives the long-run yield for the address: monthly totals and the average day of each month, south-facing at the optimal tilt, 14 % losses. Open-Meteo's hourly irradiance on the panel for each past day sets which days and hours are sunny, scaled by one factor so those hours yield what the PVGIS average days give for them; days without weather (e.g. tomorrow) use the average day. So the shape follows real weather on the price days, but a sunnier-than-average year is scaled back to the long-run yield. Weather is reanalysis on a grid of a few tens of km, hourly (15-minute steps take their hour's mean); temperature effects are folded into the scale factor.
- Feed-in is a fixed tariff, also paid at negative prices (newer German systems get nothing then), and PV is never curtailed.
- 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.