-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathmulti_symbol_trading.py
More file actions
517 lines (413 loc) · 20.6 KB
/
multi_symbol_trading.py
File metadata and controls
517 lines (413 loc) · 20.6 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
"""
Universal Symbol Trading Test Script
Tests real trading operations for any cryptocurrency symbol including:
- Query price and quantity precision from API
- Place minimum allowed amount orders (dynamically calculated)
- Test both limit and market orders
- Test order cancellation
IMPORTANT: This performs REAL trading operations with REAL money.
Only use with small amounts and ensure you understand the risks.
"""
import os
import sys
import asyncio
import logging
from decimal import Decimal, ROUND_DOWN
from dotenv import load_dotenv
from pylighter.client import Lighter
# Load environment variables
load_dotenv()
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger('UniversalTradingTest')
class UniversalTradingTester:
"""Universal Trading Test Suite - works with any supported symbol"""
def __init__(self, symbol=None):
self.lighter = None
self.symbol = symbol or "BTC-USD" # Default to BTC if not specified
self.test_results = {}
self.placed_orders = []
async def setup(self):
"""Setup the client connection"""
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.\n"
"Create them in a .env file in the project root."
)
logger.info("Initializing Lighter client...")
self.lighter = Lighter(key=api_key, secret=api_secret)
await self.lighter.init_client()
logger.info("✅ Client initialized successfully")
# Verify symbol exists and find the best match
available_symbols = list(self.lighter.ticker_to_idx.keys())
if self.symbol not in available_symbols:
# Try to find similar symbols
symbol_base = self.symbol.split('-')[0] if '-' in self.symbol else self.symbol
similar_symbols = [s for s in available_symbols if symbol_base in s]
if similar_symbols:
self.symbol = similar_symbols[0]
logger.info(f"Symbol not found, using closest match: {self.symbol}")
else:
logger.error(f"Symbol {self.symbol} not found!")
logger.info(f"Available symbols: {', '.join(available_symbols[:20])}...")
raise ValueError(f"Symbol {self.symbol} not available")
logger.info(f"✅ Trading symbol confirmed: {self.symbol}")
async def query_symbol_precision(self):
"""Query symbol precision and limits from API"""
logger.info(f"📏 Querying {self.symbol} precision and limits...")
try:
# Get precision info dynamically from API
price_precision = self.lighter.ticker_to_price_precision.get(self.symbol)
size_precision = self.lighter.ticker_to_lot_precision.get(self.symbol)
min_base = self.lighter.ticker_min_base.get(self.symbol)
min_quote = self.lighter.ticker_min_quote.get(self.symbol)
# Get current market data
orderbook = await self.lighter.orderbook_orders(self.symbol, limit=1)
current_price = 0
bid = ask = 0
if orderbook.get('bids') and orderbook.get('asks'):
bid = float(orderbook['bids'][0]['price'])
ask = float(orderbook['asks'][0]['price'])
current_price = (bid + ask) / 2
precision_info = {
'symbol': self.symbol,
'price_precision': price_precision,
'size_precision': size_precision,
'min_base_amount': min_base,
'min_quote_amount': min_quote,
'current_price': current_price,
'bid': bid,
'ask': ask
}
self.test_results['precision'] = {
'success': True,
'data': precision_info
}
logger.info(f"✅ {self.symbol} Precision Info:")
logger.info(f" Price precision: {price_precision} decimals")
logger.info(f" Size precision: {size_precision} decimals")
logger.info(f" Min base amount: {min_base} {self.symbol.split('-')[0]}")
logger.info(f" Min quote amount: ${min_quote}")
logger.info(f" Current price: ${current_price:.6f}")
logger.info(f" Spread: ${ask-bid:.6f}")
return precision_info
except Exception as e:
logger.error(f"❌ Failed to query precision: {e}")
self.test_results['precision'] = {
'success': False,
'error': str(e)
}
raise
def calculate_minimum_order_amount(self, precision_info):
"""Calculate minimum order amount that satisfies all constraints for any symbol"""
min_base = precision_info['min_base_amount']
min_quote = precision_info['min_quote_amount']
current_price = precision_info['current_price']
size_precision = precision_info['size_precision']
symbol_base = precision_info['symbol'].split('-')[0]
# Calculate minimum base amount needed for quote constraint
min_base_for_quote = min_quote / current_price
# Both constraints must be satisfied - use the MAXIMUM of the two
required_base = max(min_base, min_base_for_quote)
# Add minimal buffer only if needed for precision rounding
if size_precision > 0:
required_base = required_base * 1.01 # 1% buffer for precision rounding
# Round UP to the required precision to ensure we meet minimums
precision_factor = 10 ** size_precision
final_amount = int(required_base * precision_factor + 0.999) / precision_factor
# Double-check we meet both constraints
quote_value = final_amount * current_price
logger.info(f"💰 Minimum order calculation for {precision_info['symbol']}:")
logger.info(f" Min base constraint: {min_base} {symbol_base}")
logger.info(f" Min quote constraint: {min_base_for_quote:.6f} {symbol_base} (${min_quote} ÷ ${current_price:.6f})")
logger.info(f" Binding constraint: {'BASE' if min_base > min_base_for_quote else 'QUOTE'}")
logger.info(f" Selected amount: {final_amount} {symbol_base} (minimum allowed)")
logger.info(f" Quote value: ${quote_value:.6f}")
logger.info(f" ✓ Meets min quote: ${quote_value:.2f} >= ${min_quote:.2f}")
logger.info(f" ✓ Meets min base: {final_amount} >= {min_base}")
return final_amount
async def place_limit_buy_order(self, precision_info):
"""Place minimum allowed limit buy order"""
logger.info(f"🛒 Placing limit buy order for {self.symbol}...")
try:
amount = self.calculate_minimum_order_amount(precision_info)
# Use mid-price for better fill chance
bid_price = precision_info['bid']
ask_price = precision_info['ask']
target_price = (ask_price + bid_price) / 2
# Round price to precision
price_precision = precision_info['price_precision']
precision_factor = 10 ** price_precision
target_price = int(target_price * precision_factor) / precision_factor
symbol_base = precision_info['symbol'].split('-')[0]
logger.info(f" Amount: {amount} {symbol_base}")
logger.info(f" Price: ${target_price:.6f}")
logger.info(f" Total: ${amount * target_price:.6f}")
# Place the order
tx, tx_hash, err = await self.lighter.limit_order(
ticker=self.symbol,
amount=amount, # Positive for buy
price=target_price,
tif='GTC' # Good Till Cancelled
)
if err is not None:
raise Exception(f"Order creation failed: {err}")
order_id = 0 # Default client_order_index
self.placed_orders.append({
'id': order_id,
'type': 'limit_buy',
'symbol': self.symbol,
'amount': amount,
'price': target_price
})
self.test_results['limit_buy'] = {
'success': True,
'data': {
'order_id': order_id,
'amount': amount,
'price': target_price,
'tx_hash': str(tx_hash) if tx_hash else None
}
}
logger.info(f"✅ Limit buy order placed successfully")
logger.info(f" Order ID: {order_id}")
logger.info(f" TX Hash: {tx_hash}")
return {'tx': tx, 'tx_hash': tx_hash, 'order_id': order_id}
except Exception as e:
logger.error(f"❌ Failed to place limit buy order: {e}")
self.test_results['limit_buy'] = {
'success': False,
'error': str(e)
}
raise
async def place_limit_sell_order(self, precision_info, buy_amount):
"""Place limit sell order to close position"""
logger.info(f"💸 Placing limit sell order for {self.symbol}...")
try:
# Sell at current ask price for quick fill
ask_price = precision_info['ask']
# Round price to precision
price_precision = precision_info['price_precision']
precision_factor = 10 ** price_precision
sell_price = int(ask_price * precision_factor) / precision_factor
symbol_base = precision_info['symbol'].split('-')[0]
logger.info(f" Amount: -{buy_amount} {symbol_base} (negative = sell)")
logger.info(f" Price: ${sell_price:.6f}")
# Place the sell order
tx, tx_hash, err = await self.lighter.limit_order(
ticker=self.symbol,
amount=-buy_amount, # Negative for sell
price=sell_price,
tif='GTC'
)
if err is not None:
raise Exception(f"Sell order creation failed: {err}")
order_id = 0 # Default client_order_index
self.placed_orders.append({
'id': order_id,
'type': 'limit_sell',
'symbol': self.symbol,
'amount': -buy_amount,
'price': sell_price
})
self.test_results['limit_sell'] = {
'success': True,
'data': {
'order_id': order_id,
'amount': -buy_amount,
'price': sell_price,
'tx_hash': str(tx_hash) if tx_hash else None
}
}
logger.info(f"✅ Limit sell order placed successfully")
logger.info(f" Order ID: {order_id}")
logger.info(f" TX Hash: {tx_hash}")
return {'tx': tx, 'tx_hash': tx_hash, 'order_id': order_id}
except Exception as e:
logger.error(f"❌ Failed to place limit sell order: {e}")
self.test_results['limit_sell'] = {
'success': False,
'error': str(e)
}
raise
async def test_market_orders(self, precision_info):
"""Test market order functionality"""
logger.info(f"🏃 Testing market orders for {self.symbol}...")
try:
amount = self.calculate_minimum_order_amount(precision_info)
symbol_base = precision_info['symbol'].split('-')[0]
# Test market buy order
logger.info(" Testing market buy...")
buy_tx, buy_tx_hash, buy_err = await self.lighter.market_order(
ticker=self.symbol,
amount=amount, # Positive for buy
slippage_tolerance=0.05 # 5% slippage tolerance
)
if buy_err is not None:
raise Exception(f"Market buy failed: {buy_err}")
buy_order_id = 0 # Default client_order_index
logger.info(f"✅ Market buy order placed: {buy_order_id}")
# Small delay before sell
await asyncio.sleep(1)
# Test market sell order
logger.info(" Testing market sell...")
sell_tx, sell_tx_hash, sell_err = await self.lighter.market_order(
ticker=self.symbol,
amount=-amount, # Negative for sell
slippage_tolerance=0.05
)
if sell_err is not None:
raise Exception(f"Market sell failed: {sell_err}")
sell_order_id = 0 # Default client_order_index
logger.info(f"✅ Market sell order placed: {sell_order_id}")
self.test_results['market_orders'] = {
'success': True,
'data': {
'buy_order_id': buy_order_id,
'sell_order_id': sell_order_id,
'amount': amount,
'buy_tx_hash': str(buy_tx_hash) if buy_tx_hash else None,
'sell_tx_hash': str(sell_tx_hash) if sell_tx_hash else None
}
}
logger.info("✅ Market orders test completed")
except Exception as e:
logger.error(f"❌ Market orders test failed: {e}")
self.test_results['market_orders'] = {
'success': False,
'error': str(e)
}
async def run_full_test_suite(self):
"""Run the complete trading test suite"""
logger.info(f"🧪 Starting Universal Trading Test Suite for {self.symbol}")
logger.info("="*60)
logger.info("⚠️ WARNING: This performs REAL trading with REAL money!")
logger.info("="*60)
try:
# Setup
await self.setup()
# Test 1: Query precision (dynamic from API)
precision_info = await self.query_symbol_precision()
# Test 2: Place limit buy order
buy_order = await self.place_limit_buy_order(precision_info)
buy_amount = self.test_results['limit_buy']['data']['amount']
# Skip order fill checking due to auth issues, proceed directly to sell test
logger.info("Proceeding directly to sell test (auth issues prevent order status checks)")
# Test 3: Place limit sell order
await self.place_limit_sell_order(precision_info, buy_amount)
# Test 4: Market orders (optional - may fail due to OrderExpiry issues)
try:
await self.test_market_orders(precision_info)
except Exception as e:
logger.warning(f"Market orders test skipped due to known issues: {e}")
except Exception as e:
logger.error(f"❌ Test suite failed: {e}")
finally:
# Cleanup
if self.lighter:
await self.lighter.cleanup()
# Print results
self.print_test_summary()
def print_test_summary(self):
"""Print comprehensive test summary"""
logger.info("="*60)
logger.info(f"📋 {self.symbol} TRADING TEST SUMMARY")
logger.info("="*60)
total_tests = len(self.test_results)
passed_tests = sum(1 for result in self.test_results.values() if result['success'])
failed_tests = total_tests - passed_tests
logger.info(f"Total Tests: {total_tests}")
logger.info(f"Passed: {passed_tests} ✅")
logger.info(f"Failed: {failed_tests} ❌")
logger.info(f"Success Rate: {(passed_tests/total_tests)*100:.1f}%" if total_tests > 0 else "N/A")
logger.info("\n📊 DETAILED RESULTS:")
for test_name, result in self.test_results.items():
status = "✅" if result['success'] else "❌"
logger.info(f"{status} {test_name}")
if result['success'] and 'data' in result:
if test_name == 'precision':
data = result['data']
symbol_base = data['symbol'].split('-')[0]
logger.info(f" Price: ${data['current_price']:.6f}, Min: {data['min_base_amount']} {symbol_base}")
elif 'order_id' in result['data']:
logger.info(f" Order ID: {result['data']['order_id']}")
elif not result['success']:
logger.info(f" Error: {result.get('error', 'Unknown error')}")
if failed_tests == 0:
logger.info(f"\n🎉 ALL TESTS PASSED! {self.symbol} trading functionality is working correctly.")
else:
logger.info(f"\n⚠️ {failed_tests} test(s) failed. Please review the errors above.")
logger.info("="*60)
def show_available_symbols():
"""Show available trading symbols"""
print("\n📋 To see available symbols, the script needs to connect to the API.")
print("Run without arguments to see available symbols, or specify a symbol directly.")
print("\nCommon symbols to try:")
print(" • BTC-USD, ETH-USD, SOL-USD")
print(" • XRP, DOGE, ADA")
print(" • AVAX-USD, MATIC-USD, DOT-USD")
async def main():
"""Main function with symbol selection"""
print("🚀 Universal Symbol Trading Test Script")
print("="*60)
print("⚠️ IMPORTANT SAFETY WARNING:")
print("This script performs REAL trading operations with REAL money!")
print("Only use with small amounts and ensure you understand the risks.")
print("="*60)
# Check for environment variables
if not os.getenv("LIGHTER_KEY") or not os.getenv("LIGHTER_SECRET"):
print("\n❌ Missing API credentials!")
print("Please create a .env file with:")
print("LIGHTER_KEY=your_api_key")
print("LIGHTER_SECRET=your_api_secret")
return
# Get symbol from command line argument
symbol = None
if len(sys.argv) > 1:
symbol = sys.argv[1].upper()
# Add -USD suffix if not present and doesn't already have a suffix
if '-' not in symbol and len(symbol) <= 5:
symbol = f"{symbol}-USD"
if not symbol:
# Show available symbols first
try:
print("\n🔍 Connecting to fetch available symbols...")
temp_client = Lighter(key=os.getenv("LIGHTER_KEY"), secret=os.getenv("LIGHTER_SECRET"))
await temp_client.init_client()
available_symbols = list(temp_client.ticker_to_idx.keys())
print(f"\n📋 Available symbols ({len(available_symbols)} total):")
# Group by categories
btc_symbols = [s for s in available_symbols if 'BTC' in s]
eth_symbols = [s for s in available_symbols if 'ETH' in s]
sol_symbols = [s for s in available_symbols if 'SOL' in s]
other_symbols = [s for s in available_symbols if not any(x in s for x in ['BTC', 'ETH', 'SOL'])]
if btc_symbols:
print(f" Bitcoin: {', '.join(btc_symbols)}")
if eth_symbols:
print(f" Ethereum: {', '.join(eth_symbols)}")
if sol_symbols:
print(f" Solana: {', '.join(sol_symbols)}")
if other_symbols:
print(f" Others: {', '.join(other_symbols[:10])}...")
await temp_client.cleanup()
print(f"\n💡 Usage: uv run test_any_symbol_trading.py <SYMBOL>")
print(f" Example: uv run test_any_symbol_trading.py BTC")
print(f" Example: uv run test_any_symbol_trading.py XRP")
return
except Exception as e:
print(f"❌ Failed to fetch symbols: {e}")
show_available_symbols()
return
print(f"\n🎯 Testing symbol: {symbol}")
print("Starting in 2 seconds...")
await asyncio.sleep(2)
# Run tests
tester = UniversalTradingTester(symbol)
await tester.run_full_test_suite()
if __name__ == "__main__":
asyncio.run(main())