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?