V3: place_order is crashing web socket (KiteTicker) link and doesn't reconnect

bluenile
Hi - I'm executing *place_order* within *on_ticks* and this is a causing an exception and disconnecting the web socket link. Please note, the orders go through and are executed, but the streaming link gets disconnected and does not auto-reconnect (though reconnect is enabled by default).

The error is: Traceback (most recent call last): builtins.TypeError: the JSON object must be str, not 'bytes'
the error dump points to files and packages related to web socket connection (twister, autobahn, ticker etc.)

The logged debug info:

2018-01-21 10:09:18,795 Starting new HTTPS connection (1): api.kite.trade
2018-01-21 10:09:18,861 https://api.kite.trade:443 "POST /orders/regular HTTP/1.1" 200 58
2018-01-21 10:09:18,928 WebSocket connection lost: [Failure instance: Traceback: : the JSON object must be str, not 'bytes'

the place_order code:
kite.place_order(exchange = kite.EXCHANGE_NFO, tradingsymbol = trading_symbol,\
quantity = 75, product = kite.PRODUCT_MIS, order_type = kite.ORDER_TYPE_LIMIT,\
transaction_type = kite.TRANSACTION_TYPE_BUY, price = limit_price, variety = kite.VARIETY_REGULAR)

I'm using try/except blocks everywhere, but this error is being thrown as unhandled error. The same code was working well with V2, not sure how to address this now.

Appreciate any help. Thanks.
  • bluenile
    If it helps, I'm presenting a comparison between debug logs for placing order between V2 & V3:

    V2:
    2018-01-19 09:15:27,872 Starting new HTTPS connection (1): api.kite.trade
    2018-01-19 09:15:29,237 https://api.kite.trade:443 "POST /orders/regular HTTP/1.1" 200 None

    V3:
    2018-01-21 10:09:18,795 Starting new HTTPS connection (1): api.kite.trade
    2018-01-21 10:09:18,861 https://api.kite.trade:443 "POST /orders/regular HTTP/1.1" 200 58
    2018-01-21 10:09:18,928 WebSocket connection lost: [Failure instance: Traceback: : the JSON object must be str, not 'bytes'

    The only difference is the last field of POST, 'None' (v2) v/s 58 (v3). Guess the error is also due to this - expecting a string but getting a different data type.

    Please suggest how to change this. Thanks.
  • Vivek
    Vivek edited January 2018
    I will try to reproduce this and get back to you. Also which Python version you are using?
  • bluenile
    Python version: 3.6.3 on Anaconda. Look forward to your views.
  • Vivek
    Vivek edited January 2018
    @bluenile I just tested the scenario and it works fine. on_tick callback I was placing a market order and as expected order went through and I also got on_order_update callback.

    Here is my log
    2018-01-22 12:16:59,727 - root - DEBUG - on connect: {"peer": "tcp4:35.154.180.164:443", "headers": {"date": "Mon, 22 Jan 2018 06:46:59 GMT", "connection": "upgrade", "upgrade": "websocket", "sec-websocket-accept": "RfJyYy3Z8JMm5okvIADZoV/Z+XA="}, "version": 18, "protocol": null, "extensions": []}
    ############################################################
    2018-01-22 12:17:00,214 - urllib3.connectionpool - DEBUG - Starting new HTTPS connection (1): api.kite.trade
    2018-01-22 12:17:00,474 - urllib3.connectionpool - DEBUG - https://api.kite.trade:443 "POST /orders/regular HTTP/1.1" 200 58
    ('order update: ', {u'exchange_order_id': u'1100000003976256', u'unfilled_quantity': 0, u'order_timestamp': u'2018-01-22 12:17:00', u'market_protection': 0, u'tradingsymbol': u'INFY', u'tag': u'', u'guid': u'66336QQMlJ1Wc3XE', u'order_type': u'MARKET', u'variety': u'regular', u'cancelled_quantity': 0, u'instrument_token': 408065, u'status_message': u'', u'status': u'COMPLETE', u'product': u'MIS', u'account_id': u'DA0017', u'exchange': u'NSE', u'order_id': u'180122000901933', u'price': 0, u'pending_quantity': 0, u'validity': u'DAY', u'app_id': 5596, u'placed_by': u'DA0017', u'disclosed_quantity': 0, u'exchange_update_timestamp': u'', u'parent_order_id': u'', u'exchange_timestamp': u'2018-01-22 12:17:00', u'checksum': u'', u'average_price': 1141.3, u'trigger_price': 0, u'transaction_type': u'BUY', u'filled_quantity': 1, u'quantity': 1}

    Ideally placing order and all should be done in separate thread from the ticker thread. Check example here - https://github.com/zerodhatech/pykiteconnect/blob/kite3/examples/threaded_ticker.py
  • bluenile
    Thanks for this! I have written the program in the same threaded manner as you have suggested, but I continued to get the same error.

    Your efforts helped me realize the problem is local, so created another conda environment and ran the program there, things are working fine. Any suggestions on what could be going wrong in the other environment are welcome, but otherwise, this is good.

    And thanks for your efforts in replicating the behaviour - it guided me to think differently to solve the problem!
  • Vivek
    @bluenile Seems like Python 3 unicode related issue where the value is type bytes instead of string. If you can get me full traceback it will be useful.
  • vineet_dhandhania
    Hi @bluenile , @vivek , Have we gotten to the depths of this issue? Kindly share how to handle this unhandled exception.
Sign In or Register to comment.