o
    SŽÑhA  ã                   @   s@   d Z ddlZddlmZmZmZmZ e e¡Z	G dd„ dƒZ
dS )uš   
Module utilitaire pour la rÃ©cupÃ©ration centralisÃ©e des positions
Factorisation pour Ã©viter les incohÃ©rences entre SimpleTradingEngine et le serveur
é    N)ÚListÚDictÚOptionalÚTuplec                   @   sx   e Zd ZdZeddedee fdd„ƒZede	fdd„ƒZ
edeeeeef  fd	d
„ƒZedeeef fdd„ƒZdS )ÚPositionRetrieveruI   Classe centralisÃ©e pour rÃ©cupÃ©rer les positions de maniÃ¨re cohÃ©renteç�íµ ÷Æ°>Úmin_sizeÚreturnc              
   C   sö   z^| st  d¡ g W S |  ¡ }|st  d¡ g W S g }|D ]/}tt| dd¡ƒƒ}||kr3| |¡ q| dd¡ dd¡}t  d	|› d
|› d|› d�¡ qt  dt	|ƒ› dt	|ƒ› �¡ |W S  t
yz } zt  d|› �¡ g W  Y d}~S d}~ww )u  
        RÃ©cupÃ¨re les positions Hyperliquid avec filtrage standardisÃ©

        Args:
            hyperliquid_client: Client Hyperliquid
            min_size: Taille minimale pour considÃ©rer une position active

        Returns:
            Liste des positions filtrÃ©es
        z#Aucun client Hyperliquid disponibleu*   Aucune position retournÃ©e par HyperliquidÚsizer   ÚsymbolÚUNKNOWNú-PERPÚ z	Position u    filtrÃ©e (taille z < ú)u   Positions filtrÃ©es: ú/u-   Erreur rÃ©cupÃ©ration positions Hyperliquid: N)ÚloggerÚwarningÚget_positionsÚdebugÚabsÚfloatÚgetÚappendÚreplaceÚlenÚ	ExceptionÚerror)Úhyperliquid_clientr   Úraw_positionsÚfiltered_positionsÚposr
   r   Úe© r"   úO/var/www/html/HyperOracle/crypto_trader/simple_trading_system/position_utils.pyÚget_hyperliquid_positions   s,   

 €þz+PositionRetriever.get_hyperliquid_positionsc                 C   s   t  | ¡}t|ƒS )z%Compte le nombre de positions actives)r   r$   r   )r   Ú	positionsr"   r"   r#   Úget_positions_count6   s   
z%PositionRetriever.get_positions_countc                 C   sd   t  | ¡}g }|D ]&}| dd¡ dd¡}| dd¡ ¡ }tt| dd¡ƒƒ}| |||f¡ q	|S )uŒ   
        Retourne un rÃ©sumÃ© des positions (symbol, side, size)

        Returns:
            Liste de tuples (symbol, side, size)
        r   r   r   r   Úsider
   r   )r   r$   r   r   Úupperr   r   r   )r   r%   Úsummaryr    r   r'   r
   r"   r"   r#   Úget_positions_summary<   s   
z'PositionRetriever.get_positions_summaryc                 C   sø   t  | ¡}i }|D ]p}zQ| dd¡ dd¡}| dd¡ ¡ }tt| dd¡ƒƒ}t| dd	¡ƒ}t| d
d¡ƒ}t|| ƒ}	|	dkrH||	 d nd}
||
||t| d|¡ƒ|dœ||< W q	 tyy } zt 	d|› d|› �¡ W Y d}~q	d}~ww |S )u¶   
        RÃ©cupÃ¨re les positions avec calcul PnL pour le serveur principal

        Returns:
            Dict {symbol: {side, pnl_percent, pnl, entry_price, current_price}}
        r   r   r   r   r'   r
   r   Úentry_priceé   Úunrealized_pnléd   Ú
mark_price)r'   Úpnl_percentÚpnlr+   Úcurrent_pricer
   zErreur traitement position z: N)
r   r$   r   r   r(   r   r   r   r   r   )r   r%   Úresultr    r   r'   r
   r+   Úpnl_usdÚmargin_usedr0   r!   r"   r"   r#   Úget_positions_with_pnlO   s0   
ú	€þz(PositionRetriever.get_positions_with_pnlN)r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ústaticmethodr   r   r   r$   Úintr&   r   Ústrr*   r6   r"   r"   r"   r#   r      s    &r   )r:   ÚloggingÚtypingr   r   r   r   Ú	getLoggerr7   r   r   r"   r"   r"   r#   Ú<module>   s
   
