frequenz-client-electricity-trading 0.0.9__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,252 @@
1
+ # License: MIT
2
+ # Copyright © 2024 Frequenz Energy-as-a-Service GmbH
3
+
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.5
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
+ import asyncio
40
+ from frequenz.client.electricity_trading import Client
41
+
42
+ # Change server address if needed
43
+ SERVICE_URL = "grpc://electricity-trading.api.frequenz.com:443?ssl=true"
44
+ with open('/path/to/api_key.txt', 'r', encoding='utf-8') as f:
45
+ API_KEY = f.read().strip()
46
+
47
+ async def initialize():
48
+ client = Client(
49
+ server_url=SERVICE_URL,
50
+ auth_key=API_KEY
51
+ )
52
+
53
+ asyncio.run(initialize())
54
+ ```
55
+
56
+ ### Example Usage
57
+
58
+ #### Create an Order
59
+
60
+ Here's an example of how to create a limit order to buy energy.
61
+
62
+
63
+ ???+ example "Create a limit order"
64
+
65
+ ```python
66
+ import asyncio
67
+ from frequenz.client.electricity_trading import (
68
+ Client,
69
+ Currency,
70
+ DeliveryArea,
71
+ DeliveryPeriod,
72
+ EnergyMarketCodeType,
73
+ MarketSide,
74
+ OrderType,
75
+ Power,
76
+ Price,
77
+ )
78
+ from datetime import datetime, timedelta
79
+ from decimal import Decimal
80
+
81
+ # Change server address if needed
82
+ SERVICE_URL = "grpc://electricity-trading.api.frequenz.com:443?ssl=true"
83
+ with open('/path/to/api_key.txt', 'r', encoding='utf-8') as f:
84
+ API_KEY = f.read().strip()
85
+
86
+ async def create_order():
87
+ client = Client(
88
+ server_url=SERVICE_URL,
89
+ auth_key=API_KEY
90
+ )
91
+
92
+ # Define order parameters
93
+ gridpool_id = 1
94
+ delivery_area = DeliveryArea(
95
+ code="10YDE-EON------1", # TenneT
96
+ code_type=EnergyMarketCodeType.EUROPE_EIC
97
+ )
98
+ delivery_period = DeliveryPeriod(
99
+ start=datetime.fromisoformat("2024-05-01T00:00:00+00:00"),
100
+ duration=timedelta(minutes=15)
101
+ )
102
+ price = Price(amount=Decimal("50.0"), currency=Currency.EUR)
103
+ quantity = Power(mw=Decimal("0.1"))
104
+ order = await client.create_gridpool_order(
105
+ gridpool_id=gridpool_id,
106
+ delivery_area=delivery_area,
107
+ delivery_period=delivery_period,
108
+ order_type=OrderType.LIMIT,
109
+ side=MarketSide.BUY,
110
+ price=price,
111
+ quantity=quantity,
112
+ )
113
+
114
+ asyncio.run(create_order())
115
+ ```
116
+
117
+ #### List Orders for a Gridpool
118
+
119
+ Orders for a given gridpool can be listed using various filters.
120
+ ???+ example "List orders for a gridpool"
121
+
122
+ ```python
123
+ import asyncio
124
+ from frequenz.client.electricity_trading import ( Client, MarketSide )
125
+
126
+ # Change server address if needed
127
+ SERVICE_URL = "grpc://electricity-trading.api.frequenz.com:443?ssl=true"
128
+ with open('/path/to/api_key.txt', 'r', encoding='utf-8') as f:
129
+ API_KEY = f.read().strip()
130
+
131
+ async def list_orders():
132
+ client = Client(
133
+ server_url=SERVICE_URL,
134
+ auth_key=API_KEY
135
+ )
136
+
137
+ gridpool_id: int = 1
138
+
139
+ # List all orders for a given gridpool
140
+ orders = await client.list_gridpool_orders(
141
+ gridpool_id=gridpool_id,
142
+ )
143
+
144
+ # List only the buy orders for a given gridpool
145
+ buy_orders = await client.list_gridpool_orders(
146
+ gridpool_id=gridpool_id,
147
+ side=MarketSide.BUY,
148
+ )
149
+
150
+ asyncio.run(list_orders())
151
+ ```
152
+
153
+
154
+ #### Streaming Public Trades
155
+
156
+ To get real-time updates on market trades, use the following code:
157
+
158
+ ???+ example "Stream public trades"
159
+ ```python
160
+ import asyncio
161
+ from frequenz.client.electricity_trading import Client
162
+
163
+ # Change server address if needed
164
+ SERVICE_URL = "grpc://electricity-trading.api.frequenz.com:443?ssl=true"
165
+ with open('/path/to/api_key.txt', 'r', encoding='utf-8') as f:
166
+ API_KEY = f.read().strip()
167
+
168
+ async def stream_trades():
169
+ client = Client(
170
+ server_url=SERVICE_URL,
171
+ auth_key=API_KEY
172
+ )
173
+ stream_public_trades = await client.stream_public_trades()
174
+ async for public_trade in stream_public_trades:
175
+ print(f"Received public trade: {public_trade}")
176
+
177
+ asyncio.run(stream_trades())
178
+ ```
179
+
180
+ """
181
+
182
+ from ._client import (
183
+ MAX_PRICE,
184
+ MIN_PRICE,
185
+ MIN_QUANTITY_MW,
186
+ PRECISION_DECIMAL_PRICE,
187
+ PRECISION_DECIMAL_QUANTITY,
188
+ Client,
189
+ )
190
+ from ._types import (
191
+ Currency,
192
+ DeliveryArea,
193
+ DeliveryDuration,
194
+ DeliveryPeriod,
195
+ EnergyMarketCodeType,
196
+ GridpoolOrderFilter,
197
+ GridpoolTradeFilter,
198
+ MarketActor,
199
+ MarketSide,
200
+ Order,
201
+ OrderDetail,
202
+ OrderExecutionOption,
203
+ OrderState,
204
+ OrderType,
205
+ Power,
206
+ Price,
207
+ PublicOrder,
208
+ PublicOrderBookFilter,
209
+ PublicTrade,
210
+ PublicTradeFilter,
211
+ StateDetail,
212
+ StateReason,
213
+ Trade,
214
+ TradeState,
215
+ UpdateOrder,
216
+ )
217
+ from ._utils import quantize_quantity
218
+
219
+ __all__ = [
220
+ "Client",
221
+ "Currency",
222
+ "DeliveryArea",
223
+ "DeliveryDuration",
224
+ "DeliveryPeriod",
225
+ "EnergyMarketCodeType",
226
+ "GridpoolOrderFilter",
227
+ "GridpoolTradeFilter",
228
+ "MarketSide",
229
+ "MarketActor",
230
+ "Order",
231
+ "OrderDetail",
232
+ "OrderExecutionOption",
233
+ "OrderState",
234
+ "OrderType",
235
+ "Power",
236
+ "Price",
237
+ "PublicOrder",
238
+ "PublicOrderBookFilter",
239
+ "PublicTrade",
240
+ "PublicTradeFilter",
241
+ "UpdateOrder",
242
+ "StateDetail",
243
+ "StateReason",
244
+ "Trade",
245
+ "TradeState",
246
+ "MAX_PRICE",
247
+ "MIN_PRICE",
248
+ "MIN_QUANTITY_MW",
249
+ "PRECISION_DECIMAL_QUANTITY",
250
+ "PRECISION_DECIMAL_PRICE",
251
+ "quantize_quantity",
252
+ ]