Skip to content

Live Market Feed

Real-time Market Data across exchanges and segments can now be availed on your system via WebSocket. WebSocket provides an efficient means to receive live market data. WebSocket keeps a persistent connection open, allowing the server to push real-time data to your systems.

All Dhan platforms work on these same market feed WebSocket connections that deliver lightning fast market data to you.

You can establish upto five WebSocket connections per user with unlimited instruments on each connection.

All request-response messages over WebSocket are in Binary. You will require WebSocket library in any programming language to be able to use Live Market Feed along with Binary converter.


Using DhanHQ Libraries for WebSockets


- You can use DhanHQ Python Library to quick start with Live Market Feed.


Establishing Connection

To establish connection with DhanHQ WebSocket for Market Feed, you can send request binary packet to the below endpoint using WebSocket library.

wss://api-feed.dhan.co

While establishing connection, you need to send Authorisation Packet in binary for each feed connection.

Establishing Connection


Understanding Binary Message

Binary messages consist of sequences of bytes that represent the data. This contrasts with text messages, which use character encoding (e.g., UTF-8) to represent data in a readable format. Binary messages require parsing to extract the relevant information.

The reason for us to choose binary messages over text or JSON is to have compactness, speed and flexibility on data to be shared at lightning fast speed.

All requests on Dhan consists of Header and Payload. Header for every request message remains the same, while the payload can be different.


Request Header

The header message is of 83 bytes which will remain same as part of all the requests messages. The message structure is given as below.

Bytes Type Size Description
1 int8 1 Feed Request Code can be referred in Annexure
11 to connect new feed
12 to diconnect existing feed
2-3 int16 2 Message Length of the entire payload packet
4-33 [ ] byte 30 Client ID - User specific identification generated by Dhan
34-83 [ ] byte 50 Dhan Auth - to be passed as zero

Authorisation

Authorisation Packet will consist of the following binary message which will also include Header as given above.

Bytes Type Size Description
0-83 Header 83 Binary Header message as specified above
84-583 [ ] byte 500 API Access Token
584-585 [ ] byte 2 Authentication Type - 2P by default
Bytes in Binary Message Structure


In binary message structure, if the request parameter value does not consume the entire byte size, you will still be required to send the entire byte size as specified for the receiving server to read the request message.

For example, in case of Client ID of 10 digit, you need to convert it to binary message of 30 bytes. In case the byte size is less, you can add zeroes to ensure byte size remains same.


Adding Instruments

You can subscribe unlimited instruments in a single connection and receive market data packets. For subscribing, this can be done using Bulk Subscribe request which is default method of subscribing upto 100 instruments in single request over Live Market Feed.

Request Structure

Bytes Type Size Description
0-83 Header 83 Binary Header message as specified above.
Refer to enum to subscribe to required packet type
84-87 int 4 Number of Instruments to subscribe
88-2187 [ ] byte 2100 100 packets of 21 bytes each for each instrument.
Can be passed as zero for bytes which is not utilised

Each packet of 21 bytes should be in the followings structure:

Bytes Type Size Description
0-1 int8 1 Exchange Segment of the instrument
Refer to enum for values
2-21 [ ] byte 20 Security ID - can be found here

Keeping Connection Alive

To keep the WebSocket connection alive and prevent it from closing, the server side uses Ping-Pong module. Server side sends ping every 10 seconds to the client server (in this case, your system) to maintain WebSocket status as open.

In case the client server does not respond for more than 40 seconds, the connection is closed from server side and you will have to reestablish connection.

Feed Disconnect

In case of WebSocket disconnection due to any reason, you will receive disconnection packet, which will have disconnection reason code.

  • If more than 5 websockets are established, then the first socket will be disconnected with 805 with every additional connection.

  • Authorisation is done asynchronously after the websocket is connected. If the authorisation fails, then the socket is closed later.


Bytes Type Size Description
0-8 [ ] array 8 Response Header with code 50
Refer to enum for values
9-10 int16 2 Disconnection message code

Disconnection Message

Code Description
805 Connection limit exceeded
806 Data APIs not subscribed
807 Access token is expired
808 Authentication Failed - Check Client ID
809 Access token is invalid

Market Data

Like request structure while establishing connection and subscribing to instruments, the Market Feed data is also sent as structured binary packets.

DhanHQ Live Market Feed is real-time data and there are four modes in which you can receive the data, depending on your use case:

Subscribing Instruments

Response Header

The response header message is of 8 bytes which will remain same as part of all the response messages. The message structure is given as below.

Bytes Type Size Description
1 [ ] byte 1 Feed Response Code can be referred in Annexure
2-3 int16 2 Message Length of the entire payload packet
4 [ ] byte 1 Exchange Segment can be referred in Annexure
5-8 int32 4 Security ID - can be found here

Ticker Packet

This packet consists of Last Traded Price (LTP) and Last Traded Time (LTT) data across segments.

Bytes Type Size Description
0-8 [ ] array 8 Response Header with code 2
Refer to enum for values
9-12 float32 4 Last Traded Price of the subscribed instrument
13-16 int32 4 Last Trade Time

Prev Close

Whenever any instrument is subscribed for any data packet, we also send this packet which has Previous Day data to make it easier for day on day comparison.

Bytes Type Size Description
0-8 [ ] array 8 Response Header with code 6
Refer to enum for values
9-12 int32 4 Previous day closing price
13-16 int32 4 Open Interest - previous day
Along with Previous Close packet, you will also receive Market Status packet which is a notification on WebSocket. This notifies only on market Open and Close and message code for this packet is 7.


Quote Packet

This data packet is for all instruments across segments and exchanges which consists of complete trade data, along with Last Trade Price (LTP) and other information like update time and quantity.

Bytes Type Size Description
0-8 [ ] array 8 Response Header with code 4
Refer to enum for values
9-12 float32 4 Latest Traded Price of the subscribed instrument
13-14 int16 2 Last Traded Quantity
15-18 int32 4 Last Trade Time (LTT)
19-22 float32 4 Average Trade Price (ATP)
23-26 int32 4 Volume
27-30 int32 4 Total Sell Quantity
31-34 int32 4 Total Buy Quantity
35-38 float32 4 Day Open Value
39-42 float32 4 Day Close Value - only sent post market close
43-46 float32 4 Day High Value
47-50 float32 4 Day Low Value

OI Data

Whenever you subscribe to Quote Data, you also receive Open Interest (OI) data packets which is important for Derivative Contracts.

Bytes Type Size Description
0-8 [ ] array 8 Response Header with code 5
Refer to enum for values
9-12 int32 4 Open Interest of the contract


Market Depth Packet

Market Depth data, upto 5 best bid - best ask, can also be subscribed on the same feed connection for all instruments across exchanges and segments.

Bytes Type Size Description
0-8 [ ] array 8 Response Header with code 3
Refer to enum for values
9-12 float32 4 Latest Traded Price of the subscribed instrument
13-112 Market Depth Structure 100 5 packets of 20 bytes each for each instrument in below provided structure

Each of these 5 packets will be received in the following packet structure:

Bytes Type Size Description
1-4 int32 4 Bid Quantity
5-8 int32 4 Ask Quantity
9-10 int16 2 No. of Bid Orders
11-12 int16 2 No. of Ask Orders
13-16 float32 4 Bid Price
17-20 float32 4 Ask Price