Methodology

The battery backtest's optimisation model, the sequential decision problem it approximates, and how the leftover-energy value is fitted. The tariff comparison's battery option reuses the rolling plan described here, without the value function.

1. Notation

Time is split into the price steps of the data, t=1,…,Tt = 1,\dots,T, each of length Δtt\Delta t_t hours (1 h or 0.25 h; DST days simply have 23 or 25 hours of steps). All times are Europe/Berlin.

SymbolMeaningUnit
πt\pi_tDay-ahead spot price€/MWh
ct, dtc_t,\ d_tCharge power drawn from the grid, discharge power delivered (grid side)kW
ete_tStored energy at the end of step ttkWh
E‾, E‾\underline E,\ \overline EMinimum and maximum stored energy (usable capacity E‾−E‾\overline E-\underline E)kWh
P‾ch, P‾dis\overline P^{\mathrm{ch}},\ \overline P^{\mathrm{dis}}Charge and discharge power limitskW
ηc, ηd\eta_c,\ \eta_dCharge and discharge efficiency, ηc=ηd=ρ\eta_c=\eta_d=\sqrt{\rho} with round-trip efficiency ρ\rho–
κ\kappaWear cost per kWh delivered€/kWh
LtL_tHousehold load (household mode only)kW
ab, as, ar, τa^{b},\ a^{s},\ a^{r},\ \tauBuy 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:

ptb=πt+ab1000,pts=πt−as1000,d‾t=P‾dis. p^{b}_t = \frac{\pi_t + a^{b}}{1000}, \qquad p^{s}_t = \frac{\pi_t - a^{s}}{1000}, \qquad \overline d_t = \overline P^{\mathrm{dis}}.

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:

ptb=pts=(πt+ar)(1+τ)1000,d‾t=min⁡ ⁣(P‾dis, Lt). p^{b}_t = p^{s}_t = \frac{(\pi_t + a^{r})(1+\tau)}{1000}, \qquad \overline d_t = \min\!\big(\overline P^{\mathrm{dis}},\, L_t\big).

Because ptb=ptsp^b_t=p^s_t, the fixed part ar(1+τ)a^r(1+\tau) of the price only acts on the energy lost in a round trip, (1−ρ)(1-\rho) 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 W={t0,…,t1}W=\{t_0,\dots,t_1\}, starting from the energy e^\hat e actually stored at t0t_0. As a maximisation of net savings (the code passes the equivalent minimisation to HiGHS):

max⁡c, d, e, z∑t∈WΔtt(ptsdt−ptbct−κ dt)  +  zs.t.et=et−1+Δtt(ηcct−dtηd),t∈W,et0−1=e^,0≤ct≤P‾ch,0≤dt≤d‾t,E‾≤et≤E‾,et1≥e~,z≤Vk+mk (et1−gk),k=0,…,K−1. \begin{aligned} \max_{c,\,d,\,e,\,z}\quad & \sum_{t\in W} \Delta t_t\big(p^{s}_t d_t - p^{b}_t c_t - \kappa\, d_t\big) \;+\; z \\ \text{s.t.}\quad & e_t = e_{t-1} + \Delta t_t\Big(\eta_c c_t - \frac{d_t}{\eta_d}\Big), \qquad t\in W,\quad e_{t_0-1}=\hat e, \\ & 0 \le c_t \le \overline P^{\mathrm{ch}}, \qquad 0 \le d_t \le \overline d_t, \qquad \underline E \le e_t \le \overline E, \\ & e_{t_1} \ge \tilde e, \\ & z \le V_k + m_k\,(e_{t_1} - g_k), \qquad k = 0,\dots,K-1 . \end{aligned}

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 ηcηd=ρ\eta_c\eta_d=\rho of what was bought.

Terminal condition

The plan must end with at least e~\tilde e stored. The requested minimum eterme^{\mathrm{term}} is clipped to what can be reached at all, so the LP is never infeasible:

rt0−1=e^,rt=min⁡ ⁣(E‾,  rt−1+ηcP‾chΔtt),e~=max⁡ ⁣(E‾,  min⁡(eterm, rt1−ε)). r_{t_0-1}=\hat e,\quad r_t = \min\!\big(\overline E,\; r_{t-1} + \eta_c \overline P^{\mathrm{ch}}\Delta t_t\big), \qquad \tilde e = \max\!\big(\underline E,\; \min(e^{\mathrm{term}},\, r_{t_1}-\varepsilon)\big).

Terminal value

The last group of constraints is the hypograph of a concave piecewise-linear function V^\hat V on the grid g0<⋯<gKg_0<\dots<g_K with values VkV_k and slopes mk=(Vk+1−Vk)/(gk+1−gk)m_k=(V_{k+1}-V_k)/(g_{k+1}-g_k). Since zz is maximised, at the optimum

z=min⁡k(Vk+mk(et1−gk))=V^(et1), z = \min_k\big(V_k + m_k(e_{t_1}-g_k)\big) = \hat V(e_{t_1}),

which is exact only because V^\hat V is concave (Section 6). Without a value function the zz terms are dropped, i.e. V^≡0\hat V\equiv 0.

MILP where needed

To rule out charging and discharging in the same step, the rolling and 24-hour plans add a binary utu_t per step:

ct≤P‾chut,dt≤d‾t (1−ut),ut∈{0,1}. c_t \le \overline P^{\mathrm{ch}} u_t, \qquad d_t \le \overline d_t\,(1-u_t), \qquad u_t\in\{0,1\}.

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 10−610^{-6} 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 d=ρcd=\rho c so the stored energy is unchanged, pays

Δt c [ρ (pts−κ)−ptb]. \Delta t\, c\,\big[\rho\,(p^s_t-\kappa) - p^b_t\big].

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:

πt<− ab+ρ (as+1000 κ)1−ρ, \pi_t < -\,\frac{a^{b} + \rho\,(a^{s} + 1000\,\kappa)}{1-\rho},

e.g. below −270 €/MWh at ρ=0.9\rho=0.9, κ=0.03\kappa=0.03 €/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 −ρκ/(1−ρ)-\rho\kappa/(1-\rho), 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 D+1D+1 become known once the auction clears, at decision time θ\theta on day DD (13:00 by default). The natural stage is therefore the interval between two decision times, [θD,θD+1)[\theta_D,\theta_{D+1}). At θD\theta_D the information state is

ID={πt:t≤end of D+1}. I_D = \{\pi_t : t \le \text{end of } D+1\}.

Elements

stateSD=(eD,  ID),eD=stored energy at θD,actionxD=(ct,dt)t∈[θD,θD+1)∈X(eD)(bounds of Section 3),transitioneD+1=eD+∑t∈[θD,θD+1)Δtt(ηcct−dtηd),ID+1=ID∪{πt:t∈D+2},rewardR(xD)=∑t∈[θD,θD+1)Δtt(ptsdt−ptbct−κ dt). \begin{aligned} \text{state} \quad & S_D = (e_D,\; I_D), \qquad e_D = \text{stored energy at } \theta_D,\\ \text{action} \quad & x_D = (c_t,d_t)_{t\in[\theta_D,\theta_{D+1})} \in \mathcal X(e_D) \quad\text{(bounds of Section 3)},\\ \text{transition} \quad & e_{D+1} = e_D + \sum_{t\in[\theta_D,\theta_{D+1})} \Delta t_t\Big(\eta_c c_t - \frac{d_t}{\eta_d}\Big), \qquad I_{D+1} = I_D \cup \{\pi_t : t \in D+2\},\\ \text{reward} \quad & R(x_D) = \sum_{t\in[\theta_D,\theta_{D+1})} \Delta t_t\big(p^{s}_t d_t - p^{b}_t c_t - \kappa\, d_t\big). \end{aligned}

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

VD(e,ID)=max⁡x∈X(e){R(x)+E[VD+1(eD+1,ID+1) ∣ ID]}. \mathcal V_D(e, I_D) = \max_{x\in\mathcal X(e)} \Big\{ R(x) + \mathbb E\big[\mathcal V_{D+1}(e_{D+1}, I_{D+1}) \,\big|\, I_D\big] \Big\}.

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, W={1,…,T}W=\{1,\dots,T\}, starting at e0e_0 and ending at or above eterme^{\mathrm{term}} like the other policies, with no terminal value:

ΠPF=max⁡x∈X∑t=1TΔtt(ptsdt−ptbct−κdt). \Pi^{\mathrm{PF}} = \max_{x\in\mathcal X} \sum_{t=1}^{T} \Delta t_t\big(p^s_t d_t - p^b_t c_t - \kappa d_t\big).

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 eterme^{\mathrm{term}}, with V^≡0\hat V\equiv0. 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 θD\theta_D the window runs to the end of day D+1D+1, i.e. all prices currently known. Only the part up to θD+1\theta_{D+1} is executed, and the next plan starts from the energy actually reached. In MDP terms it is a deterministic lookahead policy:

xDroll=[arg⁡max⁡x∈X(eD)∑t=θDend D+1Δtt(ptsdt−ptbct−κdt)+V^(eend D+1)][θD,θD+1). x_D^{\mathrm{roll}} = \Big[\arg\max_{x\in\mathcal X(e_D)} \sum_{t=\theta_D}^{\text{end } D+1} \Delta t_t\big(p^s_t d_t - p^b_t c_t - \kappa d_t\big) + \hat V(e_{\text{end }D+1})\Big]_{[\theta_D,\theta_{D+1})}.

The prices inside the window are known, so the only approximation is the continuation after the window: V^≡0\hat V\equiv0 for "rolling", and the fitted V^m\hat V_m 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 [θD+1, end D+1][\theta_{D+1},\,\text{end } D+1] as fixed, although the next stage will re-plan it after seeing day D+2D+2. It therefore slightly undervalues flexibility, a certainty-equivalent step.

6. Fitting the value of stored energy

V^m(e)\hat V_m(e) estimates how much more the following days are worth when a plan ends with ee instead of E‾\underline E 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 ss and every grid point gk=E‾+kK(E‾−E‾)g_k = \underline E + \tfrac{k}{K}(\overline E-\underline E), k=0,…,Kk=0,\dots,K with K=10K=10, solve the LP of Section 3 over the H=2H=2 days starting at midnight of ss, from e^=gk\hat e=g_k, with eterm=E‾e^{\mathrm{term}}=\underline E and no terminal value:

Js(gk)=max⁡∑t∈s, s+1Δtt(ptsdt−ptbct−κdt)s.t. et0−1=gk. J_s(g_k) = \max \sum_{t\in s,\,s+1} \Delta t_t\big(p^s_t d_t - p^b_t c_t - \kappa d_t\big) \quad\text{s.t. } e_{t_0-1}=g_k.

Group the days by calendar month mm and average the differences:

Vˉm(gk)=1∣Sm∣∑s∈Sm(Js(gk)−Js(E‾)). \bar V_m(g_k) = \frac{1}{|\mathcal S_m|}\sum_{s\in\mathcal S_m}\big(J_s(g_k) - J_s(\underline E)\big).

Concavity and monotonicity

The optimal value of a linear maximisation is concave in its right-hand side, and e^\hat e enters only there. So every JsJ_s is concave in e^\hat e, 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 V^m(g0)=0\hat V_m(g_0)=0:

mk=min⁡ ⁣(mk−1,  max⁡ ⁣(0,  Vˉm(gk+1)−V^m(gk)gk+1−gk)),V^m(gk+1)=V^m(gk)+mk (gk+1−gk), m_k = \min\!\Big(m_{k-1},\; \max\!\big(0,\; \tfrac{\bar V_m(g_{k+1})-\hat V_m(g_k)}{g_{k+1}-g_k}\big)\Big), \qquad \hat V_m(g_{k+1}) = \hat V_m(g_k) + m_k\,(g_{k+1}-g_k),

with m−1=∞m_{-1}=\infty. 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 V^m≡0\hat V_m\equiv0.

Bias

Each JsJ_s uses perfect foresight over its two days, so Vˉm\bar V_m estimates E[max⁡⋅]\mathbb E[\max\cdot] rather than the value a non-anticipative policy can achieve (E[max⁡]≥max⁡E\mathbb E[\max]\ge\max\mathbb E). 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 α∈A={0, 0.25, 0.5, 0.75, 1}\alpha\in\mathcal A=\{0,\,0.25,\,0.5,\,0.75,\,1\} the rolling plan with terminal value αV^m\alpha\hat V_m is run over the history period, and the factor that earns most is kept:

α⋆=arg⁡max⁡α∈A  Πhistroll(αV^),V^m←α⋆V^m. \alpha^\star = \arg\max_{\alpha\in\mathcal A}\; \Pi^{\mathrm{roll}}_{\mathrm{hist}}\big(\alpha\hat V\big), \qquad \hat V_m \leftarrow \alpha^\star\hat V_m .

Scaling by α≥0\alpha\ge0 keeps V^m\hat V_m concave and non-decreasing, so the plan stays an LP. α=0\alpha=0 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 α\alpha 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 ∣A∣|\mathcal A| extra rolling runs over the history.

7. Evaluation

The executed schedules (Ct,Dt,Et)(C_t,D_t,E_t) of each policy are scored on the realised prices:

Π=∑tΔtt ptsDt⏟revenue  −  ∑tΔtt ptbCt⏟cost  −  ∑tΔtt κDt⏟wear,share=ΠpolicyΠPF. \Pi = \underbrace{\sum_t \Delta t_t\, p^s_t D_t}_{\text{revenue}} \;-\; \underbrace{\sum_t \Delta t_t\, p^b_t C_t}_{\text{cost}} \;-\; \underbrace{\sum_t \Delta t_t\,\kappa D_t}_{\text{wear}}, \qquad \text{share} = \frac{\Pi^{\mathrm{policy}}}{\Pi^{\mathrm{PF}}}.

In household mode, with B0=∑tΔtt ptbLtB_0=\sum_t \Delta t_t\, p^b_t L_t the bill without battery, the bill with battery is B0−∑tΔtt(ptsDt−ptbCt)B_0 - \sum_t \Delta t_t (p^s_t D_t - p^b_t C_t), and Π\Pi is the saving net of wear. Full cycles are counted on the cell side, where the cells deliver Dt/ηdD_t/\eta_d:

N=∑tΔttDt/ηdE‾−E‾. N = \frac{\sum_t \Delta t_t D_t/\eta_d}{\overline E - \underline E}.

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 μt\mu_t be the dual (shadow price) of the energy balance at step tt: the increase in the window's optimal savings per extra kWh stored at the end of tt, 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, V^\hat V. 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 ptbp^b_t and stores ηc\eta_c kWh worth ηcμt\eta_c\mu_t; discharging 1 kWh more earns pts−κp^s_t-\kappa and uses 1/ηd1/\eta_d kWh worth μt/ηd\mu_t/\eta_d. So

ct>0  ⇒  ptb≤ηc μt,dt>0  ⇒  pts≥μtηd+κ. c_t > 0 \;\Rightarrow\; p^{b}_t \le \eta_c\,\mu_t, \qquad d_t > 0 \;\Rightarrow\; p^{s}_t \ge \frac{\mu_t}{\eta_d} + \kappa .

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 (θb=1000 ηcμt\theta^b=1000\,\eta_c\mu_t, θs=1000(μt/ηd+κ)\theta^s=1000(\mu_t/\eta_d+\kappa) in €/MWh):

household: πtb=θb1+τ−ar,πts=θs1+τ−ar;wholesale: πtb=θb−ab,πts=θs+as. \text{household: } \pi^{b}_t = \frac{\theta^b}{1+\tau} - a^r,\quad \pi^{s}_t = \frac{\theta^s}{1+\tau} - a^r; \qquad \text{wholesale: } \pi^{b}_t = \theta^b - a^b,\quad \pi^{s}_t = \theta^s + a^s .

Example with the household defaults (ara^r = 13.67 ct/kWh, τ\tau = 19 %, ρ\rho = 0.9, κ\kappa = 3 ct/kWh): a charge edge at 0 €/MWh spot means μt≈17.1\mu_t \approx 17.1 ct/kWh, and the discharge edge is then about 40 €/MWh.

Which LP

Each step takes μt\mu_t 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 μt\mu_t 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 (dt≤Ltd_t \le L_t), 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 ηc\eta_c on the way in and 1/ηd1/\eta_d 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 e0e_0, must end at or above eterme^{\mathrm{term}}, and get nothing for energy left at the end. The rolling plan with value function drops V^\hat V 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 (Dt/ηdD_t/\eta_d), 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.