I am creation a Option chain using Two api
app.post('/option-chain', async (req, res) => {
try {
// Construct the data payload for the first API request
const data = JSON.stringify({
"name": req.body.data.stock,
"expirydate": req.body.data.expiry
});
// Combine CE and PE tokens from the request body
// const ceTokenValues = Object.values(req.body.ceToken).slice(2, -2);
// const peTokenValues = Object.values(req.body.peToken).slice(2, -2);
// const dataThis = [
// ...ceTokenValues,
// ...peTokenValues
// ];
const ceTokenValues = Object.values(req.body.ceToken);
const peTokenValues = Object.values(req.body.peToken);
const ceLength = ceTokenValues.length;
const peLength = peTokenValues.length;
// Calculate start and end indices for the middle 20 elements
const ceStartIndex = Math.floor((ceLength - 20) / 2);
const ceEndIndex = ceStartIndex + 20;
const peStartIndex = Math.floor((peLength - 20) / 2);
const peEndIndex = peStartIndex + 20;
// Get the middle 20 elements of the arrays
const middle20ce = ceTokenValues.slice(ceStartIndex, ceEndIndex);
const middle20pe = peTokenValues.slice(peStartIndex, peEndIndex);
const dataThis = [
...middle20ce,
...middle20pe
];
// Construct the data payload for the second API request
let data2 = JSON.stringify({
"mode": "FULL",
"exchangeTokens": {
"NSE": [],
"NFO": dataThis
}
});
console.log("this is data2",data2)
// Configure the first API request
let config = {
method: 'post',
maxBodyLength: Infinity,
url: 'https://apiconnect.angelbroking.com/rest/secure/angelbroking/marketData/v1/optionGreek',
headers: {
'X-PrivateKey': 'RVHXL1IU',
'X-SourceID': 'WEB',
'X-MACAddress': 'D0-37-45-BF-20-A1',
'Authorization': `Bearer ${apiData.data.jwtToken}`,
'Content-Type': 'application/json',
},
data: data
};
// Make the first API request
const response = await axios.request(config);
// Configure the second API request
let configTwo = {
method: 'post',
maxBodyLength: Infinity,
url: 'https://apiconnect.angelbroking.com/rest/secure/angelbroking/market/v1/quote/',
headers: {
'X-PrivateKey': 'RVHXL1IU',
'X-SourceID': 'WEB',
'X-MACAddress': 'D0-37-45-BF-20-A1',
'Authorization': `Bearer ${apiData.data.jwtToken}`,
'Content-Type': 'application/json',
},
data: data2
};
// Make the second API request
const responseTwo = await axios.request(configTwo);
console.log("responseTwo",responseTwo.data)
// Configure the second API request
// console.log("responseThree",responseThree.data)
// Modify response.data by adding a symbol to each option
response.data.data.forEach(option => {
const formattedStrikePrice = option.strikePrice.split('.')[0];
// Get the last two digits of the year in the expiry date
const expiryYear = option.expiry.slice(-2);
// Get the first three characters of the expiry month
const expiryMonth = option.expiry.slice(0, 5);
option.symbol = `${option.name}${expiryMonth}${expiryYear}${formattedStrikePrice}${option.optionType}`;
option.strikePrice = formattedStrikePrice;
});
let mergedData = [];
// Iterate through each item in responseTwo.data.data.fetched
responseTwo.data.data.fetched.forEach(itemTwo => {
// Find the corresponding item in response.data.data based on symbol
let itemOne = response.data.data.find(itemOne => itemOne.symbol === itemTwo.tradingSymbol);
// If a matching item is found, merge the data
if (itemOne) {
let mergedItem = { ...itemOne, ...itemTwo };
mergedData.push(mergedItem);
}
});
// Send the merged data in the response
res.status(200).send({ data: response.data, dataTwo: responseTwo.data, mergedData: mergedData });
} catch (error) {
console.error('API request error:', error.message);
res.status(500).send({ message: 'Failed to fetch option chain data' });
}
});
as you can see in mergedData. i am getting my option chain but ,i am facing some problem like, i want to store this data in my database so, i can show graph and other stuff for that, i need data of all day data there is no other api that giving me data of full day so bassicaly i need to store this data for every 10 second, but problem is there is 180 stock that i need to store and i can only store only one at a time and if i want to get data for 180 stock i need to calll these api so many time and it will give me error and also not effective so is there any way you can suggest me it will be help