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: Optional[str] = 'realtime', country: Optional[str] = 'brazil', ssl: Optional[bool] = 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).

close()[source]

Closes connection with websocket.

get_latest_news()[source]

Get the latest news from our High Frequency News service.

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:
      1. Data received from the server.

    • Field is not required.

    • Default: prints the data.

  • on_error (function) –

    • Called when a error occurs.

    • Arguments:
      1. Exception object.

    • Field is not required.

    • Default: prints the error.

  • on_close (function) –

    • Called when connection is closed.

    • Arguments:
      1. close_status_code.

      2. 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_websocket_client module

class btgsolutions_dataservices.websocket.market_data_websocket_client.MarketDataWebSocketClient(api_key: str, stream_type: Optional[str] = 'realtime', exchange: Optional[str] = 'b3', data_type: Optional[str] = 'trades', data_subtype: Optional[str] = None, instruments: Optional[List[str]] = [], ssl: Optional[bool] = True, **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’. 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).

available_to_subscribe()[source]

Return avaiable tickers to subscribe.

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.

clear_stoploss()[source]

Clears client stop loss notifications.

close()[source]

Closes connection with websocket.

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)[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:
      1. Data received from the server.

    • Field is not required.

    • Default: prints the data.

  • on_error (function) –

    • Called when a error occurs.

    • Arguments:
      1. Exception object.

    • Field is not required.

    • Default: prints the error.

  • on_close (function) –

    • Called when connection is closed.

    • Arguments:
      1. close_status_code.

      2. 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.

stoploss_status()[source]

Return client stop loss status.

subscribe(list_instruments, n=None)[source]

Subscribes a list of instruments.

Parameters
  • list_instruments (list) – Field is required.

  • n (int) – Field is not required. For books data_type only. Maximum book level. It must be between 1 and 10.

subscribed_to()[source]

Return client subscribed tickers.

unsubscribe(list_instruments)[source]

Unsubscribes a list of instruments.

Parameters

list_instruments (list) – Field is required.

btgsolutions_dataservices.websocket.websocket_default_functions module

Module contents