-1

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')
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Alexey
  • 3
  • 2
  • While true... save 1 hour of data . get max, if new_data < 99$ max alert – Amin S Feb 10 '23 at 09:26
  • get the data from url in certain frequency and then store the data with time stamp and keep a check of max price in last one hour and if get price is more than last one hour then send alert – sahasrara62 Feb 10 '23 at 09:44

2 Answers2

0

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)
        
林源煜
  • 26
  • 4
0
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)
Alexey
  • 3
  • 2