How to get historical prices?

I found in few topics that to getting coin prices in past I need to use /api/v3/klines but I can’t understand how I should use it properly. Let’s say I want to know BTCUSDT price in 01.01.2021 10:00 AM.

  1. Do I need to set start time 1h before and end time1h after 10:00 and set 1m interval, then iterate all results to find closest to my time? Or maybe some more practical way of to do this?
  2. Is it possible that it will be empty result? Should I then extend range of start/end time?
    Many thanks

Assuming you only want to know the price at 01.01.2021 10:00AM UTC+0, and not in a range.

Start by converting the start time to milliseconds since UNIX Epoch.

01.01.2021 10:00AM UTC+0 = 1609495200000

Then call the API<symbol>&interval=1m&startTime=1609495200000&limit=1

Is it possible that it will be empty result?

The method above will not return an empty result, unless the startTime is in the future, since the result is the next available Kline from the startTime provided, inclusive. This also implies that if the symbol was not generating klines (ex: platform was down, or symbol was not listed), which is very unlikely but possible, then the next available kline is provided. This next available kline can be any time in the future from the startTime provided, therefore, I would suggest checking the openTime of the kline to make sure the kline result is close to the startTime provided.

1 Like

The largest amount of candlesticks (historical data) that I have been able to obtain is 29,992, if we talk about 1min candlesticks we have that a single day has 1,440min, then in 1min candlesticks we would obtain 20.8 days (29,992 / 1440) which is less than a month of historical data so you will not be able to obtain the data of 01-01-21 and if you use that date as a startime parameter you will obtain the first available data and if today we are on 05-08-22 you would obtain a data of the day 17 or 18-07-22

1 Like