Skip to content

Commit 8606955

Browse files
cwasickicamille-bouvy-frequenz
authored andcommitted
Switch CLI tool to use public trade streams
Signed-off-by: cwasicki <[email protected]>
1 parent 8ff21cc commit 8606955

File tree

2 files changed

+27
-18
lines changed

2 files changed

+27
-18
lines changed

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
list_gridpool_trades as run_list_gridpool_trades,
2424
)
2525
from frequenz.client.electricity_trading.cli.etrading import (
26-
list_public_trades as run_list_public_trades,
26+
receive_public_trades as run_receive_public_trades,
2727
)
2828

2929
TZ = ZoneInfo("Europe/Berlin")
@@ -47,10 +47,18 @@ def cli() -> None:
4747
@cli.command()
4848
@click.option("--url", required=True, type=str)
4949
@click.option("--key", required=True, type=str)
50+
@click.option("--delivery-start", default=None, type=iso)
5051
@click.option("--start", default=None, type=iso)
51-
def receive_public_trades(url: str, key: str, *, start: datetime) -> None:
52+
@click.option("--end", default=None, type=iso)
53+
def receive_public_trades(
54+
url: str, key: str, *, start: datetime, end: datetime, delivery_start: datetime
55+
) -> None:
5256
"""List and/or stream public trades."""
53-
asyncio.run(run_list_public_trades(url=url, key=key, delivery_start=start))
57+
asyncio.run(
58+
run_receive_public_trades(
59+
url=url, key=key, delivery_start=delivery_start, start=start, end=end
60+
)
61+
)
5462

5563

5664
@cli.command()

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

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,22 @@ def check_delivery_start(
4141
raise ValueError("Delivery period must be a multiple of `duration`.")
4242

4343

44-
async def list_public_trades(url: str, key: str, *, delivery_start: datetime) -> None:
44+
async def receive_public_trades(
45+
url: str,
46+
key: str,
47+
*,
48+
delivery_start: datetime | None = None,
49+
start: datetime | None = None,
50+
end: datetime | None = None,
51+
) -> None:
4552
"""List trades and stream new public trades.
4653
47-
If delivery_start is provided, list historical trades and stream new trades
48-
for the 15 minute delivery period starting at delivery_start.
49-
If no delivery_start is provided, stream new trades for any delivery period.
50-
5154
Args:
5255
url: URL of the trading API.
5356
key: API key.
5457
delivery_start: Start of the delivery period or None.
58+
start: First execution time to list trades from.
59+
end: Last execution time to list trades until.
5560
"""
5661
client = Client(server_url=url, auth_key=key)
5762

@@ -65,16 +70,12 @@ async def list_public_trades(url: str, key: str, *, delivery_start: datetime) ->
6570
start=delivery_start,
6671
duration=timedelta(minutes=15),
6772
)
68-
lst = client.list_public_trades(delivery_period=delivery_period)
69-
70-
async for trade in lst:
71-
print_public_trade(trade)
72-
73-
if delivery_start <= datetime.now(timezone.utc):
74-
return
75-
76-
stream = client.public_trades_stream(delivery_period=delivery_period).new_receiver()
77-
async for trade in stream:
73+
stream = client.receive_public_trades(
74+
delivery_period=delivery_period,
75+
start_time=start,
76+
end_time=end,
77+
)
78+
async for trade in stream.new_receiver():
7879
print_public_trade(trade)
7980

8081

0 commit comments

Comments
 (0)