-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrypto_analyzer.py
More file actions
402 lines (327 loc) · 18.8 KB
/
crypto_analyzer.py
File metadata and controls
402 lines (327 loc) · 18.8 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
import threading
from io import StringIO
from typing import Dict, List, Optional, Tuple
import pandas as pd
# noinspection PyPackageRequirements
from python_pubsub_client import OrchestratorBase, ServiceBus, AllProcessingCompleted, WorkerFailed
from python_threadsafe_logger import sqlite_business_logger
from agents.data_fetcher import DataFetcher
from agents.database_manager import DatabaseManager
from agents.display_agent import DisplayAgent
from agents.rsi_calculator import RSICalculator
from analysis_job import AnalysisJob
from configuration import AnalysisConfig
from events import (
AnalysisConfigurationProvided,
AnalysisJobCompleted,
CalculateRSIRequested,
CoinProcessingFailed,
CorrelationAnalyzed,
DisplayCompleted,
FetchPrecisionDataRequested,
HistoricalPricesFetched,
PrecisionDataFetched,
RSICalculated,
RunAnalysisRequested,
TopCoinsFetched,
)
from logger import logger
class CryptoAnalyzer(OrchestratorBase):
"""Orchestrates RSI correlation analyses for multiple timeframes."""
def __init__(self, config: AnalysisConfig, session_guid: str):
self.config = config
self.session_guid = session_guid
url = self.config.pubsub_url
service_bus = ServiceBus(url=url, consumer_name=self.__class__.__name__)
super().__init__(service_bus, enable_status_page=True)
self.db_manager = None
self.data_fetcher = None
self.rsi_calculator = None
self.display_agent = None
# Initialize to None for a clear initial state.
self.coins: Optional[List[Dict]] = None
self.precision_data: Optional[Dict[str, Dict]] = None
self.market_caps: Dict[str, float] = {}
self.low_cap_threshold: float = float("inf")
self.results: List[Dict] = []
self.rsi_results: Dict[Tuple[str, str, str], pd.Series] = {}
self.analysis_jobs: Dict[str, AnalysisJob] = {tf: AnalysisJob(tf, self) for tf in self.config.timeframes}
self._job_completion_counter = len(self.config.timeframes)
self._job_lock = threading.Lock()
self._initial_data_loaded = threading.Event()
def register_services(self) -> None:
"""Creates and registers services managed by the orchestrator."""
self.db_manager = DatabaseManager(service_bus=self.service_bus)
self.data_fetcher = DataFetcher(service_bus=self.service_bus)
self.rsi_calculator = RSICalculator(service_bus=self.service_bus)
self.display_agent = DisplayAgent(service_bus=self.service_bus)
self.services = [self.db_manager, self.data_fetcher, self.rsi_calculator, self.display_agent]
def setup_event_subscriptions(self) -> None:
"""Subscribes orchestrator methods to service bus events."""
self.service_bus.subscribe("RunAnalysisRequested", self._handle_run_analysis_requested)
self.service_bus.subscribe("TopCoinsFetched", self._handle_top_coins_fetched)
self.service_bus.subscribe("PrecisionDataFetched", self._handle_precision_data_fetched)
self.service_bus.subscribe("HistoricalPricesFetched", self._handle_historical_prices_fetched)
self.service_bus.subscribe("RSICalculated", self._handle_rsi_calculated)
self.service_bus.subscribe("CorrelationAnalyzed", self._handle_correlation_analyzed)
self.service_bus.subscribe("CoinProcessingFailed", self._handle_coin_processing_failed)
self.service_bus.subscribe("AnalysisJobCompleted", self._handle_analysis_job_completed)
self.service_bus.subscribe("DisplayCompleted", self._handle_display_completed)
self.service_bus.subscribe("WorkerFailed", self._handle_worker_failed)
def start_workflow(self) -> None:
"""Starts the analysis workflow."""
config_payload = AnalysisConfigurationProvided(session_guid=self.session_guid, config=self.config)
self.service_bus.publish("AnalysisConfigurationProvided", config_payload, self.__class__.__name__)
self.service_bus.publish("RunAnalysisRequested", RunAnalysisRequested(), self.__class__.__name__)
def _handle_run_analysis_requested(self, _event: RunAnalysisRequested):
try:
logger.info("Analysis started. Requesting initial data (top coins and precision).")
if self.service_bus is not None:
self.service_bus.publish("FetchPrecisionDataRequested", FetchPrecisionDataRequested(), self.__class__.__name__)
if self.service_bus is not None:
from events import FetchTopCoinsRequested
self.service_bus.publish("FetchTopCoinsRequested", FetchTopCoinsRequested(n=self.config.top_n_coins), self.__class__.__name__)
except Exception as e:
error_msg = f"Error handling run analysis requested: {e}"
logger.critical(error_msg, exc_info=True)
self.service_bus.publish("AllProcessingCompleted", AllProcessingCompleted(), self.__class__.__name__)
def _handle_precision_data_fetched(self, event: PrecisionDataFetched):
try:
self.precision_data = {item["symbol"]: item for item in event.precision_data}
self._start_analysis_if_ready()
except Exception as e:
error_msg = f"Error handling precision data fetched: {e}"
logger.critical(error_msg, exc_info=True)
self.service_bus.publish("AllProcessingCompleted", AllProcessingCompleted(), self.__class__.__name__)
def _handle_top_coins_fetched(self, event: TopCoinsFetched):
try:
self.coins = event.coins
self._start_analysis_if_ready()
except Exception as e:
error_msg = f"Error handling top coins fetched: {e}"
logger.critical(error_msg, exc_info=True)
self.service_bus.publish("AllProcessingCompleted", AllProcessingCompleted(), self.__class__.__name__)
def _start_analysis_if_ready(self):
"""
Checks if both initial data streams have been RECEIVED (even if empty),
then starts the analysis or stops cleanly.
"""
try:
# Check that variables are no longer None.
# Using early return pattern to avoid race conditions
if self._initial_data_loaded.is_set():
return
if self.coins is None or self.precision_data is None:
logger.debug("Waiting for all initial data...")
return
# Handle the case where received data is empty.
if not self.coins or not self.precision_data:
logger.warning("No crypto or market pair found. Analysis cannot continue.")
self._processing_completed.set()
return
self._initial_data_loaded.set()
logger.info("Initial data (coins and precision) received. Starting processing.")
usdc_base_symbols = {m["base_asset"] for m in self.precision_data.values() if m["quote_asset"] == "USDC"}
original_coin_count = len(self.coins)
self.coins = [c for c in self.coins if c.get("symbol", "").upper() in usdc_base_symbols]
logger.info(f"Filtering cryptos: {original_coin_count} -> {len(self.coins)} with USDC pair.")
for coin in self.coins:
self.service_bus.publish("SingleCoinFetched", {"coin": coin}, self.__class__.__name__)
self.market_caps = {c["symbol"].lower(): c.get("market_cap", 0) for c in self.coins}
market_cap_values = [mc for mc in self.market_caps.values() if mc > 0]
if market_cap_values:
self.low_cap_threshold = pd.Series(market_cap_values).quantile(self.config.low_cap_percentile / 100)
logger.info(
f"Low capitalization threshold ({self.config.low_cap_percentile}th percentile): ${self.low_cap_threshold:,.2f}"
)
coins_to_process = [(c["id"], c["symbol"]) for c in self.coins]
for timeframe, job in self.analysis_jobs.items():
job.set_coins_to_process(coins_to_process)
logger.info(f"Launching job for {timeframe} with {len(job.coins_to_process) + 1} pairs.")
self.service_bus.publish(
"FetchHistoricalPricesRequested",
{"coin_id_symbol": ("bitcoin", "btc"), "weeks": self.config.weeks, "timeframe": timeframe},
self.__class__.__name__
)
for coin_id, symbol in job.coins_to_process:
sqlite_business_logger.log(self.__class__.__name__, f"FetchHistoricalPricesRequested pour {coin_id}")
self.service_bus.publish(
"FetchHistoricalPricesRequested",
{"coin_id_symbol": (coin_id, symbol), "weeks": self.config.weeks, "timeframe": timeframe},
self.__class__.__name__
)
except Exception as e:
logger.error(
f"Critical error during analysis initialization: {e}. Stopping application.", exc_info=True
)
# Publier l'événement de fin pour éviter le blocage
self.service_bus.publish("AllProcessingCompleted", AllProcessingCompleted(), self.__class__.__name__)
def _handle_historical_prices_fetched(self, event: HistoricalPricesFetched):
try:
prices_df = self._deserialize_prices_dataframe(event.prices_df_json, event.coin_id_symbol)
if prices_df is None or prices_df.empty:
self.service_bus.publish(
"CoinProcessingFailed",
{"coin_id_symbol": event.coin_id_symbol, "timeframe": event.timeframe},
self.__class__.__name__
)
return
close_series = prices_df["close"]
# noinspection PyUnresolvedReferences
series_json = close_series.to_json(orient="split")
rsi_request_event = CalculateRSIRequested(
coin_id_symbol=event.coin_id_symbol, prices_series_json=series_json, timeframe=event.timeframe
)
sqlite_business_logger.log(self.__class__.__name__, f"CalculateRSIRequested pour {event.coin_id_symbol}")
self.service_bus.publish("CalculateRSIRequested", rsi_request_event, self.__class__.__name__)
except Exception as e:
error_msg = f"Error handling historical prices fetched: {e}"
logger.critical(error_msg, exc_info=True)
self.service_bus.publish(
"CoinProcessingFailed",
{"coin_id_symbol": event.coin_id_symbol, "timeframe": event.timeframe},
self.__class__.__name__
)
# noinspection PyMethodMayBeStatic
def _deserialize_prices_dataframe(self, prices_json: Optional[str], coin_id_symbol: Tuple[str, str]) -> Optional[pd.DataFrame]:
if not prices_json:
return None
try:
prices_df = pd.read_json(StringIO(prices_json), orient="split")
prices_df.index = pd.to_datetime(prices_df.index, unit="ms", utc=True)
return prices_df
except Exception as e:
error_msg = f"Impossible de reconstruire le DataFrame des prix pour {coin_id_symbol}: {e}"
logger.error(error_msg)
return None
def _handle_rsi_calculated(self, event: RSICalculated):
try:
logger.debug(f"[CORRELATION-DEBUG] Received RSI for {event.coin_id_symbol} on {event.timeframe}")
job = self.analysis_jobs.get(event.timeframe)
if not job:
logger.warning(f"[CORRELATION-DEBUG] No job found for timeframe {event.timeframe}")
return
rsi_series = self._deserialize_rsi_series(event.rsi_series_json, event.coin_id_symbol)
if rsi_series is None or rsi_series.empty:
logger.debug(f"[CORRELATION-DEBUG] RSI series is None or empty for {event.coin_id_symbol}")
if self.service_bus is not None:
self.service_bus.publish(
"CoinProcessingFailed",
{"coin_id_symbol": event.coin_id_symbol, "timeframe": event.timeframe},
self.__class__.__name__
)
return
key = (event.coin_id_symbol[0], event.coin_id_symbol[1], event.timeframe)
self.rsi_results[key] = rsi_series
logger.debug(f"[CORRELATION-DEBUG] Stored RSI for {event.coin_id_symbol}")
if event.coin_id_symbol[1].lower() == "btc":
job.btc_rsi = rsi_series
logger.debug(f"[CORRELATION-DEBUG] Set BTC RSI for {event.timeframe}")
logger.debug(f"[CORRELATION-DEBUG] About to decrement counter for {event.timeframe}")
job.decrement_counter(coin_id_symbol=event.coin_id_symbol)
except Exception as e:
error_msg = f"Error handling RSI calculated: {e}"
logger.critical(error_msg, exc_info=True)
if self.service_bus is not None:
self.service_bus.publish(
"CoinProcessingFailed",
{"coin_id_symbol": event.coin_id_symbol, "timeframe": event.timeframe},
self.__class__.__name__
)
# noinspection PyMethodMayBeStatic
def _deserialize_rsi_series(self, rsi_json: Optional[str], coin_id_symbol: Tuple[str, str]) -> Optional[pd.Series]:
if not rsi_json:
return None
try:
rsi_series = pd.read_json(StringIO(rsi_json), orient="split", typ="series")
rsi_series.index = pd.to_datetime(rsi_series.index, unit="ms", utc=True)
return rsi_series
except Exception as e:
error_msg = f"Impossible de reconstruire la Series RSI pour {coin_id_symbol}: {e}"
logger.error(error_msg)
return None
def _handle_coin_processing_failed(self, event: CoinProcessingFailed):
try:
job = self.analysis_jobs.get(event.timeframe)
if job:
job.decrement_counter(coin_id_symbol=event.coin_id_symbol)
except Exception as e:
error_msg = f"Error handling coin processing failed: {e}"
logger.critical(error_msg, exc_info=True)
def analyze_correlation(
self, coin_id_symbol: Tuple[str, str], coin_rsi: pd.Series, btc_rsi: pd.Series, timeframe: str
):
common_index_rsi = btc_rsi.index.intersection(coin_rsi.index)
if len(common_index_rsi) < self.config.rsi_period:
logger.debug(f"[CORRELATION-DEBUG] Skipping {coin_id_symbol}: insufficient data ({len(common_index_rsi)} < {self.config.rsi_period})")
return
correlation = coin_rsi.loc[common_index_rsi].corr(btc_rsi.loc[common_index_rsi])
if pd.isna(correlation) or abs(correlation) < self.config.correlation_threshold:
logger.debug(f"[CORRELATION-DEBUG] Skipping {coin_id_symbol}: correlation {correlation} below threshold {self.config.correlation_threshold}")
return
logger.info(f"[CORRELATION-DEBUG] Found valid correlation for {coin_id_symbol}: {correlation}")
market_cap = self.market_caps.get(coin_id_symbol[1].lower(), 0)
low_cap_quartile = market_cap <= self.low_cap_threshold
result = {
"coin_id": coin_id_symbol[0],
"coin_symbol": coin_id_symbol[1],
"correlation": float(correlation), # Conversion de np.float64 -> float
"market_cap": market_cap,
"low_cap_quartile": bool(low_cap_quartile), # Conversion de np.bool_ -> bool
"timeframe": timeframe,
}
sqlite_business_logger.log(self.__class__.__name__, f"CorrelationAnalyzed avec {result}")
self.service_bus.publish("CorrelationAnalyzed", {"result": result, "timeframe": timeframe}, self.__class__.__name__)
def _handle_correlation_analyzed(self, event: CorrelationAnalyzed):
try:
if event.result:
self.results.append(event.result)
except Exception as e:
error_msg = f"Error handling correlation analyzed: {e}"
logger.critical(error_msg, exc_info=True)
def _handle_analysis_job_completed(self, event: AnalysisJobCompleted):
try:
with self._job_lock:
self._job_completion_counter -= 1
logger.info(f"Job for {event.timeframe} completed. Remaining: {self._job_completion_counter}")
if self._job_completion_counter <= 0:
logger.info("All analysis jobs completed. Preparing final results.")
self.service_bus.publish(
"FinalResultsReady",
{"results": self.results, "weeks": self.config.weeks, "timeframes": self.config.timeframes},
self.__class__.__name__
)
except Exception as e:
error_msg = f"Error handling analysis job completed: {e}"
logger.critical(error_msg, exc_info=True)
self.service_bus.publish("AllProcessingCompleted", AllProcessingCompleted(), self.__class__.__name__)
def _handle_worker_failed(self, event: WorkerFailed):
try:
logger.critical(
f"Worker '{event.worker_name}' reported a critical failure: {event.reason}. "
"Triggering general application shutdown."
)
# Publish the end event to unblock the main loop and stop everything cleanly
self.service_bus.publish("AllProcessingCompleted", AllProcessingCompleted(), self.__class__.__name__)
except Exception as e:
error_msg = f"Error handling worker failed: {e}"
logger.critical(error_msg, exc_info=True)
self.service_bus.publish("AllProcessingCompleted", AllProcessingCompleted(), self.__class__.__name__)
def _handle_display_completed(self, _event: DisplayCompleted):
try:
logger.info("Display completed. Triggering program shutdown.")
self.service_bus.publish("AllProcessingCompleted", AllProcessingCompleted(), self.__class__.__name__)
except Exception as e:
error_msg = f"Error handling display completed: {e}"
logger.critical(error_msg, exc_info=True)
self.service_bus.publish("AllProcessingCompleted", AllProcessingCompleted(), self.__class__.__name__)
def _stop_services(self) -> None:
"""Override to wait for DatabaseManager to finish processing before stopping."""
logger.info("Waiting for DatabaseManager to finish processing all tasks...")
if self.db_manager and self.db_manager.is_alive():
if not self.db_manager.wait_for_queue_completion(timeout=30.0):
logger.warning("DatabaseManager did not complete all tasks within timeout.")
else:
logger.info("DatabaseManager has processed all tasks successfully.")
# Call parent implementation to stop all services
super()._stop_services()