Skip to content

Commit 622e053

Browse files
authored
Add option to request public orders to CLI tool (#154)
Public orders can now be retrieved similar to the existing option for receiving public trades.
2 parents 3d8d9e7 + 85fd910 commit 622e053

File tree

3 files changed

+113
-6
lines changed

3 files changed

+113
-6
lines changed

RELEASE_NOTES.md

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,9 @@
22

33
## Upgrading
44

5-
- The minimum allowed version of `protobuf` and `grpcio` has been updated to 6.31.1 and 1.72.1 respectively.
6-
75
## New Features
86

9-
- Updated to `v0.8.0` of the Electricity Trading API.
10-
- Updated to `v0.11.0` of the base client library.
117
- Added HMAC capabilities
8+
- Add option to request public order data from the API.
129

1310
## Bug Fixes
14-
15-
- Added `OrderType` field to `PublicOrder` class.

src/frequenz/client/electricity_trading/cli/__main__.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
from frequenz.client.electricity_trading.cli.etrading import (
2323
list_gridpool_trades as run_list_gridpool_trades,
2424
)
25+
from frequenz.client.electricity_trading.cli.etrading import (
26+
receive_public_orders as run_receive_public_orders,
27+
)
2528
from frequenz.client.electricity_trading.cli.etrading import (
2629
receive_public_trades as run_receive_public_trades,
2730
)
@@ -73,6 +76,35 @@ def receive_public_trades( # pylint: disable=too-many-arguments
7376
)
7477

7578

79+
@cli.command()
80+
@click.option("--url", required=True, type=str)
81+
@click.option("--auth_key", required=True, type=str)
82+
@click.option("--delivery-start", default=None, type=iso)
83+
@click.option("--start", default=None, type=iso)
84+
@click.option("--end", default=None, type=iso)
85+
@click.option("--sign_secret", default=None, type=str)
86+
def receive_public_orders( # pylint: disable=too-many-arguments
87+
url: str,
88+
auth_key: str,
89+
*,
90+
start: datetime,
91+
end: datetime,
92+
delivery_start: datetime,
93+
sign_secret: str | None = None,
94+
) -> None:
95+
"""List and/or stream public trades."""
96+
asyncio.run(
97+
run_receive_public_orders(
98+
url=url,
99+
auth_key=auth_key,
100+
delivery_start=delivery_start,
101+
start=start,
102+
end=end,
103+
sign_secret=sign_secret,
104+
)
105+
)
106+
107+
76108
@cli.command()
77109
@click.option("--url", required=True, type=str)
78110
@click.option("--auth_key", required=True, type=str)

src/frequenz/client/electricity_trading/cli/etrading.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
OrderType,
2121
Power,
2222
Price,
23+
PublicOrder,
2324
PublicTrade,
2425
Trade,
2526
)
@@ -81,6 +82,47 @@ async def receive_public_trades( # pylint: disable=too-many-arguments
8182
print_public_trade(trade)
8283

8384

85+
async def receive_public_orders( # pylint: disable=too-many-arguments
86+
url: str,
87+
auth_key: str,
88+
*,
89+
delivery_start: datetime | None = None,
90+
start: datetime | None = None,
91+
end: datetime | None = None,
92+
sign_secret: str | None = None,
93+
) -> None:
94+
"""List trades and stream new public trades.
95+
96+
Args:
97+
url: URL of the trading API.
98+
auth_key: API key.
99+
delivery_start: Start of the delivery period or None.
100+
start: First execution time to list trades from.
101+
end: Last execution time to list trades until.
102+
sign_secret: The cryptographic secret to use for HMAC generation.
103+
"""
104+
client = Client(server_url=url, auth_key=auth_key, sign_secret=sign_secret)
105+
106+
print_public_orders_header()
107+
108+
delivery_period = None
109+
# If delivery period is selected, list historical trades also
110+
if delivery_start is not None:
111+
check_delivery_start(delivery_start)
112+
delivery_period = DeliveryPeriod(
113+
start=delivery_start,
114+
duration=timedelta(minutes=15),
115+
)
116+
stream = client.receive_public_order_book(
117+
delivery_period=delivery_period,
118+
start_time=start,
119+
end_time=end,
120+
)
121+
async for orders in stream.new_receiver():
122+
for order in orders:
123+
print_public_order(order)
124+
125+
84126
async def list_gridpool_trades(
85127
url: str,
86128
auth_key: str,
@@ -301,6 +343,44 @@ def print_public_trade(trade: PublicTrade) -> None:
301343
print(",".join(v.name if isinstance(v, Enum) else str(v) for v in values))
302344

303345

346+
def print_public_orders_header() -> None:
347+
"""Print public order header in CSV format."""
348+
header = (
349+
"public_order_id,"
350+
"create_time,"
351+
"update_time,"
352+
"delivery_period_start,"
353+
"delivery_period_duration,"
354+
"delivery_area_code,"
355+
"quantity_mw,"
356+
"side,"
357+
"price_amount,"
358+
"price_currency,"
359+
"type,"
360+
"execution_option"
361+
)
362+
print(header)
363+
364+
365+
def print_public_order(order: PublicOrder) -> None:
366+
"""Print public order details to stdout in CSV format."""
367+
values = (
368+
order.public_order_id,
369+
order.create_time.isoformat(),
370+
order.update_time.isoformat(),
371+
order.delivery_period.start.isoformat(),
372+
order.delivery_period.duration,
373+
order.delivery_area.code,
374+
order.quantity.mw,
375+
order.side,
376+
order.price.amount,
377+
order.price.currency,
378+
order.type,
379+
order.execution_option,
380+
)
381+
print(",".join(v.name if isinstance(v, Enum) else str(v) for v in values))
382+
383+
304384
def print_trade_header() -> None:
305385
"""Print trade header in CSV format."""
306386
header = (

0 commit comments

Comments
 (0)