How to use

How to listen to binance events? UserDataStreams

Concept

The idea is to create listener methods that take an event object as a parameter. These objects wrap the content of payloads from websockets in a human-readable way. You must then activate these methods within binance.py by adding them to a handler list (which can also be modified or reset like a normal list since it inherits its properties).

A detailed example for user events

from binance import events

client = binance.Client(API_KEY, API_SECRET)
await client.load() # not mandatory

def my_order_update_listener(wrapped_event):
    print(f"order for symbol {wrapped_event.symbol} updated!")

client.events.register_user_event(on_order_update, "executionReport")
await client.start_user_events_listener()

client = binance.Client(API_KEY, API_SECRET) Create a client and set its api key and secret

await client.load() Load valid tokens and the rate limits from the binance API (it is not needed but recommended)

def my_order_update_listener(wrapped_event): Defines a listener and takes a wrapped_event in parameter

print(f"order for symbol {wrapped_event.symbol} updated!") Displays the symbol: a value defined in the wrapper object.

client.events.register_user_event(on_order_update, "executionReport") Register the listener in the dedicated handlers list (inside the events object of the client)

await client.start_user_events_listener() This will start a websocket connection to the binance API and create an events object.

Check this full working example on github.

Last updated