I will help you write an article on how to recover the current Klines Binance from 00: 00utc so far for all the ticks available, without enjoying each traveler.
Recovery of Current Klines from Binance using an approach with a Request
Binance offers a strong API that allows you to get data in a single request. In this article, we will explore how to use this API to take over the current Klines for all available ticks from 00: 00utc so far.
Premise
- You have an API Binance account and you have set up a new app (if you haven’t done it already).
- Make sure you have installed the “Requests” library in your Python environment. If not, you can install it using PIP:
`Bash
Requests for installation PIP
code
We will use the "Requests" Library to send an API request to Binance. We will create a single function that will bring current klines for all available ticks.
Python
Import requests
From Datetime Import Datetime, Timedelta
def get_binance_klines ():
URL = "
Set the key and the API secret (if you have one).
API_KEY = 'your_pi_key_here'
api_secret = 'your_pi_secret_here'
headers = {
"Type of content": "app/json",
"X-API-KEY": API_KY,
"X-api-Secret": api_secret
}
Get your current date and time in the UTC.
Now_utc = Datetime.utcfromtimestam (0)
Define the ticks to get Kline. You can add or remove ticks as needed.
tickers = ['btccust', 'etusdt']
replace with desired tickers
Initialize an empty list to store Kline data
klines_data = []
While Now_utc
try:
Send a Get's request to binance with headquarters and API key
Answer = Requests.Get (URL, headers = headers)
Verify that the answer was successful
If answered.status_code == 200:
data = reply.json ()
Loop through each kline (5 seconds of data)
For i in the range (0, len (data), 10):
kline = {
"Timestamp": Datetime.utcfromtimestam (data [i] ['t']),
"Open": Float (data [i] ['o']),
'tall': float (data [i] ['h']),
'low': float (data [i] ['l']),
'Close': Float (data [i] ['c']),
"Volume": int (data [i] ['v'])
}
Add Kline data to our list
Klines_data.append (Kline)
Sleep for 5 seconds before taking over the next set of klines
Now_utc += Timedelta (seconds = 5)
Otherwise:
Raise the exception (F "API request failed with the status code {response.status_code}")
except the exception that is:
Print (F "An error occurred: {e}")
Returns Kline_Data
Using function
To use our function, just call it in your Python script. Replaceyour_pi_key_reand your_pi_secret_here 'with your API Binance credentials.
Python
kline = get_binance_klines ()
print (kline)
`
This will print all the Klines available for the ticks you specified. The number of returned klines is limited by the rate limits of the API, so make sure you manage any errors that may occur during the request process.
Note : Make sure you check Binance’s API documentation for the most updated information, useful uses, limits and lines before you integrate their API into your project.
Leave a Reply