-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrealtime_arbitrage.py
More file actions
279 lines (218 loc) · 10.7 KB
/
realtime_arbitrage.py
File metadata and controls
279 lines (218 loc) · 10.7 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
"""Real-time arbitrage detection system with WebSocket feeds."""
import time
import signal
import sys
import os
from typing import Dict, List
from logger import logger, init_logger
from ccxt_interface import CCXTInterface
from cross_rate_matrix import CrossRateMatrix
import config
from arbitrage_trade import ArbitrageTrade
class RealTimeArbitrageDetector:
"""Main class for real-time arbitrage detection using WebSocket feeds."""
def __init__(self, exchange_id: str = 'binance', limit_markets: int = 80):
self.exchange_id = exchange_id
self.limit_markets = limit_markets
self.running = False
# Initialize components
logger.info("Initializing Real-Time Arbitrage Detector...")
# Setup CCXT interface with polling support
self.ccxt_interface = CCXTInterface(
exchange_id=exchange_id,
limit_markets=limit_markets,
enable_live_updates=True
)
self.trade_executor = ArbitrageTrade(
exchange=exchange_id,
api_key=os.getenv('EXCHANGE_API_KEY', ''), # Read API key from environment
api_secret=os.getenv('EXCHANGE_API_SECRET', ''), # Read API secret from environment
)
# Setup cross-rate matrix
self.cross_rate_matrix = CrossRateMatrix()
# Register for price updates
self.ccxt_interface.add_price_callback(self._on_price_update)
# Statistics
self.update_count = 0
self.arbitrage_opportunities = []
self.last_arbitrage_check = 0
self.arbitrage_check_interval = 5.0 # Check every 5 seconds
logger.info("Real-Time Arbitrage Detector initialized")
def _on_price_update(self, symbol: str, price_data: Dict):
"""Callback for price updates from WebSocket."""
self.update_count += 1
if self.update_count % 100 == 0:
logger.info(f"Processed {self.update_count} price updates")
# Update cross-rate matrix periodically
current_time = time.time()
if current_time - self.last_arbitrage_check >= self.arbitrage_check_interval:
self._check_arbitrage_opportunities()
self.last_arbitrage_check = current_time
def _check_arbitrage_opportunities(self):
"""Check for arbitrage opportunities using current price data."""
try:
# Get all current prices
all_prices = self.ccxt_interface.get_all_cached_prices()
if len(all_prices) < 3: # Need at least 3 pairs for triangular arbitrage
logger.debug(f"Not enough price data: {len(all_prices)} symbols")
return
logger.debug(f"Checking arbitrage with {len(all_prices)} symbols")
# Update cross-rate matrix
self.cross_rate_matrix.update_from_symbol_prices(all_prices, self.ccxt_interface.markets)
# Find arbitrage cycles
cycles = self.cross_rate_matrix.find_arbitrage_cycles(
base_currency="USDT",
min_profit_threshold=0.0001 # 0.01% minimum profit
)
if cycles:
logger.info(f"Found {len(cycles)} arbitrage opportunities!")
# Store and display best opportunities
for i, cycle in enumerate(cycles[:3]): # Show top 3
self._log_arbitrage_opportunity(cycle, i + 1)
self.trade_executor.execute_cycle(cycle, initial_amount=1, dry_run=True)
self.arbitrage_opportunities = cycles
else:
if hasattr(self, '_no_arbitrage_logged') and not self._no_arbitrage_logged:
logger.info("No profitable arbitrage opportunities found")
self._no_arbitrage_logged = True
except Exception as e:
import traceback
logger.error(f"Error checking arbitrage opportunities: {e}")
logger.debug(f"Traceback: {traceback.format_exc()}")
def _log_arbitrage_opportunity(self, cycle: Dict, rank: int):
"""Log an arbitrage opportunity."""
logger.info(f"=== Arbitrage Opportunity #{rank} ===")
logger.info(f"Cycle: {' -> '.join(cycle['cycle'])}")
logger.info(f"Profit: {cycle['profit_percentage']:.4f}% ({cycle['profit']:.8f} {cycle['cycle'][0]})")
logger.info(f"Total fees: {cycle['total_fees']:.4f}")
logger.info("Steps:")
for step in cycle['steps']:
logger.info(f" {step['from']} -> {step['to']} via {step['symbol']}: "
f"rate={step['rate']:.8f}, fee={step['fee']:.4f}, "
f"{step['amount_before']:.8f} -> {step['amount_after']:.8f}")
def start(self):
"""Start the real-time arbitrage detection system."""
if self.running:
logger.warning("System already running")
return
try:
logger.info("Starting real-time arbitrage detection system...")
self.running = True
# Start live price updates
self.ccxt_interface.start_live_updates()
# Wait for initial data
logger.info("Waiting for initial price data...")
initial_wait = 15
for i in range(initial_wait):
time.sleep(1)
cached_prices = len(self.ccxt_interface.get_all_cached_prices())
if i % 3 == 0:
logger.info(f"Received prices for {cached_prices}/{len(self.ccxt_interface.symbols)} symbols")
if cached_prices >= min(20, len(self.ccxt_interface.symbols) // 3):
logger.info("Sufficient price data received, starting arbitrage detection")
break
# Initial arbitrage check
self._check_arbitrage_opportunities()
logger.info("Real-time arbitrage detection system started successfully")
logger.info("System is now monitoring for arbitrage opportunities...")
# Main monitoring loop
self._monitoring_loop()
except Exception as e:
logger.error(f"Error starting system: {e}")
self.stop()
raise
def _monitoring_loop(self):
"""Main monitoring loop."""
try:
last_stats_time = 0
stats_interval = 30 # Print stats every 30 seconds
while self.running:
time.sleep(1)
current_time = time.time()
# Print periodic statistics
if current_time - last_stats_time >= stats_interval:
self._print_system_stats()
last_stats_time = current_time
except KeyboardInterrupt:
logger.info("Received keyboard interrupt signal")
self.stop()
except Exception as e:
logger.error(f"Error in monitoring loop: {e}")
self.stop()
def _print_system_stats(self):
"""Print system statistics."""
logger.info("=== System Statistics ===")
# Live update stats
update_stats = self.ccxt_interface.get_update_stats()
logger.info(f"Price updater: {update_stats.get('running', False)}, "
f"fetch count: {update_stats.get('fetch_count', 0)}, "
f"errors: {update_stats.get('error_count', 0)}")
# Price data stats
cached_prices = len(self.ccxt_interface.get_all_cached_prices())
logger.info(f"Price updates: {self.update_count} total, {cached_prices} symbols cached")
# Show some sample prices
sample_prices = list(self.ccxt_interface.get_all_cached_prices().items())[:3]
for symbol, price_data in sample_prices:
age = time.time() - price_data.get('last_updated', 0)
logger.info(f" {symbol}: bid={price_data.get('bid', 0):.6f}, "
f"ask={price_data.get('ask', 0):.6f}, age={age:.1f}s")
# Cross-rate matrix stats
matrix_stats = self.cross_rate_matrix.get_stats()
age_seconds = matrix_stats.get('age_seconds', 0)
age_str = f"{age_seconds:.1f}s" if age_seconds is not None else "never"
logger.info(f"Cross-rate matrix: {matrix_stats['currencies']} currencies, "
f"{matrix_stats['direct_rates']} rates, "
f"updated {age_str} ago")
# Current arbitrage opportunities
if self.arbitrage_opportunities:
best_profit = self.arbitrage_opportunities[0]['profit_percentage']
logger.info(f"Arbitrage: {len(self.arbitrage_opportunities)} opportunities, "
f"best profit: {best_profit:.4f}%")
# Show top opportunity
top_cycle = self.arbitrage_opportunities[0]
logger.info(f"Top cycle: {' -> '.join(top_cycle['cycle'])}")
else:
logger.info("Arbitrage: No opportunities found")
def stop(self):
"""Stop the real-time arbitrage detection system."""
if not self.running:
return
logger.info("Stopping real-time arbitrage detection system...")
self.running = False
# Stop live updates
self.ccxt_interface.stop_live_updates()
logger.info("Real-time arbitrage detection system stopped")
def get_current_opportunities(self) -> List[Dict]:
"""Get current arbitrage opportunities."""
return self.arbitrage_opportunities.copy()
def signal_handler(signum, frame):
"""Handle shutdown signals gracefully."""
logger.info(f"Received signal {signum}, shutting down...")
sys.exit(0)
def main():
"""Main function."""
# Setup logging
init_logger("INFO", log_file="logs/arbitrage_realtime.log", console=True)
# Parse arguments
args = config.parse_args()
if not config.validate_args(args):
logger.error("Invalid parameters")
sys.exit(1)
# Setup signal handlers
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
# Create and start the detector
detector = RealTimeArbitrageDetector(
exchange_id=args.exchange,
limit_markets=args.limit
)
try:
detector.start()
except KeyboardInterrupt:
logger.info("Keyboard interrupt received")
except Exception as e:
logger.error(f"Unexpected error: {e}")
finally:
detector.stop()
if __name__ == "__main__":
main()