Ethereum: Precise Binance API prices from historical data
As a programmer using Python, you are probably familiar with the important importance of accurate and up -to -date historical prices for Ethereum. In this article, we will explore how to bring accurate prices of API -JAI in Python at some time.
Prerequisites
- API Key Binance
- Library that can submit to asynchronous data (eg
request
AIOHTTP)
- Python environment with the necessary libraries installed
Method 1: Usingrequestsand the Function of the Revelation Call
We will use the libraryrequest ‘to submit HTTP requests to Binance API. We will make a return call function that will be launched whenever a new data point is received from the API.
`Python
Importing the request
from datetime imports of datetime
Def Get_api_price (Start_date_str, End_date_str):
Place your Binance API Key and URL of the end point API here
API_KEY = “Your_api_KEY”
API_URL = F ” (End_date_strphpy.
While the truth is:
try:
Answer = Request.get (API_URL, header = {“x-API-KEY”: API_KEY})
Data = Answer.json ()
Find the ETH price on a specified date
For the item in data [“result”]:
If the “time tag” in the item and int (item [“temporal”])> = Start_date_str
Return float (subject [“Price”])
In addition to the request.exception.requestexception as E:
Print (f “error: {e}”)
`
Method 2: UseAiohttpand loops
If you prefer asynchronous approach, we can use theAIOHTTPLibrary to get data from API.
Python
Import AIOHTTP
Async def Get_api_price (Start_date_str, End_date_str):
Async with Aiohttp.Cliantsission () as a session:
API_URL = F ” (End_date_strphpy.
Start_date = DateTime.strptime (Start_date_str, “%y-%m-%d”)
End_date = DateTime.strptime (End_date_str, “%y-%m-%d”)
while start_date <= End_date:
try:
Answer = Wait session.get (API_URL)
Data = Answer.json ()
Find the price of the ETH on a specified date
For the item in data [“result”]:
If the “time tag” in the item and int (item [“temporal”])> = Start_date:
Return float (subject [“Price”])
Apart from Aiohttp.clientResponseerror as e:
Print (f “error: {e}”)
Example of use:
Start_date_str = “2022-01-01”
End_date_str = “2022-12-31” “
Get_api_price (Start_date_str, End_date_str)
`
Tips and variations
- Make sure you replaceyour_api_KEY
with API -API -API.
- In both examples you can adjust the parameter oflimit` to retrieve more or less data.
- Consider adding errors in cases where the API -JA request fails or returns an invalid answer.
- If you need to make additional processing of prices received, feel free to change the return call functions accordingly.