Skip to content

Composite Functions

Composite functions combine multiple API calls to provide ready-to-use data for common analysis tasks. They save time by handling data fetching, merging, and transformation automatically.

Available Composite Functions

Consumption and Forecast

from eptr2.composite import get_hourly_consumption_and_forecast_data

df = get_hourly_consumption_and_forecast_data(
    start_date="2024-07-01",
    end_date="2024-07-31"
)

Returns a DataFrame with:

Column Description
dt Datetime
load_plan Load plan forecast
uecm Settlement consumption (UEÇM)
rt_cons Real-time consumption
consumption Best available consumption (UECM or RT)

Price and Cost Data

from eptr2.composite import get_hourly_price_and_cost_data

df = get_hourly_price_and_cost_data(
    start_date="2024-07-01",
    end_date="2024-07-31"
)

Returns comprehensive price data including MCP, SMP, and imbalance prices.

Production Data

from eptr2.composite import get_hourly_production_data

df = get_hourly_production_data(
    start_date="2024-07-01",
    end_date="2024-07-31"
)

Production Plan Data

from eptr2.composite import get_hourly_production_plan_data

df = get_hourly_production_plan_data(
    start_date="2024-07-01",
    end_date="2024-07-31"
)

Imbalance Data

from eptr2.composite import get_imbalance_data

df = get_imbalance_data(
    start_date="2024-07-01",
    end_date="2024-07-31"
)

Using with Custom EPTR2 Instance

All composite functions accept an optional eptr parameter:

from eptr2 import EPTR2
from eptr2.composite import get_hourly_consumption_and_forecast_data

# Create your own EPTR2 instance
eptr = EPTR2(use_dotenv=True, recycle_tgt=True)

# Pass it to the composite function
df = get_hourly_consumption_and_forecast_data(
    start_date="2024-07-01",
    end_date="2024-07-31",
    eptr=eptr
)

Verbose Mode

Enable verbose mode to see progress:

df = get_hourly_consumption_and_forecast_data(
    start_date="2024-07-01",
    end_date="2024-07-31",
    verbose=True
)

Output:

Loading load plan...
Loading UECM...
Loading real time consumption...

Including Contract Symbols

Add contract symbols for easier identification:

df = get_hourly_consumption_and_forecast_data(
    start_date="2024-07-01",
    end_date="2024-07-31",
    include_contract_symbol=True
)

Example: Complete Analysis

Combine consumption and price data for cost analysis:

from eptr2 import EPTR2
from eptr2.composite import (
    get_hourly_consumption_and_forecast_data,
    get_hourly_price_and_cost_data
)
import pandas as pd

# Initialize
eptr = EPTR2(use_dotenv=True, recycle_tgt=True)

# Get consumption data
consumption_df = get_hourly_consumption_and_forecast_data(
    start_date="2024-07-01",
    end_date="2024-07-31",
    eptr=eptr
)

# Get price data
price_df = get_hourly_price_and_cost_data(
    start_date="2024-07-01",
    end_date="2024-07-31",
    eptr=eptr
)

# Merge for analysis
analysis = pd.merge(
    consumption_df,
    price_df,
    on=['dt'],
    how='inner'
)

# Calculate total cost
analysis['total_cost'] = analysis['consumption'] * analysis['mcp']
print(f"Total cost: {analysis['total_cost'].sum():,.2f} TL")

Plant Cost Analysis

from eptr2.composite import calculate_plant_costs

costs = calculate_plant_costs(
    start_date="2024-07-01",
    end_date="2024-07-31",
    pp_id=123,  # Power plant ID
    eptr=eptr
)

IDM (Intraday Market) Log

from eptr2.composite import idm_log_longer

idm_log = idm_log_longer(
    start_date="2024-07-29",
    end_date="2024-07-29",
    eptr=eptr
)

quantity in the log is in lots (1 lot = 0.1 MWh).

IDM contract statistics (MAD and TAD)

Market-wide price dispersion of each hourly contract, from the trade log:

from eptr2.composite import get_idm_contract_stats

stats = get_idm_contract_stats(
    start_date="2026-10-01",
    end_date="2026-10-07",
    eptr=eptr,
)
Column Meaning
idm_wap Weighted average price of the contract's trades
idm_mad MAD (mean absolute deviation, TL/MWh): quantity-weighted mean of abs(price - idm_wap)
idm_tad TAD (total absolute deviation, TL): sum(abs(price - idm_wap) * quantity) = idm_mad * idm_volume_mwh
idm_trade_count, idm_volume_mwh Number of trades and traded MWh

The log is slow to fetch (roughly 30–70 s per week), so by default the function writes idm_log_{start_date}_{end_date}.csv and idm_contract_stats_{start_date}_{end_date}.csv to export_dir ("data") and, on later calls, reads the statistics file (or else the log file) before fetching. Turn this off with export_to_csv=False / check_existing=False. get_org_estimated_activity(include_tad=True) uses the same function and files. end_date must be before today (Europe/Istanbul).

Organization Estimated Activity

Estimates a company's hourly trading activity from its own matched quantities only: GÖP, İA and GİP matchings by organization id (get_dabi_idm_data, about a second per week):

from eptr2.composite import get_org_estimated_activity

df = get_org_estimated_activity(
    start_date="2026-10-01",
    end_date="2026-10-07",
    org_id=104000,
    eptr=eptr,
    include_pnl=True,   # + MCP, published WAP and dabi_idm_pnl (fast)
    verbose=True,       # progress with timings on stderr
)

include_tad=True adds idm_mad and day_trade_tad, volatility metrics rather than PnL; it doesn't need include_pnl. MAD needs the market-wide intraday trade log (about 20k trades a day), fetched by idm_log_longer in multi-day chunks, so it is much slower: roughly 30–70 s for a week, depending on EPİAŞ. idm_log_days_interval sets the chunk length.

Column Meaning
day_trade min(idm_long, idm_short), bought and sold back within the intraday market
dabi_net da_net + bi_net: day-ahead (GÖP + İA) net position
dabi_idm_net dabi_net + idm_net; positive must be consumed, negative generated (subtract bi_net for GÖP + GİP only)
dabi_idm_close Day-ahead (GÖP + İA) position reversed in the intraday market: min(abs(dabi_net), abs(idm_net)) when they have opposite signs
mcp, idm_wap With include_pnl=True: MCP and the published intraday WAP
idm_mad With include_tad=True: MAD (mean absolute deviation, TL/MWh), the quantity-weighted mean of abs(price - WAP) over the contract's trades in the log
dabi_idm_pnl dabi_idm_close * sign(dabi_net) * (idm_wap - mcp); bilateral quantities are valued at MCP too
day_trade_tad With include_tad=True: day_trade * idm_mad (TL), the TAD (total absolute deviation) of the day-trade volume: the price room around WAP the day-trade volume was traded in; a volatility metric, not a PnL

The PnL is fictitious: the company's own trade prices are not public. To analyse several companies with MAD, fetch the log once with idm_log_longer and pass it as idm_log_df, or rely on the CSV files, which are written and read by default:

for org_id in [104000, 105000]:
    df = get_org_estimated_activity(
        start_date="2026-10-01",
        end_date="2026-10-07",
        org_id=org_id,
        eptr=eptr,
        include_pnl=True,
        include_tad=True,
        export_dir="data",    # default; export_to_csv and check_existing are True by default
    )

By default (export_to_csv=True) the function writes {name}_{start_date}_{end_date}.csv files: org_{org_id}_positions, prices (MCP, SMP, WAP and imbalance prices from get_hourly_price_and_cost_data), idm_log, idm_contract_stats (WAP, MAD, trade count and volume per contract) and org_{org_id}_activity, for the steps that ran. With check_existing=True files of the same name are read instead of fetched, so the second organization above reuses the market-wide MCP, WAP and trade log. Pass export_to_csv=False to write nothing. The files are CSV because the trade log can exceed Excel's row limit.

end_date must be before today (Europe/Istanbul); the function raises ValueError otherwise, because data of the current day isn't final.

MMS (Market Management System) Data

from eptr2.composite import get_mms_data

mms = get_mms_data(
    start_date="2024-07-29",
    end_date="2024-07-29",
    eptr=eptr
)

BPM (Balancing Power Market) Data

from eptr2.composite import get_bpm_data

bpm = get_bpm_data(
    start_date="2024-07-29",
    end_date="2024-07-29",
    eptr=eptr
)

Organization Periodic Data

from eptr2.composite import get_periodic_org_data

org_data = get_periodic_org_data(
    start_date="2024-07-01",
    end_date="2024-07-31",
    org_id=123,
    eptr=eptr
)

Function Signatures

All composite functions share a similar signature:

def composite_function(
    start_date: str,           # YYYY-MM-DD format
    end_date: str,             # YYYY-MM-DD format
    eptr: EPTR2 | None = None, # Optional EPTR2 instance
    verbose: bool = False,     # Print progress
    **kwargs                   # Additional parameters
) -> pd.DataFrame:
    ...

Best Practices

  1. Reuse EPTR2 instance - Pass the same instance to multiple composite calls:

    eptr = EPTR2(use_dotenv=True, recycle_tgt=True)
    df1 = get_hourly_consumption_and_forecast_data(..., eptr=eptr)
    df2 = get_hourly_price_and_cost_data(..., eptr=eptr)
    

  2. Use reasonable date ranges - Large date ranges may be slow:

    # Good: 1 month
    df = get_hourly_consumption_and_forecast_data(
        start_date="2024-07-01", end_date="2024-07-31", ...
    )
    
    # May be slow: 1 year
    df = get_hourly_consumption_and_forecast_data(
        start_date="2024-01-01", end_date="2024-12-31", ...
    )
    

  3. Enable verbose for debugging - See what's being loaded:

    df = get_hourly_consumption_and_forecast_data(..., verbose=True)
    

Next Steps