Repository navigation
Expand file tree
/
Copy pathstart.py
More file actions
586 lines (503 loc) · 21.3 KB
/
Copy pathstart.py
File metadata and controls
586 lines (503 loc) · 21.3 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
"""
Crypto Trading Bot - Entry Point
Automated trading with AI-powered decisions.
"""
import torch # needed for SentenceSplitter (NLP model)
import asyncio
import sys
import warnings
# Suppress library warnings - MUST be done before other imports
warnings.filterwarnings("ignore", category=SyntaxWarning, module="docopt")
warnings.filterwarnings("ignore", category=DeprecationWarning, module="discord")
warnings.filterwarnings("ignore", category=DeprecationWarning, module="google.genai")
import time
import os
import aiohttp
import signal
import atexit
from pathlib import Path
from typing import Optional
if sys.platform != "win32":
import fcntl
from src.config.loader import config
from src.app import CryptoTradingBot
from src.logger.logger import Logger
from src.utils.graceful_shutdown_manager import GracefulShutdownManager
from src.platforms.alternative_me import AlternativeMeAPI
from src.platforms.coingecko import CoinGeckoAPI
from src.platforms.cryptocompare.news_api import CryptoCompareNewsAPI
from src.platforms.cryptocompare.market_api import CryptoCompareMarketAPI
from src.platforms.cryptocompare.categories_api import CryptoCompareCategoriesAPI
from src.platforms.exchange_manager import ExchangeManager
from src.analyzer.analysis_engine import AnalysisEngine
from src.rag import RagEngine
from src.utils.token_counter import TokenCounter
from src.utils.format_utils import FormatUtils
from src.contracts.model_contract import ModelManagerProtocol
from src.managers.model_manager import ModelManager
from src.managers.persistence_manager import PersistenceManager
from src.managers.risk_manager import RiskManager
from src.trading import (
TradingStrategy, TradingBrainService,
TradingStatisticsService, TradingMemoryService, PositionExtractor
)
from src.dashboard.server import DashboardServer
from src.notifiers import DiscordNotifier, ConsoleNotifier
from src.utils.keyboard_handler import KeyboardHandler
from src.rag.text_splitting import SentenceSplitter
from src.parsing.unified_parser import UnifiedParser
from src.factories import TechnicalIndicatorsFactory
from src.rag.article_processor import ArticleProcessor
from src.analyzer.formatters import (
MarketOverviewFormatter,
LongTermFormatter,
MarketFormatter,
MarketPeriodFormatter
)
from src.rag import (
RagFileHandler, NewsManager, MarketDataManager,
IndexManager, ContextBuilder, CategoryFetcher,
CategoryProcessor, TickerManager, NewsCategoryAnalyzer
)
from src.analyzer import (
TechnicalCalculator, PatternAnalyzer, MarketDataCollector,
MarketMetricsCalculator, AnalysisResultProcessor,
TechnicalFormatter
)
from src.analyzer.prompts import PromptBuilder
from src.analyzer.pattern_engine import ChartGenerator
try:
from PyQt6.QtWidgets import QApplication, QMessageBox
from PyQt6.QtCore import Qt
PYQT_AVAILABLE = True
except ImportError:
PYQT_AVAILABLE = False
lock_file_handle: Optional[int] = None
def check_single_instance() -> bool:
"""
Check if another instance is already running using a lock file.
Works cross-platform (Windows, Linux, macOS).
Returns:
True if this is the only instance, False if another instance is running.
"""
global lock_file_handle
lock_file_path = Path.home() / ".llm_trader.lock"
try:
lock_file_handle = os.open(str(lock_file_path), os.O_CREAT | os.O_RDWR)
if sys.platform == "win32":
import msvcrt
try:
msvcrt.locking(lock_file_handle, msvcrt.LK_NBLCK, 1)
except OSError:
return False
else:
try:
fcntl.flock(lock_file_handle, fcntl.LOCK_EX | fcntl.LOCK_NB)
except BlockingIOError:
return False
atexit.register(_cleanup_lock_file)
return True
except Exception as e:
print(f"Warning: Could not create lock file: {e}")
return True
def _cleanup_lock_file() -> None:
"""Release the lock file on exit."""
global lock_file_handle
if lock_file_handle is not None:
try:
if sys.platform == "win32":
import msvcrt
msvcrt.locking(lock_file_handle, msvcrt.LK_UNLCK, 1)
else:
fcntl.flock(lock_file_handle, fcntl.LOCK_UN)
os.close(lock_file_handle)
except Exception:
pass
lock_file_path = Path.home() / ".llm_trader.lock"
try:
lock_file_path.unlink(missing_ok=True)
except Exception:
pass
class CompositionRoot:
"""Composition Root for the trading bot application.
Responsible for building and wiring all dependencies following the
Dependency Injection pattern before injecting them into CryptoTradingBot.
"""
def __init__(self):
self.config = config
self.logger = Logger(logger_name="Bot", logger_debug=config.LOGGER_DEBUG)
self.loop = None
self.shutdown_manager = None
async def build_dependencies(self) -> dict:
"""Build all dependencies for the trading bot.
Returns:
Dictionary of all initialized components ready for injection.
"""
start_time = time.perf_counter()
self.logger.info("Initializing Crypto Trading Bot...")
# Initialize data directory
data_dir = self.config.DATA_DIR
os.makedirs(data_dir, exist_ok=True)
os.makedirs(os.path.join(data_dir, "news_cache"), exist_ok=True)
os.makedirs(os.path.join(data_dir, "trading"), exist_ok=True)
os.makedirs(os.path.join(data_dir, "charts"), exist_ok=True)
# Initialize ExchangeManager
exchange_manager = ExchangeManager(logger=self.logger, config=self.config)
await exchange_manager.initialize()
self.logger.debug("ExchangeManager initialized")
# Initialize utilities
token_counter = TokenCounter()
format_utils = FormatUtils()
sentence_splitter = SentenceSplitter(logger=self.logger)
ti_factory = TechnicalIndicatorsFactory()
unified_parser = UnifiedParser(self.logger, format_utils=format_utils)
# Initialize ArticleProcessor
article_processor = ArticleProcessor(
logger=self.logger,
unified_parser=unified_parser,
format_utils=format_utils,
sentence_splitter=sentence_splitter
)
# Initialize API Clients
coingecko_api = CoinGeckoAPI(
logger=self.logger,
api_key=self.config.COINGECKO_API_KEY,
cache_dir='cache',
update_interval_hours=self.config.RAG_COINGECKO_UPDATE_INTERVAL_HOURS,
global_api_url=self.config.RAG_COINGECKO_GLOBAL_API_URL
)
await coingecko_api.initialize()
self.logger.debug("CoinGeckoAPI initialized")
# Initialize CryptoCompare components
cryptocompare_session = aiohttp.ClientSession()
news_api = CryptoCompareNewsAPI(
logger=self.logger,
config=self.config,
cache_dir='data/news_cache',
update_interval_hours=self.config.RAG_UPDATE_INTERVAL_HOURS
)
await news_api.initialize()
categories_api = CryptoCompareCategoriesAPI(
logger=self.logger,
config=self.config,
data_dir='data',
categories_update_interval_hours=self.config.RAG_CATEGORIES_UPDATE_INTERVAL_HOURS
)
await categories_api.initialize()
market_api = CryptoCompareMarketAPI(logger=self.logger, config=self.config)
self.logger.debug("CryptoCompare components initialized")
alternative_me_api = AlternativeMeAPI(logger=self.logger)
await alternative_me_api.initialize()
self.logger.debug("AlternativeMeAPI initialized")
# Create RAG component managers
rag_file_handler = RagFileHandler(
logger=self.logger,
config=self.config,
unified_parser=unified_parser
)
news_manager = NewsManager(
logger=self.logger,
file_handler=rag_file_handler,
news_api=news_api,
categories_api=categories_api,
session=cryptocompare_session,
article_processor=article_processor
)
market_data_manager = MarketDataManager(
logger=self.logger,
file_handler=rag_file_handler,
coingecko_api=coingecko_api,
market_api=market_api,
exchange_manager=exchange_manager,
unified_parser=unified_parser
)
index_manager = IndexManager(
logger=self.logger,
article_processor=article_processor
)
category_fetcher = CategoryFetcher(
logger=self.logger,
categories_api=categories_api
)
category_processor = CategoryProcessor(
logger=self.logger,
file_handler=rag_file_handler
)
ticker_manager = TickerManager(
logger=self.logger,
file_handler=rag_file_handler,
exchange_manager=exchange_manager
)
news_category_analyzer = NewsCategoryAnalyzer(
logger=self.logger,
category_processor=category_processor,
unified_parser=unified_parser
)
context_builder = ContextBuilder(
logger=self.logger,
token_counter=token_counter,
article_processor=article_processor,
sentence_splitter=sentence_splitter
)
context_builder.config = self.config
self.logger.debug("RAG components created")
# Initialize RagEngine
rag_engine = RagEngine(
logger=self.logger,
token_counter=token_counter,
config=self.config,
coingecko_api=coingecko_api,
exchange_manager=exchange_manager,
file_handler=rag_file_handler,
news_manager=news_manager,
market_data_manager=market_data_manager,
index_manager=index_manager,
category_fetcher=category_fetcher,
category_processor=category_processor,
ticker_manager=ticker_manager,
news_category_analyzer=news_category_analyzer,
context_builder=context_builder
)
await rag_engine.initialize()
self.logger.debug("RagEngine initialized")
# Initialize ModelManager
model_manager = ModelManager(self.logger, self.config, unified_parser=unified_parser)
self.logger.debug("ModelManager initialized")
# Create formatters for dependency injection
overview_formatter = MarketOverviewFormatter(self.logger, format_utils)
long_term_formatter = LongTermFormatter(self.logger, format_utils)
market_formatter = MarketFormatter(self.logger, format_utils)
period_formatter = MarketPeriodFormatter(self.logger, format_utils)
# Create Analyzer components
technical_calculator = TechnicalCalculator(
logger=self.logger,
format_utils=format_utils,
ti_factory=ti_factory
)
pattern_analyzer = PatternAnalyzer(logger=self.logger)
try:
pattern_analyzer.warmup()
except Exception as warmup_error:
self.logger.warning(f"Pattern analyzer warm-up could not run: {warmup_error}")
technical_formatter = TechnicalFormatter(
technical_calculator,
self.logger,
format_utils
)
prompt_builder = PromptBuilder(
timeframe=self.config.TIMEFRAME,
logger=self.logger,
technical_calculator=technical_calculator,
config=self.config,
format_utils=format_utils,
overview_formatter=overview_formatter,
long_term_formatter=long_term_formatter,
technical_formatter=technical_formatter,
market_formatter=market_formatter,
period_formatter=period_formatter
)
market_data_collector = MarketDataCollector(
logger=self.logger,
rag_engine=rag_engine,
alternative_me_api=alternative_me_api
)
metrics_calculator = MarketMetricsCalculator(logger=self.logger)
result_processor = AnalysisResultProcessor(
model_manager=model_manager,
logger=self.logger,
unified_parser=unified_parser
)
chart_generator = ChartGenerator(
logger=self.logger,
config=self.config,
format_utils=format_utils
)
self.logger.debug("Analyzer components created")
# Initialize AnalysisEngine
market_analyzer = AnalysisEngine(
logger=self.logger,
rag_engine=rag_engine,
coingecko_api=coingecko_api,
model_manager=model_manager,
alternative_me_api=alternative_me_api,
market_api=market_api,
config=self.config,
technical_calculator=technical_calculator,
pattern_analyzer=pattern_analyzer,
prompt_builder=prompt_builder,
data_collector=market_data_collector,
metrics_calculator=metrics_calculator,
result_processor=result_processor,
chart_generator=chart_generator
)
self.logger.debug("AnalysisEngine initialized")
# Initialize trading services
position_extractor = PositionExtractor(self.logger, unified_parser=unified_parser)
persistence = PersistenceManager(self.logger, data_dir="data/trading")
# Initialize Risk Manager
risk_manager = RiskManager(self.logger, self.config)
brain_service = TradingBrainService(
self.logger,
persistence,
symbol=self.config.CRYPTO_PAIR,
timeframe=self.config.TIMEFRAME
)
memory_service = TradingMemoryService(self.logger, persistence, max_memory=10)
statistics_service = TradingStatisticsService(self.logger, persistence)
trading_strategy = TradingStrategy(
self.logger,
persistence=persistence,
brain_service=brain_service,
statistics_service=statistics_service,
memory_service=memory_service,
risk_manager=risk_manager,
config=self.config,
position_extractor=position_extractor
)
self.logger.debug("Trading components initialized")
# Initialize notifier - Discord if enabled, otherwise Console fallback
discord_notifier = None
discord_task = None
if self.config.DISCORD_BOT_ENABLED and hasattr(self.config, 'BOT_TOKEN_DISCORD') and self.config.BOT_TOKEN_DISCORD:
try:
discord_notifier = DiscordNotifier(
logger=self.logger,
config=self.config,
unified_parser=unified_parser,
formatter=format_utils
)
discord_task = asyncio.create_task(discord_notifier.start())
await discord_notifier.wait_until_ready()
self.logger.debug("Discord notifier initialized and ready")
except Exception as e:
self.logger.warning(f"Discord initialization failed: {e}. Falling back to console output.")
discord_notifier = ConsoleNotifier(
logger=self.logger,
config=self.config,
unified_parser=unified_parser,
formatter=format_utils
)
else:
discord_notifier = ConsoleNotifier(
logger=self.logger,
config=self.config,
unified_parser=unified_parser,
formatter=format_utils
)
self.logger.info("Discord disabled, using console notifications")
# Start periodic RAG updates
await rag_engine.start_periodic_updates()
# Initialize keyboard handler
keyboard_handler = KeyboardHandler(logger=self.logger)
end_time = time.perf_counter()
init_duration = end_time - start_time
self.logger.info(f"All dependencies initialized successfully in {init_duration:.2f} seconds")
deps = {
'exchange_manager': exchange_manager,
'market_analyzer': market_analyzer,
'trading_strategy': trading_strategy,
'discord_notifier': discord_notifier,
'discord_task': discord_task,
'keyboard_handler': keyboard_handler,
'rag_engine': rag_engine,
'coingecko_api': coingecko_api,
'news_api': news_api,
'market_api': market_api,
'categories_api': categories_api,
'alternative_me_api': alternative_me_api,
'cryptocompare_session': cryptocompare_session,
'persistence': persistence,
'model_manager': model_manager,
'brain_service': brain_service,
'statistics_service': statistics_service,
'memory_service': memory_service,
}
# Initialize Dashboard Server
dashboard_server = DashboardServer(
brain_service=brain_service,
vector_memory=brain_service.vector_memory if brain_service and hasattr(brain_service, 'vector_memory') else None,
analysis_engine=market_analyzer,
config=self.config,
logger=self.logger,
unified_parser=unified_parser,
persistence=persistence,
exchange_manager=exchange_manager,
host=self.config.DASHBOARD_HOST,
port=self.config.DASHBOARD_PORT
)
# Add dashboard to dependencies
deps['dashboard_server'] = dashboard_server
deps['dashboard_state'] = dashboard_server.dashboard_state
return deps
async def run_async(self):
"""Async entry point for the application."""
dependencies = await self.build_dependencies()
# Extract dashboard_server before passing to bot (bot doesn't accept it)
dashboard_server = dependencies.pop('dashboard_server', None)
bot = CryptoTradingBot(
logger=self.logger,
config=self.config,
shutdown_manager=self.shutdown_manager,
**dependencies
)
try:
await bot.initialize()
symbol = self.config.CRYPTO_PAIR
timeframe = self.config.TIMEFRAME
# Create tasks
bot_task = asyncio.create_task(bot.run(symbol, timeframe))
# Start dashboard if available
if dashboard_server:
dashboard_task = await dashboard_server.start()
await asyncio.gather(bot_task, dashboard_task, return_exceptions=True)
else:
await bot_task
except asyncio.CancelledError:
self.logger.info("Trading cancelled, shutting down...")
finally:
# Clean up dashboard server
if dashboard_server:
await dashboard_server.stop()
def start(self):
"""Main entry point with clean shutdown delegation."""
if not check_single_instance():
if PYQT_AVAILABLE:
app = QApplication.instance()
if app is None:
app = QApplication(sys.argv)
QApplication.setHighDpiScaleFactorRoundingPolicy(
Qt.HighDpiScaleFactorRoundingPolicy.PassThrough
)
QMessageBox.critical(
None,
"Crypto Trading Bot",
"Another instance of Crypto Trading Bot is already running.",
QMessageBox.StandardButton.Ok
)
else:
print("Another instance of Crypto Trading Bot is already running.")
sys.exit(1)
if sys.platform == 'win32':
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
self.loop = asyncio.new_event_loop()
asyncio.set_event_loop(self.loop)
self.shutdown_manager = GracefulShutdownManager(
self.loop,
logger=self.logger,
confirmation_callback=GracefulShutdownManager.show_exit_confirmation
)
self.shutdown_manager.setup_signal_handlers()
try:
self.loop.run_until_complete(self.run_async())
except KeyboardInterrupt:
print("\nKeyboardInterrupt received - initiating graceful shutdown...")
self.loop.run_until_complete(self.shutdown_manager.shutdown_gracefully())
except Exception as e:
print(f"Unhandled exception: {e}")
import traceback
traceback.print_exc()
self.loop.run_until_complete(self.shutdown_manager.shutdown_gracefully())
finally:
self.loop.close()
if __name__ == "__main__":
CompositionRoot().start()