
    ":h#                        d dl Zd dlmZmZmZmZ d dlZd dlm	Z	m
Z
mZmZmZmZmZmZmZmZmZm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  G d	 d
ej:                  j<                        Zy)    N)
ArrayCacheArrayCacheBySymbolByIdArrayCacheBySymbolBySideArrayCacheByTimestamp)AnyBalancesIntLiquidationOrder	OrderBookPositionStrStringsTickerTickersTrade)Client)List)ExchangeError)AuthenticationError)RateLimitExceededc            
           e Zd Zdef fdZi fdedefdZdi fdede	fdZ
defd	Zddi fded
ededee   fdZdddi fdee   d
ededee   fdZdefdZi fdefdZdefdZdefdZddi fded
ededee   fdZi fdZdefdZdddi fded
ededee   fdZd Zdddi fded
ededee   fdZ defdZ!dddi fded
ededee   fdZ"defdZ#di fdedede$fdZ%di fdee   dede$fdZ&ddi fdee   d
ededee   fdZ'dddi fded
ededee(   fdZ)defd Z*i fd!Z+defd"Z,defd#Z-defd$Z.defd%Z/defd&Z0 xZ1S )'bitmexreturnc           
          | j                  t        t        |          i dddddddddd dd ddd	dd
dddddddddddddddddiddiddddddi dt        idid      S )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/var/www/html/turnos/venv/lib/python3.12/site-packages/ccxt/pro/bitmex.pyr;   zbitmex.describe   s:   fd < >d $T .t	
 &t 0   d !$ +D t   t  t  (!( @ 8	 (5#"  ./@G,A
 , ,	    symbolc                    K   | j                          d{    | j                  |      }| j                  |g|       d{   }||   S 7 57 w)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_marketsr@   watch_tickers)r<   r@   paramstickerss       r>   watch_tickerzbitmex.watch_tickerB   sR      !!!V$**F8V<<v 	"<s!   AA+AA
AANsymbolsc                 t  K   | j                          d{    | j                  |dd      }d}| j                  d   d   }g }g }|ct        dt	        |            D ]J  }||   }| j                  |      }	|dz   |	d   z   }
|j                  |
       d	|z   }|j                  |       L n"|j                  |       |j                  d
       d|d}| j                  ||| j                  ||      |       d{   }| j                  ri }|||d   <   |S | j                  | j                  d|      S 7 7 <w)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argsr@   )rB   market_symbolsr6   rangelenmarketappendwatch_multipleextend
newUpdatesfilter_by_arrayrE   )r<   rG   rD   nameurlmessageHashesrawSubscriptionsir@   rU   subscriptionmessageHashrequesttickerresults                  r>   rC   zbitmex.watch_tickersQ   sX     !!!%%gtT:iit$1c'l+ 2 V,#czF4L8 ''5'&0$$[12 ##D)  .$
 **3t{{7TZ?[]mnn??F'-F6(#$M##DLL(GDD5 	"* os"   D8D3C"D89D6::D86D8clientc                    | j                  |dg       }i }t        dt        |            D ]  }||   }| j                  |d      }| j	                  |      }|| j
                  vr| j                  i       | j
                  |<   | j                  |      }	| j                  | j
                  |   |	      }
|
||<   |
| j
                  |<   d|z   }|j                  |
|       |j                  |
d        |S )Ndatar   r@   rL   rM   )		safe_listrS   rT   safe_stringsafe_symbolrE   parse_tickerr9   resolve)r<   re   messagerg   rE   r_   updatemarketIdr@   updatedTickerfullParsedTickerra   s               r>   handle_tickerzbitmex.handle_tickerw   s    F ~~gvr2q#d)$ 	;A!WF''9H%%h/Fdll*'+'8'8'<V$ --f5M#//V0DmT.GFO#3DLL #f,KNN+[9NN+\:	; r?   sincelimitc                 4   K   | j                  |g|||      S w)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<   r@   rs   rt   rD   s        r>   watch_liquidationszbitmex.watch_liquidationsk  s!      22F8UE6RRs   c                 r  K   | j                          d{    | j                  |ddd      }g }g }| j                  |      r#|j                  d       |j                  d       n[t	        dt        |            D ]C  }||   }| j                  |      }	|j                  d|	d   z          |j                  d|z          E | j                  d	   d
   }
d|d}| j                  |
|| j                  ||      |       d{   }| j                  r|S | j                  | j                  |||d      S 7 7 4w)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:rK   liquidations::r-   r   rN   rO   )rB   rR   is_emptyrV   rS   rT   rU   r6   rW   r9   rY   filter_by_symbols_since_limitr{   )r<   rG   rs   rt   rD   r]   subscriptionHashesr_   r@   rU   r\   rb   newLiquidationss                r>   rv   z%bitmex.watch_liquidations_for_symbolsy  sH     !!!%%gtT4@==!%%m4  01c'l+ @ V,")).6$<*GH$$%5%>?	@
 iit$&
 !% 3 3CHXHXY`bhHik} ~~??""11$2C2CWeUZ\`aa+ 	"$ s"   D7D2C)D7 D52D75D7c                 n   | j                  |dg       }g }t        dt        |            D ]  }||   }| j                  |      }|d   }| j                  | j                  |      }	|	(| j                  | j                  dd      }
t        |
      }	|	j                  |       |	| j                  |<   |j                  |        |j                  |d       | j                  |d      }t        |j                               }t        dt        |            D ]  }||   }|j                  ||   d|z          ! y )Nrg   r   r@   liquidationsLimitr/   r{   r|   )
safe_valuerS   rT   parse_liquidationr{   safe_integerr7   r   rV   rl   index_bylistkeys)r<   re   rm   rawLiquidationsr   r_   rawLiquidationrz   r@   r{   rt   liquidationsBySymbolrG   s                r>   handle_liquidationzbitmex.handle_liquidation  s9   6 //'62>q#o./ 
	0A,Q/N00@K *F??4+<+<fEL#))$,,8KTR)%0,(4Df%"";/
	0 	7#}}_hG+0023q#g,' 	TAQZFNN/79IF9RS	Tr?   c                   K   | j                          d{    | j                          d{    d}| j                  d   d   }d|gd}| j                  ||| j	                  ||      |       d{   S 7 b7 L7 w)aV  
        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   rN   rO   )rB   authenticater6   watchrX   )r<   rD   ra   r\   rb   s        r>   watch_balancezbitmex.watch_balance  s      !!!!!!iit$
 ZZ[$++gv2NP[\\\ 	"! ]s2   A?A9A?A;AA?4A=5A?;A?=A?c                     | j                  |d      }| j                  |      }| j                  | j                  |      | _        | j	                  |d      }|j                  | j                  |       y )Nrg   table)r   parse_balancerX   balanceri   rl   )r<   re   rm   rg   r   ra   s         r>   handle_balancezbitmex.handle_balance  s_    F w/$$T*{{4<<9&&w8t||[1r?   c                 R   d}| j                  |dg       }| j                  |d      }t        |j                               }t	        dt        |            D ]  }||   }| j                  |      }	|	d   }
|dz   |
z   }| j                  ||   |	      }| j                  | j                  |
      }|7| j                  | j                  dd      }t        |      }|| j                  |
<   t	        dt        |            D ]  }|j                  ||           |j                  ||        y )Ntraderg   r@   r   rJ   r1   r/   )r   group_byr   r   rS   rT   safe_marketparse_tradestradesr   r7   r   rV   rl   )r<   re   rm   r   rg   dataByMarketIds	marketIdsr_   ro   rU   r@   ra   r   storedrt   js                   r>   handle_tradeszbitmex.handle_tradesI  s"   x w3--h7--/0	q#i.) 	0A |H%%h/FH%F#+.K&&x'@&IF__T[[&9F~))$,,tL#E*&,F#1c&k* )fQi()NN6;/	0r?   c                 H   K   | j                  |g|||       d{   S 7 w)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_symbolsrw   s        r>   watch_tradeszbitmex.watch_trades  s'      22F8UE6RRRRs   " "c                 (  K   | j                   d   d   }| j                  |      }d}|j                  |      }| j                  |j                  |      }|| j                          | j                         }dt        |      z   }| j                  | j                  |      | j                  | j                        t        j                        }	d| j                  ||	gd}
| j                  |
|      }| j                  ||||       | d {   S 7 w)Nr-   r   authenticatedzGET/realtimeauthKeyExpiresrO   )r6   re   futurer   subscriptionscheck_required_credentialsmillisecondsstrhmacencodesecrethashlibsha256apiKeyrX   r   )r<   rD   r\   re   ra   r   r   	timestamppayload	signaturerb   rm   s               r>   r   zbitmex.authenticate  s     iit$S!%{+(<(<kJ ++-))+I)C	N:G		$++g"6DKK8PRYR`R`aI&KKG kk'62GJJsK+>||s   D	DDDc                    | j                  |dd      }d}|r.| j                  |j                  |      }|j                  d       y t	        | j                  |            }|j                  ||       ||j                  v r|j                  |= y y )NsuccessFr   T)	safe_boolr   futuresrl   r   jsonrejectr   )r<   re   rm   r   ra   r   errors          r>   handle_authentication_messagez$bitmex.handle_authentication_message  s~    w	5A%__V^^[AFNN4 '		'(:;EMM%-f222((5 3r?   c                   K   | j                          d{    | j                          d{    d}d}| j                  |      sddj                  |      z   }| j                  d   d   }d|gd	}| j                  ||||       d{   }	| j                  r|	S | j                  | j                  |||d
      S 7 7 7 5w)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   rN   rO   T)	rB   r   r}   joinr6   r   rY   r~   r   )
r<   rG   rs   rt   rD   subscriptionHashra   r\   rb   newPositionss
             r>   watch_positionszbitmex.watch_positions  s      !!!!!!%!}}W%'!22Kiit$ 
 "ZZ['CSTT??11$..'5RWY]^^! 	"! Us2   CB=CB?ACC2C?CCc                 d   | j                   t               | _         | j                   }| j                  |dg       }g }t        dt	        |            D ]:  }||   }| j                  |      }|j                  |       |j                  |       < | j                  |d      }	t        dt	        |	            D ]f  }|	|   }
|
j                  d      }|d   }|j                  d      }| j                  |d|d      }| j                  |      rU|j                  ||
       h |j                  |d	       y )
Nrg   r   zpositions::r      r   r@   Fr   )r   r   r   rS   rT   parse_positionrV   find_message_hashessplitrZ   r}   rl   )r<   re   rm   cacherawPositionsr   r_   rawPositionr   r]   ra   partssymbolsStringrG   r   s                  r>   handle_positionszbitmex.handle_positions  s)   j >>!57DNw;q#l+, 	#A&q/K**;7H)LL"		#
 00Gq#m,- 	7A'*K%%d+E!!HM#))#.G,,\8WeTI==+y+6	7 	|[1r?   c                   K   | j                          d{    | j                          d{    d}|}|}|| j                  |      }|d|z   z  }| j                  d   d   }d|gd}	| j	                  |||	|       d{   }
| j
                  r|
j                  ||      }| j                  |
|||d      S 7 7 7 ;w)	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orderrJ   r-   r   rN   rO   TrB   r   r@   r6   r   rY   getLimitfilter_by_symbol_since_limit)r<   r@   rs   rt   rD   r[   r   ra   r\   rb   orderss              r>   watch_orderszbitmex.watch_orders  s      !!!!!![[(F3<'Kiit$ 
 zz#{G=MNN??OOFE2E00tTT% 	"! O2   CB;CB=ACB?8C=C?Cc                    | j                  |dg       }d}t        |      }|dkD  rG| j                  -| j                  | j                  dd      }t        |      | _        | j                  }i }t        d|      D ]z  }	||	   }
| j                  |
d      }| j                  |j                  |      }|
}|| j                  |d   |
      }| j                  |      }|j                  |       |d   }d	||<   | |j                  | j                  |       t        |j                               }t        dt        |            D ])  }	||	   }|j                  | j                  |d
z   |z          + y y )Nrg   r   r   ordersLimitr/   orderIDinfor@   TrJ   )r   rT   r   r   r7   r   rS   ri   hashmaprX   parse_orderrV   rl   r   r   )r<   re   rm   rg   ra   
dataLengthrt   r   rG   r_   currentOrderorderIdpreviousOrderrawOrderr   r@   r   s                    r>   handle_orderszbitmex.handle_orders  se   l w3Y
>{{"))$,,tL4U;[[FG1j) 
'#Aw**<C $ H' ,#{{=+@,OH((2e$x"&
' NN4;;4'D1c$i( Hat{{K#,=,FGH' r?   c                   K   | j                          d{    | j                          d{    d}|}|}|| j                  |      }|d|z   z  }| j                  d   d   }d|gd}	| j	                  |||	|       d{   }
| j
                  r|
j                  ||      }| j                  |
|||d      S 7 7 7 ;w)	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	executionrJ   r-   r   rN   rO   Tr   )r<   r@   rs   rt   rD   r[   r   ra   r\   rb   r   s              r>   watch_my_tradeszbitmex.watch_my_tradesc  s      !!!!!![[(F3<'Kiit$ 
 zz#{G=MNN??OOFE2E00tTT% 	"! Or   c                    | j                  |d      }| j                  |dg       }| j                  |d      }| j                  |dg       }| j                  |      }| j                  -| j                  | j                  dd      }t        |      | _        | j                  }	i }
t        dt        |            D ]"  }||   }|d   }|	j                  |       ||
|<   $ t        |      }|dkD  r|j                  |	|       t        |
j                               }t        dt        |            D ]  }|j                  |	|d	z   ||   z           y )
Nr   rg   execTyper   r1   r/   r   r@   rJ   )ri   r   r   r   myTradesr   r7   r   rS   rT   rV   rl   r   r   )r<   re   rm   ra   rg   dataByExecType	rawTradesr   rt   r   rG   r   r   r@   	numTradesr   r_   s                    r>   handle_my_tradeszbitmex.handle_my_trades  s@   t &&w8w3tZ8OONGR@	""9-== %%dllM4HE259DMq#f+& 	$A1IE8_FMM% #GFO		$
 K	q=NN6;/GLLN#q#d)$ 	@ANN6;#4tAw#>?	@r?   c                 F   K   | j                  |g||       d{   S 7 w)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<   r@   rt   rD   s       r>   watch_order_bookzbitmex.watch_order_book  s%      66xOOOOs   !!c                 p  K   d}|| j                  | j                  dd      }n(|dk(  rd}n |dk(  rd}nt        | j                  dz         | j	                          d{    | j                  |      }g }g }t        d	t        |            D ]M  }||   }| j                  |      }	|d
z   |	d   z   }
|j                  |
       |d
z   |z   }|j                  |       O | j                  d   d   }d|d}| j                  ||| j                  ||      |       d{   }|j                         S 7 7 w)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
        Nr0   r.      orderBookL2_25
   orderBookL10zK watchOrderBookForSymbols limit argument must be None(L2), 25(L2) or 10(L3)r   rJ   rK   r-   r   rN   rO   )ri   r7   r   rK   rB   rR   rS   rT   rU   rV   r6   rW   r9   rt   )r<   rG   rt   rD   r   topicsr]   r_   r@   rU   topicra   r\   rb   	orderbooks                  r>   r   z#bitmex.watch_order_book_for_symbols  sU     =$$T\\3H-XEb[$Eb["E*w wxx!!!%%g.q#g,' 	.AQZF[[(FCK&,.EMM% #+.K  -	. iit$
 --c=$BRBRSZ\bBcekll	  # 	"  ms%   AD6 D2!B<D6D4D64D6c                 t  K   | j                          d{    | j                  |dd      }d}g }g }t        dt        |            D ]M  }||   }	| j	                  |	      }
|dz   |
d   z   }|j                  |       |dz   |	z   }|j                  |       O | j                  d   d   }d	|d
}| j                  ||| j                  ||      |       d{   }| j                  r6| j                  |d      }| j                  |d      }|j                  ||      }| j                  |||dd      S 7 7 ^w)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   rJ   rK   r-   r   rN   rO   r@   r   T)rB   rR   rS   rT   rU   rV   r6   rW   r9   rY   r   ri   r   filter_by_since_limit)r<   rG   rs   rt   rD   r   r   r]   r_   r@   rU   r   ra   r\   rb   r   firsttradeSymbols                     r>   r   zbitmex.watch_trades_for_symbols  sS     !!!%%gtU;q#g,' 	.AQZF[[(FCK&,.EMM% #+.K  -	. iit$
 **3t?O?OPWY_?`bhii??OOFA.E**5(;KOOK7E))&%TRR- 	"" js#   D8D3C D8D6AD86D81mc                   K   | j                          d{    | j                  |      }|d   }d| j                  | j                  ||      z   }|dz   |d   z   }| j                  d   d   }	d|gd	}
| j                  |	|| j                  |
|      |       d{   }| j                  r|j                  ||      }| j                  |||d
d      S 7 7 9w)a  
        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
        Nr@   tradeBinrJ   rK   r-   r   rN   rO   r   T)
rB   rU   ri   
timeframesr6   r   rX   rY   r   r   )r<   r@   	timeframers   rt   rD   rU   r   ra   r\   rb   ohlcvs               r>   watch_ohlcvzbitmex.watch_ohlcv+  s      !!!V$!T--dooy)TTckF4L0iit$
 jjk4;;w3OQ\]]??NN651E))%q$GG 	" ^s"   CCBCC8CCc           
         | j                  |d      }|j                  dd      }| j                  |      }| j                  |      }| j	                  |dg       }i }t        dt        |            D ]C  }	||	   }
| j                  |
d      }| j                  |      }|d   }|dz   |d   z   }| j                  | j                  |
d	            |d
z  z
  d | j                  |
d      | j                  |
d      | j                  |
d      | j                  |
d      g}| j	                  | j                  |i       | j                  |<   | j	                  | j                  |   |      }|:| j                  | j                  dd
      }t        |      }|| j                  |   |<   |j                  |       |||<   F t        |j!                               }t        dt        |            D ]  }	||	   }|j#                  ||   |        y )Nr   r   rg   r   r@   rJ   rK   r   r/   highlowclosevolumer2   )ri   replacefind_timeframeparse_timeframer   rS   rT   r   	parse8601
safe_floatohlcvsr   r7   r   rV   r   r   rl   )r<   re   rm   r   intervalr  durationcandlesresultsr_   candlero   rU   r@   ra   rd   r   rt   r]   s                      r>   handle_ohlcvzbitmex.handle_ohlcvI  s   D   '2==R0''1	''	2//'626q#g,' 	*AQZF''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&!#)GK +	*, W\\^,q#m,- 	>A'*KNN7;/=	>r?   c                    K   | j                          d {    d}| j                  d   d   }| j                  ||       d {   S 7 27 w)N	heartbeatr-   r   )rB   r6   r   )r<   rD   eventr\   s       r>   watch_heartbeatzbitmex.watch_heartbeat  sO     !!!iit$ZZU+++ 	" ,s!   AA	-AAAAc           	      ,   | j                  |d      }| j                  |d      }|y | j                  |dg       }|dk(  r| j                  |di       }| j                  |d      }|y | j                  |      }|d   }	|dk(  r| j	                         | j
                  |	<   nI|dk(  r | j	                  i d	      | j
                  |	<   n$|d
k(  r| j	                  i d      | j
                  |	<   | j
                  |	   }
|	|
d<   t        dt        |            D ]  }| j                  ||   d      }| j                  |	| j                  ||   d            }| j                  ||   d      }| j                  ||   d      }|dk(  rdnd}|
|   }|j                  |||g       | j                  ||   d      }| j                  |      |
d<   ||
d<    |dz   |	z   }|j                  |
|       y i }t        dt        |            D ]  }| j                  ||   d      }| y ||vrd||<   | j                  |d      ||<   | j                  |      }|d   }	| j
                  |	   }
| j                  ||   d      }|dk(  rdn%| j                  |	| j                  ||   dd            }| j                  ||   d      }| j                  ||   d      }|dk(  rdnd}|
|   }|j                  |||g       | j                  ||   d      }| j                  |      |
d<   ||
d<    t        |j!                               }t        dt        |            D ]F  }||   }| j                  |      }|d   }	|dz   |	z   }| j
                  |	   }
|j                  |
|       H y )Nactionr   rg   partialfilterr@   r.   r   r   orderBook10r   r   pricesizerK   sideBuybidsasksr   datetimerJ   r   delete0)ri   r   	safe_dictr   indexed_order_book
orderbooksrS   rT   r  convertFromRawQuantity
storeArrayr  rl   sumsafe_numberr   r   )r<   re   rm   r  r   rg   r  ro   rU   r@   r   r_   r!  r"  rK   r#  booksider'  ra   numUpdatesByMarketIdr   s                        r>   handle_order_bookz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"(Ih1c$i( 
1Q92264;K;KDQRGU[;\]%%d1gt4''Q8"&%-vf$T?##UD"$56++DG[A)-)A	+&(0	*%
1  #+.KNN9k2)+ 1c$i( 1??47H=# $8856(215:NPQ1R$X.))(3) OOF3	((a':#x/qd6Q6QRXZ^ZjZjkopqkrtz|  [A  7B%%d1gt4''Q8"&%-vf$T?##UD"$56++DG[A)-)A	+&(0	*%'1( 16689I1c)n- 7$Q<))(3)#ckF2 OOF3	y+67r?   c                     |S N r<   re   rm   s      r>   handle_system_statuszbitmex.handle_system_status%  s	     r?   c                     |S r5  r6  r7  s      r>   handle_subscription_statusz!bitmex.handle_subscription_status5  s	     r?   c                 F   | j                  |d      }|| j                  |di       }| j                  |dg       }t        |      }|dkD  rW|d   }| j                  d   d   }| j	                  ||      }	d }
|	t        |      }
n ||	   |      }
|j                  |
|       yy)	Nr   rb   rQ   r   r   r4   FT)ri   r   rT   r8   find_broadly_matched_keyr   r   )r<   re   rm   r   rb   rQ   numArgsra   r4   broadKey	exceptions              r>   handle_error_messagezbitmex.handle_error_message?  s    "   '2oogy"=G??7FB7D$iG{"1g-g688F 	# -e 4I /h 6Ii5r?   c                 @   | j                  ||      r| j                  |d      }| j                  | j                  | j                  | j                  | j                  | j
                  | j
                  | j
                  | j
                  | j                  | j                  | j                  | j                  | j                  d}| j                  ||      }|>| j                  |di       }| j                  |d      }|dk(  r| j                  ||       y y  |||       y y )Nr   )r.   r   r   rI   r   
tradeBin1m
tradeBin5m
tradeBin1h
tradeBin1dr   r   r   rz   r   rb   rP   r   )r@  ri   r3  rr   r   r  r   r   r   r   r   r   r   )r<   re   rm   r   methodsmethodrb   rP   s           r>   handle_messagezbitmex.handle_messageb  s   F $$VW5$$Wg6E#55"&"8"8#55"00++"//"//"//"//++!22--#66 11G  __We4F~//'9bA__Wd3))66vwG * vw'3 6r?   )2__name__
__module____qualname__r   r;   r   r   rF   r   r   rC   r   rr   r	   r   r
   rx   rv   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  r3  r8  r:  r@  rH  __classcell__)r=   s   @r>   r   r      sr   -# -^ 68  F  6:" $E7 $E $ELrF rh BFTXac Ss S3 Sc Shlmxhy S IM[_nr{} !bDI !bUX !bhk !b  CG  HS  CT !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 8<$]ajl H HC HWZ Hquvzq{ H<a>6 a>F ,. ,q7 q7f6   !6 !F<(V <(r?   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   !ccxt.async_support.base.ws.clientr   typingr   ccxt.base.errorsr   r   r   async_supportr   r6  r?   r>   <module>rU     sN     A  A  } } } } 4  * 0 .M(T&& M(r?   