From 4d206e05d87fa1bd3992a0e63513e4f988811d0f Mon Sep 17 00:00:00 2001 From: warproxxx Date: Sun, 22 Jun 2025 10:28:13 -0700 Subject: [PATCH] Fix excessive order cancellation and recreation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Implement smarter order management logic to avoid constantly cancelling and recreating orders: - Only cancel orders when price differs by >0.5 cents or size differs by >10% - Keep existing orders if changes are minor to reduce unnecessary market activity - Add detailed logging to show when orders are kept vs cancelled This should resolve the issue where orders were being cancelled and recreated repeatedly. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- trading.py | 42 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/trading.py b/trading.py index df6bd77..7434970 100644 --- a/trading.py +++ b/trading.py @@ -31,9 +31,26 @@ def send_buy_order(order): """ client = global_state.client - # Cancel existing orders for this token to avoid conflicts - if order['orders']['buy']['size'] > 0 or order['orders']['sell']['size'] > 0: + # Only cancel existing orders if we need to make significant changes + existing_buy_size = order['orders']['buy']['size'] + existing_buy_price = order['orders']['buy']['price'] + + # Cancel orders if price changed significantly or size needs major adjustment + price_diff = abs(existing_buy_price - order['price']) if existing_buy_price > 0 else float('inf') + size_diff = abs(existing_buy_size - order['size']) if existing_buy_size > 0 else float('inf') + + should_cancel = ( + price_diff > 0.005 or # Cancel if price diff > 0.5 cents + size_diff > order['size'] * 0.1 or # Cancel if size diff > 10% + existing_buy_size == 0 # Cancel if no existing buy order + ) + + if should_cancel and (existing_buy_size > 0 or order['orders']['sell']['size'] > 0): + print(f"Cancelling buy orders - price diff: {price_diff:.4f}, size diff: {size_diff:.1f}") client.cancel_all_asset(order['token']) + elif not should_cancel: + print(f"Keeping existing buy orders - minor changes: price diff: {price_diff:.4f}, size diff: {size_diff:.1f}") + return # Don't place new order if existing one is fine # Calculate minimum acceptable price based on market spread incentive_start = order['mid_price'] - order['max_spread']/100 @@ -75,9 +92,26 @@ def send_sell_order(order): """ client = global_state.client - # Cancel existing orders for this token to avoid conflicts - if order['orders']['buy']['size'] > 0 or order['orders']['sell']['size'] > 0: + # Only cancel existing orders if we need to make significant changes + existing_sell_size = order['orders']['sell']['size'] + existing_sell_price = order['orders']['sell']['price'] + + # Cancel orders if price changed significantly or size needs major adjustment + price_diff = abs(existing_sell_price - order['price']) if existing_sell_price > 0 else float('inf') + size_diff = abs(existing_sell_size - order['size']) if existing_sell_size > 0 else float('inf') + + should_cancel = ( + price_diff > 0.005 or # Cancel if price diff > 0.5 cents + size_diff > order['size'] * 0.1 or # Cancel if size diff > 10% + existing_sell_size == 0 # Cancel if no existing sell order + ) + + if should_cancel and (existing_sell_size > 0 or order['orders']['buy']['size'] > 0): + print(f"Cancelling sell orders - price diff: {price_diff:.4f}, size diff: {size_diff:.1f}") client.cancel_all_asset(order['token']) + elif not should_cancel: + print(f"Keeping existing sell orders - minor changes: price diff: {price_diff:.4f}, size diff: {size_diff:.1f}") + return # Don't place new order if existing one is fine print(f'Creating new order for {order["size"]} at {order["price"]}') client.create_order(