LUCIT-Systems-and-Development / unicorn-binance-websocket-api

A Python SDK by LUCIT to use the Binance Websocket API`s (com+testnet, com-margin+testnet, com-isolated_margin+testnet, com-futures+testnet, com-coin_futures, us, tr, dex/chain+testnet) in a simple, fast, flexible, robust and fully-featured way.
https://unicorn-binance-websocket-api.docs.lucit.tech/
Other
678 stars 166 forks source link

Read single keys from userData #159

Closed mmxxst7 closed 3 years ago

mmxxst7 commented 3 years ago

Hello,

I'm trying to access single key values from the pop_stream_data_from_stream_buffer, in order to cancel either the stop order if the take profit has been filled, or vice versa.

I use your code this way:

from unicorn_binance_websocket_api.unicorn_binance_websocket_api_manager import BinanceWebSocketApiManager
import threading
import time
import config

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.01)
        else:
            print(oldest_stream_data_from_stream_buffer)

binance_com_api_key = config.binance_com_api_key
binance_com_api_secret = config.binance_com_api_secret

binance_com_websocket_api_manager = BinanceWebSocketApiManager(exchange="binance.com-futures-testnet",
                                                               throw_exception_if_unrepairable=True)

binance_com_user_data_stream_id = binance_com_websocket_api_manager.create_stream('arr', '!userData',
                                                                                  api_key=binance_com_api_key,
                                                                                  api_secret=binance_com_api_secret)

worker_thread = threading.Thread(target=print_stream_data_from_stream_buffer, args=(binance_com_websocket_api_manager,))
worker_thread.start()

How can I access the value from key 'X' ("X":"FILLED", ), or 'ot' ("ot":"STOP_MARKET") ?

I tried it this way: print(oldest_stream_data_from_stream_buffer['ot']) But I get an TypeError: string indices must be integers

oliver-zehentleitner commented 3 years ago

Hello!

You are receiving a string with a JSON structure (as sent by Binance). You can convert it to a dict by using output="dict" or you convert it to well named dicts by using output="UnicornFy".

Here is an example file: https://github.com/oliver-zehentleitner/unicorn-binance-websocket-api/blob/master/example_kline_1m_with_unicorn_fy.py

You can controll this once for all streams with output_default and also overwrite this for every specific stream with output: https://oliver-zehentleitner.github.io/unicorn-binance-websocket-api/unicorn_binance_websocket_api.html?highlight=output#module-unicorn_binance_websocket_api.unicorn_binance_websocket_api_manager

UnicornFy: https://github.com/oliver-zehentleitner/unicorn-fy

mmxxst7 commented 3 years ago

Thank you for your reply! It works!

mmxxst7 commented 3 years ago

May I ask you why I get the following error when I retrieve oldest_stream_data_from_stream_buffer['o']['ot']:

'STOP_MARKET'
'STOP_MARKET'
Exception in thread Thread-5:
Traceback (most recent call last):
  File "/Users/xxx/.conda/envs/untitled3/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
  File "/Users/xxx/.conda/envs/untitled3/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/Users/xxx/Workfiles/Python/vs_binance_algo/testnet/binance_testnet_futures_stp+tp separately_merge.py", line 59, in print_stream_data_from_stream_buffer
    pprint.pprint(oldest_stream_data_from_stream_buffer['o']['ot'])
KeyError: 'o'
oliver-zehentleitner commented 3 years ago

KeyError: 'o' means that oldest_stream_data_from_stream_buffer['o'] does not exists.

Since the initial topic is solved i am closing this issue.