|
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 | +```python |
| 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 | + API_KEY = open('/path/to/api_key.txt').read().strip() |
| 44 | + client = Client( |
| 45 | + server_url=SERVICE_URL, |
| 46 | + auth_key=API_KEY |
| 47 | + ) |
| 48 | + ``` |
| 49 | +
|
| 50 | +### Example Usage |
| 51 | +
|
| 52 | +#### Create an Order |
| 53 | +
|
| 54 | +Here's an example of how to create a limit order to buy energy. |
| 55 | +
|
| 56 | +
|
| 57 | +???+ example "Create a limit order" |
| 58 | +
|
| 59 | + ```python |
| 60 | + from frequenz.client.electricity_trading import ( |
| 61 | + Currency, |
| 62 | + DeliveryArea, |
| 63 | + DeliveryPeriod, |
| 64 | + Energy, |
| 65 | + EnergyMarketCodeType, |
| 66 | + MarketSide, |
| 67 | + OrderType, |
| 68 | + Price, |
| 69 | + ) |
| 70 | + from datetime import datetime, timedelta |
| 71 | + from decimal import Decimal |
| 72 | +
|
| 73 | + # Define order parameters |
| 74 | + gridpool_id = 1 |
| 75 | + delivery_area = DeliveryArea( |
| 76 | + code="10YDE-EON------1", # TenneT |
| 77 | + code_type=EnergyMarketCodeType.EUROPE_EIC |
| 78 | + ) |
| 79 | + delivery_period = DeliveryPeriod( |
| 80 | + start=datetime.fromisoformat("2024-05-01T00:00:00+00:00"), |
| 81 | + duration=timedelta(minutes=15) |
| 82 | + ) |
| 83 | + price = Price(amount=Decimal("50.0"), currency=Currency.EUR) |
| 84 | + quantity = Energy(mwh=Decimal("0.1")) |
| 85 | + order = await client.create_gridpool_order( |
| 86 | + gridpool_id=gridpool_id, |
| 87 | + delivery_area=delivery_area, |
| 88 | + delivery_period=delivery_period, |
| 89 | + order_type=OrderType.LIMIT, |
| 90 | + side=MarketSide.BUY, |
| 91 | + price=price, |
| 92 | + quantity=quantity, |
| 93 | + ) |
| 94 | + ``` |
| 95 | +
|
| 96 | +#### List Orders for a Gridpool |
| 97 | +
|
| 98 | +Orders for a given gridpool can be listed using various filters. |
| 99 | +???+ example "List orders for a gridpool" |
| 100 | +
|
| 101 | + ```python |
| 102 | + from frequenz.client.electricity_trading import MarketSide |
| 103 | +
|
| 104 | + # List all orders for a given gridpool |
| 105 | + orders = await client.list_gridpool_orders( |
| 106 | + gridpool_id=gridpool_id, |
| 107 | + ) |
| 108 | +
|
| 109 | + # List only the buy orders for a given gridpool |
| 110 | + buy_orders = await client.list_gridpool_orders( |
| 111 | + gridpool_id=gridpool_id, |
| 112 | + side=MarketSide.BUY, |
| 113 | + ) |
| 114 | + ``` |
| 115 | +
|
| 116 | +
|
| 117 | +#### Streaming Public Trades |
| 118 | +
|
| 119 | +To get real-time updates on market trades, use the following code: |
| 120 | +
|
| 121 | +???+ example "Stream public trades" |
| 122 | + ```python |
| 123 | + stream_public_trades = await client.stream_public_trades() |
| 124 | + async for public_trade in stream_public_trades: |
| 125 | + print(f"Received public trade: {public_trade}") |
| 126 | + ``` |
| 127 | +
|
| 128 | +""" |
5 | 129 |
|
6 | 130 | from ._client import Client |
7 | 131 | from ._types import ( |
|
0 commit comments