---
updatedAt: 2025-11-05T21:23:16.000Z
agentTools:
  projectIndex: https://developers.modoenergy.com/llms.txt
---

# Calculate rolling EPEX continuous trading price

```python Python
from datetime import datetime, timedelta
import requests
from pytz import timezone

import pandas as pd


# Pull details of trades executed in the past 8 hours
now = datetime.now(tz=timezone("UTC"))
tminus_8_hours = now - timedelta(hours=8)
strftime_format = "%Y-%m-%dT%H:%M:%S"
url = (
    "https://api.modoenergy.com/pub/v1/gb/epex/intra-day/detailed-trades?"
    f"date_from={tminus_8_hours.strftime(strftime_format)}"
    f"&date_to={now.strftime(strftime_format)}"
)
headers = {"X-Token": <YOUR API KEY>}
detailed_trades = pd.DataFrame()
while url is not None:
    response = requests.get(url=url, headers=headers)
    if response.status_code != 200:
        print(response.text)
    else:
        detailed_trades = pd.concat(
            [detailed_trades, pd.DataFrame(response.json()["results"])]
        )
        url = response.json()["next"]
 
for column in ["dlvryStart", "dlvryEnd", "tradeExecTime"]:
    detailed_trades[column] = pd.to_datetime(detailed_trades[column])

# Identify the active HH trades
latest_trades = detailed_trades.sort_values("tradeRevisionNo").groupby("tradeId").tail(1)
active_trades = latest_trades[
    (~latest_trades["selfTrade"]) & (latest_trades["state"]=="ACTI")
]
active_hh_trades = active_trades[active_trades["duration"]==0.5]

# Select trades for a specific product
product_of_interest = "HH240111-30"
trades_of_interest = active_hh_trades[active_hh_trades["name"]==product_of_interest].copy()

# Turn these trades into a 1 minute timeseries and calculate rolling averages
trades_of_interest.set_index("tradeExecTime", inplace=True)
trade_timeseries = trades_of_interest["price"].resample("T").mean().ffill().to_frame()
trade_timeseries.rename(columns={"price": "1min_avg_price"}, inplace=True)
trade_timeseries["5min_avg_price"] = trade_timeseries["1min_avg_price"].rolling(5).mean()
trade_timeseries["10min_avg_price"] = trade_timeseries["1min_avg_price"].rolling(10).mean()
trade_timeseries["15min_avg_price"] = trade_timeseries["1min_avg_price"].rolling(15).mean()
trade_timeseries["30min_avg_price"] = trade_timeseries["1min_avg_price"].rolling(30).mean()
trade_timeseries["60min_avg_price"] = trade_timeseries["1min_avg_price"].rolling(60).mean()
```

# Import required packages

<!-- python@1-5 -->

Most of these packages come with a base Python 3 installation. However you should ensure you also have pandas installed in your environment.

# Download Detailed Trades from the Modo API

<!-- python@8-30 -->

This block of code pulls the details of trades executed in the past 8 hours. You can adjust the `date_from` and `date_to` parameters to suit your own analysis.

Remember to add your own API key on line 17.

# Select the active HH trades

<!-- python@32-37 -->

Trades can be updated and revoked. This step identifies the latest revision of each trade and selects for active HH trades.

# Select a specific HH product

<!-- python@39-41 -->

Here I'm selecting products for half hour delivery in period 30 on 2024-01-11. You can adjust this to suit your own analysis.

# Resample to 1-minute timeseries

<!-- python@43-46 -->

This step resamples the data so that we get the average price of trades every minute. 

Note I forward fill the DataFrame to fill in any minutes where no trades were executed.

# Calculate moving averages

<!-- python@47-51 -->

Here I calculate a 5, 10, 15, 30, and 60 minute moving average. You can adjust this to suit your own needs.