I used this code: markpricedata = client.tickerprice(symbol=symbol) mp =…
- User
I used this code: mark_price_data = client.ticker_price(symbol=symbol) mp = float(mark_price_data['price']) if 'price' in mark_price_data else 0.0 mark_price_data_change = client.ticker_24hr_price_change(symbol=symbol) mark_price_percent = float(mark_price_data_change['priceChangePercent']) if 'priceChangePercent' in mark_price_data_change else 0.0
# Retrieve depth data depth_data = client.depth(symbol=symbol) bids = depth_data['bids'] bid_prices = [float(bid[0]) for bid in bids if float(bid[0]) > mark_price] asks = depth_data['asks'] ask_prices = [float(ask[0]) for ask in asks if float(ask[0]) < mark_price] print("buy:", bid_prices) print("sell:", ask_prices) print(mark_price)but terminal returned me: buy: [227.49, 227.48] sell: [] 227.47
It gives me few numbers , I need middle price between them
- Assistant
To find the middle price between the buy and sell prices, you can sort the bid_prices and ask_prices lists and take the average of their respective middle elements. Here's an example of how you can modify the code to calculate the middle price:
# Sort the bid and ask prices sorted_bids = sorted(bid_prices) sorted_asks = sorted(ask_prices) # Calculate the middle price if sorted_bids: middle_bid = sorted_bids[len(sorted_bids)//2] else: middle_bid = 0.0 if sorted_asks: middle_ask = sorted_asks[len(sorted_asks)//2] else: middle_ask = 0.0 middle_price = (middle_bid + middle_ask) / 2 print("Middle price:", middle_price)This code will calculate the middle price between the buy and sell prices by taking the averages of their respective middle elements. Note that if there are no bid or ask prices available, it will set the middle_bid and middle_ask variables to 0.0.
- User
I mean between bids and why devided by 2 ?
- Assistant
Apologies for the confusion. If you want to find the middle price between the bid prices, you can take the average of the highest bid price and the lowest bid price. Here's how you can modify the code to calculate the middle price based on the bid prices:
# Sort the bid prices sorted_bids = sorted(bid_prices) # Calculate the middle price if sorted_bids: highest_bid = sorted_bids[-1] lowest_bid = sorted_bids[0] middle_price = (highest_bid + lowest_bid) / 2 else: middle_price = 0.0 print("Middle price:", middle_price)In this code, we sort the bid_prices list and then take the highest bid price (last element) and the lowest bid price (first element). We then calculate the middle price by taking the average of these two values. If there are no bid prices available, the middle_price variable is set to 0.0.