|
1 | 1 | # License: MIT |
2 | 2 | # Copyright © 2024 Frequenz Energy-as-a-Service GmbH |
3 | 3 |
|
4 | | -"""Electricity Trading API client for Python.""" |
| 4 | +""" |
| 5 | +Electricity Trading API client for Python. |
| 6 | +
|
| 7 | +## Frequenz Electricity Trading API Client |
| 8 | +
|
| 9 | +This module provides an easy-to-use Python interface to interact with the Frequenz Electricity |
| 10 | +Trading API. It allows you to create orders, manage market data, and interact with the electricity |
| 11 | +trading ecosystem. |
| 12 | +
|
| 13 | +### Features |
| 14 | +
|
| 15 | +- **Create and manage gridpool orders**: Place new orders, update existing ones, and cancel orders |
| 16 | +when necessary. |
| 17 | +- **Stream live data**: Get real-time updates on market data, including order books, trades, |
| 18 | +and market prices. |
| 19 | +- **Retrieve historical data**: Access historical data on market trades. |
| 20 | +
|
| 21 | +### Installation |
| 22 | +
|
| 23 | +You can install the Frequenz Electricity Trading API client via pip. Replace `VERSION` |
| 24 | +with the specific version you wish to install. |
| 25 | +
|
| 26 | +# Choose the version you want to install |
| 27 | +```bash |
| 28 | +VERSION=0.2.3 |
| 29 | +pip install frequenz-client-electricity-trading==$VERSION |
| 30 | +``` |
| 31 | +
|
| 32 | +### Initialization |
| 33 | +
|
| 34 | +First, initialize the client with the appropriate server URL and API key. |
| 35 | +
|
| 36 | +???+ example "Initialize the client" |
| 37 | +
|
| 38 | + ```python |
| 39 | + from frequenz.client.electricity_trading import Client |
| 40 | +
|
| 41 | + # Change server address if needed |
| 42 | + SERVICE_URL = "grpc://electricity-trading.api.frequenz.com:443?ssl=true" |
| 43 | + with open('/path/to/api_key.txt', 'r', encoding='utf-8') as f: |
| 44 | + API_KEY = f.read().strip() |
| 45 | + client = Client( |
| 46 | + server_url=SERVICE_URL, |
| 47 | + auth_key=API_KEY |
| 48 | + ) |
| 49 | + ``` |
| 50 | +
|
| 51 | +### Example Usage |
| 52 | +
|
| 53 | +#### Create an Order |
| 54 | +
|
| 55 | +Here's an example of how to create a limit order to buy energy. |
| 56 | +
|
| 57 | +
|
| 58 | +???+ example "Create a limit order" |
| 59 | +
|
| 60 | + ```python |
| 61 | + from frequenz.client.electricity_trading import ( |
| 62 | + Client, |
| 63 | + Currency, |
| 64 | + DeliveryArea, |
| 65 | + DeliveryPeriod, |
| 66 | + Energy, |
| 67 | + EnergyMarketCodeType, |
| 68 | + MarketSide, |
| 69 | + OrderType, |
| 70 | + Price, |
| 71 | + ) |
| 72 | + from datetime import datetime, timedelta |
| 73 | + from decimal import Decimal |
| 74 | +
|
| 75 | + # Change server address if needed |
| 76 | + SERVICE_URL = "grpc://electricity-trading.api.frequenz.com:443?ssl=true" |
| 77 | + with open('/path/to/api_key.txt', 'r', encoding='utf-8') as f: |
| 78 | + API_KEY = f.read().strip() |
| 79 | + client = Client( |
| 80 | + server_url=SERVICE_URL, |
| 81 | + auth_key=API_KEY |
| 82 | + ) |
| 83 | +
|
| 84 | + # Define order parameters |
| 85 | + GRIDPOOL_ID = 1 |
| 86 | + delivery_area = DeliveryArea( |
| 87 | + code="10YDE-EON------1", # TenneT |
| 88 | + code_type=EnergyMarketCodeType.EUROPE_EIC |
| 89 | + ) |
| 90 | + delivery_period = DeliveryPeriod( |
| 91 | + start=datetime.fromisoformat("2024-05-01T00:00:00+00:00"), |
| 92 | + duration=timedelta(minutes=15) |
| 93 | + ) |
| 94 | + price = Price(amount=Decimal("50.0"), currency=Currency.EUR) |
| 95 | + quantity = Energy(mwh=Decimal("0.1")) |
| 96 | + order = await client.create_gridpool_order( |
| 97 | + gridpool_id=GRIDPOOL_ID, |
| 98 | + delivery_area=delivery_area, |
| 99 | + delivery_period=delivery_period, |
| 100 | + order_type=OrderType.LIMIT, |
| 101 | + side=MarketSide.BUY, |
| 102 | + price=price, |
| 103 | + quantity=quantity, |
| 104 | + ) |
| 105 | + ``` |
| 106 | +
|
| 107 | +#### List Orders for a Gridpool |
| 108 | +
|
| 109 | +Orders for a given gridpool can be listed using various filters. |
| 110 | +???+ example "List orders for a gridpool" |
| 111 | +
|
| 112 | + ```python |
| 113 | + from frequenz.client.electricity_trading import ( Client, MarketSide ) |
| 114 | +
|
| 115 | + # Change server address if needed |
| 116 | + SERVICE_URL = "grpc://electricity-trading.api.frequenz.com:443?ssl=true" |
| 117 | + with open('/path/to/api_key.txt', 'r', encoding='utf-8') as f: |
| 118 | + API_KEY = f.read().strip() |
| 119 | + client = Client( |
| 120 | + server_url=SERVICE_URL, |
| 121 | + auth_key=API_KEY |
| 122 | + ) |
| 123 | +
|
| 124 | + gridpool_id: int = 1 |
| 125 | +
|
| 126 | + # List all orders for a given gridpool |
| 127 | + orders = await client.list_gridpool_orders( |
| 128 | + gridpool_id=gridpool_id, |
| 129 | + ) |
| 130 | +
|
| 131 | + # List only the buy orders for a given gridpool |
| 132 | + buy_orders = await client.list_gridpool_orders( |
| 133 | + gridpool_id=gridpool_id, |
| 134 | + side=MarketSide.BUY, |
| 135 | + ) |
| 136 | + ``` |
| 137 | +
|
| 138 | +
|
| 139 | +#### Streaming Public Trades |
| 140 | +
|
| 141 | +To get real-time updates on market trades, use the following code: |
| 142 | +
|
| 143 | +???+ example "Stream public trades" |
| 144 | + ```python |
| 145 | + from frequenz.client.electricity_trading import Client |
| 146 | +
|
| 147 | + # Change server address if needed |
| 148 | + SERVICE_URL = "grpc://electricity-trading.api.frequenz.com:443?ssl=true" |
| 149 | + with open('/path/to/api_key.txt', 'r', encoding='utf-8') as f: |
| 150 | + API_KEY = f.read().strip() |
| 151 | + client = Client( |
| 152 | + server_url=SERVICE_URL, |
| 153 | + auth_key=API_KEY |
| 154 | + ) |
| 155 | + stream_public_trades = await client.stream_public_trades() |
| 156 | + async for public_trade in stream_public_trades: |
| 157 | + print(f"Received public trade: {public_trade}") |
| 158 | + ``` |
| 159 | +
|
| 160 | +""" |
5 | 161 |
|
6 | 162 | from ._client import Client |
7 | 163 | from ._types import ( |
|
0 commit comments