forked from defi-maker/pylighter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrid_strategy.py
More file actions
1444 lines (1192 loc) · 71.1 KB
/
grid_strategy.py
File metadata and controls
1444 lines (1192 loc) · 71.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Simplified Grid Trading Strategy for Lighter Protocol
Based on Binance reference but adapted for Lighter Protocol
Key simplifications:
1. Reduced WebSocket complexity
2. Simplified order tracking
3. Focused error handling
4. Cleaner code structure
"""
import os
import asyncio
import logging
import time
import math
import json
import argparse
import signal
import lighter
from decimal import Decimal, ROUND_DOWN
from dotenv import load_dotenv
from pylighter.client import Lighter
# Load environment variables
load_dotenv()
# Create log directory
os.makedirs("log", exist_ok=True)
# ==================== Configuration ====================
COIN_NAME = "TON"
GRID_SPACING = 0.0003 # 0.03% grid spacing (ultra-high frequency for zero fees!)
DEFAULT_ORDER_AMOUNT = 10.0 # Default order amount in USD (quote currency)
LEVERAGE = 5 # Leverage for TON (conservative vs OKX's 50x)
SYNC_TIME = 10 # Sync interval (seconds)
UPDATE_INTERVAL = 5 # Price update interval
MAX_ACTIVE_ORDERS = 8 # Maximum active orders
# Ultra-high frequency advantage with zero fees
# ==================== Logging Configuration ====================
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s",
handlers=[
logging.FileHandler("log/grid_strategy.log", mode='a'),
logging.StreamHandler(),
],
force=True
)
logger = logging.getLogger()
# Set WebSocket-related loggers to DEBUG level to reduce noise
logging.getLogger('websockets').setLevel(logging.WARNING)
logging.getLogger('lighter').setLevel(logging.INFO)
class SimplifiedGridBot:
"""Simplified Grid Trading Bot for Lighter Protocol"""
def __init__(self, dry_run=False, order_amount=None):
self.dry_run = dry_run
self.lighter = None
self.ws_client = None
self.symbol = COIN_NAME
self.market_id = None
self.grid_spacing = GRID_SPACING
self.leverage = LEVERAGE
# Order amount configuration
self.order_amount = order_amount or DEFAULT_ORDER_AMOUNT # USD amount per order
# Shutdown control
self.shutdown_requested = False
# Market constraints
self.min_quote_amount = 10.0
self.price_precision = 6
self.amount_precision = 1
self.step_size = None # Will be calculated from amount_precision
# Position tracking
self.long_position = 0
self.short_position = 0
self.long_initial_quantity = 0 # Will be calculated dynamically
self.short_initial_quantity = 0 # Will be calculated dynamically
# Order counting for display
self.buy_long_orders = 0.0 # Long buy orders count
self.sell_long_orders = 0.0 # Long sell orders count
self.sell_short_orders = 0.0 # Short sell orders count
self.buy_short_orders = 0.0 # Short buy orders count
# Price tracking
self.latest_price = 0
self.best_bid_price = None
self.best_ask_price = None
self.price_updated = False
# Order tracking
self.active_orders = {} # order_id -> order_info
self.last_position_update_time = 0
# Enhanced order management
self.max_orders = 8 # Restore original limit for better grid coverage
self.sync_warning_throttle = {} # Throttle sync warnings by type
# WebSocket connection status and health monitoring
self.ws_connected = False
self._last_account_update_time = None # Track when we last received account updates
# WebSocket health monitoring
self._ws_health_check_interval = 60 # Check every minute
self._last_ws_health_check = 0
self._ws_connection_failures = 0
self._max_ws_connection_failures = 5
self._ws_start_time = None # Track WebSocket start time
# Initialize WebSocket order count tracking for force cleanup
self._last_ws_order_count = 0
self._ws_zero_count = 0
async def setup(self):
"""Initialize the Lighter client"""
api_key = os.getenv("LIGHTER_KEY")
api_secret = os.getenv("LIGHTER_SECRET")
if not api_key or not api_secret:
raise ValueError("Please set LIGHTER_KEY and LIGHTER_SECRET environment variables")
logger.info("Initializing Lighter client...")
self.lighter = Lighter(key=api_key, secret=api_secret)
await self.lighter.init_client()
logger.info("✅ Client initialized successfully")
# Get market_id and constraints
self.market_id = self.lighter.ticker_to_idx.get(self.symbol)
if self.market_id is None:
raise ValueError(f"Market ID not found for symbol {self.symbol}")
# Fetch market constraints
await self.fetch_market_constraints()
# Setup account orders WebSocket for order tracking
await self.setup_account_orders_websocket()
logger.info(f"Grid bot setup complete for {self.symbol}")
logger.info(f"Market ID: {self.market_id}, Min quote: ${self.min_quote_amount}")
logger.info(f"Leverage: {self.leverage}x, Grid spacing: {self.grid_spacing*100}%")
# Get initial positions
await self.update_positions()
# Initial API sync to get existing orders
logger.info("📋 Performing initial order sync...")
await self.sync_orders_from_api()
self._last_api_sync = time.time()
async def fetch_market_constraints(self):
"""Fetch market constraints and calculate step_size"""
try:
constraints = await self.lighter.get_market_constraints(self.symbol)
self.min_quote_amount = constraints['min_quote_amount']
self.price_precision = constraints['price_precision']
self.amount_precision = constraints['amount_precision']
# Calculate step_size from amount_precision (simplified approach)
self.step_size = 10 ** (-self.amount_precision)
logger.info(f"Constraints: min_quote=${self.min_quote_amount}, price_precision={self.price_precision}, amount_precision={self.amount_precision}")
logger.info(f"Calculated step_size: {self.step_size}, Order amount: ${self.order_amount} USD per order")
except Exception as e:
logger.warning(f"Failed to fetch constraints: {e}, using defaults")
self.min_quote_amount = 10.0
self.price_precision = 6
self.amount_precision = 1
self.step_size = 0.1 # Default step size
async def init_websocket(self):
"""Initialize WebSocket client for price updates"""
logger.info("Initializing WebSocket client...")
try:
def on_order_book_update(market_id, order_book):
try:
# Skip ping/pong handling in sync callback
if isinstance(order_book, dict) and order_book.get('type') in ['ping', 'pong']:
return
if int(market_id) == int(self.market_id):
bids = order_book.get('bids', [])
asks = order_book.get('asks', [])
if bids and asks:
self.best_bid_price = float(bids[0]['price'])
self.best_ask_price = float(asks[0]['price'])
old_price = self.latest_price
self.latest_price = (self.best_bid_price + self.best_ask_price) / 2
self.price_updated = True
# Update quantities on first price
if old_price == 0 and self.latest_price > 0:
self.update_initial_quantities()
except Exception as e:
if not any(keyword in str(e).lower() for keyword in ['ping', 'pong', 'unhandled', 'connection']):
logger.error(f"Error processing orderbook: {e}")
self.ws_client = lighter.WsClient(
order_book_ids=[self.market_id],
account_ids=[],
on_order_book_update=on_order_book_update,
on_account_update=lambda a, b: None,
)
logger.info("✅ WebSocket client initialized with enhanced ping/pong handling")
except Exception as e:
logger.error(f"❌ WebSocket initialization failed: {e}")
raise
def calculate_order_quantity(self, price):
"""Calculate order quantity based on configured USD amount and step_size"""
try:
if price <= 0:
return 0
# Calculate base quantity from USD amount
base_quantity = self.order_amount / price
# Round to step_size (which is derived from amount_precision)
if self.step_size and self.step_size > 0:
# Round down to nearest step_size multiple
quantity = math.floor(base_quantity / self.step_size) * self.step_size
# Ensure minimum quantity
if quantity < self.step_size:
quantity = self.step_size
else:
# Fallback to amount_precision rounding
quantity = round(base_quantity, self.amount_precision)
# Validate minimum quote amount
quote_value = quantity * price
if quote_value < self.min_quote_amount:
# Adjust quantity to meet minimum quote requirement
min_quantity = self.min_quote_amount / price
if self.step_size and self.step_size > 0:
quantity = math.ceil(min_quantity / self.step_size) * self.step_size
else:
quantity = round(min_quantity, self.amount_precision)
return quantity
except Exception as e:
logger.error(f"Failed to calculate order quantity: {e}")
# Fallback to original method
return self.lighter.calculate_min_quantity_for_quote_amount(
price, max(self.order_amount, self.min_quote_amount), self.symbol
)
def update_initial_quantities(self):
"""Update quantities based on current price and configured order amount (OKX-style)"""
if self.latest_price > 0:
calculated_quantity = self.calculate_order_quantity(self.latest_price)
self.long_initial_quantity = calculated_quantity
self.short_initial_quantity = calculated_quantity
quote_value = calculated_quantity * self.latest_price
logger.info(f"Updated quantities based on price ${self.latest_price:.6f}: {calculated_quantity:.{self.amount_precision}f} {self.symbol} (${quote_value:.2f})")
async def update_positions(self):
"""Update current positions (simplified)"""
if self.dry_run:
# In DRY RUN mode, don't reset positions - they should persist from simulated fills
# Only update timestamp to prevent issues
self.last_position_update_time = time.time()
else:
# In real mode, you might want to fetch actual positions from the API
# For now, positions are managed through order fills
pass
async def sync_orders_from_api(self):
"""Sync active orders using the new account_active_orders API"""
try:
if self.dry_run:
logger.debug("🔄 DRY RUN - Skipping API order sync")
return
# Use the new account_active_orders API
response = await self.lighter.account_active_orders(self.symbol)
# Extract orders from response
orders = response.get('orders', [])
if not orders:
# Clear all active orders if API returns empty
if self.active_orders:
logger.info(f"🧹 API shows no active orders, clearing {len(self.active_orders)} tracked orders")
self.active_orders.clear()
return
# Process orders from API
api_order_ids = set()
active_api_orders = []
for order in orders:
# Check if order is truly active
status = order.get('status', '').lower()
remaining_amount = float(order.get('remaining_base_amount', '0'))
order_id = str(order.get('order_id', order.get('order_index', '')))
if status in ['active', 'open', 'pending', 'live'] and remaining_amount > 0:
if order_id:
api_order_ids.add(order_id)
active_api_orders.append(order)
# Update local tracking with API data
is_ask = order.get('is_ask', False)
side = 'sell' if is_ask else 'buy'
price = float(order.get('price', '0'))
quantity = remaining_amount
# Determine position type based on current positions and side
position_type = 'short' if (side == 'sell' and self.long_position <= 0) or (side == 'buy' and self.short_position > 0) else 'long'
self.active_orders[order_id] = {
'side': side,
'price': price,
'quantity': quantity,
'position_type': position_type,
'order_id': order_id,
'api_synced': True
}
# Remove orders that are no longer active according to API
tracked_order_ids = set(self.active_orders.keys())
completed_order_ids = tracked_order_ids - api_order_ids
if completed_order_ids:
logger.info(f"📋 API sync: {len(completed_order_ids)} orders completed, {len(active_api_orders)} still active")
for order_id in completed_order_ids:
completed_order = self.active_orders.pop(order_id, None)
if completed_order:
logger.debug(f"🎯 API removed completed order: {order_id} ({completed_order['side']} {completed_order['quantity']} @ {completed_order['price']})")
logger.debug(f"✅ API sync complete: {len(self.active_orders)} active orders")
except Exception as e:
logger.warning(f"Failed to sync orders from API: {e}")
def check_orders_status(self):
"""Update order counters from active orders"""
# Reset counters
self.buy_long_orders = 0.0
self.sell_long_orders = 0.0
self.sell_short_orders = 0.0
self.buy_short_orders = 0.0
# Count active orders by type
for order_id, order_info in self.active_orders.items():
side = order_info.get('side', '')
quantity = order_info.get('quantity', 0)
if side == 'buy':
if self.short_position > 0:
self.buy_short_orders += quantity
else:
self.buy_long_orders += quantity
elif side == 'sell':
if self.long_position > 0:
self.sell_long_orders += quantity
else:
self.sell_short_orders += quantity
# Log order counts when there are orders
active_count = len(self.active_orders)
if active_count > 0:
logger.debug(f"Order counts: {active_count} active orders - Long(buy={self.buy_long_orders:.1f}, sell={self.sell_long_orders:.1f}), Short(sell={self.sell_short_orders:.1f}, buy={self.buy_short_orders:.1f})")
async def place_order(self, side, price, quantity, position_type='long'):
"""Place an order with order limit control"""
try:
# Check order limits only
if len(self.active_orders) >= self.max_orders:
logger.warning(f"Max orders ({self.max_orders}) reached, skipping order placement")
return None
# Validate and format
formatted_price = self.lighter.format_price(price, self.symbol)
is_valid, formatted_quantity, error_msg = self.lighter.validate_order_amount(
formatted_price, quantity, self.symbol
)
if not is_valid:
logger.info(f"Order adjusted: {error_msg}")
quantity = formatted_quantity
if self.dry_run:
quote_value = formatted_price * abs(quantity)
logger.info(f"🔄 DRY RUN - {side.upper()}: {quantity} @ ${formatted_price:.6f} (${quote_value:.2f})")
# Generate fake order ID for dry run tracking
order_id = str(int(time.time() * 1000))
# Track order for dry run
self.active_orders[order_id] = {
'side': side,
'price': formatted_price,
'quantity': abs(quantity),
'position_type': position_type,
'timestamp': time.time(),
'tx_hash': None
}
return order_id
# Real order placement
logger.info(f"📈 REAL - {side}: {quantity} {self.symbol} @ ${formatted_price:.6f}")
# Adjust quantity for short positions
if position_type == 'short' and side == 'sell':
quantity = -abs(quantity)
elif position_type == 'long' and side == 'sell':
quantity = -abs(quantity) # Exit long position with negative quantity
result = await self.lighter.limit_order(
ticker=self.symbol,
amount=quantity,
price=formatted_price,
tif='GTC'
)
if result is None:
logger.error("Order failed: No result returned")
return None
# Extract order information from result
# Lighter returns (tx_info, tx_hash, error) or similar structure
if isinstance(result, tuple) and len(result) >= 2:
tx_info, tx_hash = result[0], result[1]
error = result[2] if len(result) > 2 else None
if error is not None:
logger.error(f"Order failed: {error}")
return None
# Generate order ID for tracking
order_id = str(int(time.time() * 1000)) # Timestamp-based ID
# Extract real order ID from transaction info if available
if hasattr(tx_info, 'event_info') and tx_info.event_info:
try:
# Try to extract order index from event_info
if hasattr(tx_info.event_info, 'order_index'):
order_id = str(tx_info.event_info.order_index)
elif hasattr(tx_info.event_info, 'order_id'):
order_id = str(tx_info.event_info.order_id)
except:
pass # Use timestamp ID as fallback
# Track order
self.active_orders[order_id] = {
'side': side,
'price': formatted_price,
'quantity': abs(quantity),
'position_type': position_type,
'timestamp': time.time(),
'tx_hash': str(tx_hash) if tx_hash else None
}
logger.info(f"✅ Order placed: {order_id}")
return order_id
else:
logger.error(f"Unexpected result format: {result}")
return None
except Exception as e:
logger.error(f"Failed to place order: {e}")
return None
async def setup_account_orders_websocket(self):
"""Setup WebSocket subscription for account orders with proper authentication"""
try:
if self.dry_run:
logger.info("🔄 DRY RUN - Would setup account orders WebSocket")
return
logger.info("🔌 Setting up account orders WebSocket with authentication...")
self._ws_start_time = time.time() # Track WebSocket start time
# Generate authentication token using SignerClient
auth_token = self.lighter.client.create_auth_token_with_expiry()
if not auth_token or len(auth_token) < 2 or auth_token[1]: # Check for error
logger.error(f"Failed to generate auth token: {auth_token}")
return
auth_token_str = auth_token[0] # Extract token string
logger.info("✅ Authentication token generated successfully")
# Create custom WebSocket client for account orders
import websockets
import asyncio
async def run_account_orders_websocket():
"""Run dedicated WebSocket for account orders with proper connection handling"""
websocket_url = "wss://mainnet.zklighter.elliot.ai/stream"
max_retries = 5
retry_count = 0
while retry_count < max_retries and not self.shutdown_requested:
try:
logger.info("🌐 Connecting to account orders WebSocket...")
async with websockets.connect(websocket_url) as ws:
# Wait for connection confirmation
connected = False
subscribed = False
# Set up connection timeout
connection_timeout = 10 # seconds
start_time = asyncio.get_event_loop().time()
# Listen for messages with proper state handling
async for message in ws:
if self.shutdown_requested:
break
# Check for connection timeout
if asyncio.get_event_loop().time() - start_time > connection_timeout and not connected:
logger.warning("⏱️ WebSocket connection timeout")
break
try:
data = json.loads(message)
message_type = data.get('type', '')
# Log all messages for debugging
logger.debug(f"📨 WebSocket message: type={message_type}")
# Handle connection lifecycle
if message_type == 'connected':
logger.info("🔗 WebSocket connected, sending subscription...")
connected = True
# Now send subscription request
subscribe_msg = {
"type": "subscribe",
"channel": f"account_orders/{self.market_id}/{self.lighter.account_idx}",
"auth": auth_token_str
}
await ws.send(json.dumps(subscribe_msg))
logger.info(f"📋 Sent subscription for account orders market {self.market_id}")
elif message_type == 'subscribed/account_orders' or message_type.startswith('subscribed'):
channel = data.get('channel', '')
if 'account_orders' in channel:
logger.info(f"✅ Successfully subscribed to account orders: {channel}")
subscribed = True
retry_count = 0 # Reset retry count on successful subscription
elif message_type == 'update/account_orders':
if subscribed: # Only process if properly subscribed
logger.info(f"🔍 Processing account orders update: {len(data.get('orders', {}).get(str(self.market_id), []))} orders")
await self.handle_account_orders_update(data)
else:
logger.debug("Ignoring account orders update - not properly subscribed yet")
elif message_type == 'error':
error_msg = data.get('message', data.get('error', 'Unknown error'))
logger.error(f"❌ WebSocket error: {error_msg}")
# Break to trigger reconnection
break
elif message_type == 'ping':
# Respond to ping with pong
await ws.send(json.dumps({"type": "pong"}))
logger.debug("🏓 Responded to ping")
elif message_type == 'pong':
logger.debug("🏓 Received pong")
else:
# Log unhandled messages for debugging
if message_type and message_type not in ['heartbeat', 'status']:
logger.info(f"📨 Unhandled WebSocket message: type={message_type}, data={data}")
else:
logger.debug(f"📨 Minor WebSocket message: {message_type}")
except json.JSONDecodeError as e:
logger.warning(f"❌ Failed to parse WebSocket message: {e}")
except Exception as e:
logger.error(f"❌ Error handling WebSocket message: {e}")
except websockets.exceptions.ConnectionClosed as e:
logger.warning(f"🔌 WebSocket connection closed: {e}")
retry_count += 1
if retry_count < max_retries:
wait_time = min(2 ** retry_count, 10) # Exponential backoff, max 10s
logger.info(f"⏳ Retrying WebSocket connection in {wait_time}s (attempt {retry_count}/{max_retries})")
await asyncio.sleep(wait_time)
except websockets.exceptions.WebSocketException as e:
logger.error(f"❌ WebSocket error: {e}")
retry_count += 1
if retry_count < max_retries:
wait_time = min(2 ** retry_count, 10)
logger.info(f"⏳ Retrying WebSocket connection in {wait_time}s (attempt {retry_count}/{max_retries})")
await asyncio.sleep(wait_time)
except Exception as e:
logger.error(f"❌ Unexpected WebSocket error: {e}")
retry_count += 1
if retry_count < max_retries:
await asyncio.sleep(5)
if retry_count >= max_retries:
logger.error(f"❌ WebSocket connection failed after {max_retries} retries")
else:
logger.info("✅ WebSocket connection closed normally")
# Start WebSocket in background
asyncio.create_task(run_account_orders_websocket())
logger.info("✅ Account orders WebSocket setup complete")
except Exception as e:
logger.error(f"Failed to setup account orders WebSocket: {e}")
async def handle_account_orders_update(self, data):
"""Handle account orders updates from dedicated WebSocket with improved lifecycle tracking"""
try:
# Mark that we received an account update
self._last_account_update_time = time.time()
# Extract orders data - Lighter format: {"orders": {"{MARKET_INDEX}": [Order]}}
orders_data = data.get('orders', {})
market_orders = orders_data.get(str(self.market_id), [])
# Process order lifecycle events - Lighter Protocol format
order_events = data.get('order_events', [])
for event in order_events:
await self.handle_order_lifecycle_event(event)
# Reset order counters when we have meaningful WebSocket data
if market_orders or len(self.active_orders) == 0:
self.buy_long_orders = 0.0
self.sell_long_orders = 0.0
self.sell_short_orders = 0.0
self.buy_short_orders = 0.0
else:
# WebSocket empty but we have tracked orders - use internal tracking for counters
logger.debug(f"WebSocket empty, using internal tracking for {len(self.active_orders)} orders")
self._update_order_counters_from_tracking()
# Get current WebSocket order IDs
websocket_order_ids = set()
real_active_orders = []
# Lighter Protocol uses array format: [{Order}, {Order}, ...]
if isinstance(market_orders, list):
for order in market_orders:
# Check Lighter Protocol order status and remaining amount
status = order.get('status', '').lower()
remaining_base_amount = float(order.get('remaining_base_amount', '0'))
order_id = order.get('order_id', order.get('order_index', ''))
if order_id:
websocket_order_ids.add(str(order_id))
# Only count orders that are truly active (open status AND have remaining amount)
if status in ['active', 'open', 'pending', 'live'] and remaining_base_amount > 0:
if order_id:
real_active_orders.append(str(order_id))
self._process_active_order_from_websocket(order)
# Find orders that are no longer in WebSocket data (filled/cancelled)
tracked_order_ids = set(self.active_orders.keys())
missing_order_ids = tracked_order_ids - websocket_order_ids
if missing_order_ids:
logger.info(f"🎯 Orders no longer in WebSocket (filled/cancelled): {len(missing_order_ids)} orders")
# Remove orders that are no longer present in WebSocket
for order_id in missing_order_ids:
filled_order = self.active_orders.pop(order_id, None)
if filled_order:
logger.info(f"🎯 Removed completed order: {order_id} ({filled_order['side']} {filled_order['quantity']} @ {filled_order['price']})")
# Assume order was filled and update positions
await self._update_positions_from_completed_order(filled_order)
# Reconcile tracked vs real orders with improved logic
await self._reconcile_order_tracking(real_active_orders)
except Exception as e:
logger.error(f"Failed to handle account orders update: {e}")
logger.debug(f"Account orders data: {data}")
async def _update_positions_from_completed_order(self, order_info):
"""Update positions when an order is completed (filled/cancelled)"""
try:
side = order_info.get('side', '')
position_type = order_info.get('position_type', '')
quantity = order_info.get('quantity', 0)
price = order_info.get('price', 0)
# Assume the order was filled (we can't distinguish between filled vs cancelled from missing orders)
# This is safer than not updating positions at all
if quantity > 0:
logger.info(f"💰 Position update from completed order: {side.upper()} {quantity} {position_type} @ ${price:.6f}")
# Update positions based on completed order
if side == 'buy' and position_type == 'long':
self.long_position += quantity
elif side == 'sell' and position_type == 'long':
self.long_position = max(0, self.long_position - quantity)
elif side == 'sell' and position_type == 'short':
self.short_position += quantity
elif side == 'buy' and position_type == 'short':
self.short_position = max(0, self.short_position - quantity)
logger.info(f"💰 Updated positions - Long: {self.long_position}, Short: {self.short_position}")
except Exception as e:
logger.error(f"Failed to update positions from completed order: {e}")
async def _update_positions_from_fill(self, ws_order, local_order):
"""Update positions based on order fill from WebSocket data"""
try:
side = local_order.get('side', '')
position_type = local_order.get('position_type', '')
filled_quantity = float(ws_order.get('filled_base_amount', '0'))
fill_price = float(ws_order.get('price', '0'))
if filled_quantity > 0:
logger.info(f"💰 Position update from fill: {side.upper()} {filled_quantity} {position_type} @ ${fill_price:.6f}")
# Update positions based on fill
if side == 'buy' and position_type == 'long':
self.long_position += filled_quantity
elif side == 'sell' and position_type == 'long':
self.long_position = max(0, self.long_position - filled_quantity)
elif side == 'sell' and position_type == 'short':
self.short_position += filled_quantity
elif side == 'buy' and position_type == 'short':
self.short_position = max(0, self.short_position - filled_quantity)
logger.info(f"💰 Updated positions - Long: {self.long_position}, Short: {self.short_position}")
except Exception as e:
logger.error(f"Failed to update positions from fill: {e}")
def _update_order_counters_from_tracking(self):
"""Update order counters from internal tracking"""
for order_id, order_info in self.active_orders.items():
side = order_info.get('side', '')
quantity = order_info.get('quantity', 0)
if side == 'buy':
if self.short_position > 0:
self.buy_short_orders += quantity
else:
self.buy_long_orders += quantity
elif side == 'sell':
if self.long_position > 0:
self.sell_long_orders += quantity
else:
self.sell_short_orders += quantity
def _process_active_order_from_websocket(self, order):
"""Process an active order from WebSocket data"""
side = order.get('side', '').lower()
remaining_amount = float(order.get('remaining_base_amount', '0'))
if side == 'buy':
if self.short_position > 0:
self.buy_short_orders += remaining_amount
else:
self.buy_long_orders += remaining_amount
elif side == 'sell':
if self.long_position > 0:
self.sell_long_orders += remaining_amount
else:
self.sell_short_orders += remaining_amount
async def _reconcile_order_tracking(self, real_active_orders):
"""Reconcile internal tracking with WebSocket reality"""
real_count = len(real_active_orders)
tracked_count = len(self.active_orders)
current_time = time.time()
# Log order counts when there are orders
if real_count > 0 or tracked_count > 0:
logger.info(f"📋 Orders: WebSocket={real_count}, Tracked={tracked_count} | Long(buy={self.buy_long_orders:.1f}, sell={self.sell_long_orders:.1f}), Short(sell={self.sell_short_orders:.1f}, buy={self.buy_short_orders:.1f})")
# Track last WebSocket order count for force cleanup
self._last_ws_order_count = real_count
# Improved sync logic with better reconciliation
if real_count != tracked_count:
sync_key = f"{real_count}_{tracked_count}"
if sync_key not in self.sync_warning_throttle or current_time - self.sync_warning_throttle[sync_key] > 30:
logger.warning(f"🔄 Order sync: WebSocket={real_count}, Tracked={tracked_count}")
self.sync_warning_throttle[sync_key] = current_time
# Smart reconciliation based on order age and WebSocket consistency
if real_count < tracked_count:
excess_count = tracked_count - real_count
if real_count == 0 and tracked_count > 0:
# WebSocket shows 0 orders - use progressive cleanup based on order age
# Initialize WebSocket zero counter for this specific case
if not hasattr(self, '_ws_zero_consecutive'):
self._ws_zero_consecutive = 0
self._last_ws_zero_time = current_time
# Count consecutive WebSocket=0 readings
if current_time - getattr(self, '_last_ws_zero_time', current_time) < 10: # Within last 10 seconds
self._ws_zero_consecutive += 1
else:
self._ws_zero_consecutive = 1 # Reset if gap in readings
self._last_ws_zero_time = current_time
# Progressive cleanup based on how long WebSocket has shown 0
if self._ws_zero_consecutive >= 3: # WebSocket consistently 0 for 3+ readings
# First try to verify with REST API if we have significant discrepancy
if self._ws_zero_consecutive >= 5 and tracked_count >= 3:
logger.info(f"🔍 WebSocket=0 for {self._ws_zero_consecutive} cycles with {tracked_count} tracked orders - verifying via REST API")
try:
# Query actual orders from REST API as fallback
actual_orders = await self._verify_orders_via_rest_api()
if actual_orders is not None:
logger.info(f"🔍 REST API verification: {len(actual_orders)} actual orders found")
# If REST API shows 0 orders, trust it and clear stale local tracking
if len(actual_orders) == 0:
logger.warning(f"🔄 REST API confirms 0 orders - clearing {tracked_count} stale tracked orders")
self.active_orders.clear()
self._ws_zero_consecutive = 0
return
except Exception as e:
logger.debug(f"REST API verification failed: {e}")
# Fallback to time-based cleanup if REST verification not available
old_threshold = 60 if self._ws_zero_consecutive >= 5 else 120
old_orders = [
(order_id, order_info) for order_id, order_info in self.active_orders.items()
if current_time - order_info['timestamp'] > old_threshold
]
if old_orders:
logger.info(f"🔄 WebSocket=0 for {self._ws_zero_consecutive} readings: Clearing {len(old_orders)} orders (>{old_threshold}s old)")
for order_id, _ in old_orders:
self.active_orders.pop(order_id, None)
elif self._ws_zero_consecutive >= 12 and tracked_count <= 4:
# If WebSocket consistently shows 0 for very long time (3+ minutes) and we don't have too many orders
logger.warning(f"🔄 WebSocket=0 for {self._ws_zero_consecutive} readings (3+ min): Clearing all {tracked_count} remaining orders")
self.active_orders.clear()
self._ws_zero_consecutive = 0
else:
logger.debug(f"WebSocket=0 (reading #{self._ws_zero_consecutive}), keeping {tracked_count} recent orders")
else:
# Remove stale orders progressively
stale_orders = [
(order_id, order_info) for order_id, order_info in self.active_orders.items()
if current_time - order_info['timestamp'] > 90 # 1.5 minutes old
]
stale_orders.sort(key=lambda x: x[1]['timestamp'])
orders_to_remove = stale_orders[:min(excess_count, len(stale_orders))]
for order_id, _ in orders_to_remove:
self.active_orders.pop(order_id, None)
logger.info(f"🔄 Synced: Removed stale order {order_id} (>1.5min old)")
elif real_count > tracked_count:
logger.debug(f"📋 WebSocket shows {real_count - tracked_count} additional orders (normal - others' orders)")
# Reset WebSocket zero counters since we have valid data
if hasattr(self, '_ws_zero_consecutive'):
self._ws_zero_consecutive = 0
else:
# Orders are in sync
if real_count > 0:
logger.debug(f"📋 Orders in sync: {real_count} orders")
# Reset WebSocket zero counters since sync is good
if hasattr(self, '_ws_zero_consecutive'):
self._ws_zero_consecutive = 0
async def handle_order_lifecycle_event(self, event):
"""Handle order lifecycle events (fill, cancel, etc.)"""
try:
event_type = event.get('type', '').lower()
order_id = str(event.get('order_id', ''))
if event_type in ['fill', 'partial_fill', 'cancel', 'cancelled']:
# Remove filled or cancelled orders from tracking
if order_id in self.active_orders:
order_info = self.active_orders.pop(order_id, None)
if order_info:
logger.info(f"🔄 Order {event_type}: Removed order {order_id} ({order_info['side']} {order_info['quantity']} @ {order_info['price']})")
# Update positions for fills
if event_type in ['fill', 'partial_fill']:
await self.handle_order_fill(event, order_info)
except Exception as e:
logger.error(f"Failed to handle order lifecycle event: {e}")
async def handle_order_fill(self, fill_event, order_info):
"""Handle order fill events and update positions"""
try:
side = order_info.get('side', '')
position_type = order_info.get('position_type', '')
fill_quantity = float(fill_event.get('fill_quantity', fill_event.get('quantity', 0)))
fill_price = float(fill_event.get('fill_price', fill_event.get('price', 0)))
logger.info(f"💰 Order filled: {side.upper()} {fill_quantity} {position_type} @ ${fill_price:.6f}")
# Update positions based on fill
if side == 'buy' and position_type == 'long':
self.long_position += fill_quantity
elif side == 'sell' and position_type == 'long':
self.long_position = max(0, self.long_position - fill_quantity)
elif side == 'sell' and position_type == 'short':
self.short_position += fill_quantity
elif side == 'buy' and position_type == 'short':
self.short_position = max(0, self.short_position - fill_quantity)
logger.info(f"💰 Position update - Long: {self.long_position}, Short: {self.short_position}")
except Exception as e:
logger.error(f"Failed to handle order fill: {e}")
async def cancel_all_orders(self):
"""Cancel all orders (simplified)"""
try:
if self.dry_run:
logger.info("🔄 DRY RUN - Would cancel all orders")
cancelled_count = len(self.active_orders)
self.active_orders.clear()
logger.info(f"✅ DRY RUN: {cancelled_count} orders cancelled")
return cancelled_count
logger.info("🚫 Cancelling all orders...")
# Clear internal tracking immediately to prevent displaying stale counts
cancelled_count = len(self.active_orders)
self.active_orders.clear()
# Attempt actual cancellation with improved error handling
try:
result = await self.lighter.cancel_all_orders()
# Handle different possible return formats from the API
if result is None:
logger.warning("⚠️ Bulk cancellation returned None")
elif isinstance(result, tuple):
if len(result) >= 2:
response, error = result[0], result[1]
if error is None:
logger.info(f"✅ {cancelled_count} orders cancelled successfully")
else:
logger.warning(f"⚠️ Cancellation error: {error}")
elif len(result) == 1:
# Single tuple element - could be success response
logger.info(f"✅ {cancelled_count} orders cancelled (single response)")
else:
logger.warning(f"⚠️ Unexpected tuple format: {result}")
else:
# Single value returned - assume success
logger.info(f"✅ {cancelled_count} orders cancelled successfully")
except Exception as api_error:
logger.warning(f"⚠️ API cancellation failed: {api_error}")
return cancelled_count
except Exception as e:
logger.error(f"Cancel all orders failed: {e}")
# Still clear tracking to prevent stale display
cancelled_count = len(self.active_orders)
self.active_orders.clear()
return cancelled_count
def force_cleanup_stale_orders(self):
"""Force cleanup of stale order tracking to prevent display issues"""
try:
current_time = time.time()
initial_count = len(self.active_orders)
# Conservative cleanup - only very old orders (5 minutes)
stale_threshold = 300 # 5 minutes (was 2 minutes - too aggressive)
stale_orders = [
order_id for order_id, order_info in self.active_orders.items()
if current_time - order_info['timestamp'] > stale_threshold
]
# Remove truly stale orders
for order_id in stale_orders:
self.active_orders.pop(order_id, None)