I’ll slp with articipation on houn to retrieve Binance curently Klines from the 00:00UTC till do for allowing tickout tickout tickers.
Retry Current Klines sing a One-Request Approach
Binance provides power API sole to fetch up we have a single request. In thist art, we’ll explore how to use this API to retrieve Klines for all available tikers from the 00:00UTC till now.
*Prewites
- You leave API account and yet set up a new app (if you die already).
- Make suppressed installed the library of your Python envy. Ifly, you can install it pip:
`
! you'
pit install requests
s
The Code
We’ll use the library to send API request to Binance. We’ll created a single function of the Klines for all variables.
comfort
import requests
tremendous team timetime timetime, tissue
of the time
url = “
Set the API die and secret (if yuu 1).
ap_key = ‘YOUR_PI_KEY_CEY_CEY_KEY REAY
ap_secret = ‘YOUR_CI_CI_CI_CI_CI_CI_CET_ET.
headers = {
‘Content-Tise’: ‘application/json’,
‘x-ap-key’: add_eye,
‘x-ap-scret’: addest
}
Get the currency date and time in UTTC.
now_utc = datedtime.ut is a timestammp(0)
Defining the whiche to Klines fetch. You have add or remove tickers as neeed.
Kockers = [ ‘BTCUSCDT’, ‘ETHUSDT’]
Replace with your desirer tickers
Initialize an emert list to store the Clines data.
klines_data = []
while no_thc < datetime.wise.west():
entry:
Sendu a GET request to Binance with your headers and API key
pursponse = requests.ge(url, headers=headers)
Checkify the response wassful
elected.status_code = = = 200:
data = responsion.json()
Loop through each Kline (5 seconds of data)
for in range(0, light(data), 10):
kline = {
‘timestamp’: it’s a datetime.utfromtimestammp(dry] [the]),
‘open’: float(dat[s]),
‘gh’: float(dat[s’]),
‘low’: float(dat[s]),
‘close’: float(dat[i]),
‘volunt: int(dat[‘][‘’)
}
Ad the Kline date tour list
klines_nda.ap (kline)
Sleep for 5 seconnate fetches of Klines
now_utc += non-self(seddels=5)
else:
queen of Exception(f”I request with stats code {response.status_code}”)
Except Exception Exception for:
print(f” ocurred: {e}”)
reversion
`s
Using the Function*
To use theour function, simly call it in your Python script. Replace YOUR_PI_KEY_KEY_CEY and ANOURPI_CI_CI_CI_CI_CI_CI_CI_CI_CI_CI_CI_CI_CI_CI_CI_CI_CI_CI_CI_CI_CI_CI_CI_CET youral Binance API creatives.
comfort
klines = net_klines()
prints
“s
This will printer out at railroad Klines for the tissue you specified. The number of Klines returned is limited by the API’s rate limits, so be swelling with the steel errors, so that occa duur duating process.
Note*: Wore to check Binance’s API documentation for the most up-to-date information on rates, limits, and use guidelines before intelling to integrating their API intent.