
    i                     ,   d Z ddlZddlZddlZddlmZ ddlmZ ej                  j                  ej                  j                  e
            Zej                  j                  de       	 ddlmZ ddlmZ ddlmZmZmZ ej                  j3                  ed
      Zej                  j3                  ed      Zd Zd Zd Zd Zd Z e!dk(  r ej0                   e               yy# e$ r'Z ede         ej0                  d	       Y dZ[dZ[ww xY w)u   
Synchronisation et diagnostic des positions Binance
====================================================
Vérifie et synchronise les positions entre le bot et Binance
    N)datetime)Decimal)Client)BinanceAPIException)BINANCE_API_KEYBINANCE_API_SECRETTESTNET_MODEu   ❌ Erreur d'import:    zpositions.jsonztrade_history.jsonc                  <   t         j                  j                  t              rF	 t	        t        dd      5 } t        j                  |       }t        |t              r|ni cddd       S i S # 1 sw Y   i S xY w# t        $ r}t        d|        Y d}~i S d}~ww xY w)zCharger les positions localesrutf-8encodingNu)   ⚠️ Erreur lecture positions locales: )ospathexistsPOSITIONS_FILEopenjsonload
isinstancedict	Exceptionprint)fdataes      ./sync_positions.pyload_local_positionsr      s    	ww~~n%	CncG< >yy|)$5t2> >
 I>
 I  	C=aSABBI	Cs4   A9 )A, 	A9 ,A61A9 6A9 9	BBBc                     	 t        t        dd      5 }t        j                  | |dd       ddd       y# 1 sw Y   yxY w# t        $ r}t        d	|        Y d}~yd}~ww xY w)
z!Sauvegarder les positions localeswr   r      F)indentensure_asciiNTu!   ❌ Erreur sauvegarde positions: )r   r   r   dumpr   r   )	positionsr   r   s      r   save_local_positionsr'   )   sb    .#8 	BAIIi15A	B	B 1!56s0   A 7A A A  A 	A$AA$c                    	 | j                         }i }|j                  dg       D ]<  }|d   }t        |d         }t        |d         }||z   }|dkD  s|dk(  s4|||d||<   > |S # t        $ r2}t	        d|        |j
                  d	k(  rt	        d
       Y d}~yd}~wt        $ r}t	        d|        Y d}~yd}~ww xY w)u'   Récupérer les balances Binance (SPOT)balancesassetfreelockedr   USDT)r+   r,   totalu   ❌ Erreur API Binance: i!u<   ⚠️ Problème d'authentification - vérifie tes clés APINu   ❌ Erreur inattendue: )get_accountgetfloatr   r   coder   )	clientaccountr)   balancer*   r+   r,   r.   r   s	            r   get_binance_balancesr6   3   s    $$&{{:r2 	GG$E)D78,-F6ME qyEVO $"#	  (,-66U?PQ 's+,s*   AA' A' '	C0(BC)B<<Cc                 R    	 | j                  |      }t        |d         S #  Y yxY w)u'   Récupérer le prix actuel d'un symbole)symbolpriceN)get_symbol_tickerr1   )r3   r8   tickers      r   get_current_pricer<   Q   s3    )))8VG_%%s   " &c                  &	   t        d       t        d       t        d       t        dt        rdnd d       	 t        t        t        t              } t        d	       t        d       t               }t        dt        |              |rXt        d       |j                         D ]:  \  }}t        d| d|j                  dd       d|j                  dd       d       < t        d       t        |       }|t        d       yt        dt        |       d       t        d       |j                  di       j                  dd      }t        d|dd |j                  di       j                  d!d      dd"       |j                         D ci c]  \  }}|dk7  s|d   dkD  s|| }	}}|	rt        d#       d}
t        |	j                               D ]\  \  }}| d}t        | |      }|r-|d   |z  }|
|z  }
t        d|d$d|d   d%d|d&d'|d&d	       Ft        d|d$d|d   d%d(       ^ t        d)|
dd       t        d*||
z   dd       nt        d+       t        d       t        d,       t        d       |sTt        d-       |	rt        d.t        |	       d/       |	j                         D ]  }t        d0|         t        d1       nd}d}|j                         D ]  \  }}|j                  dd2      }||v rX||   d   }|j                  dd      }t        ||z
        d3k  r|dz  }t        d4| d5| d"       _t        d6| d7| d8|        t|dz  }t        d9| d:        t        d;| d<| d=       |	rag }|	j                         D ]  }| d}||vs|j!                  |        |r-t        d>t        |       d?       |D ]  }t        d0|         t        d       t        d@       t        d       t        dA       t        dBt        |              t        dCt        |	              t        dD|d       t        dE       y# t
        $ r}t        d
|        Y d}~yd}~ww xY wc c}}w )FzFonction principalezQ
================================================================================u0   🔄 SYNCHRONISATION ET DIAGNOSTIC DES POSITIONSzP================================================================================u   
📡 Connexion à Binance Testnet
Productionz...)api_key
api_secrettestnetu      ✅ Connectéu      ❌ Erreur connexion: Nr
   u)   
📁 Chargement des positions locales...z   Positions locales: u   
   Positions enregistrées:u
         • z: quantityr   z @ entry_pricez USDTu,   
💰 Récupération des balances Binance...u-      ❌ Impossible de récupérer les balancesu      ✅ u    actifs trouvész
   Balances principales:r-   r.   u         💵 USDT: z,.2fz	 (libre: r+   )u   
   Cryptos détenues:z>6z>15.8fz>10.2fz = z (prix indisponible)u$   
   💎 Valeur totale des cryptos: u      💰 Capital total: u5         ℹ️ Aucune crypto détenue (uniquement USDT)u-   🔍 COMPARAISON POSITIONS LOCALES VS BINANCEu'   ✅ Aucune position locale enregistréeu   ⚠️ Mais u#    crypto(s) trouvée(s) sur Binance:u      • u2   
💡 Ces cryptos ne sont pas trackées par le bot gh㈵>u   ✅ u   : Synchronisé (u   ⚠️ u   : Différence - Local: z, Binance: u   ❌ u0   : Enregistré localement mais absent sur Binanceu   
📊 Résumé: z OK, z manquantesu   
⚠️ u2    crypto(s) sur Binance non trackée(s) par le bot:u   ✅ SYNCHRONISATION OKu   
💡 ÉTAT ACTUEL:u      • Positions locales: u      • Cryptos sur Binance: u      • Capital USDT: u)      • Connexion API: ✅ Opérationnelle)r   r	   r   r   r   r   r   lenitemsr0   r6   sortedr<   keysreplaceabsappend)r3   r   local_positionsr8   posbinance_balancesusdt_balancekvcrypto_balancestotal_value_usdtr*   r5   current_price
value_usdtpositions_okpositions_missingbinance_qty	local_qty	untrackeds                       r   mainr]   Y   s9   	-	
<=	&M 
(l(UUX
YZ	#) 

 	 ! 

67*,O	"3#7"8
9:-.*002 	fKFCJvhbQ)?(@CGGM[\D]C^^cde	f 

9:+F3=>	GC()**:
;< 

&'#''377CL	l40	:J:N:NvWY:Z:^:^_egh:ijn9oop
qr )9(>(>(@c1AKTUV]T^abTbq!tcOc'($_%:%:%<= 		^NE7wd^F-ff=M$W-=
 J. 
5*Bww/?.Gs=Y_J``cdnoucvv{|}
5*Bww/?.GG[\]		^ 	56Ft5LERS'7G(G'MUSTEF 
-	
9:	&M78L_!5 66YZ[(--/ )w'()GH *002 	WKFCNN62.E((.u5g>GGJ2	{Y./'9 A%LD(81EFGF8+B9+[YdXefg!Q&!VH$TUV	W  	!,u5F4G{ST 	$))+ 	(Ewd^F_,  '	(
 Ic)n--_`a" )w'() 
-	
"#	&M	
 !	&s?';&<
=>	(_)=(>
?@	!,t!4
56	57[  )!-.< ds)   %Q) R#R,R)	R
2RR
__main__)"__doc__sysr   r   r   decimalr   r   dirnameabspath__file__
SCRIPT_DIRinsertbinance.clientr   binance.exceptionsr   configr   r   r	   ImportErrorr   r   exitjoinr   TRADE_HISTORY_FILEr   r'   r6   r<   r]   __name__     r   <module>rq      s     	    WW__RWW__X67
 : %6HH j*:;WW\\*.BC 	<|| zCHHTV C  	!!
%&CHHQKKs   ,C' 'D,DD