
    5i;                         S SK r S SKJr  S SKrS SKrS SKJrJrJr  SSK	J
r
  SSKJr  SSKJr   " S	 S
\5      rSr " S S5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      rg)    N)
itemgetter)OptionalDictCallable   )get_loop   )BinanceSocketManager)ThreadedApiManagerc                   `    \ rS rSr\4S\4S jjrS rS rS r	S r
\S\4S\4S	 jj5       rS
rg)
DepthCache   	conv_typec                     Xl         0 U l        0 U l        SU l        X l        [
        R                  " [        5      U l        g)zInitialise the DepthCache

:param symbol: Symbol to create depth cache for
:type symbol: string
:param conv_type: Optional type to represent price, and amount, default is float.
:type conv_type: function.

N)	symbol_bids_asksupdate_timer   logging	getLogger__name___log)selfr   r   s      c/var/www/html/BTCUSD/btcusdt_trading_app/venv/lib/python3.13/site-packages/binance/ws/depthcache.py__init__DepthCache.__init__   s6     

#,%%h/	    c                     U R                  US   5      U R                  US   '   US   S:X  a  U R                  US   	 gg)z.Add a bid to the cache

:param bid:
:return:

r	   r   
0.00000000N)r   r   )r   bids     r   add_bidDepthCache.add_bid   D     "^^CF3

3q6q6\!

3q6" "r   c                     U R                  US   5      U R                  US   '   US   S:X  a  U R                  US   	 gg)z/Add an ask to the cache

:param ask:
:return:

r	   r   r   N)r   r   )r   asks     r   add_askDepthCache.add_ask(   r#   r   c                 T    [         R                  U R                  SU R                  S9$ )a  Get the current bids

:return: list of bids with price and quantity as conv_type

.. code-block:: python

    [
        [
            0.0001946,  # Price
            45.0        # Quantity
        ],
        [
            0.00019459,
            2384.0
        ],
        [
            0.00019158,
            5219.0
        ],
        [
            0.00019157,
            1180.0
        ],
        [
            0.00019082,
            287.0
        ]
    ]

Treverser   )r   
sort_depthr   r   r   s    r   get_bidsDepthCache.get_bids3   s$    > $$TZZ$XXr   c                 T    [         R                  U R                  SU R                  S9$ )a  Get the current asks

:return: list of asks with price and quantity as conv_type.

.. code-block:: python

    [
        [
            0.0001955,  # Price
            57.0'       # Quantity
        ],
        [
            0.00019699,
            778.0
        ],
        [
            0.000197,
            64.0
        ],
        [
            0.00019709,
            1130.0
        ],
        [
            0.0001971,
            385.0
        ]
    ]

Fr)   )r   r+   r   r   r,   s    r   get_asksDepthCache.get_asksT   s+    > $$JJ % 
 	
r   Fc                 x   [        U [        5      (       a3  U R                  5        VVs/ s H  u  p4U" U5      U" U5      /PM     nnnOQ[        U [        5      (       a%  U  VVs/ s H  u  p4U" U5      U" U5      /PM     nnnO[	        S[        U 5       35      e[        U[        S5      US9nU$ s  snnf s  snnf )zSort bids or asks by pricez$Unknown order book depth data type: r   )keyr*   )
isinstancedictitemslist
ValueErrortypesortedr   )valsr*   r   pricequantitylsts         r   r+   DepthCache.sort_depthw   s     dD!! (,zz|'3OE 5!9X#67'3  C d##RVWRVuIe$i&9:RVCWCCDJ<PQQSjmW=

 Xs   B0#B6)r   r   r   r   r   r   N)r   
__module____qualname____firstlineno__floatr   r   r!   r&   r-   r0   staticmethodr+   __static_attributes__ r   r   r   r      sH    5: 0( 0 	#	#YB!
F !&e 8  r   r   i  c                       \ rS rSrSrS\SS\4S\\   4S jjr	S r
S rS	 rS
 rS rS rS rS rS rS rS rS rSrg)BaseDepthCacheManager   <   N
   refresh_intervalc                    Xl         SU l        U=(       d
    [        5       U l        X l        X`l        SU l        U=(       d    [        U R                   5      U l        X@l	        SU l
        Xpl        [        R                  " [        5      U l        g)aG  Create a DepthCacheManager instance

:param client: Binance API client
:type client: binance.Client
:param loop:
:type loop:
:param symbol: Symbol to create depth cache for
:type symbol: string
:param refresh_interval: Optional number of seconds between cache refresh, use 0 or None to disable
:type refresh_interval: int
:param bm: Optional BinanceSocketManager
:type bm: BinanceSocketManager
:param limit: Optional number of orders to get from orderbook
:type limit: int
:param conv_type: Optional type to represent price, and amount, default is float.
:type conv_type: function.

N)_client_depth_cacher   _loop_symbol_limit_last_update_idr
   _bm_refresh_interval	_conn_key
_conv_typer   r   r   r   )r   clientr   looprL   bmlimitr   s           r   r   BaseDepthCacheManager.__init__   sk    :  'XZ
#;-dll;!1#%%h/	r   c                    #    [         R                  " U R                  5       U R                  5       5      I S h  vN   U R                  R                  5       I S h  vN   U $  N( N7fN)asynciogather_init_cache_start_socket_socket
__aenter__r,   s    r   rd    BaseDepthCacheManager.__aenter__   sO     nnT--/1C1C1EFFFll%%''' 	G's!   7A&A"!A&A$A&$A&c                    #    U R                   R                  SU R                   35        U R                  R                  " U0 UD6I S h  vN   g  N7f)Nz Exiting depth cache manager for )r   debugrQ   rc   	__aexit__)r   argskwargss      r   rh   BaseDepthCacheManager.__aexit__   s>     		:4<<.IJll$$d5f555s   AA
AAc                   #    S nU(       d   [         R                  " U R                  R                  5       U R                  S9I S h  vN nU R
                  R                  SU 35        U R                  U5      I S h  vN nU(       d  M  U$  NF N! [         a=  nU R
                  R                  SUR                  R                   S35         S nANQS nAff = f7f)N)timeoutzReceived message: zException recieving message: z (e) )r_   wait_forrc   recvTIMEOUTr   rg   _depth_event	Exceptionwarning	__class__r   )r   dcreses       r   ro   BaseDepthCacheManager.recv   s     2#,,T\\->->-@$,,WW		"4SE :;  ,,S11 " 	 X
 2  _		!!$A!++BVBVAWW\"]^^_sX   
C:B B"B *C>B?CCB C
C3CCCCc                 &  #    U R                   R                  SU R                   35        [        U R                  U R                  S9U l        U R                  (       a1  [        [        R                  " 5       5      U R                  -   U l	        gg7f);Initialise the depth cache calling REST endpoint

:return:
zInitialising depth cache for )r   N)
r   rg   rQ   r   rW   rO   rU   inttime_refresh_timer,   s    r   ra   !BaseDepthCacheManager._init_cache   sh     
 			7~FG&t||tO !!!$TYY[!1D4J4J!JD "s   BBc                 6   #    U R                  5       U l        g7f)'Start the depth cache socket

:return:
N)_get_socketrc   r,   s    r   rb   #BaseDepthCacheManager._start_socket   s     
 '')s   c                     [         er^   )NotImplementedErrorr,   s    r   r   !BaseDepthCacheManager._get_socket   s    !!r   c                    #    U R                   R                  SU 35        U(       d  gSU;   a)  US   S:X  a   U R                   R                  SU 35        U$ U R                  U5      I Sh  vN $  N7f)z,Handle a depth event

:param msg:
:return:

zReceived depth event: Nrw   errorz'Error in depth event restarting cache: )r   rg   r   _process_depth_messager   msgs     r   rq   "BaseDepthCacheManager._depth_event   sk      			067#:#c(g-IIOOEcUKLJ005555s   A*A3,A1-A3c                    #    U R                  U5        U R                  nU R                  (       aD  [        [        R                  " 5       5      U R
                  :  a  U R                  5       I Sh  vN   U$  N7f)KProcess a depth event message.

:param msg: Depth event message.
:return:

N)_apply_ordersrO   rU   r{   r|   r}   ra   r   r   rv   s      r   r   ,BaseDepthCacheManager._process_depth_message   sa      	3  !!c$))+&69K9K&K""$$$
 %s   A.A90A71A9c                    U R                   (       d   eUR                  S/ 5      UR                  S/ 5      -    H  nU R                   R                  U5        M      UR                  S/ 5      UR                  S/ 5      -    H  nU R                   R                  U5        M      UR                  S5      =(       d    UR                  S5      U R                   l        g )NbbidsaasksElastUpdateId)rO   getr!   r&   r   )r   r   r    r%   s       r   r   #BaseDepthCacheManager._apply_orders	  s        773#cggfb&99C%%c* :773#cggfb&99C%%c* : ),(O8O%r   c                     U R                   $ )z9Get the current depth cache

:return: DepthCache object

rO   r,   s    r   get_depth_cache%BaseDepthCacheManager.get_depth_cache  s        r   c                    #    SU l         g7f)z1Close the open socket for this manager

:return:
Nr   r,   s    r   closeBaseDepthCacheManager.close  s     
 !s   	c                     U R                   $ )z Get the symbol

:return: symbol
)rQ   r,   s    r   
get_symbol BaseDepthCacheManager.get_symbol"  s    
 ||r   )rT   rN   rV   rW   rO   rS   rR   r   rP   rU   r}   rc   rQ   )r   r@   rA   rB   rp   DEFAULT_REFRESHrC   r   r{   r   rd   rh   ro   ra   rb   r   rq   r   r   r   r   r   rE   rF   r   r   rH   rH      sj    G *9'0
 #3-'0R
6
K*"6&(P!!r   rH   c                   j   ^  \ rS rSrSSSS\S4S\\   4U 4S jjjrU 4S jrU 4S jr	S r
S	 rS
rU =r$ )DepthCacheManageri*  Ni  rL   c	           	      6   > [         T	U ]  XX4XVU5        Xl        g)a  Initialise the DepthCacheManager

:param client: Binance API client
:type client: binance.Client
:param loop: asyncio loop
:param symbol: Symbol to create depth cache for
:type symbol: string
:param refresh_interval: Optional number of seconds between cache refresh, use 0 or None to disable
:type refresh_interval: int
:param limit: Optional number of orders to get from orderbook
:type limit: int
:param conv_type: Optional type to represent price, and amount, default is float.
:type conv_type: function.
:param ws_interval: Optional interval for updates on websocket, default None. If not set, updates happen every second. Must be 0, None (1s) or 100 (100ms).
:type ws_interval: int

N)superr   _ws_interval)
r   rX   r   rY   rL   rZ   r[   r   ws_intervalrt   s
            r   r   DepthCacheManager.__init__+  s    8 	IV'r   c                 ,  >#    SU l         / U l        U R                  R                  U R                  U R
                  S9I Sh  vN n[        TU ]  5       I Sh  vN   U R                  U5        U R                  (       d   eUS    H  nU R                  R                  U5        M      US    H  nU R                  R                  U5        M      US   U l         U R                   H  nU R                  U5      I Sh  vN   M     / U l        g N N N7f)rz   N)r   r[   r   r   r   )rS   _depth_message_bufferrN   get_order_bookrQ   rR   r   ra   r   rO   r!   r&   r   )r   rv   r    r%   r   rt   s        r   ra   DepthCacheManager._init_cacheJ  s    
  $%'"LL//t||4;;/WW g!### 	3    v;C%%c* v;C%%c*   #>2 --C--c222 . &("+ X 	$ 3s7   A DDDDB#D=D>DDDc                 p   >#    [        U SS5      (       d  / U l        [        TU ]  5       I Sh  vN   g N7f)r   r   N)getattrr   r   rb   )r   rt   s    r   rb   DepthCacheManager._start_socketi  s1     
 t4d;;)+D&g#%%%s   +646c                 ^    U R                   R                  U R                  U R                  S9$ )N)interval)rT   depth_socketrQ   r   r,   s    r   r   DepthCacheManager._get_sockets  s&    xx$$T\\D<M<M$NNr   c                   #    U R                   c  U R                  R                  U5        gUS   U R                   ::  a  gUS   U R                   S-   :w  a  U R                  5       I Sh  vN   U R	                  U5        U R
                  nUS   U l         U R                  (       aD  [        [        R                  " 5       5      U R                  :  a  U R                  5       I Sh  vN   U$  N N7f)r   NuUr	   )
rS   r   appendra   r   rO   rU   r{   r|   r}   r   s      r   r   (DepthCacheManager._process_depth_messagev  s      '&&--c2s8t+++X--11 ""$$$ 	3 "3x !!c$))+&69K9K&K""$$$
 % %s%   A'C0)C,*A;C0%C.&C0.C0)r   rS   r   )r   r@   rA   rB   rC   r   r{   r   ra   rb   r   r   rE   __classcell__rt   s   @r   r   r   *  sI    
 *.(
 #3-( (>(>&O! !r   r   c                   4   ^  \ rS rSrU 4S jrS rS rSrU =r$ )FuturesDepthCacheManageri  c                 `   >#    UR                  S5      n[        TU ]	  U5      I Sh  vN $  N7f)r   dataN)r   r   r   )r   r   rt   s     r   r   /FuturesDepthCacheManager._process_depth_message  s+      ggfoW3C8888s   $.,.c                    U R                   (       d   eUR                  S/ 5      U R                   l        UR                  S/ 5      U R                   l        UR                  S5      =(       d    UR                  S5      U R                   l        g )Nr   r   r   r   )rO   r   r   r   r   r   s     r   r   &FuturesDepthCacheManager._apply_orders  si        "%''#r"2"%''#r"2 ),(O8O%r   c                 P    U R                   R                  U R                  5      nU$ r^   )rT   futures_depth_socketrQ   )r   socks     r   r   $FuturesDepthCacheManager._get_socket  s    xx,,T\\:r   rF   )	r   r@   rA   rB   r   r   r   rE   r   r   s   @r   r   r     s    9P r   r   c                       \ rS rSrS rSrg)OptionsDepthCacheManageri  c                 L    U R                   R                  U R                  5      $ r^   )rT   options_depth_socketrQ   r,   s    r   r   $OptionsDepthCacheManager._get_socket  s    xx,,T\\::r   rF   N)r   r@   rA   rB   r   rE   rF   r   r   r   r     s    ;r   r   c                      ^  \ rS rSr     SS\\   S\\   S\\\\4      S\S\4
U 4S jjjrSSS	\	4S
\
S\S\4S jjrSSS	\	S4S
\
S\S\4S jjrSSS	\	4S
\
S\S\4S jjrSSS	\	4S
\
S\S\4S jjrSrU =r$ )ThreadedDepthCacheManageri  Napi_key
api_secretrequests_paramstldtestnetc                 (   > [         TU ]  XX4U5        g r^   )r   r   )r   r   r   r   r   r   rt   s         r   r   "ThreadedDepthCacheManager.__init__  s     	oGLr   rK   callbackr   returnc                    U R                   (       d)  [        R                  " S5        U R                   (       d  M)  U" SU R                   UU R                  UUUUS.UD6n	UR	                  5       S-   [        U5      -   n
SU R                  U
'   U R                  R                  [        R                  U R                  XU5      5        U
$ )Ng{Gz?)rX   r   rY   rL   rZ   r[   r   z@depthTrF   )rN   r|   sleeprP   lowerstr_socket_running	call_soonr_   create_taskstart_listener)r   	dcm_classr   r   rL   rZ   r[   r   rj   dcmpaths              r   _start_depth_cache,ThreadedDepthCacheManager._start_depth_cache  s     ,,JJt ,,,  	
<<-	
 	
 ||~(3u:5%)T"

!4!4S!I	
 r   r   c                 6    U R                  [        UUUUUUUS9$ )N)r   r   r   rL   rZ   r[   r   r   )r   r   )r   r   r   rL   rZ   r[   r   r   s           r   start_depth_cache+ThreadedDepthCacheManager.start_depth_cache  s4     &&'-# ' 	
 		
r   c           
      4    U R                  [        UUUUUUS9$ N)r   r   r   rL   rZ   r[   r   )r   r   r   r   r   rL   rZ   r[   r   s          r   start_futures_depth_socket4ThreadedDepthCacheManager.start_futures_depth_socket  1     &&.- ' 
 	
r   c           
      4    U R                  [        UUUUUUS9$ r   )r   r   r   s          r   start_options_depth_socket4ThreadedDepthCacheManager.start_options_depth_socket	  r   r   rF   )NNNcomF)r   r@   rA   rB   r   r   r   boolr   rC   r   r   r   r   r   rE   r   r   s   @r   r   r     s1    "&$(48M#M SMM "$sCx.1	M
 M M M   	 
F 

 
 

2 

 
 

. 

 
 

 
r   r   )r   operatorr   r_   r|   typingr   r   r   helpersr   streamsr
   threaded_streamr   objectr   r   rH   r   r   r   r   rF   r   r   <module>r      s|        + +  ) /x xv ] ]@m- m`4 0;4 ;
c
 2 c
r   