Missing depth data while passsing ticks to another function

r95990 edited January 29 in Python client
Hi! While running the below code, I observed that the depth data is missing, can anyone help me with this?
def on_ticks(ws, ticks):

def feed_data(ticks):

Output is
[{'tradable': False, 'mode': 'full', 'instrument_token': 265, 'last_price': 71860.89, 'ohlc': {'high': 71965.28, 'low': 70880.54, 'open': 70968.1, 'close': 70700.67}, 'change': 1.6410311245989624, 'exchange_timestamp': datetime.datetime(2024, 1, 29, 14, 16, 14)}]

where as expected output should be this
[{'tradable': True, 'mode': 'full', 'instrument_token': 9691394, 'last_price': 2.6, 'last_traded_quantity': 450, 'average_traded_price': 3.04, 'volume_traded': 11329500, 'total_buy_quantity': 2712350, 'total_sell_quantity': 331300, 'ohlc': {'open': 3.4, 'high': 3.65, 'low': 2.5, 'close': 3.35}, 'change': -22.388059701492537, 'last_trade_time': datetime.datetime(2024, 1, 29, 13, 50, 57), 'oi': 4095750, 'oi_day_high': 4481600, 'oi_day_low': 3605900, 'exchange_timestamp': datetime.datetime(2024, 1, 29, 13, 50, 57), 'depth': {'buy': [{'quantity': 57800, 'price': 2.55, 'orders': 76}, {'quantity': 26650, 'price': 2.5, 'orders': 31}, {'quantity': 17050, 'price': 2.45, 'orders': 18}, {'quantity': 13900, 'price': 2.4, 'orders': 12}, {'quantity': 11950, 'price': 2.35, 'orders': 8}], 'sell': [{'quantity': 23450, 'price': 2.6, 'orders': 32}, {'quantity': 55400, 'price': 2.65, 'orders': 68}, {'quantity': 15550, 'price': 2.7, 'orders': 17}, {'quantity': 12850, 'price': 2.75, 'orders': 15}, {'quantity': 19350, 'price': 2.8, 'orders': 17}]}}]
  • sujith
    Sensex is an index and hence can't be traded. Therefore it will not have any bid and ask details in the full mode tick.
  • r95990
    My bad, that I have pasted a sensex data, but scrip data is also similar,
    {'tradable': True, 'mode': 'quote', 'instrument_token': 9691394, 'last_price': 2.6, 'last_traded_quantity': 150, 'average_traded_price': 3.03, 'volume_traded': 11756650, 'total_buy_quantity': 2698100, 'total_sell_quantity': 296500, 'ohlc': {'open': 3.4, 'high': 3.65, 'low': 2.5, 'close': 3.35}, 'change': -22.388059701492537}, {'tradable': True, 'mode': 'quote', 'instrument_token': 9699586, 'last_price': 3.75, 'last_traded_quantity': 50, 'average_traded_price': 4.76, 'volume_traded': 3245200, 'total_buy_quantity': 71850, 'total_sell_quantity': 60900, 'ohlc': {'open': 5.6, 'high': 5.7, 'low': 3.15, 'close': 5.8}, 'change': -35.34482758620689}, {'tradable': True, 'mode': 'quote', 'instrument_token': 9760514, 'last_price': 1.6, 'last_traded_quantity': 50, 'average_traded_price': 1.44, 'volume_traded': 11267300, 'total_buy_quantity': 3615650, 'total_sell_quantity': 287050, 'ohlc': {'open': 2.2, 'high': 2.2, 'low': 1.15, 'close': 2.25}, 'change': -28.888888888888882},
  • MAG
    'mode': 'quote' says it all.
    You need to change the mode to full. In quote mode you will not get market depth.
    in your on_connect method you would first subscribe to the instrument tokens and then set the mode to full as follows
    def on_connect(ws, response):
    # Callback on successful connect.
    # Subscribe to a list of instrument_tokens
    ws.subscribe([9691394, 9699586, 9760514])

    # Set the subscriptions to tick in `full` mode.
    ws.set_mode(ws.MODE_FULL, [9691394, 9699586, 9760514])

    Note that you could set full mode for only select instruments from the subscription list
    So once you subscribed to instrument tokens 9691394, 9699586, 9760514, if you wanted full mode for only one of them say, 9699586 you could set the mode as full for only that instrument token

    ws.subscribe([9691394, 9699586, 9760514])
    ws.set_mode(ws.MODE_FULL, [ 9699586])
    # In this case ticks for 9699586 will be received in full mode
    # While ticks for 9691394 and 9760514 will be received in quote mode.

    Also the default mode is quote. You need to explicitly change the mode to full for any and all instruments for which you want to receive the market depth.
  • r95990

    def on_ticks(ws, ticks):

    gets me the depth data, but the same thing behaves differently when

    def on_ticks(ws, ticks):

    def feed_data(ticks):

    here it does not print the depth data, rest of the code remains same!

    kws.set_mode(ws.MODE_FULL, subscribed_tokens) is my existing subs request. When I print the ticks directly in on_ticks, I can view the entire depth, but when send it to another function, I'm unable to view the depth data.
    Is there anything to be processed before I assign the data to feed_data.

    Thanks for taking the time MAG!
    Hope you are my savior!

  • MAG
    MAG edited February 2
    @r95990 the only way to properly debug this is to print a tick as its received by on_ticks and then print the very same tick passed on to the feed_data method.
    In order to format the output better, add the following lines to the beginning of your file
    import pprint #you may need to install the module with pip3 install pprint
    pp = pprint.PrettyPrinter( indent = 4 )

    next just subscribe to one instrument and set mode = full

    Then change your code to
    def on_ticks(ws, ticks):
    pp.pprint( ticks )
    feed_data( ticks )

    def feed_data( ticks1 ):
    print( "%%% Printing from feed_data %%%" )
    pp.pprint( ticks1 )

    Do this, run the code and copy paste the output
    Note I have changed the variable name in feed_data just to rule out any variable scope issues.
Sign In or Register to comment.