Alex
Alex

Reputation: 732

Get historical data from binance

I am trying to extract historical data between [curr_time - 2years, curr_time]. Time gap is 1 day. So, I expect about 700 items, but i received only 3 items.

How can I fix this problem?

My code

from binance.client import Client

# Binance test_key https://testnet.binance.vision/key/generate
API_KEY = "---"
API_SECRET = "---"
DAYS_IN_YEAR = 365
DB_NAME = "charts"

def GetHistoricalData(
    timedelta_days=DAYS_IN_YEAR * 2,
    ticker="BTCUSDT",
    kline_interval=Client.KLINE_INTERVAL_1HOUR
    ):

    start_time = time.time()
    untilThisDate = datetime.datetime.now()
    sinceThisDate = untilThisDate - datetime.timedelta(days=timedelta_days)

    print("ZZZZZZZZZ_ ", str(sinceThisDate), str(untilThisDate)) # 2019-11-06 00:23:43.620016 2021-11-05 00:23:43.620016
    client = Client(API_KEY, API_SECRET) 
    client.API_URL = 'https://testnet.binance.vision/api'
    candle = client.get_historical_klines(ticker, kline_interval, str(sinceThisDate), str(untilThisDate))
    
    print("CANDLE_", len(candle)) # 3

I tried this request:

candle = client.get_historical_klines(ticker, kline_interval, "01 January, 2019", "04 November 2021") 

but received only 3 items again

dateTime                                              ...                             
2021-11-02 00:00:00  61722.80000000  150535.61000000  ...  448.99018200  1635897599999
2021-11-03 00:00:00  63208.69000000  100000.00000000  ...  451.03367500  1635983999999
2021-11-04 00:00:00  62894.04000000   70000.00000000  ...  401.86212800  1636070399999

Upvotes: 2

Views: 3948

Answers (2)

valentinmk
valentinmk

Reputation: 1021

Well....

If you try to request this data with API call it will give you:

In [1]: import requests
   ...: len(requests.get('https://testnet.binance.vision/api/v3/klines?symbol=BTCUSDT&interval=1h&limit=1000').json())
Out[1]: 65

but if you try to run it with production env of binance (btw klines/candles is a public data and don't require apiKey):

In [2]: import requests
   ...: len(requests.get('https://api.binance.com/api/v3/klines?symbol=BTCUSDT&interval=1h&limit=1000').json())
Out[2]: 1000

So, to fix you example, you need replace BASE_URL

client.API_URL = 'https://api.binance.com/api'

It gives me:

ZZZZZZZZZ_  2019-11-06 01:15:15.122873 2021-11-05 01:15:15.122873
CANDLE_ 17483

Upvotes: 3

J.R. BEATS
J.R. BEATS

Reputation: 93

Try the code below. I get a bunch of data, but its not formatted:

 import datetime
    from binance.client import Client
    import time
    
    # Binance test_key https://testnet.binance.vision/key/generate
    API_KEY = "---"
    API_SECRET = "---"
    DAYS_IN_YEAR = 365
    DB_NAME = "charts"
    
    
    def GetHistoricalData(
            timedelta_days=DAYS_IN_YEAR * 2,
            ticker="BTCUSDT",
            kline_interval=Client.KLINE_INTERVAL_1HOUR
    ):
        start_time = time.time()
        untilThisDate = datetime.datetime.now()
        sinceThisDate = untilThisDate - datetime.timedelta(days=timedelta_days)
    
        print("ZZZZZZZZZ_ ", str(sinceThisDate),
              str(untilThisDate))  # 2019-11-06 00:23:43.620016 2021-11-05 00:23:43.620016
        client = Client(API_KEY, API_SECRET)
        client.API_URL = 'https://testnet.binance.vision/api'
        candle = client.get_historical_klines(ticker, kline_interval, str(sinceThisDate), str(untilThisDate))
        print(candle)
    
    
    GetHistoricalData()

Upvotes: 0

Related Questions