Ethereum: Precise API Prices Binance Api from Historical Data
As developers who use Python, they are likely to know the importance of Ethereum’s exact and current historical data. In this article, we examine how to load the exact prices of API Binance API in Python.
Conditions
- Key binance api
- Library that can handle asynchronous data (eg.
- Python environment installed with required libraries
Method 1: Use “Request” and Call Call function
With the help of the “Request” library, we submit the http application to the Binance API interface. We make a recall feature that begins when a new data point comes from API.
`Python
Import requirements
From DateTSime Import Datesme
Def get_api_price (start_date_str, end_date_str):
Set Binance API key and API endpoint URL
Api_key = “Your_api_key”
Api_url = f ” = =
Although true:
try:
Answer = Requests.gget (Api_url, Headers = {“X-API-KEY”: API_KEY})
Data = Respony.json ()
Find the ETH price on the specified date
For the item in the data [“Result”]:
If “time stamp” in the element and int (item [“time stamp”])> = start_date_str:
Returning Swim (Lot [“Price”])
In addition to the requirements.Exceptions.equestException as e:
Print (F “Error: {E}”)
`
Method 2: Use aihttp
and loop
If you prefer the asynchronous approach, you can use the “Aioohttp” directory to load data from API.
`Python
Import aiohttp
Async Defi -pi_aprice Defprice (start_date_str, end_date_str):
Async aiohttp.clientsession () as a session:
Api_url = f ” = =
start_date = datedime.strptime (start_date_str, “%y-%m-%d”)
end_date = datedime.strptime (end_date_str, “%y-%m-%d”)
While start_date <= end_date:
try:
Answer = Acait Session.get (Api_url)
Data = Accit Response.json ()
Find the ETH price on the specified date
For the item in the data [“Result”]:
If “time stamp” in the element and int (item [“time stamp”])> = start_date:
Returning Swim (Lot [“Price”])
The aihttp.ClientSentSentsponseerror as e:
Print (F “Error: {E}”)
Example of use:
Start_date_st = “2022-01-01”
end_date_st = “2022-12-31”
Get_api_price (start_date_str, end_date_st)
`
Tips and variations
- Don’t forget to replace “yopi_api_key” with the real API Binance key.
- In both examples, you can edit the “limited” parameter to load more or less data points.
- Consider adding an error in cases where an API application fails or returns an invalid answer.
- If you have to do the additional processing of the received prices, please do not hesitate to set the rear call functions.