Skip to content

Full Market Depth

The FullDepth class is designed to facilitate asynchronous communication with the DhanHQ API via WebSocket. It enables users to subscribe to market data for a list of instruments and receive real-time updates.

:copyright: (c) 2025 by Dhan. :license: see LICENSE for details.

dhanhq.fulldepth

The FullDepth class is designed to facilitate asynchronous communication with the DhanHQ API via WebSocket. It enables users to subscribe to market data for a list of instruments and receive real-time updates.

:copyright: (c) 2025 by Dhan. :license: see LICENSE for details.

Classes

FullDepth

FullDepth(dhan_context, instruments)

WebSocket URL for DhanHQ Live Market Feed

Attributes
NSE_FNO class-attribute instance-attribute
NSE_FNO = 2

Constants for Request Code

depth_feed_wss class-attribute instance-attribute
depth_feed_wss = 'wss://depth-api-feed.dhan.co/twentydepth'

Constants for Exchange Segment

Functions
close_connection
close_connection()

Close WebSocket connection with this.

combine_and_format_depth
combine_and_format_depth(bid_data, ask_data)

Combine and sort bid/ask data, then format for display

connect async
connect()

Initiates the connection to the Websockets.

create_header
create_header(feed_request_code, message_length, client_id)

Creates header packet for the subscription packet.

create_subscription_packet
create_subscription_packet(instruments, feed_request_code)

Creates the subscription packet with specified instruments and subscription code

disconnect async
disconnect()

Closes the WebSocket connection and sends a disconnect message.

get_data
get_data()

Fetch instruments data while the event loop is open.

get_exchange_segment
get_exchange_segment(exchange_code)

Convert numeric exchange code to string representation

get_instrument_data async
get_instrument_data()

Fetches data and processes messages one at a time

on_connection_opened async
on_connection_opened(websocket)

Callback function executed when the WebSocket connection is opened.

pad_with_zeros
pad_with_zeros(data, length)

Pads a binary data string with zeros to a specified length for server to read.

process_data
process_data(data)

Read binary data and process messages one at a time

process_depth_data
process_depth_data(data, is_bid=True)

Parse and process depth data for both bid and ask

run_forever
run_forever()

Starts the WebSocket connection and runs the event loop.

server_disconnection
server_disconnection(data)

Parse and process server disconnection error

subscribe_instruments async
subscribe_instruments()

Subscribe Instruments on the Open Websocket

subscribe_symbols
subscribe_symbols(symbols)

Function to subscribe to additional symbols when connection is already established.

unsubscribe_symbols
unsubscribe_symbols(symbols)

Function to unsubscribe symbols from connection when connection is already active.

utc_time
utc_time(epoch_time)

Converts EPOCH time to UTC time.

validate_and_process_tuples
validate_and_process_tuples(tuples_list, batch_size=50)

Create a list of all instruments to be added and a maximum of 50 instruments in single socket