
    i                        d dl Z d dlZd dlmZ d dl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 eddg	          Zdad
 Ze                    d          defd            Ze                    d          defd            Ze                    d          defd            Ze                    d          d             ZdS )    N)datetime)	APIRouterHTTPException)get_current_pricefetch_multi_timeframe)detect_market_condition)binance_clientz/api/v1/marketmarket)prefixtagsc                 
    | a d S )N)_decision_engine)engines    9/var/www/html/crypto-bot/backend/app/api/routes/market.pyset_decision_enginer      s        z/price/{pair:path}pairc                    K   |                                                      dd          } t          |           }|t          dd|            | |dS )z@Get the current market price for a trading pair (e.g. BTC/USDT).-/N  zCould not fetch price for status_codedetailr   price)upperreplacer   r   r   s     r   	get_pricer      sb       ::<<S))Dd##E}4WQU4W4WXXXX5)))r   z/signal/{pair:path}c                 X  K   t           t          dd          |                                                     dd          } 	 t	          | ddg	          }n&# t
          $ r}t          dd
|           d}~ww xY wt          |           }t                               | ||          }|S )zj
    Run the decision engine on demand for a pair and return the signal.
    Does NOT open any trade.
    Nr   zDecision engine not initialisedr   r   r   15m1h
timeframeszMarket data error: )r   market_datacurrent_price)r   r   r   r   r   	Exceptionr   analyze)r   r%   er&   decisions        r   
get_signalr+   !   s       4UVVVV::<<S))DO+DeT]KKK O O O4M!4M4MNNNNO &d++M''# (  H
 Os   A 
A;"A66A;z/condition/{pair:path}c                 0  K   |                                                      dd          } 	 t          | dg          }|                    d          }t	          |          }n0# t
          $ r#}t          dt          |                    d}~ww xY w| |dS )	z0Return the detected market condition for a pair.r   r   r"   r#   r   r   N)r   	condition)r   r   r   getr   r'   r   str)r   r%   dfr-   r)   s        r   get_market_conditionr1   9   s       ::<<S))D<+DdVDDD__T""+B//		 < < <CFF;;;;<y111s   6A# #
B-BBz/account/balancec                  J  K   	 t          j                     d{V } g }| d                                         D ]]\  }}|dk    rR| d                             |d          }| d                             |d          }|                    ||||d           ^| d                             dd          }d||t          j                                                    d	S # t          $ r;}t          
                    d
|            ddg t          |          dcY d}~S d}~ww xY w)z,Fetch real account balance from Binance SpotNtotalr   freeused)assetr4   r5   r3   USDTT)successusdt_balanceassets	timestampzFailed to fetch balance: F)r8   r9   r:   error)r	   fetch_balanceitemsr.   appendr   utcnow	isoformatr'   loggerr<   r/   )balancer:   r6   r3   r4   r5   r9   r)   s           r   get_account_balancerD   F   s     
&466666666 #G,2244 		 		LE5qyyv**5!44v**5!44"  "	     w'++FA66 (!**4466	
 
 	
  
 
 
444555VV	
 
 	
 	
 	
 	
 	
 	

s   CC 
D"'0DD"D")asynciologgingr   fastapir   r   app.data_layer.market_datar   r   app.core.market_conditionr   app.data_layer.binance_clientr	   	getLogger__name__rB   routerr   r   r.   r/   r   r+   r1   rD    r   r   <module>rO      s           , , , , , , , , O O O O O O O O = = = = = = 8 8 8 8 8 8		8	$	$	*(	<	<	<    
  !!*# * * * "!* !""3    #". $%%	2S 	2 	2 	2 &%	2 !
 !
  !
 !
 !
r   