API Ver 2.0
Please help how to get the Previous day closing price using market feed
As per the API documentation whenever any instrument is subscribed for any data packet, also Prev Close packet is sent which has Previous Day data.
But when I convert the byte i get some random integer number instead of last day close price. Also the datatype is supposed to be a float32 value rather than int32
Find the attached screenshot of the API documentation and also the screenshot of the console log.
Find the code below for reference:
if (responseCode === 6 && messageLength === 16) {
const securityId = dataView.getInt32(4, true); // Assuming securityId at offset 4
const prevClose = dataView.getInt32(8, true); // Offset 8
const openInterest = dataView.getInt32(12, true); // Offset 12
console.log(openInterest, prevClose, securityId)
@Hardik please guide and help.
Hardik
November 5, 2024, 10:16am
2
Hello @Rajkaran_Mishra
Welcome to MadeForTrade community!
Let me try and replicate this. The code looks good, so have to try it once.
Thanks for your greetings Hardik, Here’s the full code snippet of mine, if you’d like to try it by yourself.
if (packetype === 1) { // PacketType 1 == 'Ticker'
if (messageLength === 16 && responseCode === 2) { // ResponseCode 2 mean data for LTP (Last Traded Price)
const securityId = dataView.getInt32(4, true); // Assuming securityId at offset 4
const lastTradedPrice = dataView.getFloat32(8, true);
const lastTradedTime = dataView.getInt32(12, true);
const instrumentIndex = instrumentList.findIndex(inst => inst.SecurityId === String(securityId));
if (instrumentIndex !== -1) {
setLiveData((prevData) => {
const newData = [...prevData]; // Copy the previous state
newData[instrumentIndex] = { responseCode, lastTradedPrice, lastTradedTime, securityId }; // Update the specific index
return newData; // Return the updated state
});
} else {
console.warn('Received data for an untracked instrument.');
}
} else if (responseCode === 6 && messageLength === 16) { // responseCode 6 means previous day data.
const securityId = dataView.getInt32(4, true); // Assuming securityId at offset 4
const prevClose = dataView.getInt32(8, true); // Offset 8
console.log(prevClose)
const instrumentIndex = instrumentList.findIndex(inst => inst.SecurityId === String(securityId));
const openInterest = dataView.getInt32(12, true); // Offset 12
if (instrumentIndex !== -1) {
setPrevData((prevData) => {
const newData = [...prevData]; // Copy the previous state
newData[instrumentIndex] = { prevClose, securityId, openInterest }; // Update the specific index
return newData; // Return the updated state
});
}
}
else {
console.warn(`Expected message length of 16, but received ${messageLength}.`);
}
}
Looking forward to Solve this issue.
Hey Hardik,
What’s the update about the issue?
Do update me if you need anything from my side, so we can close this issue asap.
Hardik
November 7, 2024, 4:01am
5
@Rajkaran_Mishra
Can you try with previous close as float 32 instead of int 32? I checked on Python and the data being parsed is correct.
Hey Hardik,
Its was great help from your side, the issue has been resolved. Now data it decoding properly and we can calculate change.
And please update the docs, it would save other developers time too.
Thanks Again.
1 Like
Hardik
November 7, 2024, 7:03am
7
Yes, will update that. Great to know it worked out!