API return data not match with tradingview ?

Hi All,
I use this code to get OCHLV at 1h time, but Volume not correct with tradingview. Why and How to fix ??

const axios = require('axios');
const moment = require('moment');
const fs = require('fs');

const start = moment('2022-04-25').utc().startOf('day');
const end = moment('2023-04-26').utc().endOf('day');
const symbol = 'BTCUSDT';
const interval = '1h';
const minVolume = 500;

const url = `https://fapi.binance.com/fapi/v1/klines?symbol=${symbol}&interval=${interval}&startTime=${start.valueOf()}&endTime=${end.valueOf()}`;

axios.get(url)
  .then(response => {
    const klines = response.data.filter(kline => parseFloat(kline[5]) >= minVolume);
    
    const greaterThanTenK = response.data.filter(arr => Number(arr[5]) > minVolume);
    //Array count = greaterThanTenK.length;
    console.log(klines);
    const rows = klines.map(kline => {
      return {
        timestamp: moment(kline[0]).format('YYYY-MM-DD HH:mm:ss'),
        open: parseFloat(kline[1]),
        high: parseFloat(kline[2]),
        low: parseFloat(kline[3]),
        close: parseFloat(kline[4]),
        volume: parseFloat(kline[5])
      };
    });

    console.log(rows);

    const csv = 'Timestamp,Open,High,Low,Close,Volume\n' + rows.map(row => `${row.timestamp},${row.open},${row.high},${row.low},${row.close},${row.volume}`).join('\n');

    fs.writeFile('BTCUSDT_Klines.csv', csv, err => {
      if (err) {
        console.error(err);
      } else {
        console.log('BTCUSDT_Klines.csv saved!');
      }
    });
  })
  .catch(error => {
    console.error(error);
  });

image

Hi,

From looking at your screenshots, the data is in fact correct. It looks like TradingView just rounds the volume to the nearest whole number. So 7,097.571 == 7.098k

1 Like

Ok, thank you very much !

Dear Mr.Jonte,
I want to get data during time:

const start = moment('2022-04-25').utc().startOf('day');
const end = moment('2023-04-26').utc().endOf('day');

image
image

But only start(2022-04-24 7:00) - end(2022-05-15 2:00) data received. Why ? And how to fix ?
And Volume data is BTC or USDT ?

Hi @Mr_Engineer,

The reason is there is a limit on the number of records that are returned in each API response. By default, klines endpoint will return 500 records. The maximum you can configure is 1500 records (Binance API Documentation). The way you can fetch more data is either by downloading it manually here: https://data.binance.vision or by utilising the startTime and endTime parameters to incrementally fetch data by programmatically adjusting the startTime and endTime on each subsequent API call.

Hope that helps. Cheers,

1 Like