sammchardy / python-binance

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

get_historical_klines return different values for the same close candle #1246

Closed pepo205 closed 1 year ago

pepo205 commented 1 year ago

Describe the bug Im trying to get the most recent candle after close. For example 18:01, I want the 18 candle closed (interval 15 min). And the value that it is return, is different each time i run the script, until the next quarter (when the bug starts to be in the newest one)

To Reproduce client.get_historical_klines('BTCUSDT','15m', start_str='15 min ago', end_str='tomorrow', limit=1)

Expected behavior Every time the same close value

Environment (please complete the following information):

Logs or Additional context Add any other context about the problem here.

halfelf commented 1 year ago

IMHO, it is not a bug and the code works as expected. Please check open time and close time in return value to find out why.

pepo205 commented 1 year ago

Thanks for the answer halfelf, but there is no too much complexity in the open and close time to review. Here is how you can reproduce it, and the console output. You can see that the time is after the candle suppose to be closed

from binance.client import Client import datetime as dt import pandas as pd import time

config = open('config','r').read().splitlines()

client = Client(config[0], config[1])

print('Current time', dt.datetime.now())

data = pd.DataFrame(client.get_historical_klines('BTCUSDT','15m', limit=1, start_str='15 min ago', end_str='tomorrow'), columns=['Open Time','open', 'high','low','close','volume','6', '7', '8', '9', '10', '11'], dtype=float) data = data.drop(columns=[ '6', '7', '8', '9', '10', '11']) data['Open Time2'] = [dt.datetime.fromtimestamp(int(x)/1000.0) for x in data['Open Time']] print(data)

time.sleep(3)

data = pd.DataFrame(client.get_historical_klines('BTCUSDT','15m', limit=1, start_str='15 min ago', end_str='tomorrow'), columns=['Open Time','open', 'high','low','close','volume','6', '7', '8', '9', '10', '11'], dtype=float) data = data.drop(columns=[ '6', '7', '8', '9', '10', '11']) data['Open Time2'] = [dt.datetime.fromtimestamp(int(x)/1000.0) for x in data['Open Time']] print(data) `

and here is the output console

Current time 2022-09-07 09:31:59.312941 Open Time open high low close volume Open Time2 0 1.662536e+12 18758.13 18759.0 18742.95 18743.76 282.31399 2022-09-07 09:30:00 Open Time open high low close volume Open Time2 0 1.662536e+12 18758.13 18759.0 18742.95 18745.03 291.1601 2022-09-07 09:30:00

pepo205 commented 1 year ago

Ok, i was analyzing and the thing is that the candle opened 9.30 is not close until 9.45. Thats why the close value is changing all the time.