WebSocket¶
Real-time streaming via the eToro WebSocket API.
WsClient¶
- class etoropy.WsClient(options)[source]¶
Bases:
objectLow-level WebSocket client for the eToro streaming API.
Handles authentication, automatic reconnection with exponential backoff, heartbeat (via the
websocketslibrary’s built-inping_interval), and topic subscription tracking.Message dispatch uses a lightweight event-emitter pattern:
on()/off()/once().Emitted events:
"open" -> () # TCP connection established "authenticated" -> () # auth handshake succeeded "message" -> (WsEnvelope) # raw data envelope "instrument:rate" -> (instrument_id, WsInstrumentRate) "private:event" -> (WsPrivateEvent) # order status changes "close" -> (code, reason) # connection closed "error" -> (Exception)
- Parameters:
options (
WsClientOptions) – Connection and reconnection settings.
- once(event, handler)[source]¶
Register handler for event, then auto-unregister after the first call.
- async connect()[source]¶
Open the WebSocket, authenticate, and start the receive loop.
- Raises:
EToroAuthError – If authentication fails or times out.
- Return type:
WsClientOptions¶
- class etoropy.WsClientOptions(api_key='', user_key='', ws_url='wss://ws.etoro.com/ws', reconnect_attempts=10, reconnect_delay=1.0, auth_timeout=10.0, heartbeat_interval=30.0, heartbeat_timeout=10.0)[source]¶
Bases:
objectConnection options for
WsClient.- Parameters:
api_key (
str(default:''))user_key (
str(default:''))ws_url (
str(default:'wss://ws.etoro.com/ws'))reconnect_attempts (
int(default:10))reconnect_delay (
float(default:1.0))auth_timeout (
float(default:10.0))heartbeat_interval (
float(default:30.0))heartbeat_timeout (
float(default:10.0))
Message Parser¶
- class etoropy.ws.message_parser.ParsedInstrumentRate(instrument_id, rate)[source]¶
Bases:
object- Parameters:
instrument_id (
int)rate (
WsInstrumentRate)
- rate: WsInstrumentRate¶
- class etoropy.ws.message_parser.ParsedPrivateEvent(event)[source]¶
Bases:
object- Parameters:
event (
WsPrivateEvent)
- event: WsPrivateEvent¶
- class etoropy.ws.message_parser.ParsedMessage(type, data=None)[source]¶
Bases:
object- Parameters:
type (
str)data (
ParsedInstrumentRate|ParsedPrivateEvent|Any(default:None))
- data: ParsedInstrumentRate | ParsedPrivateEvent | Any = None¶
- etoropy.ws.message_parser.parse_messages(envelope)[source]¶
Parse a WebSocket envelope into a list of typed messages.
Recognises two topic patterns:
instrument:<id>– parsed asinstrument:ratewith aParsedInstrumentRatepayload.private– parsed asprivate:eventwith aParsedPrivateEventpayload (order status changes).
Any other topic is returned as
"unknown".- Parameters:
envelope (
WsEnvelope)- Return type: