Binance Python Spot API returns wrong data for certain timestamps

I came across a weird problem with Binance API. There are 438 ETH and 434 BTC timestamps for which the API returns the wrong data. For ETH, some of them are:

  • 2017-09-06 16:01:00
  • 2017-12-04 06:01:00
  • 2017-12-18 10:01:00
  • 2017-12-18 12:30:00

When I request the data for one of these defunct timestamps, the Binance response returns a different timestamp, while for all others, the response timestamp is the same as the request timestamp.

I put together the simplest reproducible example to illustrate the difference in Binance’s response for normal and defunct timestamps.

Code:

import json
from binance.spot import Spot
from datetime import timezone
from datetime import datetime
import numpy as np

f = open('../data.json')

cred = json.load(f)
key = cred['key']
secret = cred['secret']

client = Spot(key=key, secret=secret)

def to_milliseconds(time):
    return int(np.datetime64(time, 'ms').astype('int64'))


def to_timestamp(time):
    return np.datetime64(datetime.fromtimestamp(int(time) / 1000, tz=timezone.utc), 'm')

normal_timestamp = "2018-05-02T10:00"
defunct_timestamp = "2017-09-06T16:01"

for id, timestamp in enumerate([normal_timestamp, defunct_timestamp]):

    start_time = np.datetime64(timestamp, 'm')

    start_time_ms = to_milliseconds(start_time)

    ticker = client.klines(symbol='ETHUSDT', interval='1m', limit=1, startTime=start_time_ms)

    return_time_ms = ticker[0][0]

    print('Case {id}. Request time: {request_time} (in ms: {request_time_in_ms}). Response time: {response_time} (in ms: {response_time_in_ms})'.format(
        id=id,
        request_time=start_time,
        request_time_in_ms=start_time_ms,
        response_time=to_timestamp(return_time_ms),
        response_time_in_ms=return_time_ms
    ))

Output:

Case 0. Request time: 2018-05-02T10:00 (in ms: 1525255200000). Response time: 2018-05-02T10:00 (in ms: 1525255200000)
Case 1. Request time: 2017-09-06T16:01 (in ms: 1504713660000). Response time: 2017-09-06T23:00 (in ms: 1504738800000)

Did someone encounter that too? What would be the reason for that?

Sorry, can’t understand the code.

  • How is the result printed?
  • Not seeing any method is called from client

Hi Dino,

Posting here as a response to your comment, hope you’ll get notified :slight_smile:

This code illustrates that for some time stamps, the klines data is requested for one time but returned for completely another.

The output Case 1 illustrates that. The code requests the klines data for 2017-09-06T16:01, but Binance API returns the data for 2017-09-06T23:00.

> How is the result printed?

The code prints the request time passed to the Binance API (as a timestamp and in milliseconds) and the Binance API response time (as a timestamp and in milliseconds). It should be the same, and it is in almost all cases (see Case 0 output).

But for some timestamps, the response timestamp is different from the requested (see Case 1 output).

I wonder why Binance API behaves differently for these ‘defunct’ time stamps, like Case 1?

> Not seeing any method is called from client

Please refer to the line in for loop:

ticker = client.klines(symbol='ETHUSDT', interval='1m', limit=1, startTime=start_time_ms)