python-binance icon indicating copy to clipboard operation
python-binance copied to clipboard

get_historical_klines last candle stick close price

Open cutlersswim opened this issue 4 years ago • 3 comments

Describe the bug close price on last candle stick is always wrong when using get_historical_klines eg. currently 10.14 UTC

kline time stamp - 10.10UTC get klines close price - 2119.81000000 trading view close price -2122.16

To Reproduce

import pytz
import dateparser
from datetime import datetime
from binance.client import Client

binance_api_key = config.API_KEY    #Enter your own API-key here
binance_api_secret = config.API_SECRET #Enter your own API-secret here

binance_client = Client(api_key=binance_api_key, api_secret=binance_api_secret)
def date_to_milliseconds(date_str):
    """Convert UTC date to milliseconds
    If using offset strings add "UTC" to date string e.g. "now UTC", "11 hours ago UTC"
    See dateparse docs for formats http://dateparser.readthedocs.io/en/latest/
    :param date_str: date in readable format, i.e. "January 01, 2018", "11 hours ago UTC", "now UTC"
    :type date_str: str
    """
    # get epoch value in UTC
    epoch = datetime.utcfromtimestamp(0).replace(tzinfo=pytz.utc)
    # parse our date string
    d = dateparser.parse(date_str)
    # if the date is not timezone aware apply UTC timezone
    if d.tzinfo is None or d.tzinfo.utcoffset(d) is None:
        d = d.replace(tzinfo=pytz.utc)

    # return the difference in time
    return int((d - epoch).total_seconds() * 1000.0)

start_ts = date_to_milliseconds("15 minutes ago UTC")

# fetch 1 minute klines for the last day up until now
klines = binance_client.get_historical_klines(symbol="ETHAUD", interval=Client.KLINE_INTERVAL_5MINUTE , start_str=start_ts)

for elem in klines:
        print(elem)

Expected behavior get the exact close price

Environment (please complete the following information):

  • Python version: 3.92
  • OS: Mac
  • python-binance version - 0.7.9

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

cutlersswim avatar Mar 25 '21 10:03 cutlersswim

you're using proprietary classes (pytz and dateparser). could it be something to do with that? the API simply returns what Binance gives...

Bob12345 avatar Mar 29 '21 11:03 Bob12345

Just passing through. I might be wrong but I think the problem is:

# fetch 1 minute klines for the last day up until now
klines = binance_client.get_historical_klines(symbol="ETHAUD", interval=Client.KLINE_INTERVAL_5MINUTE , start_str=start_ts)

Hard to know without seeing your trading view, but it looks like you're requesting 5 minute intervals and comparing the price to the 1m candlestick base. If I am correct then I believe this issue can be closed.

lnxd avatar Dec 15 '21 01:12 lnxd

https://github.com/sammchardy/python-binance/issues/1246

pepo205 avatar Sep 07 '22 09:09 pepo205