Skip to main content
Stream market summary updates for a specific market and yield JSON objects.
market_id
object
required
The market identifier
yields
object
required
Dictionary containing market summary data, or None on error
Raises
  • ValueError — If API_SERVER_URL environment variable is not set
Example
>>> import os
>>> os.environ["API_SERVER_URL"] = "http://localhost:8080"
>>> async for summary in streamMarketSummary(market_id=1):
...     print(summary["openInterest"])
async def streamMarketSummary(market_id: Union[str, int]) -> AsyncIterator[Optional[Dict[str, Any]]]:
    base_url = os.getenv('API_SERVER_URL')
    if not base_url:
        raise ValueError('API_SERVER_URL environment variable not set')
    async with httpx.AsyncClient() as session:
        client = PerpetualsClient(base_url=base_url, session=session)
        try:
            async for market_summary in client.stream_market_summary(market_id):
                market_summary_dict = MessageToDict(market_summary, always_print_fields_with_no_presence=True)
                yield market_summary_dict
        except Exception as e:
            print(f'Error streaming market summary: {e}')
            print('\nTroubleshooting:')
            print('  - Is the server running?')
            print('  - Is this a valid market ID in the database?')
            print('  - Check network connectivity')
            yield None