Ethereum: how to get binance current klines from 00:00UTC till now for whole available tickers at ones?

I will help you write an article on how to get current Binance clients from 00:00 UTC to now for all available tickers without going through each ticker.

Retrieving current customers from Binance via request

Binance provides a powerful API that allows you to retrieve data in a single request. In this article, we will look at how to use this API to get the current Klines values ​​for all available tickers from 00:00 UTC to now.

Prerequisites

  • You have a Binance API account and have set up a new application (if you haven’t already).

  • Make sure you have installed the requests library in your Python environment. If not, you can install it with pip:

requests to install pip

Code

We use the “requests” library to send API requests to Binance. We create a single function that retrieves the current price of all available tickers.

import requests

from datetime import datetime, timedelta

definition of get_binance_klines():

URL="










Set the API key and secret (if any).

api_key = 'YOUR_API_KEY_HERE'

api_secret = 'YOUR_API_SECRET_HERE'

headers = {

"Content-Type": "application/json",

'x-api-key': api_key,

'x-api-secret': api_secret

}


Get the current date and time in UTC format.

now_utc = datetime.utcfromtimestamp(0)


Determine the tickers for which wedges should be extracted. If necessary, you can add or remove tickers.

tickers = ['BTCUSDT', 'ETHUDT']

Replace the desired tickers


Initialize an empty list to store Clines data

klines_data = []

while now_utc < datetime.now():

to ask:


Send a GET request to Binance with our headers and API key

response = requests.get(url, headers=headers)


Check if the response was successful

if response.status_code == 200:

data = response.json()


Loop through each Kline (5 seconds of data)

for i in the range (0, len(data), 10):

wedge = {

'timestamp': datetime.utcfromtimestamp(data[i]['t']),

'open': float(data[i]['o']),

'high': float(data[i]['h']),

'low': float(data[i]['l']),

'close': float(data[i]['c']),

'volume': int(data[i]['v'])

}


Add Klein data to our list

klines_data.append(kline)


Sleep for 5 seconds before loading the next Cline set

now_utc += timedelta(seconds=5)

second:

raise Exception(f"API request failed, status code: {response.status_code}")

except for this exception:

print(f"An error occurred: {e}")

return klines_data

Using this feature

To use our function, simply call it in your Python script. Replace “YOUR_API_KEY_HERE” and “YOUR_API_SECRET_HERE” with your actual Binance API credentials.

klines = get_binance_klines()

print (strings)

This will print all available Klines for the specified tickers. The number of Klines returned is limited by API rate limits, so be sure to handle any errors that may occur during the request process.

Note: Before integrating the Binance API into your project, be sure to read the Binance API documentation for up-to-date information on fees, limits, and usage rules.

Bài viết liên quan

Để lại một bình luận

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *