Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions examples/03_subscribe_to_stream.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import asyncio
import logging
from asyncio import run
from signal import SIGINT, SIGTERM

from examples.init_env import init_env
from x10.perpetual.configuration import MAINNET_CONFIG
from x10.perpetual.stream_client import PerpetualStreamClient

LOGGER = logging.getLogger()
ENDPOINT_CONFIG = MAINNET_CONFIG


async def subscribe_to_streams(stop_event: asyncio.Event):
env_config = init_env()
stream_client = PerpetualStreamClient(api_url=ENDPOINT_CONFIG.stream_url)

async def subscribe_to_orderbook():
async with stream_client.subscribe_to_orderbooks("BTC-USD") as orderbook_stream:
while not stop_event.is_set():
try:
msg = await asyncio.wait_for(orderbook_stream.recv(), timeout=1)
LOGGER.info("Orderbook: %s#%s", msg.type, msg.seq)
except asyncio.TimeoutError:
pass

async def subscribe_to_account():
async with stream_client.subscribe_to_account_updates(env_config.api_key) as account_stream:
while not stop_event.is_set():
try:
msg = await asyncio.wait_for(account_stream.recv(), timeout=1)
LOGGER.info("Account: %s#%s", msg.type, msg.seq)
except asyncio.TimeoutError:
pass

LOGGER.info("Press Ctrl+C to stop")

await asyncio.gather(subscribe_to_orderbook(), subscribe_to_account())


async def run_example():
stop_event = asyncio.Event()
loop = asyncio.get_running_loop()

def signal_handler():
LOGGER.info("Signal received, stopping...")
stop_event.set()

loop.add_signal_handler(SIGINT, signal_handler)
loop.add_signal_handler(SIGTERM, signal_handler)

await subscribe_to_streams(stop_event)


if __name__ == "__main__":
run(main=run_example())
102 changes: 0 additions & 102 deletions examples/stream_example.py

This file was deleted.