Extracting Ethereum Price from Binance API in Python

==

In this article, we will cover a detailed tutorial on how to save and update the latest Ethereum (ETH) price using Binance API in Python.

Required conditions

—————–

  • You have installed the “requests” library (pip install requirements) and the Binance API client library for Python (pip install binance-client)
  • You have a Binance API account with sufficient balance and permissions to access the ETH market
  • You have set the API credentials in your Python script

Code

——

import os

import json file

from binance.client import Client


Set your Binance API credentials

API_KEY = 'your_api_key'

API_SECRET = 'your_api_secret'


Create a new Binance client instance with the API credentials

client = client (api_key=API_KEY, api_secret=API_SECRET)

def get_latest_eth_price():

"""

Get the latest ETH price from the Binance API.

Returns:

section: latest ETH price and its corresponding exchange symbol (e.g. "ETHUDT")

"""


Get a list of available exchanges

exchanges = client.get_exchanges()


Filter the list to only include Ethereum (ETH) exchanges.

eth_exchange = [exchange to exchange on exchanges if exchange['symbol'] == 'ETH']


If no ETH exchange is found, raise an error

if not eth_exchange:

raise ValueError('No ETH exchange found')


Get the latest price from an ETH exchange

latest_price = eth_exchange[0]['latestPrice']

return latest_price

def store_latest_eth_price(price):

"""

Store the latest ETH price to a file.

Args:

price (float): the latest ETH price stored.

"""

with open('latest_eth_prices.json', 'w') as f:

json.dump({'latestPrice': price, 'symbol': 'ETHUDT'}, f)

def main():


Get the latest ETH price

latest_price = get_latest_eth_price()


Store the price to file

store_latest_eth_price(latest_price['latest price'])

print(f'Last stored ETH price: {latest_price["latest price"]} USD')

if __name__ == '__main__':

main()

Explanation

—————-

  • In the “get_latest_eth_price” function, we create a new instance of the Binance client with our API credentials.
  • Then, using the client.get_exchanges() method, we filter the list of available exchanges to include only Ethereum (ETH) exchanges.
  • If no ETH exchange is found, we raise an error.
  • We get the latest price from the ETH exchange and store it in a dictionary for later use.
  • In the “store_latest_eth_price” function, we open a file named “latest_eth_prices.json” in write mode (“w”) and use JSON formatting to load the latest ETH price and symbol (ETHSDT) into the file.
  • Finally, in the “main” function, we call the “get_latest_eth_price” function to retrieve the latest ETH price and store it in a file.

Usage Example

——————-

To use this script, save it as a Python file (e.g. “eth_price_tracker.py”) and run it with your Binance API credentials. The script will print the latest ETH price stored in the file. You can modify the script to suit your needs, add additional features, or fix bugs.

Note: Be sure to replace “your_api_key” and “your_api_secret” with your actual Binance API credentials.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *