
    lWiS
                         d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlmZ d dl	m
Z
mZmZmZ  e j        e          ZdZdZ G d	 d
          Z e            ZdS )    N)datetime)Optional)AsyncSession)binance_client)Trade	TradeModeTradeSignalTradeStatus      c                   n    e Zd ZdZdededededededed	ed
e	e
         fdZdededed
e	e         fdZdS )ExecutionEnginezo
    Places REAL orders on Binance Spot via CCXT.
    Retries up to MAX_RETRIES times on transient errors.
    dbpairsignalentry_price	stop_losstake_profit_levelsquantitystrategy_snapshotreturnc	                   K   |dk    rdnd}	|                      ||	|           d{V }
|
+t                              d|	 d| dt           d           dS |
                    d	          p|
                    d
          p|}t          |t          j        t          |          ||||dgt          |          z  |t          j                    t          j        |          }|                    |           |                                 d{V  |                    |           d{V  t                              d| d| d| d|j         d	           |S )z:Place a market order and record the trade in the database.BUYbuysellNzFailed to place z order for z after z	 attemptsaveragepriceF)r   moder   r   current_pricer   r   tp_hitsr   	opened_atstatusr   zREAL trade opened:  z @ z (id=))_place_order_with_retryloggererrorMAX_RETRIESgetr   r   REALr	   lenr   utcnowr
   OPENaddcommitrefreshinfoid)selfr   r   r   r   r   r   r   r   sideorderfilled_pricetrades                @/var/www/html/crypto-bot/backend/app/engines/execution_engine.py
open_tradezExecutionEngine.open_trade   s      %uuV224xHHHHHHHH=LL`D``T``+```aaa4 yy++Puyy/A/AP[v&&$&1Gc"4555o''#/
 
 
 	uiikkjjZ$ZZZZLZZuxZZZ[[[    r4   c           
        K   t          j                    }t          dt          dz             D ]}	 |                    |||          }t
                              d| d|                    d                      |c S # t          $ r\}t
          	                    d| dt           d|            |t          k     rt          j        t                     d {V  Y d }~d }~ww xY wd S )N   zOrder attempt z
 success: r2   /z	 failed: )r   get_exchangeranger(   create_market_orderr&   r1   r)   	ExceptionwarningasynciosleepRETRY_DELAY)r3   r   r4   r   exchangeattemptr5   es           r8   r%   z'ExecutionEngine._place_order_with_retryA   s	      ".00Qa00 	5 	5G5 44T4JJQWQQ		$QQRRR 5 5 5SSS+SSPQSSTTT[((!-4444444445 ts   AA??
C%	AC  C%N)__name__
__module____qualname____doc__r   strfloatlistdictr   r   r9   r%    r:   r8   r   r      s         
)) ) 	)
 ) ) !) )  ) 
%) ) ) )V".3	$     r:   r   )loggingrC   r   typingr   sqlalchemy.ext.asyncior   app.data_layer.binance_clientr   app.models.trader   r   r	   r
   	getLoggerrI   r&   r(   rE   r   execution_enginerQ   r:   r8   <module>rY      s                  / / / / / / 8 8 8 8 8 8 G G G G G G G G G G G G		8	$	$> > > > > > > >B #?$$   r:   