I am having big difficulties in getting this done so I have to ask you guys for help now.
I am hitting the binance API endpoint for historical futurerates and I need all available data for every symbol in my array (from current to the very first created item).
The API offers query parameters and limits to do so "startTime" and "endTime" of type long timestamp in ms.
Here is the link to the docs [fundingrate api endpoint[1] [1]: https://binance-docs.github.io/apidocs/futures/en/#get-funding-rate-history
Using my approach, I am getting a bunch of results, but they randomly stop at some point in the past, so my pagination logic must be wrong. I just can not seem to find out where.
This is my current code
(async () => {
try {
const markets = symbols;
const targetDate = moment("2018-01-01"); //just one random past date I know it is for sure before any future contract creation on binance
for (let i = 0; i < markets.length; i++) {
let fundingRates = [];
symbol = markets[i];
let startTime, endTime, days_per_batch;
const initialResult = await binance.futuresFundingRate(symbol, { //make a request without a range to get a start and endpoint for pagination
limit: 1000,
});
startTime = moment(initialResult[0].fundingTime); //use the first retrieved item as startTime
endTime = moment(initialResult[initialResult.length - 1].fundingTime); //use last received item as endTime
days_per_batch = endTime.diff(startTime, "days"); //difference in days between first and last retrieved item of initial request
while (endTime.isAfter(targetDate)) {
const result = await binance.futuresFundingRate(symbol, {
startTime: startTime.unix(), //cast to timestamps
endTIme: endTime.unix(), //cast to timestamps
limit: 1000,
});
//concat retrieved result with result array that gets exported to csv
fundingRates = [].concat(
...result.map((e) => {
return {
symbol: e.symbol,
fundingRate: e.fundingRate,
fundingTime: moment(e.fundingTime),
};
})
);
//set the endTime to the previosu startTime and the startTime to startTime - days per batch (difference in days between first item and last item of result)
endTime = startTime;
startTime.subtract(days_per_batch, "days");
}
exportToCSV(symbol, fundingRates);
console.log("processing " + i + " / " + markets.length)
}
} catch (error) {
console.log(error);
}
})();