sammchardy / python-binance

Binance Exchange API python implementation for automated trading
https://python-binance.readthedocs.io/en/latest/
MIT License
6.01k stars 2.2k forks source link

How to calculate the right timestamp to get klines history. Time calculation error with .get_historical_klines, get time difference of 2 hours. #760

Open lordAnubis opened 3 years ago

lordAnubis commented 3 years ago

Describe the bug If I request the server to get 560 '1 minute' klines back from the current time ( timestamp server ) I get always for two hours less. Is this a .get_historical_klines error? Is it using the wrong time UTC?

To Reproduce

import binance.client as bCl
from binance.client import Client
from binance.exceptions import BinanceAPIException, BinanceOrderException

bin_client = bCl.Client(API_KEY, API_SECRET)
RSI_PERIOD = 14

def getKLinesAndStats(_binance_client, _trade_symbol, _klines_period, _klines_number_of_periodes):
        _timeStampServer = int(_binance_client.get_server_time()['serverTime'] / 1000)
        print(_timeStampServer)

        # get the first trade action timestamp 
        _first_timestamp = _binance_client._get_earliest_valid_timestamp(_trade_symbol, _klines_period) / 1000

        if _klines_number_of_periodes == -1:             # get them all
            _start_download_timestamp = _first_timestamp
        elif _klines_number_of_periodes == 0:           # just return empty
            return None
        elif _klines_number_of_periodes > 0:             # calc the timestamp delta
            if _klines_period == '1m':
                _seconds = 60

            elif klines_period == '3m':
                _seconds = 180
            else:
                pass # for demo
        else:
            pass     # for demo      

        # timestamp difference from now to the past to get the requested amount of klines
        timestampDeltaFromNow = _klines_number_of_periodes * _seconds
        print(timestampDeltaFromNow)

        _start_download_timestamp = round(_timeStampServer - timestampDeltaFromNow)
        print(_start_download_timestamp) # the start timestamp

        if _first_timestamp > _start_download_timestamp:
            _start_download_timestamp = _first_timestamp

        klines = bin_client.get_historical_klines(_trade_symbol, _klines_period, str(_start_download_timestamp), limit=1000)
        print(len(klines))
    else:
        return None

    return klines

# klines_number_of_periodes = 10
klines_number_of_periodes = RSI_PERIOD * 40
print(klines_number_of_periodes)
bars = getKLinesAndStats(bin_client, 'ADAEUR', '1m', klines_number_of_periodes)

Expected behavior Instead of 440 kline I expect 560 klines. Missing 120 seconds/kline what is two hours. This happens with all the timestamps that I use. If under the 2 hours I get nothing.

Environment (please complete the following information):

Logs or Additional context

if I use klines_number_of_periodes = 10 then the result is 10 1617708026 600 1617707426 0 The difference between the two timestamps is 600 seconds, but the reult is 0 klines

if I use klines_number_of_periodes = RSI_PERIOD * 40 then the result is 560 1617708402 33600 1617674802 440 missing 120 klines == two hours.

Why is this? The serve time is utc = 0 I guess My time is utc = -1 It looks like something is using utc = -2

What is going on?

I could add two hours on seconds on the delta, but.... not the way.

lordAnubis commented 3 years ago

Is it posable that someone can confirm this bug? Thank you.

lordAnubis commented 3 years ago

Anyone?