From cce9241c8d3b08563b6787645a90d32f27d11354 Mon Sep 17 00:00:00 2001 From: nicolasakf Date: Mon, 5 Jan 2026 22:02:48 -0300 Subject: [PATCH] Enhance data processing and utility functions - Updated `process_price_change` to accept an optional `asset_id` parameter for better asset validation. - Modified `process_data` to ensure `json_datas` is always a list and to pass `asset_id` when available. - Added checks in `update_markets` to ensure the 'multiplier' column exists and fill NaN values with empty strings. - Refactored multiplier application in `get_buy_sell_amount` to use `row.get()` for safer access. --- poly_data/data_processing.py | 18 +++++++++++++----- poly_data/data_utils.py | 6 ++++++ poly_data/trading_utils.py | 7 ++++--- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/poly_data/data_processing.py b/poly_data/data_processing.py index 4dea873..0fcd909 100644 --- a/poly_data/data_processing.py +++ b/poly_data/data_processing.py @@ -18,9 +18,12 @@ def process_book_data(asset, json_data): global_state.all_data[asset]['bids'].update({float(entry['price']): float(entry['size']) for entry in json_data['bids']}) global_state.all_data[asset]['asks'].update({float(entry['price']): float(entry['size']) for entry in json_data['asks']}) -def process_price_change(asset, side, price_level, new_size): - if asset_id != global_state.all_data[asset]['asset_id']: - return # skip updates for the No token to prevent duplicated updates +def process_price_change(asset, side, price_level, new_size, asset_id=None): + # Skip updates for the No token to prevent duplicated updates + # Only process if asset_id matches the stored asset_id for this market + if asset_id and asset in global_state.all_data and asset_id != global_state.all_data[asset]['asset_id']: + return + if side == 'bids': book = global_state.all_data[asset]['bids'] else: @@ -33,7 +36,10 @@ def process_price_change(asset, side, price_level, new_size): book[price_level] = new_size def process_data(json_datas, trade=True): - + # Ensure input is always a list + if isinstance(json_datas, dict): + json_datas = [json_datas] + for json_data in json_datas: event_type = json_data['event_type'] asset = json_data['market'] @@ -49,7 +55,9 @@ def process_data(json_datas, trade=True): side = 'bids' if data['side'] == 'BUY' else 'asks' price_level = float(data['price']) new_size = float(data['size']) - process_price_change(asset, side, price_level, new_size) + # Pass asset_id if available in the data + asset_id = data.get('asset_id', None) + process_price_change(asset, side, price_level, new_size, asset_id) if trade: asyncio.create_task(perform_trade(asset)) diff --git a/poly_data/data_utils.py b/poly_data/data_utils.py index 25976a8..4645a5e 100644 --- a/poly_data/data_utils.py +++ b/poly_data/data_utils.py @@ -149,6 +149,12 @@ def update_markets(): received_df, received_params = get_sheet_df() if len(received_df) > 0: + # Ensure multiplier column exists and fill NaN values with empty string + if 'multiplier' not in received_df.columns: + received_df['multiplier'] = '' + else: + received_df['multiplier'] = received_df['multiplier'].fillna('') + global_state.df, global_state.params = received_df.copy(), received_params diff --git a/poly_data/trading_utils.py b/poly_data/trading_utils.py index 752c034..979fe72 100644 --- a/poly_data/trading_utils.py +++ b/poly_data/trading_utils.py @@ -188,9 +188,10 @@ def get_buy_sell_amount(position, bid_price, row, other_token_position=0): # Apply multiplier for low-priced assets if bid_price < 0.1 and buy_amount > 0: - if row['multiplier'] != '': - print(f"Multiplying buy amount by {int(row['multiplier'])}") - buy_amount = buy_amount * int(row['multiplier']) + multiplier = row.get('multiplier', '') + if multiplier != '': + print(f"Multiplying buy amount by {int(multiplier)}") + buy_amount = buy_amount * int(multiplier) return buy_amount, sell_amount