Skip to content

Live Market Feed

The marketfeed 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.marketfeed

The marketfeed 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

MarketFeed

MarketFeed(dhan_context, instruments, version='v1')

WebSocket URL for DhanHQ Live Market Feed

Attributes
BSE_FNO class-attribute instance-attribute
BSE_FNO = 8

Constants for Request Code

market_feed_wss class-attribute instance-attribute
market_feed_wss = 'wss://api-feed.dhan.co'

Constants for Exchange Segment

Functions
authorize async
authorize()

Establishes the WebSocket connection and authorizes the user

close_connection
close_connection()

Close WebSocket connection with this.

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 initiates process for conversion.

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 initiate processing in received format

process_full
process_full(data)

Parse and process Full Packet Data

process_market_depth
process_market_depth(data)

Parse and process Market Depth Data

process_oi
process_oi(data)

Parse and process OI Data

process_prev_close
process_prev_close(data)

Parse and process Previous Day Data

process_quote
process_quote(data)

Parse and process Quote Data

process_status
process_status(data)

Parse and process market status

process_ticker
process_ticker(data)

Parse and process Ticker Data

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=100)

Create a list of all instruments to be added and add in batches of 100