I am able to fetch BANK NIFTY, live data but please guide me in how to collect 5 min candle high, open, low, close info from that live data.
How to calculate 5 min candle high, low, close, open from the data we received from live web socket?
You have to save the LTP data and create 1 minute OHLC for every minute live data at your end and convert to 5 minute. We dont provide ready 5 mins data.
Many methods are available for this. 1 method you can use this way
let currentCandle = null;
let intervalID = null;
let candles = ;
function processIndexLtpPacket(dvu:DataView,callback:Function) {
const price = dvu.getFloat32(position.value, true);
const security_id = dvu.getInt32(position.value + 8, true);
const timestamp = dvu.getFloat32(position.value + 4, true);
if (!currentCandle) {
currentCandle = {
open: price,
high: price,
low: price,
close: price,
startTime: timestamp
};
// Set interval to close the current candle and start a new one after 5 minutes
intervalID = setInterval(function() {
currentCandle.endTime = new Date(); // The end time of the candle is now
candles.push(currentCandle); // Push the completed candle to the array
// Start a new candle
currentCandle = {
open: price,
high: price,
low: price,
close: price,
startTime: new Date() // The start time of the new candle is now
};
}, 5 * 60 * 1000); // 5 minutes in milliseconds
} else {
// If the price is higher than the current high, update the high
if (price > currentCandle.high) {
currentCandle.high = price;
}
// If the price is lower than the current low, update the low
if (price < currentCandle.low) {
currentCandle.low = price;
}
// The current price is always the close price
currentCandle.close = price;
}
};
@rajilesh
can you please provide a proper formatted code it will be easy to understand.