Prerequisites
Install the required Python packages.Download the Protobuf Definitions
Clone the Anthid protobuf repository.Generate Python Client Code
Generate Python classes from the protobuf definitions.trading namespace contains the message definitions and gRPC service stubs required to interact with Anthid services.
Authentication
Streaming requests authenticate with either a Bearer access token or an API key passed as gRPC metadata. This guide uses an API key.Subscribe to Events
The example below demonstrates connecting to the server-streaming endpoint and subscribing to broker order and position events.environment is required: set it to trading_types_pb2.BROKER_ENVIRONMENT_PAPER or trading_types_pb2.BROKER_ENVIRONMENT_LIVE to match the account. The enum is defined in trading_types.proto, so it is generated into trading_types_pb2 rather than trading_service_pb2. To receive retained order snapshots first, set order_delivery=trading_pb2.ORDER_DELIVERY_PRIMED in Subscribe. Positions always prime retained state. Wait for the corresponding primed event before treating initial delivery as complete.
Terminal orders and zero-quantity positions expire from retained stream state after one hour. Rebuild a fresh snapshot set on reconnect so an old cache does not retain entries that no longer replay. See Rebuilding a cached view.
Bidirectional Streaming
Backend and native applications may prefer the bidirectionalStreamEvents endpoint.
This endpoint allows subscriptions and unsubscriptions to be sent over an existing stream without creating a new connection.
Handling Disconnects
Applications should automatically reconnect when a stream is interrupted. After reconnecting:- Establish a new stream.
- Re-authenticate using a Bearer access token or API key.
- Re-subscribe to required trading accounts.
- Resume event processing.
Related Resources
- Streaming Overview
- Trading Accounts
- Orders
- Positions
- Ledger
- Intents