From fa2c3eff972d8bba6688441a33e163e5dd028d0c Mon Sep 17 00:00:00 2001 From: warproxxx Date: Sun, 22 Jun 2025 10:22:31 -0700 Subject: [PATCH] Fix TypeError in market data processing when prices are None MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Handle None values returned from find_best_price_with_size() to prevent arithmetic operations on NoneType. Added null checks for: - Mid price calculation - Token2 price transformations (1-x operations) - Bid/ask sum calculations 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- poly_data/trading_utils.py | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/poly_data/trading_utils.py b/poly_data/trading_utils.py index eef5704..752c034 100644 --- a/poly_data/trading_utils.py +++ b/poly_data/trading_utils.py @@ -30,15 +30,36 @@ def get_best_bid_ask_deets(market, name, size, deviation_threshold=0.05): best_bid, best_bid_size, second_best_bid, second_best_bid_size, top_bid = find_best_price_with_size(global_state.all_data[market]['bids'], size, reverse=True) best_ask, best_ask_size, second_best_ask, second_best_ask_size, top_ask = find_best_price_with_size(global_state.all_data[market]['asks'], size, reverse=False) - mid_price = (best_bid + best_ask) / 2 - - bid_sum_within_n_percent = sum(size for price, size in global_state.all_data[market]['bids'].items() if best_bid <= price <= mid_price * (1 + deviation_threshold)) - ask_sum_within_n_percent = sum(size for price, size in global_state.all_data[market]['asks'].items() if mid_price * (1 - deviation_threshold) <= price <= best_ask) + # Handle None values in mid_price calculation + if best_bid is not None and best_ask is not None: + mid_price = (best_bid + best_ask) / 2 + bid_sum_within_n_percent = sum(size for price, size in global_state.all_data[market]['bids'].items() if best_bid <= price <= mid_price * (1 + deviation_threshold)) + ask_sum_within_n_percent = sum(size for price, size in global_state.all_data[market]['asks'].items() if mid_price * (1 - deviation_threshold) <= price <= best_ask) + else: + mid_price = None + bid_sum_within_n_percent = 0 + ask_sum_within_n_percent = 0 if name == 'token2': - best_bid, second_best_bid, top_bid, best_ask, second_best_ask, top_ask = 1 - best_ask, 1 - second_best_ask, 1 - top_ask, 1 - best_bid, 1 - second_best_bid, 1 - top_bid - best_bid_size, second_best_bid_size, best_ask_size, second_best_ask_size = best_ask_size, second_best_ask_size, best_bid_size, second_best_bid_size - bid_sum_within_n_percent, ask_sum_within_n_percent = ask_sum_within_n_percent, bid_sum_within_n_percent + # Handle None values before arithmetic operations + if all(x is not None for x in [best_bid, best_ask, second_best_bid, second_best_ask, top_bid, top_ask]): + best_bid, second_best_bid, top_bid, best_ask, second_best_ask, top_ask = 1 - best_ask, 1 - second_best_ask, 1 - top_ask, 1 - best_bid, 1 - second_best_bid, 1 - top_bid + best_bid_size, second_best_bid_size, best_ask_size, second_best_ask_size = best_ask_size, second_best_ask_size, best_bid_size, second_best_bid_size + bid_sum_within_n_percent, ask_sum_within_n_percent = ask_sum_within_n_percent, bid_sum_within_n_percent + else: + # Handle case where some prices are None - use available values or defaults + if best_bid is not None and best_ask is not None: + best_bid, best_ask = 1 - best_ask, 1 - best_bid + best_bid_size, best_ask_size = best_ask_size, best_bid_size + if second_best_bid is not None: + second_best_bid = 1 - second_best_bid + if second_best_ask is not None: + second_best_ask = 1 - second_best_ask + if top_bid is not None: + top_bid = 1 - top_bid + if top_ask is not None: + top_ask = 1 - top_ask + bid_sum_within_n_percent, ask_sum_within_n_percent = ask_sum_within_n_percent, bid_sum_within_n_percent