Hi!
I’m trying to fetch daily historical OHLC data using the DhanHQ API, but I keep getting the “DH-905: Missing required fields, bad values for parameters etc.” error.
I’ve double-checked my code, and I’m not sure what’s causing the issue.
Here’s the relevant part of my code:
const accessToken = 'my_actual_access_token';
const clientId = 'my_actual_client_id';
function fetchHistoricalOpenData() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName('BSE Ltd');
const instrumentId = 19585;
const fromDate = Utilities.formatDate(sheet.getRange('C1').getValue(), ss.getSpreadsheetTimeZone(), 'yyyy-MM-dd');
const toDate = Utilities.formatDate(sheet.getRange('D1').getValue(), ss.getSpreadsheetTimeZone(), 'yyyy-MM-dd');
const options = {
'method': 'post',
'headers': {
'Accept': 'application/json',
'access-token': accessToken
},
'muteHttpExceptions': true,
'payload': JSON.stringify({
'securityId': `${instrumentId}`,
'exchangeSegment': 'NSE_EQ',
'instrument': 'EQUITY',
'expiryCode': 0,
'fromDate': fromDate,
'toDate': toDate
})
};
try {
const response = UrlFetchApp.fetch(apiUrl, options);
if (response.getResponseCode() !== 200) {
Logger.log(`Error: API request failed with response code ${response.getResponseCode()}`);
Logger.log(response.getContentText());
return;
}
const data = JSON.parse(response.getContentText());
// Check if 'open' array exists and has data
if (data.open && data.open.length > 0) {
const outputData = data.open.map(openPrice => [openPrice]); // Create a 2D array for open prices
sheet.getRange(3, 2, outputData.length, outputData[0].length).setValues(outputData);
} else {
Logger.log("No historical open data found for this instrument and date range.");
Logger.log(data);
}
} catch (error) {
Logger.log("Error fetching or parsing historical data: " + error);
}
}
I have already double-checked everything, but still getting the errors.
Any help would be greatly appreciated!