btgsolutions_dataservices.websocket package
Submodules
btgsolutions_dataservices.websocket.hfn_websocket_client module
- class btgsolutions_dataservices.websocket.hfn_websocket_client.HFNWebSocketClient(api_key: str, stream_type: str | None = 'realtime', country: str | None = 'brazil', ssl: bool | None = True, **kwargs)[source]
Bases:
object
This class connects with BTG Solutions Data Services HFN WebSocket, receiving high frequency news in realtime or delayed feeds.
Main use case:
>>> from btgsolutions_dataservices import HFNWebSocketClient >>> ws = HFNWebSocketClient( >>> api_key='YOUR_API_KEY', >>> stream_type='realtime', >>> country='brazil', >>> ssl=True >>> ) >>> ws.run() >>> ws.get_latest_news() >>> ws.close()
- Parameters:
api_key (str) – User identification key. Field is required.
stream_type (str) – Websocket connection feed. Options: ‘realtime’, ‘delayed’. Field is not required. Default: ‘realtime’.
country (str) – Country name. Options: ‘brazil’ or ‘chile’. Field is not required. Default: ‘brazil’.
ssl (bool) – Enable or disable ssl configuration. Field is not required. Default: True (enable).
- run(on_open=None, on_message=None, on_error=None, on_close=None, reconnect=True)[source]
Initializes a connection to websocket and starts to receive high frequency news.
- Parameters:
on_open (function) –
Called at opening connection to websocket.
Field is not required.
Default: prints that the connection was opened in case of success.
on_message (function) –
Called every time it receives a message.
- Arguments:
Data received from the server.
Field is not required.
Default: prints the data.
on_error (function) –
Called when a error occurs.
- Arguments:
Exception object.
Field is not required.
Default: prints the error.
on_close (function) –
Called when connection is closed.
- Arguments:
close_status_code.
close_msg.
Field is not required.
Default: prints a message that the connection was closed.
reconnect (bool) – Try reconnect if connection is closed. Field is not required. Default: True.
btgsolutions_dataservices.websocket.market_data_feed module
- class btgsolutions_dataservices.websocket.market_data_feed.MarketDataFeed(api_key: str, stream_type: str | None = 'realtime', exchange: str | None = 'b3', data_type: str | None = 'trades', data_subtype: str | None = None, feed: str | None = 'A', ssl: bool | None = True, reconnect: bool = True, on_open: Callable | None = None, on_message: Callable | None = None, on_error: Callable | None = None, on_close: Callable | None = None)[source]
Bases:
object
WebSocket client that connects with BTG Solutions Data Services WebSocket servers. The servers streams realtime and delayed market data, such as trades and book events. This is a multiprocessing-based WebSocket client designed for high-performance, scalable message handling applications. It leverages a system of inter-process communication to efficiently separate concerns and prevent the main application thread from blocking during WebSocket operations or message processing.
Main use case:
>>> from btgsolutions_dataservices import MarketDataFeed >>> ws = MarketDataFeed( >>> api_key='YOUR_API_KEY', >>> stream_type='realtime', >>> exchange='b3', >>> data_type='trades', >>> data_subtype='stocks', >>> ssl=True >>> ) >>> ws.run() >>> ws.subscribe(['MGLU3']) >>> ws.unsubscribe(['PETR4']) >>> ws.close()
- Parameters:
api_key (str) – User identification key. Field is required.
stream_type (str) – Websocket connection feed. Options: ‘realtime’, ‘delayed’. Field is not required. Default: ‘realtime’.
exchange (str) – Exchange name. Options: ‘b3’ or ‘bmv’. Field is not required. Default: ‘b3’.
data_type (str) – Market Data type. Options: ‘trades’, ‘processed-trades’, ‘books’, ‘indices’, ‘securities’, ‘stoploss’, ‘candles-1S’, ‘candles-1M’, ‘instrument_status’. Field is not required. Default: ‘trades’.
data_subtype (str) – Market Data subtype (when applicable). Options: ‘stocks’, ‘options’, ‘derivatives’. Field is not required. Default: None.
feed (str) – Market Data Feed. Options: ‘A’, ‘B’. Field is not required. Default: ‘A’ (enable).
ssl (bool) – Enable or disable ssl configuration. Field is not required. Default: True (enable).
reconnect (bool) – Try reconnect if connection is closed. Field is not required. Default: True.
on_open (function) –
Called at opening connection to websocket.
Field is not required.
Default: prints that the connection was opened in case of success.
on_message (function) –
Called every time it receives a message.
- Arguments:
Data received from the server.
Field is not required.
Default: prints the data.
on_error (function) –
Called when a error occurs.
- Arguments:
Exception object.
Field is not required.
Default: prints the error.
on_close (function) –
Called when connection is closed.
- Arguments:
close_status_code.
close_msg.
Field is not required.
Default: prints a message that the connection was closed.
- get_last_event(ticker: str)[source]
Get the last event for the provided ticker.
- Parameters:
ticker (str) – Field is required.
btgsolutions_dataservices.websocket.market_data_websocket_client module
- class btgsolutions_dataservices.websocket.market_data_websocket_client.MarketDataWebSocketClient(api_key: str, stream_type: str | None = 'realtime', exchange: str | None = 'b3', data_type: str | None = 'trades', data_subtype: str | None = None, instruments: List[str] | None = [], ssl: bool | None = True, feed: str | None = 'A', **kwargs)[source]
Bases:
object
This class connects with BTG Solutions Data Services WebSocket, receiving trade and index data, in real time or delayed.
Main use case:
>>> from btgsolutions_dataservices import MarketDataWebSocketClient >>> ws = MarketDataWebSocketClient( >>> api_key='YOUR_API_KEY', >>> stream_type='realtime', >>> exchange='b3', >>> data_type='trades', >>> data_subtype='stocks', >>> instruments=['PETR4'], >>> ssl=True >>> ) >>> ws.run() >>> ws.subscribe(['MGLU3']) >>> ws.unsubscribe(['PETR4']) >>> ws.close()
- Parameters:
api_key (str) – User identification key. Field is required.
stream_type (str) – Websocket connection feed. Options: ‘realtime’, ‘delayed’. Field is not required. Default: ‘realtime’.
exchange (str) – Exchange name. Options: ‘b3’ or ‘bmv’. Field is not required. Default: ‘b3’.
data_type (str) – Market Data type. Options: ‘trades’, ‘processed-trades’, ‘books’, ‘indices’, ‘securities’, ‘stoploss’, ‘candles-1S’, ‘candles-1M’, ‘instrument_status’. Field is not required. Default: ‘trades’.
data_subtype (str) – Market Data subtype (when applicable). Options: ‘stocks’, ‘options’, ‘derivatives’. Field is not required. Default: None.
instruments (list) – List of tickers or indexes to subscribe. Field is not required. Default: [].
ssl (bool) – Enable or disable ssl configuration. Field is not required. Default: True (enable).
feed (str) – Market Data Feed. Options: ‘A’, ‘B’. Field is not required. Default: ‘A’ (enable).
- candle_subscribe(list_instruments: list, candle_type: str)[source]
Subscribes a list of instruments, for partial/closed candle updates.
- Parameters:
list_instruments (list) – Field is required.
candle_type (str) – Field is required.
- candle_unsubscribe(list_instruments: list, candle_type: str)[source]
Unsubscribes a list of instruments, for partial/closed candle updates.
- Parameters:
list_instruments (list) – Field is required.
candle_type (str) – Field is required.
- get_last_event(ticker: str)[source]
Get the last event for the provided ticker.
- Parameters:
ticker (str) – Field is required.
- notify_stoploss(instrument_params)[source]
Create a stoploss notification routine on the provided instrument(s).
- Parameters:
instrument_params (dict) – Field is required.
- run(on_open=None, on_message=None, on_error=None, on_close=None, reconnect=True, spawn_thread: bool = True, default_logs: bool = True)[source]
Initializes a connection to websocket and subscribes to the instruments, if it was passed in the class initialization.
- Parameters:
on_open (function) –
Called at opening connection to websocket.
Field is not required.
Default: prints that the connection was opened in case of success.
on_message (function) –
Called every time it receives a message.
- Arguments:
Data received from the server.
Field is not required.
Default: prints the data.
on_error (function) –
Called when a error occurs.
- Arguments:
Exception object.
Field is not required.
Default: prints the error.
on_close (function) –
Called when connection is closed.
- Arguments:
close_status_code.
close_msg.
Field is not required.
Default: prints a message that the connection was closed.
reconnect (bool) – Try reconnect if connection is closed. Field is not required. Default: True.
spawn_thread (bool) – Spawn a new thread for incoming server messages (on_message callback function) Field is not required. Default: True.
default_logs (bool) – Default non-required logs, detailing messages sent from the client to the WebSocket server. Field is not required. Default: True.