
    _i                         d dl Z d dlmZmZmZmZ d dlmZmZ d dl	m
Z
 d dlmZ  e j        e          ZdZ G d d          ZdS )	    N)ListDictAnyOptional)BaseStrategyStrategyResult)detect_levels)detect_market_conditiong     K@c                       e Zd ZdZdee         fdZ	 	 ddedeee	f         de
eeef                  de
e         d	eee	f         f
d
Zdeeee	f                  d	eeef         fdZdS )DecisionEnginez
    Aggregates signals from multiple strategies using weighted scoring.
    Produces a final BUY/SELL/HOLD decision with metadata.
    
strategiesc                     || _         d S )N)r   )selfr   s     ?/var/www/html/crypto-bot/backend/app/engines/decision_engine.py__init__zDecisionEngine.__init__   s    $    Npairmarket_datastrategy_weightscurrent_pricereturnc           	      F   |i }g }| j         D ]}	 |                    |          }|                    |                                d          }|                    |                                |j        |j        ||j        d           }# t          $ r<}	t          
                    d|                                 d|	            Y d}	~	d}	~	ww xY w|                     |          \  }
}|                    d          }|<|r:|                    t          |                                          d                   }|t          |          ni }|t          |          nd}|p#| t!          |d	         j        d
                   nd}||
t%          |d          ||||dS )a  
        Run all strategies and aggregate results.

        Args:
            pair:             Trading pair symbol, e.g. "BTC/USDT"
            market_data:      Multi-timeframe OHLCV dict {"15m": df, "1h": df}
            strategy_weights: Optional dict mapping strategy name to weight (0-1)
            current_price:    Latest price (used as entry estimate)

        Returns:
            dict with final signal, confidence, entry, SL, TPs, breakdown, S/R levels.
        Ng      ?)strategysignal
confidenceweightmetadataz	Strategy z	 failed: 1hr   UNKNOWNclose           )r   r   r   entrymarket_conditionstrategies_breakdownsupport_resistance)r   analyzegetget_nameappendr   r   r   	Exceptionloggererror
_aggregatelistkeysr	   r
   floatilocround)r   r   r   r   r   resultsr   resultr   efinal_signalr   
primary_df	sr_levelsr%   r$   s                   r   r(   zDecisionEngine.analyze   s   & #!(* 	L 	LHL)1)9)9+)F)F)--h.?.?.A.A3GG ( 1 1 3 3$m"("3$ &         L L LJ):):)<)<JJqJJKKKKKKKKL $(??7#;#; j !__T**
+$k.>.>.@.@)A)A!)DEEJ1;1GM*---R	BLBX2:>>>^g 
3=3IE*W%*2.///s 	
 "
A.. 0$+"+
 
 	
s   A9B
C2CCr5   c                 @   |sdS d}d}d}|D ]D}|d         }||z  }|d         dk    r||d         |z  z  }*|d         dk    r||d         |z  z  }E|dk    rdS ||z  }||z  }||k    r|t           k    rd|fS ||k    r|t           k    rd|fS d	t          ||          fS )
z
        Weighted voting:
        - Sum weighted confidence for BUY signals.
        - Sum weighted confidence for SELL signals.
        - Winning direction must exceed CONFIDENCE_THRESHOLD.
        )HOLDr"   r"   r   r   BUYr   SELLr   r<   )CONFIDENCE_THRESHOLDmax)	r   r5   	buy_score
sell_scoretotal_weightrwbuy_conf	sell_confs	            r   r/   zDecisionEngine._aggregateR   s     	;	
 	2 	2A(AAL{e##Q|_q00		8&&ao11
1;|+-	y  X1E%E%E(?"!!i3G&G&G9$$s8Y////r   )NN)__name__
__module____qualname____doc__r   r   r   strr   r   r   r2   r(   tupler/    r   r   r   r      s         
%4#5 % % % % 8<)-:
 :
:
 #s(^:
 #4U
#34	:

  :
 
c3h:
 :
 :
 :
x#0DcN+#0	sEz	#0 #0 #0 #0 #0 #0r   r   )loggingtypingr   r   r   r   app.strategies.base_strategyr   r   app.core.support_resistancer	   app.core.market_conditionr
   	getLoggerrH   r-   r?   r   rN   r   r   <module>rU      s     , , , , , , , , , , , , E E E E E E E E 5 5 5 5 5 5 = = = = = =		8	$	$  h0 h0 h0 h0 h0 h0 h0 h0 h0 h0r   