
    E#i#                        S SK rS SKJrJrJrJr  S SKrS SKJ	r	J
r
JrJrJrJrJrJrJrJrJrJrJr  S SKJr  S SKJr  S SKJr  S SKJr  S SKJr   " S	 S
\R<                  R>                  5      rg)    N)
ArrayCacheArrayCacheBySymbolByIdArrayCacheBySymbolBySideArrayCacheByTimestamp)AnyBalancesBoolIntLiquidationOrder	OrderBookPositionStrStringsTickerTickersTrade)Client)List)ExchangeError)AuthenticationError)RateLimitExceededc                     ^  \ rS rSrS\4U 4S jjr0 4S\S\4S jjrS0 4S\	S\
4S jjrS	\4S
 jrSS0 4S\S\S\S\\   4S jjrSS0 4S\\   S\S\S\\   4S jjrS	\4S jr0 4S\4S jjrS	\4S jrS	\4S jrSS0 4S\S\S\S\\   4S jjr0 4S jrS	\4S jrSSS0 4S\	S\S\S\\   4S jjrS rSSS0 4S\S\S\S\\    4S jjr!S	\4S jr"SSS0 4S\S\S\S\\   4S jjr#S	\4S jr$S0 4S\S\S\%4S jjr&S0 4S\\   S\S\%4S jjr'SS0 4S\\   S\S\S\\   4S jjr(SSS0 4S\S \S\S\S\\)   4
S! jjr*S	\4S" jr+0 4S# jr,S	\4S$ jr-S	\4S% jr.S	\4S& jr/S	\S\04S' jr1S	\4S( jr2S)r3U =r4$ )*bitmex   returnc           
         > U R                  [        [        U ]  5       0 SS_SS_SS_SS_SS _SS _SS_S	S_S
S_SS_SS_SS_SS_SS_SS_SS_SS0SS0S.SSSS.S0 S[        0S.0S.5      $ )NwsTwatchBalancewatchLiquidationswatchLiquidationsForSymbolswatchMyLiquidationswatchMyLiquidationsForSymbolswatchMyTrades
watchOHLCVwatchOrderBookwatchOrderBookForSymbolswatchOrderswatchPostionswatchTickerwatchTickerswatchTradeswatchTradesForSymbolsz$wss://ws.testnet.bitmex.com/realtimezwss://ws.bitmex.com/realtime)testapiorderBookL2  )watchOrderBookLeveltradesLimit
OHLCVLimitzRate limit exceeded)exactbroad)hasurlsoptions
exceptions)deep_extendsuperr   describer   )self	__class__s    I/home/james-whalen/.local/lib/python3.13/site-packages/ccxt/pro/bitmex.pyr=   bitmex.describe   s:   fd < >d $T .t	
 &t 0   d !$ +D t   t  t  (!( @ 8	 (5#"  ./@G,A
 , ,	    symbolc                    #    U R                  5       I Sh  vN   U R                  U5      nU R                  U/U5      I Sh  vN nX1   $  N4 N
7f)a  
watches a price ticker, a statistical calculation with the information calculated over the past 24 hours for a specific market

https://www.bitmex.com/app/wsAPI#Subscriptions

:param str symbol: unified symbol of the market to fetch the ticker for
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict: a `ticker structure <https://docs.ccxt.com/#/?id=ticker-structure>`
N)load_marketsrC   watch_tickers)r>   rC   paramstickerss       r@   watch_tickerbitmex.watch_tickerB   sN      !!!V$**F8V<< 	"<s!   AA+AA	AANsymbolsc                 |  #    U R                  5       I Sh  vN   U R                  USS5      nSnU R                  S   S   n/ n/ nUbd  [        S[	        U5      5       HJ  nX   nU R                  U5      n	US-   U	S   -   n
UR                  U
5        S	U-   nUR                  U5        ML     O"UR                  U5        UR                  S
5        SUS.nU R                  XEU R                  X5      U5      I Sh  vN nU R                  (       a  0 nXUS   '   U$ U R                  U R                  SU5      $  GN! N@7f)a  
watches a price ticker, a statistical calculation with the information calculated over the past 24 hours for all markets of a specific list

https://www.bitmex.com/app/wsAPI#Subscriptions

:param str[] symbols: unified symbol of the market to fetch the ticker for
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict: a `ticker structure <https://docs.ccxt.com/#/?id=ticker-structure>`
NT
instrumentr/   r   r   :idticker:
alltickers	subscribeopargsrC   )rE   market_symbolsr8   rangelenmarketappendwatch_multipleextend
newUpdatesfilter_by_arrayrH   )r>   rK   rG   nameurlmessageHashesrawSubscriptionsirC   rY   subscriptionmessageHashrequesttickerresults                  r@   rF   bitmex.watch_tickersQ   sH     !!!%%gtT:iit$1c'l+ V,#czF4L8 ''5'&0$$[1 , ##D)  .$
 **3t{{7?[]mnn??F'-6(#$M##DLL(GDD5 	"* os"   D<D7C"D<9D::>D<:D<clientc                    U R                  US/ 5      n0 n[        S[        U5      5       H  nX5   nU R                  US5      nU R	                  U5      nXR
                  ;  a  U R                  0 5      U R
                  U'   U R                  U5      n	U R                  U R
                  U   U	5      n
XU'   XR
                  U'   SU-   nUR                  X5        UR                  U
S5        M     U$ )Ndatar   rC   rP   rQ   )		safe_listrW   rX   safe_stringsafe_symbolrH   parse_tickerr;   resolve)r>   rj   messagerl   rH   rc   updatemarketIdrC   updatedTickerfullParsedTickerre   s               r@   handle_tickerbitmex.handle_tickerw   s    F ~~gvr2q#d)$AWF''9H%%h/Fll*'+'8'8'<V$ --f5M#//V0DmT.FO#3LL #f,KNN+9NN+\: % rB   sincelimitc                 2   #    U R                  U/X#U5      $ 7f)a  
watch the public liquidations of a trading pair

https://www.bitmex.com/app/wsAPI#Liquidation

:param str symbol: unified CCXT market symbol
:param int [since]: the earliest time in ms to fetch liquidations for
:param int [limit]: the maximum number of liquidation structures to retrieve
:param dict [params]: exchange specific parameters for the bitmex api endpoint
:returns dict: an array of `liquidation structures <https://github.com/ccxt/ccxt/wiki/Manual#liquidation-structure>`
)watch_liquidations_for_symbolsr>   rC   ry   rz   rG   s        r@   watch_liquidationsbitmex.watch_liquidationsk  s      22F8U6RRs   c                   #    U R                  5       I Sh  vN   U R                  USSS5      n/ n/ nU R                  U5      (       a#  UR                  S5        UR                  S5        O\[	        S[        U5      5       HC  nX   nU R                  U5      n	UR                  SU	S   -   5        UR                  SU-   5        ME     U R                  S	   S
   n
SUS.nU R                  XU R                  X5      U5      I Sh  vN nU R                  (       a  U$ U R                  U R                  XUS5      $  GN$ N87f)a  
watch the public liquidations of a trading pair

https://www.bitmex.com/app/wsAPI#Liquidation

:param str[] symbols:
:param int [since]: the earliest time in ms to fetch liquidations for
:param int [limit]: the maximum number of liquidation structures to retrieve
:param dict [params]: exchange specific parameters for the bitmex api endpoint
:returns dict: an array of `liquidation structures <https://github.com/ccxt/ccxt/wiki/Manual#liquidation-structure>`
NTliquidationliquidationsr   zliquidation:rO   liquidations::r/   r   rR   rS   )rE   rV   is_emptyrZ   rW   rX   rY   r8   r[   r;   r]   filter_by_symbols_since_limitr   )r>   rK   ry   rz   rG   ra   subscriptionHashesrc   rC   rY   r`   rf   newLiquidationss                r@   r|   %bitmex.watch_liquidations_for_symbolsy  s9     !!!%%gtT4@==!!%%m4  01c'l+ V,")).6$<*GH$$%5%>?	 ,
 iit$&
 !% 3 3CHXHXY`Hik} ~~??""11$2C2CWUZ\`aa+ 	"$ s"   D?D:C-D?D=6D?=D?c                 p   U R                  US/ 5      n/ n[        S[        U5      5       H  nX5   nU R                  U5      nUS   nU R                  U R                  U5      n	U	c(  U R                  U R                  SS5      n
[        U
5      n	U	R                  U5        XR                  U'   UR                  U5        M     UR                  US5        U R                  US5      n[        UR                  5       5      n[        S[        U5      5       H  nX   nUR                  X   SU-   5        M      g )Nrl   r   rC   liquidationsLimitr1   r   r   )
safe_valuerW   rX   parse_liquidationr   safe_integerr9   r   rZ   rq   index_bylistkeys)r>   rj   rr   rawLiquidationsr   rc   rawLiquidationr   rC   r   rz   liquidationsBySymbolrK   s                r@   handle_liquidationbitmex.handle_liquidation  s%   6 //'62>q#o./A,/N00@K *F??4+<+<fEL#))$,,8KTR)%0,(4f%"";/ 0 	7#}}_hG+0023q#g,'AZFNN/79IF9RS (rB   c                    #    U R                  5       I Sh  vN   U R                  5       I Sh  vN   SnU R                  S   S   nSU/S.nU R                  X2U R	                  XA5      U5      I Sh  vN $  N` NJ N7f)a.  
watch balance and get the amount of funds available for trading or funds locked in orders

https://www.bitmex.com/app/wsAPI#Subscriptions

:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict: a `balance structure <https://docs.ccxt.com/#/?id=balance-structure>`
Nmarginr/   r   rR   rS   )rE   authenticater8   watchr\   )r>   rG   re   r`   rf   s        r@   watch_balancebitmex.watch_balance  s      !!!!!!iit$
 ZZ$++g2NP[\\\ 	"! ]s2   A=A7A=A9AA=2A;3A=9A=;A=c                     U R                  US5      nU R                  U5      nU R                  U R                  U5      U l        U R	                  US5      nUR                  U R                  U5        g )Nrl   table)r   parse_balancer\   balancern   rq   )r>   rj   rr   rl   r   re   s         r@   handle_balancebitmex.handle_balance  s_    F w/$$T*{{4<<9&&w8t||[1rB   c                 R   SnU R                  US/ 5      nU R                  US5      n[        UR                  5       5      n[	        S[        U5      5       H  nXg   nU R                  U5      n	U	S   n
US-   U
-   nU R                  XX   U	5      nU R                  U R                  U
5      nUc6  U R                  U R                  SS5      n[        U5      nXR                  U
'   [	        S[        U5      5       H  nUR                  X   5        M     UR                  X5        M     g )Ntraderl   rC   r   rN   r3   r1   )r   group_byr   r   rW   rX   safe_marketparse_tradestradesr   r9   r   rZ   rq   )r>   rj   rr   r   rl   dataByMarketIds	marketIdsrc   rt   rY   rC   re   r   storedrz   js                   r@   handle_tradesbitmex.handle_tradesI  s   x w3--h7--/0	q#i.)A |H%%h/FH%F#+.K&&'@&IF__T[[&9F~))$,,tL#E*&,F#1c&k*fi( +NN6/ *rB   c                 F   #    U R                  U/X#U5      I Sh  vN $  N7f)a  
get the list of most recent trades for a particular symbol

https://www.bitmex.com/app/wsAPI#Subscriptions

:param str symbol: unified symbol of the market to fetch trades for
:param int [since]: timestamp in ms of the earliest trade to fetch
:param int [limit]: the maximum amount of trades to fetch
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict[]: a list of `trade structures <https://docs.ccxt.com/#/?id=public-trades>`
N)watch_trades_for_symbolsr}   s        r@   watch_tradesbitmex.watch_trades  s$      22F8U6RRRRs   !!c                 $  #    U R                   S   S   nU R                  U5      nSnUR                  U5      nU R                  UR                  U5      nUc  U R                  5         U R                  5       nS[        U5      -   nU R                  U R                  U5      U R                  U R                  5      [        R                  5      n	SU R                  UU	/S.n
U R                  X5      nU R                  X$X5        UI S h  vN $  N7f)Nr/   r   authenticatedzGET/realtimeauthKeyExpiresrS   )r8   rj   reusableFuturer   subscriptionscheck_required_credentialsmillisecondsstrhmacencodesecrethashlibsha256apiKeyr\   r   )r>   rG   r`   rj   re   futurer   	timestamppayload	signaturerf   rr   s               r@   r   bitmex.authenticate  s     iit$S!%&&{3(<(<kJ ++-))+I)C	N:G		$++g"6DKK8PRYR`R`aI&KKG kk'2GJJs>||s   DD	D
Dc                 (   U R                  USS5      nSnU(       a.  U R                  UR                  U5      nUR                  S5        g [	        U R                  U5      5      nUR                  Xd5        XAR                  ;   a  UR                  U	 g g )NsuccessFr   T)	safe_boolr   futuresrq   r   jsonrejectr   )r>   rj   rr   r   re   r   errors          r@   handle_authentication_message$bitmex.handle_authentication_message  sz    w	5A%__V^^[AFNN4 '		'(:;EMM%-222((5 3rB   c                   #    U R                  5       I Sh  vN   U R                  5       I Sh  vN   SnSnU R                  U5      (       d  SSR                  U5      -   nU R                  S   S   nSU/S	.nU R                  XvX5      I Sh  vN n	U R                  (       a  U	$ U R                  U R                  XUS
5      $  N N N97f)a  
watch all open positions

https://www.bitmex.com/app/wsAPI#Subscriptions

:param str[]|None symbols: list of unified market symbols
:param int [since]: the earliest time in ms to watch positions for
:param int [limit]: the maximum number of positions to retrieve
:param dict params: extra parameters specific to the exchange API endpoint
:returns dict[]: a list of `position structure <https://docs.ccxt.com/en/latest/manual.html#position-structure>`
Nposition	positions::,r/   r   rR   rS   T)	rE   r   r   joinr8   r   r]   r   r   )
r>   rK   ry   rz   rG   subscriptionHashre   r`   rf   newPositionss
             r@   watch_positionsbitmex.watch_positions  s      !!!!!!%!}}W%%'!22Kiit$ 
 "ZZ'TT??11$..'RWY]^^! 	"! Us2   C
CC
CAC
C6C
C
C
c                 t   U R                   c  [        5       U l         U R                   nU R                  US/ 5      n/ n[        S[	        U5      5       H:  nXF   nU R                  U5      nUR                  U5        UR                  U5        M<     U R                  US5      n	[        S[	        U	5      5       Hk  nX   n
U
R                  S5      nUS   nUR                  S5      nU R                  USUS5      nU R                  U5      (       a  MZ  UR                  X5        Mm     UR                  US	5        g )
Nrl   r   zpositions::r      r   rC   Fr   )r   r   r   rW   rX   parse_positionrZ   find_message_hashessplitr^   r   rq   )r>   rj   rr   cacherawPositionsr   rc   rawPositionr   ra   re   partssymbolsStringrK   r   s                  r@   handle_positionsbitmex.handle_positions  s   j >>!57DNw;q#l+,A&/K**;7H)LL"	 -
 00Gq#m,-A'*K%%d+E!!HM#))#.G,,\8WeTI==++y6 . 	|[1rB   c                   #    U R                  5       I Sh  vN   U R                  5       I Sh  vN   SnUnUnUb  U R                  U5      nUSU-   -  nU R                  S   S   nSU/S.n	U R	                  XX5      I Sh  vN n
U R
                  (       a  U
R                  X5      nU R                  XX#S5      $  N N N=7f)	a  
watches information on multiple orders made by the user

https://www.bitmex.com/app/wsAPI#Subscriptions

:param str symbol: unified market symbol of the market orders were made in
:param int [since]: the earliest time in ms to fetch orders for
:param int [limit]: the maximum number of order structures to retrieve
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict[]: a list of `order structures <https://docs.ccxt.com/#/?id=order-structure>`
NorderrN   r/   r   rR   rS   TrE   r   rC   r8   r   r]   getLimitfilter_by_symbol_since_limit)r>   rC   ry   rz   rG   r_   r   re   r`   rf   orderss              r@   watch_ordersbitmex.watch_orders  s      !!!!!![[(F3<'Kiit$ 
 zz#GNN??OOF2E00tTT% 	"! O2   CB<CB>ACC :C>C Cc                    U R                  US/ 5      nSn[        U5      nUS:  GaK  U R                  c-  U R                  U R                  SS5      n[        U5      U l        U R                  n0 n[        SU5       Hz  n	X9   n
U R                  U
S5      nU R                  UR                  U5      nU
nUb  U R                  US   U
5      nU R                  U5      nUR                  U5        US   nS	X'   M|     UR                  U R                  U5        [        UR                  5       5      n[        S[        U5      5       H*  n	UU	   nUR                  U R                  US
-   U-   5        M,     g g )Nrl   r   r   ordersLimitr1   orderIDinforC   TrN   )r   rX   r   r   r9   r   rW   rn   hashmapr\   parse_orderrZ   rq   r   r   )r>   rj   rr   rl   re   
dataLengthrz   r   rK   rc   currentOrderorderIdpreviousOrderrawOrderr   rC   r   s                    r@   handle_ordersbitmex.handle_orders  sU   l w3Y
>{{"))$,,tL4U;[[FG1j)#w**<C $ H' ,#{{=+@,OH((2e$x"& * NN4;;4'D1c$i(at{{K#,=,FG )' rB   c                   #    U R                  5       I Sh  vN   U R                  5       I Sh  vN   SnUnUnUb  U R                  U5      nUSU-   -  nU R                  S   S   nSU/S.n	U R	                  XX5      I Sh  vN n
U R
                  (       a  U
R                  X5      nU R                  XX#S5      $  N N N=7f)	a  
watches information on multiple trades made by the user

https://www.bitmex.com/app/wsAPI#Subscriptions

:param str symbol: unified market symbol of the market trades were made in
:param int [since]: the earliest time in ms to fetch trades for
:param int [limit]: the maximum number of trade structures to retrieve
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict[]: a list of `trade structures <https://docs.ccxt.com/#/?id=trade-structure>`
N	executionrN   r/   r   rR   rS   Tr   )r>   rC   ry   rz   rG   r_   r   re   r`   rf   r   s              r@   watch_my_tradesbitmex.watch_my_tradesc  s      !!!!!![[(F3<'Kiit$ 
 zz#GNN??OOF2E00tTT% 	"! Or   c                    U R                  US5      nU R                  US/ 5      nU R                  US5      nU R                  US/ 5      nU R                  U5      nU R                  c-  U R                  U R                  SS5      n[        U5      U l        U R                  n	0 n
[        S[        U5      5       H!  nX{   nUS   nU	R                  U5        XU'   M#     [        U5      nUS:  a  UR                  X5        [        U
R                  5       5      n[        S[        U5      5       H  nUR                  XS	-   UU   -   5        M     g )
Nr   rl   execTyper   r3   r1   r   rC   rN   )rn   r   r   r   myTradesr   r9   r   rW   rX   rZ   rq   r   r   )r>   rj   rr   re   rl   dataByExecType	rawTradesr   rz   r   rK   r   r   rC   	numTradesr   rc   s                    r@   handle_my_tradesbitmex.handle_my_trades  s,   t &&w8w3tZ8OONGR@	""9-== %%dllM4HE259DMq#f+&AIE8_FMM% #FO	 '
 K	q=NN6/GLLN#q#d)$ANN6#4tAw#>? %rB   c                 D   #    U R                  U/X#5      I Sh  vN $  N7f)a  
watches information on open orders with bid(buy) and ask(sell) prices, volumes and other data

https://www.bitmex.com/app/wsAPI#OrderBookL2

:param str symbol: unified symbol of the market to fetch the order book for
:param int [limit]: the maximum amount of order book entries to return
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict: A dictionary of `order book structures <https://docs.ccxt.com/#/?id=order-book-structure>` indexed by market symbols
N)watch_order_book_for_symbols)r>   rC   rz   rG   s       r@   watch_order_bookbitmex.watch_order_book  s"      66xOOOOs     c                 t  #    SnUc  U R                  U R                  SS5      nO*US:X  a  SnO!US:X  a  SnO[        U R                  S-   5      eU R	                  5       I Sh  vN   U R                  U5      n/ n/ n[        S	[        U5      5       HM  nX   nU R                  U5      n	US
-   U	S   -   n
UR                  U
5        US
-   U-   nUR                  U5        MO     U R                  S   S   nSUS.nU R                  XU R                  X5      U5      I Sh  vN nUR                  5       $  N N7f)a  
watches information on open orders with bid(buy) and ask(sell) prices, volumes and other data

https://www.bitmex.com/app/wsAPI#OrderBookL2

:param str[] symbols: unified array of symbols
:param int [limit]: the maximum amount of order book entries to return
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict: A dictionary of `order book structures <https://docs.ccxt.com/#/?id=order-book-structure>` indexed by market symbols
Nr2   r0      orderBookL2_25
   orderBookL10zK watchOrderBookForSymbols limit argument must be None(L2), 25(L2) or 10(L3)r   rN   rO   r/   r   rR   rS   )rn   r9   r   rO   rE   rV   rW   rX   rY   rZ   r8   r[   r;   rz   )r>   rK   rz   rG   r   topicsra   rc   rC   rY   topicre   r`   rf   	orderbooks                  r@   r  #bitmex.watch_order_book_for_symbols  sG     =$$T\\3H-XEb[$Eb["E*w wxx!!!%%g.q#g,'AZF[[(FCK&,.EMM% #+.K  - ( iit$
 --c$BRBRSZBcekll	  # 	"  ms%   A!D8#D4$B;D8D6 D86D8c                 z  #    U R                  5       I Sh  vN   U R                  USS5      nSn/ n/ n[        S[        U5      5       HM  nX   n	U R	                  U	5      n
US-   U
S   -   nUR                  U5        US-   U	-   nUR                  U5        MO     U R                  S   S   nS	US
.nU R                  XU R                  X5      U5      I Sh  vN nU R                  (       a6  U R                  US5      nU R                  US5      nUR                  UU5      nU R                  XUSS5      $  GN  Nb7f)a  
get the list of most recent trades for a list of symbols

https://www.bitmex.com/app/wsAPI#Subscriptions

:param str[] symbols: unified symbol of the market to fetch trades for
:param int [since]: timestamp in ms of the earliest trade to fetch
:param int [limit]: the maximum amount of trades to fetch
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict[]: a list of `trade structures <https://docs.ccxt.com/#/?id=public-trades>`
NFr   r   rN   rO   r/   r   rR   rS   rC   r   T)rE   rV   rW   rX   rY   rZ   r8   r[   r;   r]   r   rn   r   filter_by_since_limit)r>   rK   ry   rz   rG   r   r  ra   rc   rC   rY   r  re   r`   rf   r   firsttradeSymbols                     r@   r   bitmex.watch_trades_for_symbols  sC     !!!%%gtU;q#g,'AZF[[(FCK&,.EMM% #+.K  - ( iit$
 **3t?O?OPW?`bhii??OOFA.E**5(;KOOK7E))&TRR- 	"" js#   D;D6B?D;D9A D;9D;1m	timeframec                   #    U R                  5       I Sh  vN   U R                  U5      nUS   nSU R                  U R                  X"5      -   nUS-   US   -   nU R                  S   S   n	SU/S	.n
U R                  XU R                  X5      U5      I Sh  vN nU R                  (       a  UR                  X5      nU R                  XUS
S5      $  N N<7f)aB  
watches historical candlestick data containing the open, high, low, and close price, and the volume of a market

https://www.bitmex.com/app/wsAPI#Subscriptions

:param str symbol: unified symbol of the market to fetch OHLCV data for
:param str timeframe: the length of time each candle represents
:param int [since]: timestamp in ms of the earliest candle to fetch
:param int [limit]: the maximum amount of candles to fetch
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns int[][]: A list of candles ordered, open, high, low, close, volume
NrC   tradeBinrN   rO   r/   r   rR   rS   r   T)
rE   rY   rn   
timeframesr8   r   r\   r]   r   r  )r>   rC   r  ry   rz   rG   rY   r   re   r`   rf   ohlcvs               r@   watch_ohlcvbitmex.watch_ohlcv+  s      !!!V$!T--dooyTTckF4L0iit$
 jj4;;w3OQ\]]??NN61E))%q$GG 	" ^s"   CCBCC;CCc           
         U R                  US5      nUR                  SS5      nU R                  U5      nU R                  U5      nU R	                  US/ 5      n0 n[        S[        U5      5       GHC  n	Xy   n
U R                  U
S5      nU R                  U5      nUS   nUS-   US   -   nU R                  U R                  U
S	5      5      US
-  -
  S U R                  U
S5      U R                  U
S5      U R                  U
S5      U R                  U
S5      /nU R	                  U R                  U0 5      U R                  U'   U R	                  U R                  U   U5      nUc:  U R                  U R                  SS
5      n[        U5      nUU R                  U   U'   UR                  U5        UX'   GMF     [        UR!                  5       5      n[        S[        U5      5       H  n	UU	   nUR#                  X   U5        M     g )Nr   r   rl   r   rC   rN   rO   r   r1   highlowclosevolumer4   )rn   replacefind_timeframeparse_timeframer   rW   rX   r   	parse8601
safe_floatohlcvsr   r9   r   rZ   r   r   rq   )r>   rj   rr   r   intervalr  durationcandlesresultsrc   candlert   rY   rC   re   rh   r   rz   ra   s                      r@   handle_ohlcvbitmex.handle_ohlcvI  s   D   '2==R0''1	''	2//'626q#g,'AZF''9H%%h/FH%F#+t4Kt//DESWW/.01F #'//$++vr"JDKK__T[[%8)DF~))$,,dK.u517F#I.MM&!#)G + (, W\\^,q#m,-A'*KNN7/= .rB   c                    #    U R                  5       I S h  vN   SnU R                  S   S   nU R                  X25      I S h  vN $  N1 N7f)N	heartbeatr/   r   )rE   r8   r   )r>   rG   eventr`   s       r@   watch_heartbeatbitmex.watch_heartbeat  sK     !!!iit$ZZ+++ 	" ,s!   AA,AA
A
Ac           	      ,   U R                  US5      nU R                  US5      nUc  g U R                  US/ 5      nUS:X  Ga  U R                  US0 5      nU R                  US5      nUc  g U R                  U5      nUS   n	US:X  a  U R	                  5       U R
                  U	'   OKUS:X  a   U R	                  0 S	5      U R
                  U	'   O%US
:X  a  U R	                  0 S5      U R
                  U	'   U R
                  U	   n
XS'   [        S[        U5      5       H  nU R                  X[   S5      nU R                  XR                  X[   S5      5      nU R                  X[   S5      nU R                  X[   S5      nUS:X  a  SOSnX   nUR                  XU/5        U R                  X[   S5      nU R                  U5      U
S'   UU
S'   M     US-   U	-   nUR                  U
U5        g 0 n[        S[        U5      5       GH  nU R                  X[   S5      nUc    g UU;  a  SUU'   U R                  US5      UU'   U R                  U5      nUS   n	U R
                  U	   n
U R                  X[   S5      nUS:X  a  SO#U R                  XR                  X[   SS5      5      nU R                  X[   S5      nU R                  X[   S5      nUS:X  a  SOSnX   nUR                  XU/5        U R                  X[   S5      nU R                  U5      U
S'   UU
S'   GM     [        UR!                  5       5      n[        S[        U5      5       HG  nUU   nU R                  U5      nUS   n	US-   U	-   nU R
                  U	   n
UR                  U
U5        MI     g )Nactionr   rl   partialfilterrC   r0   r  r  orderBook10r  r   pricesizerO   sideBuybidsasksr   datetimerN   r   delete0)rn   r   	safe_dictr   indexed_order_book
orderbooksrW   rX   r*  convertFromRawQuantity
storeArrayr)  rq   sumsafe_numberr   r   )r>   rj   rr   r9  r   rl   r;  rt   rY   rC   r  rc   r=  r>  rO   r?  booksiderC  re   numUpdatesByMarketIdr   s                        r@   handle_order_bookbitmex.handle_order_book  s   d !!'84  '2=w3Y^^GXr:Fvx8H%%h/FH%F%*.*A*A*C'***.*A*A"b*I'-'*.*A*A"b*I'/I"(h1c$i(9226;K;KDGU[;\]%%dgt4''8"&%-vf$?##U"$56++DG[A)-)A	+&(0	*% )  #+.KNN9k2)+ 1c$i(??47H=# $8856(215:NPQ1R$X.))(3) OOF3	((':#x/qd6Q6QRXZjZjkokrtz|  [A  7B%%dgt4''8"&%-vf$?##U"$56++DG[A)-)A	+&(0	*%' )( 16689I1c)n-$Q<))(3)#ckF2 OOF3	y+6 .rB   c                     U$ N r>   rj   rr   s      r@   handle_system_statusbitmex.handle_system_status%  s	     rB   c                     U$ rR  rS  rT  s      r@   handle_subscription_status!bitmex.handle_subscription_status5  s	     rB   c                 F   U R                  US5      nUb  U R                  US0 5      nU R                  US/ 5      n[        U5      nUS:  aU  US   nU R                  S   S   nU R	                  X5      n	S n
U	c  [        U5      n
O
X   " U5      n
UR                  X5        gg)	Nr   rf   rU   r   r   r6   FT)rn   r   rX   r:   find_broadly_matched_keyr   r   )r>   rj   rr   r   rf   rU   numArgsre   r6   broadKey	exceptions              r@   handle_error_messagebitmex.handle_error_message?  s    "   '2oogy"=G??7FB7D$iG{"1g-g688F 	# -e 4I % 6Ii5rB   c                 F   U R                  X5      (       Ga
  U R                  US5      nU R                  U R                  U R                  U R                  U R                  U R
                  U R
                  U R
                  U R
                  U R                  U R                  U R                  U R                  U R                  S.nU R                  XC5      nUc>  U R                  US0 5      nU R                  US5      nUS:X  a  U R                  X5        g g U" X5        g g )Nr   )r0   r  r<  rM   r   
tradeBin1m
tradeBin5m
tradeBin1h
tradeBin1dr   r   r   r   r   rf   rT   r   )r_  rn   rO  rw   r   r1  r   r  r   r   r   r   r   )r>   rj   rr   r   methodsmethodrf   rT   s           r@   handle_messagebitmex.handle_messageb  s   F $$V55$$Wg6E#55"&"8"8#55"00++"//"//"//"//++!22--#66 11G  __W4F~//'9bA__Wd3))66vG * v'3 6rB   )r   r   r   r   )5__name__
__module____qualname____firstlineno__r   r=   r   r   rI   r   r   rF   r   rw   r
   r   r   r~   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r   r  r  r   r   r  r1  r6  rO  rU  rX  r	   r_  rh  __static_attributes____classcell__)r?   s   @r@   r   r      s   -# -^ 68  F  6:" $E7 $E $ELrF rh BFTXac Ss S3 Sc Shlmxhy S UYgktv !bDI !bc !bad !b{  AL  |M !bF-T -T^ *, ] ]*g2V g2RM0F M0^ <@d[] S SS S Sbfglbm S )+ .6F 6 8<$]ajl _W _C _WZ _quv~q _<h2T 04$UYbd U UC Us Uimnsit U@oHF oHb 37TX\eg UC Us URU Ulpqvlw U@N@v N@` @DB PS P PS\ P SW_a %!$s) %!C %!fo %!N OSaenp "Sd3i "S "S[^ "Suyz  vA "SH ?CQUdhqs H H H3 H^a Hx|  ~B  yC H<a>6 a>F ,. ,q7 q7f6   !6 !t !F<(V <( <(rB   r   ) ccxt.async_supportccxt ccxt.async_support.base.ws.cacher   r   r   r   r   ccxt.base.typesr   r   r	   r
   r   r   r   r   r   r   r   r   r   !ccxt.async_support.base.ws.clientr   typingr   ccxt.base.errorsr   r   r   async_supportr   rS  rB   r@   <module>rx     sZ     A  A  D  D  D  D 4  * 0 .M(T&& M(rB   