import marketRepo from '../repositories/MarketDataRepo.js';
import signalRepo from '../repositories/SignalRepo.js';
import StrategyFactory from '../factories/StrategyFactory.js';
+const ALERTS_URL = process.env.ALERTS_BASE_URL || 'http://127.0.0.1:3003';
class BotService {
* Traite un utilisateur spécifique
*/
async processUserStrategy(userConfig) {
- const { user_strategy_id, pair_id, mode, params } = userConfig;
+ const { user_strategy_id, user_id, pair_id, pair_code, mode, params } = userConfig;
try {
// A. Récupérer les données de marché
const strategyAlgo = StrategyFactory.getStrategy(mode);
// C. Lancer l'analyse
- // Note : params est un JSON qui vient de la DB (ex: {rsi_period: 14})
const signal = strategyAlgo.analyze(candles, params);
// D. Si un signal est trouvé, sauvegarde
if (signal) {
console.log(`SIGNAL DÉTECTÉ pour ${user_strategy_id} (${mode}) !`);
- await signalRepo.saveSignal({
+ // On stocke le retour de saveSignal dans la variable signalId
+ const signalId = await signalRepo.saveSignal({
userStrategyId: user_strategy_id,
action: signal.action,
confidence: signal.confidence,
price: candles[candles.length - 1].close_price,
indicators: signal.indicators
});
+
+ // Envoi au module alerte
+ await this.sendToAlertsModule({
+ userId: user_id,
+ pairId: pair_id,
+ pair: pair_code || `PAIR_${pair_id}`,
+ action: signal.action,
+ confidence: signal.confidence,
+ criticality: this.getCriticality(signal.confidence),
+ reason: signal.reason,
+ correlationId: signalId
+ });
}
} catch (error) {
console.error(`Erreur sur la stratégie ${user_strategy_id} :`, error.message);
}
}
+
+ async sendToAlertsModule(signalPayload) {
+ try {
+ const response = await fetch(
+ `${ALERTS_URL}/api/alerts/process-signal`,
+ {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify(signalPayload)
+ }
+ );
+ if (!response.ok) {
+ const err = await response.json();
+ console.error('Module Alertes a refusé le signal :', err);
+ return;
+ }
+ const result = await response.json();
+ console.log('Signal envoyé au module Alertes :', result.data);
+ } catch (error) {
+ // Si alertes-service est éteint, le bot continue quand même
+ console.error('Impossible de joindre le module Alertes :', error.message);
+ }
+ }
+
+ getCriticality(confidence) {
+ if (confidence >= 0.85) return 'CRITICAL';
+ if (confidence >= 0.65) return 'WARNING';
+ return 'INFO';
+ }
+
+
}
const botService = new BotService();