Live Market Depth Metrics Using Dhan_Tradehull

Live Market Depth Metrics Using Dhan_Tradehull (Bid/Ask, Midprice, Imbalance)

Market depth is useful when watching real order-flow around an option strike. With Dhan_Tradehull, it’s possible to fetch the full market depth for an instrument and compute quick micro-structure metrics like best bid/ask, midprice, order-book imbalance, and weighted midprice.

Below example pulls depth for NIFTY 19 JAN 25900 CALL (NFO) and continuously reads the top-of-book.

What it calculates:
• Best bid price and bid quantity
• Best ask price and ask quantity
• Midprice (average of best bid and ask)
• Order-book imbalance (bid dominance vs ask dominance)
• Weighted midprice (mid adjusted by imbalance)

from Dhan_Tradehull import Tradehull
import time
import datetime
import pdb

client_id    = "1107245360"
access_token = "YOUR_ACCESS_TOKEN"
tsl          = Tradehull(client_id, access_token)

depth_data = tsl.full_market_depth_data(("NIFTY 19 JAN 25900 CALL", "NFO"))

while True:

    bid_df, ask_df = tsl.get_market_depth_df(depth_data['NIFTY 19 JAN 25900 CALL|NFO'])

    bid = bid_df.iloc[0]
    ask = ask_df.iloc[0]

    bid_price = bid.bid_price
    bid_qty   = bid.bid_qty
    ask_price = ask.ask_price
    ask_qty   = ask.ask_qty

    midprice          = (bid_price + ask_price) / 2
    imbalance         = bid_qty / (bid_qty + ask_qty)
    weighted_midprice = bid_price * (1 - imbalance) + ask_price * imbalance

This is helpful for:
• Tracking liquidity and spread changes
• Spotting bid/ask dominance shifts
• Building execution filters (avoid wide spreads, low depth)
• Entry timing confirmation for option scalping systems