0

I'm trying to cancel an order on binance if another order has been filled.
The connection is websocket with unicorn library
In the while loop the listener waits for the response of a filled order
I'm trying to check for the order type which was filled and then cancel the opposed order.
The cancelling itself works, but I can't access the value 'STOP' in key 'c' for the condition

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:
            if oldest_stream_data_from_stream_buffer['o']['c'] == 'STOP':
                exchange.cancel_order(orders[1]['id'], 'BCH/USDT')

This is the JSON response from the filled order:

{'E': 1617807091189,
 'T': 1617807091187,
 'e': 'ORDER_TRADE_UPDATE',
 'o': {'L': '0',
       'R': False,
       'S': 'BUY',
       'T': 1617807091187,
       'X': 'NEW',
       'a': '0',
       'ap': '0',
       'b': '0',
       'c': 'STOP',
       'cp': False,
       'f': 'GTC',
       'i': 471573385,
       'l': '0',
       'm': False,
       'o': 'MARKET',
       'ot': 'STOP_MARKET',
       'p': '0',
       'pP': False,
       'ps': 'BOTH',
       'q': '1',
       'rp': '0',
       's': 'BCHUSDT',
       'si': 0,
       'sp': '629.68',
       'ss': 0,
       't': 0,
       'wt': 'CONTRACT_PRICE',
       'x': 'NEW',
       'z': '0'}}

I get a KeyError: 'o'
What am I doing wrong?

mmxxst7
  • 1
  • 1
  • Have you tried printing ```oldest_stream_data_from_stream_buffer```? Looks like it doesn't evaluate to ```False``` but it also doesn't contain an ```'o'``` key. – adamgy Apr 07 '21 at 15:16
  • yes, if I print it I get the mentioned JSON response – mmxxst7 Apr 07 '21 at 15:18
  • If it's a JSON response you should also do ```some_new_var = json.loads(oldest_stream_data_from_stream_buffer)``` to convert the JSON response to a python dictionary – adamgy Apr 07 '21 at 15:29
  • It is already converted to a dict. If I read this key `oldest_stream_data_from_stream_buffer['o']['ot'] == 'STOP_MARKET':`the order will be cancelled, but I get tons of error messages in the aftermath – mmxxst7 Apr 07 '21 at 15:31
  • Where exactly are you printing this value and where exactly are you able to access ```oldest_stream_data_from_stream_buffer['o']['ot']```? Are you getting ```KeyError: 'o'``` or ```KeyError: 'c'```? – adamgy Apr 07 '21 at 16:06
  • I'm not sure if I understand your question - where exactly I'm printing? However, it's not throwing an key error anymore with the 'ot' key. The bottom line is `ccxt.base.errors.OrderNotFound: binance {"code":-2011,"msg":"Unknown order sent."}` although the order has been canceled. I think this is an issue for ccxt or binance forum – mmxxst7 Apr 07 '21 at 18:28

0 Answers0