Skip to content

ARBITRAGE

Buys energy when prices are low and sells when they are high, using day-ahead spot prices.

Mode string ARBITRAGE (EMS_OP_ARBITRAGE)
Dispatcher ems_main.py → control_logic_updated.control_step_arbitrage
Config block functions.Arbitrage
Queue source tag control_logic (the writer's default)
Status Implemented

This is the currently selected mode (ems_operation_mode: "ARBITRAGE") but it is enabled: false.

Purpose

Fetches today's and tomorrow's 15-minute spot prices, decides a direction (charge/sell/idle), and writes per-PCS setpoints. Between ticks it deliberately holds the last setpoints rather than recomputing, to avoid churn on a 2 Hz loop.

How it decides

  1. Tick gate. _arb_tick_gate(ar) allows a recompute only every tick.interval_seconds (300 s). If the gate is closed the function returns early and holds — with tick.run_on_start and tick.zero_power_on_skip influencing the start-up case.
  2. Fetch prices. Downloads the provider feed described by price_source.url_template, parsing format: json_array and the field mapping in price_source.fields. resolution_minutes: 15 and slot_mode: current_only select the active slot. Failure is non-fatal — the last setpoints are held.
  3. Decide direction, in priority order:
  4. Tomorrow plan (strategy.tomorrow_plan.enabled) — rank tomorrow's slots and charge in the charge_cheapest_slots cheapest, discharge in the discharge_most_expensive_slots most expensive, at charge_power_kw / discharge_power_kw, tie-broken by tie_breaker (earlier_first). Uses _pick_cheapest_slots / _pick_most_expensive_slots.
  5. Threshold engine — buy_threshold / sell_threshold with min_spread guarding the round trip.
  6. prefer_next_slot prefers the upcoming slot over the current one.
  7. Apply SOC gating and per-PCS caps, then write.

Configuration

functions.Arbitrage

Key Live value Meaning
enabled false Master switch.
timezone Europe/Stockholm Local time for slot matching.
tick.interval_seconds 300 Minimum seconds between recomputes.
tick.run_on_start true Compute immediately on start.
tick.skip_cycles_log true Log skipped cycles.
tick.zero_power_on_skip true Force 0 kW on skipped cycles.
price_zone SE4 Bidding zone.
price_currency SEK Currency for thresholds.
price_source.provider elprisetjustnu Provider name.
price_source.url_template https.gess.se Fetch URL.
price_source.timeout_ms 4000 HTTP timeout.
price_source.format json_array Response parsing mode.
price_source.fields time_start, time_end, price_sek, price_eur Field mapping.
price_source.resolution_minutes 15 Price slot length.
price_source.slot_mode current_only Slot selection mode.
strategy.prefer_next_slot true Prefer the upcoming slot.
strategy.min_spread 0 Minimum spread required to trade.
strategy.buy_threshold 0.1 Buy below this price.
strategy.sell_threshold 0.12 Sell above this price.
strategy.buy_power_kw 20 Charge power.
strategy.sell_power_kw 20 Discharge power.
strategy.tomorrow_plan enabled: true charge_cheapest_slots: 8, discharge_most_expensive_slots: 8, charge_power_kw: 20, discharge_power_kw: 20, tie_breaker: earlier_first.
optional.hysteresis 0 Price hysteresis.
optional.hysteresis_kw 0 Power hysteresis.

Safety

  • Returns when is_external_control_active(), when enabled is false, or when the tick gate is closed.
  • A failed price fetch holds the previous setpoints — it never trades on stale or empty data.
  • SOC gating is direction-aware: charge against max_soc_pct, discharge against min_soc_pct.
  • Final per-PCS clamp in _pcs_connect_and_set_power_kw.

Check: price_source.url_template is set to https.gess.se, which does not look like a real elprisetjustnu endpoint. Verify this before enabling.

Outputs

_pcs_connect_and_set_power_kw(cfg, pcs_id, ±kw) → queue action set_power_kw, source control_logic.