Demolition of Ethereum’s price from Binance API in Python
=========================================== ======= ====
In this article, we will review the step -by -step guide to save and update the latest Ethereum (ETH) price with the Python Binance application subscription.
Prerequisites
—————–
- You have installed the
requests 'library (
PIP installation requests’) and Binance API customer library for Python (Pip Install Binance -Client
)
- You have a Binance API account with sufficient balance and access to access the ETH market
- You have set up your application subscription in the Python command series
Code
——-
`Python
Bring the operating system
Bring JSON
Binance.Client Import customer
Set your Binance application subscription
API_KEY = ‘Your_API_KEY’
Api_secret = ‘Your_api_secret’
Create a new Binance Client display with your API access information
Customer = Customer (API_KEY = API_KEY, API_SECRET = API_SECRET)
Def get_latest_eth_price ():
“” “
Search the latest ETH price from Binance’s API.
Returns:
TUPLE: The latest ETH price and its corresponding exchange symbol (eg ‘Ethusdt’)
“” “
Get a list of available stock markets
Stock Exchange = Client.Get_Exchanges ()
Filter the list to include only Ethereum (ETH) exchange
ETH_EXCHANGE = [Exchange in Stock Exchange if exchange [‘symbol’] == ‘eth’]
If ETH Exchange is not found, raise an error
If not eh_Exchange:
Raise the value of the value (‘eh exchange not found’)
Get the latest price from ETH Exchange
Latest_Price = Eth_Exchange [0] [‘newsprice’]
Return the last_price
Def Store_latest_eth_price (Price):
“” “
Save the latest ETH price in the file.
Args:
Price (floating): The latest ETH to be saved.
“” “
Open (‘Last_eth_Prices.json’, ‘W’) as as F:
JSON.DUMP ({‘newsprice’: price, ‘symbol’: ‘Etusdt’}, f)
Def Main ():
Get the latest ETH price
Latest_Price = Get_latest_eth_price ()
Save the price to file
Store_latest_eth_price (last_price [‘newsprice’])
Print (F’latest Eth Price Saved: {Latest_Price [“NewPrice”]} USD ‘)
If __nimi__ == ‘__Main__’:
Main ()
`
Explanation
—————–
- In “Get_latest_eth_Price”, we create a new Binance customer exhibition with our access subscriptions.
- Then we will filter the available exchange list for the Ethereum (ETH) exchange only using the
client.get_exchanges () method.
- If no ETH exchange is found, we will raise the error.
4.
- In Store_latest_TH_PRICE, we open a file callednew_eth_PRICES.JSONI ‘in writing mode (
' w'
) and leave the latest ETH price and symbol (Etosdt) in the file using JSON design.
- Finally, “In the main function, we invite
get_latest_eth_price
to retrieve the latest ETH price and save it to the file.
Example of use
———————
You can use this script Save it as a Python file (`eg” eth_price_tracker.py) and perform it using the Binance API access right. The script prints the latest ETH to the file.
Note: Make sure “your_api_Key” and “your_api_secret” with your real Binance application subscription in your access information.