Skip to content

20 Market Depth

Level 3 data includes market depth upto 20 levels - this shows complete picture of the market movements and it is streamed real-time via websockets.

This data can be used to detect demand supply zones, outside of 5 level market depth and build trading systems to detect market movements.

Only NSE Equity and Derivatives segments are enabled for 20 Level Market Depth.

Similar to Live Market Feed, all request messages over WebSocket are in JSON whereas all response messages over WebSocket are in Binary.

Establishing Connection

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

wss://depth-api-feed.dhan.co/twentydepth?token=eyxxxxx&clientId=100xxxxxxx&authType=2


Query Parameters

Field Description
token
required
Access Token generated via Dhan
clientId
required
User specific identification generated by Dhan
authType
required
2 by Default

Adding Instruments

You can subscribe upto 50 instruments in a single connection and receive market data packets. For subscribing, this can be done using JSON message which needs to be sent over WebSocket connection.

Note

You can send all 50 instruments in a single JSON message. You can send multiple messages over a single connection as well to subscribe to all instruments in parts and receive data.

Request Structure

{
    "RequestCode" : 23,
    "InstrumentCount" : 2,
    "InstrumentList" : [
        {
            "ExchangeSegment" : "NSE_EQ",
            "SecurityId" : "1333"
        },
        {
            "ExchangeSegment" : "NSE_FNO",
            "SecurityId" : "532540"
        }
    ]
}


Parameters

Field Type Description
RequestCode
required
int Code for subscribing to particular data mode. 23 for 20 Level Market Depth.
Refer to feed request code to subscribe to required data mode
InstrumentCount
required
int No. of instruments to subscribe from this request
InstrumentList.ExchangeSegment
required
enum string Exchange Segment of instrument to be subscribed as found in Annexure
InstrumentList.SecurityId
required
string Exchange standard ID for each scrip. Refer 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.

20-Level Depth Packet

The market depth data is sent as structured binary packet. It will require parsing to readable format to extract the relevant information.

All responses from Dhan Market Feed consists of Response Header and Payload. Header for every response message remains the same with different feed response code, while the payload can be different.


Response Header

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

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

Depth Packet

Depth Data Packet for 20 level market depth is structured differently from 5 level depth. Over here, you will receive the bid (sell) and ask (buy) data packets separately, each containing 20 packets of 10 bytes each.

Bytes Type Size Description
0-12 [ ] array 8 Response Header
41 for Bid Data (Buy)
51 for Ask Data (Sell)
Refer to enum for values
13-332 Bid/Ask Depth Structure 320 20 packets of 16 bytes each for each instrument in below provided structure

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

Bytes Type Size Description
1-8 float64 8 Price
9-12 uint32 4 Quantity
13-16 uint32 4 No. of Orders

Feed Disconnect

If you want to disconnect WebSocket, you can send below JSON request message via the connection.

{
    "RequestCode" : 12
}

In case of WebSocket disconnection from server side, 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.


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

You can find detailed Disconnection message code description here.