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

455 Views Asked by At

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')
2

There are 2 best solutions below

0
On BEST ANSWER

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)
        
0
On
import requests
import time

def get_price():
    url = "https://api.binance.com/api/v3/ticker/price?symbol=XRPUSDT"
    response = requests.get(url)
    return float(response.json()["price"])

def check_price_drop(price, highest_price):
    if price / highest_price < 0.99:
        print("Price dropped by 1%!")

highest_price = 0
while True:
    price = get_price()
    if price > highest_price:
        highest_price = price
    check_price_drop(price, highest_price)
    time.sleep(10)