Adding balanceUpdate key to UnicornFy. When a transfer from spot to cross/isolated margin (and vice-versa) happens, Binance sends a json with event type as 'balanceUpdate'. This event type was not mapped in UnicornFy.
I've create a unittest for this payload, also I've tested this release using a Docker image python3.8.1, unicorn-binance-websocket-api 0.29
import unicorn_binance_websocket_api
import logging
import os
import sys
import time
import requests
import threading
###############
### Logging ###
###############
# https://docs.python.org/3/library/logging.html#logging-levels
logging.basicConfig(level=logging.DEBUG,
filename=os.path.basename(__file__) + '.log',
format="{asctime} [{levelname:8}] {process} {thread} {module}: {message}",
style="{")
############
### Main ###
############
binance_api_key = os.environ.get('BAPI_KEY')
binance_api_secret = os.environ.get('BSECRET_KEY')
def print_stream_data_from_stream_buffer(binance_websocket_api_manager):
while True:
if binance_websocket_api_manager.is_manager_stopping():
exit(0)
oldest_stream_data_from_stream_buffer = binance_websocket_api_manager.pop_stream_data_from_stream_buffer()
if oldest_stream_data_from_stream_buffer is False:
time.sleep(0.1)
else:
print(oldest_stream_data_from_stream_buffer)
def main():
ubwa_com_spot = unicorn_binance_websocket_api.BinanceWebSocketApiManager(throw_exception_if_unrepairable=True)
ubwa_com_cross = unicorn_binance_websocket_api.BinanceWebSocketApiManager(exchange="binance.com-margin", throw_exception_if_unrepairable=True)
ubwa_com_im = unicorn_binance_websocket_api.BinanceWebSocketApiManager(exchange="binance.com-isolated_margin", throw_exception_if_unrepairable=True)
ubwa_com_spot.create_stream(
"arr",
"!userData",
output="UnicornFy",
api_key=binance_api_key,
api_secret=binance_api_secret,
stream_label="spot"
)
ubwa_com_cross.create_stream(
"arr",
"!userData",
api_key=binance_api_key,
api_secret=binance_api_secret,
output="UnicornFy",
stream_label="margin"
)
ubwa_com_im.create_stream(
"arr",
"!userData",
symbols="BTCUSDT",
output="UnicornFy",
api_key=binance_api_key,
api_secret=binance_api_secret,
stream_label="BTCUSDT"
)
# start a worker process to process to move the received stream_data from the stream_buffer to a print function
worker_thread_spot = threading.Thread(target=print_stream_data_from_stream_buffer, args=(ubwa_com_spot,))
worker_thread_spot.start()
# start a worker process to process to move the received stream_data from the stream_buffer to a print function
worker_thread_cross = threading.Thread(target=print_stream_data_from_stream_buffer, args=(ubwa_com_cross,))
worker_thread_cross.start()
# start a worker process to process to move the received stream_data from the stream_buffer to a print function
worker_thread_im = threading.Thread(target=print_stream_data_from_stream_buffer, args=(ubwa_com_im,))
worker_thread_im.start()
# Notifies
while True:
ubwa_com_spot.print_summary()
ubwa_com_cross.print_summary()
ubwa_com_im.print_summary()
time.sleep(60)
if __name__ == "__main__":
main()
With this script running just transfer assets between accounts (spot/cross/isolated) and now we get the correct output:
I've just noticed that some of the logging inside the function binance_websocket says 'binance_futures_websocket'. I can fix this later for you as well.
PR Details
Adding balanceUpdate key to UnicornFy. When a transfer from spot to cross/isolated margin (and vice-versa) happens, Binance sends a json with event type as 'balanceUpdate'. This event type was not mapped in UnicornFy.
Here shows exactly what to expect from the payload: https://github.com/binance/binance-spot-api-docs/blob/master/user-data-stream.md#balance-update
Description
Related Issue
https://github.com/oliver-zehentleitner/unicorn-fy/issues/20
Motivation and Context
When create_stream is using output=UnicornFy an error stops the websocket whenever a transfer occurs between accounts.
How Has This Been Tested
I've create a unittest for this payload, also I've tested this release using a Docker image python3.8.1, unicorn-binance-websocket-api 0.29
With this script running just transfer assets between accounts (spot/cross/isolated) and now we get the correct output:
Types of changes
Checklist