Search code examples
pythonbinancepython-binance

The current price of the futures on the Binance exchange (in real time) in Python


This is my assignment:

You need to write a Python code that will read the current price of the XRP/USDT futures on the Binance exchange in real time (as fast as possible). If the price falls by 1% from the maximum price in the last hour, the program should print a message to the console, and the program should continue to work, constantly reading the current price.

I learned how to receive data, but how can I go further?

import requests
import json
import pandas as pd
import datetime


base = 'https://testnet.binancefuture.com'
path = '/fapi/v1/klines'
url = base + path
param = {'symbol': 'XRPUSDT', 'interval': '1h', 'limit': 10}
r = requests.get(url, params = param)
if r.status_code == 200:
  data = pd.DataFrame(r.json())
  print(data)
else:
  print('Error')

Solution

  • You can try this, I've defined a function for price check and the rest is the main operation

    def price_check(df):
        max_value = max(df['Price'])  #max price within 1 hour
        min_value = min(df['Price'])  #min price within 1 hour
        if min_value/max_value < 0.99:  #1% threshold
            print("Alert")
    
    while True: # you can adjust the check frequency by sleep() function
        response = requests.get(url)
        if response.status_code==200:
            data = pd.Dataframe(response.json())
            price_check(data)