Hi @Santosh, You get binary data in websocket. Please take a look at documentation for more details about packet structue. For more details about parsing, you can take a look at any Kite Connect client code.
We are doing parsing for you. You can use endpoint provided by us after doing all parsing. You can take a look at usage example here.
Hi @sujith , Thanks for quick response. I got it figured out. So for the sake of anyone who face same issues and program in c++ or elsewhere( where packed data support is not available): here are the important points: 1. What you receive on websocket is binary data in ASCII Hex format. 2. It is a packed data....as in Python/Perl...maybe due to Zerodha's back-end processing technology/scripts 3. The data is not sent on a binary frame basis but as a consolidated packet. 4. The easiest way to parse is to use a stream object operating on bytearray. 5. You need to utilize message structure to split and parse this data as provided in Kite connect documentation. 6. To achieve this utilize streamObject methods. 7. Zerodha documentation says"The first two bytes ([0 - 2] – SHORT or int16) represent the number of packets in the message."------ Don't try to read the first two bytes...The approach which works is stream In>> a variable of type int16
I am able to successfully parse all data in websocket call except "orders" of Market Depth.
Kite connect doc mentions: First 2 bytes = number of packets next 2 bytes = length of packet
Packet Structure mentions: a "full" packet is 164 bytes out of which first 44 bytes are quotes and remaining 120 bytes are byte array of Market Depth. Market Depth contain 5 levels each of bid and offer. So each level is 12 byte...which in turn contain 3 data each of 4 bytes.
If I send request for 1 instrument in "full" mode, the response is 1 "full" packet. So the received data is: 2+2+164 = 168
This means that after index 2+2+44+4+4 = 56 i.e. starting at 57 and spanning 4 bytes I should get "number of orders" for topmost bid.
This is what I do: //attach stream object dataStream in(data)
//fake read till the required byte is reached in.skipRawData(56);
//allocate a buffer byteArray buffer; buffer.resize(4);
//fill buffer with required amount of data in.readRawData(buffer.data(),4);
//cast data to a meaningful variable int32 orders = buffer.data; cout<<orders;
This works for all other items including all the 10 market depth, but gives wrong result for all "orders". I tested with 2 scrips EURINR APR FUT and INFY APR FUT and both had the issue.
So either I am missing something or there is an error in sent data.
This means that after index 2+2+44+4+4 = 56 i.e. starting at 57 and spanning 4 bytes I should get "number of orders" for topmost bid.
Orders are 2 bytes. So span only 2 bytes.
Parsing depth is something like this,
Depth depth = new Depth(); depth.setQuantity((int)convertToDouble(getBytes(depthBytes, s, s + 4))); depth.setPrice(convertToDouble(getBytes(depthBytes, s + 4, s + 8))/dec); depth.setOrders((int)convertToDouble(getBytes(depthBytes, s + 8, s + 10)));
You get binary data in websocket. Please take a look at documentation for more details about packet structue.
For more details about parsing, you can take a look at any Kite Connect client code.
We are doing parsing for you. You can use endpoint provided by us after doing all parsing.
You can take a look at usage example here.
Thanks for quick response. I got it figured out. So for the sake of anyone who face same issues and program in c++ or elsewhere( where packed data support is not available): here are the important points:
1. What you receive on websocket is binary data in ASCII Hex format.
2. It is a packed data....as in Python/Perl...maybe due to Zerodha's back-end processing technology/scripts
3. The data is not sent on a binary frame basis but as a consolidated packet.
4. The easiest way to parse is to use a stream object operating on bytearray.
5. You need to utilize message structure to split and parse this data as provided in Kite connect documentation.
6. To achieve this utilize streamObject methods.
7. Zerodha documentation says"The first two bytes ([0 - 2] – SHORT or int16) represent the number of packets in the message."------ Don't try to read the first two bytes...The approach which works is stream In>> a variable of type int16
Thanks.
Kite connect doc mentions:
First 2 bytes = number of packets
next 2 bytes = length of packet
Packet Structure mentions: a "full" packet is 164 bytes out of which first 44 bytes are quotes and remaining 120 bytes are byte array of Market Depth. Market Depth contain 5 levels each of bid and offer. So each level is 12 byte...which in turn contain 3 data each of 4 bytes.
If I send request for 1 instrument in "full" mode, the response is 1 "full" packet. So the received data is: 2+2+164 = 168
This means that after index 2+2+44+4+4 = 56 i.e. starting at 57 and spanning 4 bytes I should get "number of orders" for topmost bid.
This is what I do:
//attach stream object
dataStream in(data)
//fake read till the required byte is reached
in.skipRawData(56);
//allocate a buffer
byteArray buffer;
buffer.resize(4);
//fill buffer with required amount of data
in.readRawData(buffer.data(),4);
//cast data to a meaningful variable
int32 orders = buffer.data;
cout<<orders;
This works for all other items including all the 10 market depth, but gives wrong result for all "orders".
I tested with 2 scrips EURINR APR FUT and INFY APR FUT and both had the issue.
So either I am missing something or there is an error in sent data.
Kindly help.
Thanks.
Parsing depth is something like this, where depthBytes is
byte[] depthBytes = getBytes(bin, 44, 164);
and dec = 10000000 for CDS or 100 for others.
I really appreciate your support. I was not expecting a response at this hour.
Thanks a lot.