Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

114
Visualizações
Query paginated API endpoint from end to the very first item by providing start and endtime parameters

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);
 }
  })();
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda