I have this code: depthdata = client.depth(symbol=symbol) bids = depthdata['bids']…
- User
I have this code: depth_data = client.depth(symbol=symbol)
bids = depth_data['bids'] buy_qty = round(sum(float(bid[1]) for bid in bids), 0) bid_prices = [float(bid[0]) for bid in bids] highest_bid_price = max(bid_prices) lowest_bid_price = min(bid_prices) b_h = highest_bid_price b_l = lowest_bid_priceYou need to give me system which will subtract bid qty of every order which price is higher than mark_price
- Assistant
To create a system that subtracts the bid quantity of every order which price is higher than the mark_price, you can use the following code:
mark_price = 0.0 # Replace with your desired mark price filtered_bids = [bid for bid in bids if float(bid[0]) > mark_price] updated_bids = [[float(bid[0]), max(float(bid[1]) - mark_price, 0)] for bid in filtered_bids] # Calculate the total bid quantity after subtracting updated_buy_qty = round(sum(bid[1] for bid in updated_bids), 0)The
filtered_bidslist comprehension filters out the bids that have a price higher than themark_price. Theupdated_bidslist comprehension creates a new list of bid prices and updated bid quantities. It subtracts themark_pricefrom each bid's quantity but ensures the result is at least 0 to prevent negative quantities. Finally, theupdated_buy_qtycalculates the total bid quantity after subtracting the mark_price from each relevant bid quantity. - User
But it equal 0
- Assistant
Apologies for the mistake. If the calculated
updated_buy_qtyis always equal to 0, it means that all the bid prices are lower than or equal to themark_price.To reflect this, you can modify the code as follows:
filtered_bids = [bid for bid in bids if float(bid[0]) > mark_price] if filtered_bids: # Check if there are any bids above the mark_price updated_bids = [[float(bid[0]), max(float(bid[1]) - mark_price, 0)] for bid in filtered_bids] updated_buy_qty = round(sum(bid[1] for bid in updated_bids), 0) else: updated_buy_qty = buy_qty # No bids to subtract, so the updated buy quantity remains the sameIn this updated code, a check is added to determine if there are any bids above the
mark_price. If there are no such bids, theupdated_buy_qtywill remain the same as the originalbuy_qty. - User
Too wrong
- Assistant
Apologies for the confusion. If you want to subtract the bid quantity of each order whose price is higher than the
mark_price, you can modify the code as follows:mark_price = 0.0 # Replace with your desired mark price updated_bids = [] for bid in bids: price = float(bid[0]) qty = float(bid[1]) if price > mark_price: # Check if the bid price is higher than the mark_price updated_qty = max(qty - mark_price, 0) else: updated_qty = qty updated_bids.append([price, updated_qty])The updated code iterates over each bid and checks if the bid price is higher than the
mark_price. If it is, it subtracts themark_pricefrom the bid quantity (qty) to get the updated quantity (updated_qty). If the bid price is lower than or equal to themark_price, the bid quantity remains the same. The updated bids, with their respective prices and quantities, are stored in theupdated_bidslist.