
    ei#                        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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dS )    N)
ArrayCacheArrayCacheBySymbolByIdArrayCacheBySymbolBySideArrayCacheByTimestamp)AnyBalancesBool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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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de/fd&Z0defd'Z1 xZ2S )(bitmexreturnc           
         |                      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    M/var/www/html/volatility/venv/lib/python3.11/site-packages/ccxt/pro/bitmex.pyr<   zbitmex.describe   s`   fd 3 3 < < > >d $T .t	
 &t 0   d !$ +D t   t  t  (!( @ 8	  (5#"   ./@ G,A
 ,A
 , , ,	    symbolc                    K   |                                   d{V  |                     |          }|                     |g|           d{V }||         S )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_marketsrA   watch_tickers)r=   rA   paramstickerss       r?   watch_tickerzbitmex.watch_tickerB   sp       !!!!!!!!!V$$**F8V<<<<<<<<vr@   Nsymbolsc                   K   |                                   d{V  |                     |dd          }d}| j        d         d         }g }g }|{t          dt	          |                    D ]\}||         }|                     |          }	|dz   |	d         z   }
|                    |
           d	|z   }|                    |           ]n*|                    |           |                    d
           d|d}|                     |||                     ||          |           d{V }| j	        ri }|||d         <   |S | 
                    | j        d|          S )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argsrA   )rC   market_symbolsr7   rangelenmarketappendwatch_multipleextend
newUpdatesfilter_by_arrayrF   )r=   rH   rE   nameurlmessageHashesrawSubscriptionsirA   rV   subscriptionmessageHashrequesttickerresults                  r?   rD   zbitmex.watch_tickersQ   s      !!!!!!!!!%%gtT::it$1c'll++ 2 2 V,,#czF4L8 ''555'&0$$[11112 ##D)))  ...$
 
 **3t{{7TZ?[?[]mnnnnnnnn? 	F'-F6(#$M##DL(GDDDr@   clientc                    |                      |dg           }i }t          dt          |                    D ]}||         }|                     |d          }|                     |          }|| j        vr|                     i           | j        |<   |                     |          }	|                     | j        |         |	          }
|
||<   |
| j        |<   d|z   }|                    |
|           |                    |
d           |S )Ndatar   rA   rM   rN   )		safe_listrT   rU   safe_stringsafe_symbolrF   parse_tickerr:   resolve)r=   rf   messagerh   rF   r`   updatemarketIdrA   updatedTickerfullParsedTickerrb   s               r?   handle_tickerzbitmex.handle_tickerw   s   F ~~gvr22q#d))$$ 	; 	;A!WF''99H%%h//Fdl**'+'8'8'<'<V$ --f55M#//V0DmTT.GFO#3DL #f,KNN+[999NN+\::::r@   sincelimitc                 8   K   |                      |g|||          S )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=   rA   rt   ru   rE   s        r?   watch_liquidationszbitmex.watch_liquidationsk  s$       22F8UE6RRRr@   c                   K   |                                   d{V  |                     |ddd          }g }g }|                     |          r+|                    d           |                    d           nst	          dt          |                    D ]U}||         }|                     |          }	|                    d|	d         z              |                    d|z              V| j        d	         d
         }
d|d}|                     |
|| 	                    ||          |           d{V }| j
        r|S |                     | j        |||d          S )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:rL   liquidations::r.   r   rO   rP   )rC   rS   is_emptyrW   rT   rU   rV   r7   rX   r:   rZ   filter_by_symbols_since_limitr|   )r=   rH   rt   ru   rE   r^   subscriptionHashesr`   rA   rV   r]   rc   newLiquidationss                r?   rw   z%bitmex.watch_liquidations_for_symbolsy  s      !!!!!!!!!%%gtT4@@==!! 	@%%m444  00001c'll++ @ @ V,,")).6$<*GHHH$$%5%>????it$&
 
 !% 3 3CHXHXY`bhHiHik} ~ ~~~~~~~? 	#""11$2CWeUZ\`aaar@   c                    |                      |dg           }g }| j        0|                     | j        dd          }t	          |          | _        | j        }t          dt          |                    D ]I}||         }|                     |          }	|                    |	           |                    |	           J|	                    |d           | 
                    |d          }
t          |
                                          }t          dt          |                    D ])}||         }|	                    |
|         d|z              *d S )Nrh   liquidationsLimitr0   r   r|   rA   r}   )
safe_valuer|   safe_integerr8   r   rT   rU   parse_liquidationrW   rm   index_bylistkeys)r=   rf   rn   rawLiquidationsr   ru   cacher`   rawLiquidationr{   liquidationsBySymbolrH   rA   s                r?   handle_liquidationzbitmex.handle_liquidation  s[   6 //'62>>$%%dl4GNNE *5 1 1D!q#o..// 	0 	0A,Q/N00@@KLL%%%"";////777#}}_hGG+002233q#g,,'' 	T 	TAQZFNN/79IF9RSSSS	T 	Tr@   c                   K   |                                   d{V  |                                  d{V  d}| j        d         d         }d|gd}|                     |||                     ||          |           d{V S )aT  
        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   rO   rP   )rC   authenticater7   watchrY   )r=   rE   rb   r]   rc   s        r?   watch_balancezbitmex.watch_balance  s       !!!!!!!!!!!!!!!!!!it$
 
 ZZ[$++gv2N2NP[\\\\\\\\\r@   c                     |                      |d          }|                     |          }|                     | j        |          | _        |                     |d          }|                    | j        |           d S )Nrh   table)r   parse_balancerY   balancerj   rm   )r=   rf   rn   rh   r   rb   s         r?   handle_balancezbitmex.handle_balance  ss    F w//$$T**{{4<99&&w88t|[11111r@   c                    d}|                      |dg           }|                     |d          }t          |                                          }t	          dt          |                    D ]}||         }|                     |          }	|	d         }
|dz   |
z   }|                     ||         |	          }|                      | j        |
          }|5| 	                    | j
        dd          }t          |          }|| j        |
<   t	          dt          |                    D ]}|                    ||                    |                    ||           d S )Ntraderh   rA   r   rK   r2   r0   )r   group_byr   r   rT   rU   safe_marketparse_tradestradesr   r8   r   rW   rm   )r=   rf   rn   r   rh   dataByMarketIds	marketIdsr`   rp   rV   rA   rb   r   storedru   js                   r?   handle_tradeszbitmex.handle_tradesG  s]   x w33--h77--//00	q#i..)) 	0 	0A |H%%h//FH%F#+.K&&x'@&IIF__T[&99F~))$,tLL#E**&,F#1c&kk** ) )fQi((((NN6;////	0 	0r@   c                 D   K   |                      |g|||           d{V S )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_symbolsrx   s        r?   watch_tradeszbitmex.watch_trades  s6       22F8UE6RRRRRRRRRr@   c                 P  K   | j         d         d         }|                     |          }d}|                    |          }|                     |j        |          }||                                  |                                 }dt          |          z   }|                     | 	                    |          | 	                    | j
                  t          j                  }	d| j        ||	gd}
|                     |
|          }|                     ||||           | d {V S )Nr.   r   authenticatedzGET/realtimeauthKeyExpiresrP   )r7   rf   reusableFuturer   subscriptionscheck_required_credentialsmillisecondsstrhmacencodesecrethashlibsha256apiKeyrY   r   )r=   rE   r]   rf   rb   futurer   	timestamppayload	signaturerc   rn   s               r?   r   zbitmex.authenticate  s      it$S!!%&&{33(<kJJ ++---))++I)C	NN:G		$++g"6"6DK8P8PRYR`aaI&K G kk'622GJJsK+>>>||||||r@   c                 6   |                      |dd          }d}|r2|                     |j        |          }|                    d           d S t	          |                     |                    }|                    ||           ||j        v r
|j        |= d S d S )NsuccessFr   T)	safe_boolr   futuresrm   r   jsonrejectr   )r=   rf   rn   r   rb   r   errors          r?   handle_authentication_messagez$bitmex.handle_authentication_message  s    w	5AA% 	6__V^[AAFNN4     '		'(:(:;;EMM%---f222(555 32r@   c                   K   |                                   d{V  |                                  d{V  d}d}|                     |          sdd                    |          z   }| j        d         d         }d|gd	}|                     ||||           d{V }	| j        r|	S |                     | j        |||d
          S )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   rO   rP   T)	rC   r   r~   joinr7   r   rZ   r   r   )
r=   rH   rt   ru   rE   subscriptionHashrb   r]   rc   newPositionss
             r?   watch_positionszbitmex.watch_positions  s	      !!!!!!!!!!!!!!!!!!%!}}W%% 	3'!2!22Kit$ 
 
 "ZZ['CSTTTTTTTT? 	 11$.'5RWY]^^^r@   c                    | j         t                      | _         | j         }|                     |dg           }g }t          dt	          |                    D ]I}||         }|                     |          }|                    |           |                    |           J|                     |d          }	t          dt	          |	                    D ]}|	|         }
|
                    d          }|d         }|                    d          }| 	                    |d|d          }| 
                    |          s|                    ||
           |                    |d	           d S )
Nrh   r   zpositions::r      r   rA   Fr   )r   r   r   rT   rU   parse_positionrW   find_message_hashessplitr[   r~   rm   )r=   rf   rn   r   rawPositionsr   r`   rawPositionr   r^   rb   partssymbolsStringrH   r   s                  r?   handle_positionszbitmex.handle_positions  sl   j >!577DNw;;q#l++,, 	# 	#A&q/K**;77H)))LL""""00GGq#m,,-- 	7 	7A'*K%%d++E!!HM#))#..G,,\8WeTTI==++ 7y+666|[11111r@   c                   K   |                                   d{V  |                                  d{V  d}|}|}||                     |          }|d|z   z  }| j        d         d         }d|gd}	|                     |||	|           d{V }
| j        r|
                    ||          }|                     |
|||d          S )	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orderrK   r.   r   rO   rP   TrC   r   rA   r7   r   rZ   getLimitfilter_by_symbol_since_limit)r=   rA   rt   ru   rE   r\   r   rb   r]   rc   orderss              r?   watch_orderszbitmex.watch_orders  s      !!!!!!!!!!!!!!!!!![[((F3<'Kit$ 
 
 zz#{G=MNNNNNNNN? 	3OOFE22E00tTTTr@   c                 8   |                      |dg           }d}t          |          }|dk    rh| j        0|                     | j        dd          }t          |          | _        | j        }i }t          d|          D ]}	||	         }
|                     |
d          }|                      |j        |          }|
}|| 	                    |d         |
          }| 
                    |          }|                    |           |d         }d	||<   |                    | j        |           t          |                                          }t          dt          |                    D ]-}	||	         }|                    | j        |d
z   |z              ,d S d S )Nrh   r   r   ordersLimitr0   orderIDinforA   TrK   )r   rU   r   r   r8   r   rT   rj   hashmaprY   parse_orderrW   rm   r   r   )r=   rf   rn   rh   rb   
dataLengthru   r   rH   r`   currentOrderorderIdpreviousOrderrawOrderr   rA   r   s                    r?   handle_orderszbitmex.handle_orders  s   l w33YY
>>{"))$,tLL4U;;[FG1j)) 
' 
'#Aw**<CC $ H H' ,#{{=+@,OOH((22e$$$x"&NN4;444''D1c$ii(( H Hat{K#,=,FGGGG+ >&H Hr@   c                   K   |                                   d{V  |                                  d{V  d}|}|}||                     |          }|d|z   z  }| j        d         d         }d|gd}	|                     |||	|           d{V }
| j        r|
                    ||          }|                     |
|||d          S )	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	executionrK   r.   r   rO   rP   Tr   )r=   rA   rt   ru   rE   r\   r   rb   r]   rc   r   s              r?   watch_my_tradeszbitmex.watch_my_tradesa  s      !!!!!!!!!!!!!!!!!![[((F3<'Kit$ 
 
 zz#{G=MNNNNNNNN? 	3OOFE22E00tTTTr@   c                    |                      |d          }|                     |dg           }|                     |d          }|                     |dg           }|                     |          }| j        0|                     | j        dd          }t          |          | _        | j        }	i }
t          dt          |                    D ],}||         }|d         }|	
                    |           ||
|<   -t          |          }|dk    r|                    |	|           t          |
                                          }t          dt          |                    D ]$}|                    |	|d	z   ||         z              %d S )
Nr   rh   execTyper   r2   r0   r   rA   rK   )rj   r   r   r   myTradesr   r8   r   rT   rU   rW   rm   r   r   )r=   rf   rn   rb   rh   dataByExecType	rawTradesr   ru   r   rH   r   r   rA   	numTradesr   r`   s                    r?   handle_my_tradeszbitmex.handle_my_trades  s   t &&w88w33tZ88OONGR@@	""9--= %%dlM4HHE2599DMq#f++&& 	$ 	$A1IE8_FMM%   #GFOOKK	q==NN6;///GLLNN##q#d))$$ 	@ 	@ANN6;#4tAw#>????	@ 	@r@   c                 B   K   |                      |g||           d{V S )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=   rA   ru   rE   s       r?   watch_order_bookzbitmex.watch_order_book  s4       66xOOOOOOOOOr@   c                   K   d}||                      | j        dd          }n)|dk    rd}n |dk    rd}nt          | j        dz             |                                  d{V  |                     |          }g }g }t          d	t          |                    D ]_}||         }|                     |          }	|d
z   |	d         z   }
|	                    |
           |d
z   |z   }|	                    |           `| j
        d         d         }d|d}|                     |||                     ||          |           d{V }|                                S )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
        Nr1   r/      orderBookL2_25
   orderBookL10zK watchOrderBookForSymbols limit argument must be None(L2), 25(L2) or 10(L3)r   rK   rL   r.   r   rO   rP   )rj   r8   r   rL   rC   rS   rT   rU   rV   rW   r7   rX   r:   ru   )r=   rH   ru   rE   r   topicsr^   r`   rA   rV   topicrb   r]   rc   	orderbooks                  r?   r   z#bitmex.watch_order_book_for_symbols  s      =$$T\3H-XXEEb[[$EEb[["EE*w wxxx!!!!!!!!!%%g..q#g,,'' 	. 	.AQZF[[((FCK&,.EMM%   #+.K  ----it$
 
 --c=$BRBRSZ\bBcBcekllllllll	   r@   c                   K   |                                   d{V  |                     |dd          }d}g }g }t          dt          |                    D ]_}||         }	|                     |	          }
|dz   |
d         z   }|                    |           |dz   |	z   }|                    |           `| j        d         d         }d	|d
}|                     |||                     ||          |           d{V }| j	        rB| 
                    |d          }|                     |d          }|                    ||          }|                     |||dd          S )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   rK   rL   r.   r   rO   rP   rA   r   T)rC   rS   rT   rU   rV   rW   r7   rX   r:   rZ   r   rj   r   filter_by_since_limit)r=   rH   rt   ru   rE   r   r   r^   r`   rA   rV   r   rb   r]   rc   r   firsttradeSymbols                     r?   r   zbitmex.watch_trades_for_symbols  s      !!!!!!!!!%%gtU;;q#g,,'' 	. 	.AQZF[[((FCK&,.EMM%   #+.K  ----it$
 
 **3t?O?OPWY_?`?`bhiiiiiiii? 	8OOFA..E**5(;;KOOK77E))&%TRRRr@   1m	timeframec                   K   |                                   d{V  |                     |          }|d         }d|                     | j        ||          z   }|dz   |d         z   }| j        d         d         }	d|gd	}
|                     |	||                     |
|          |           d{V }| j        r|                    ||          }| 	                    |||d
d          S )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
        NrA   tradeBinrK   rL   r.   r   rO   rP   r   T)
rC   rV   rj   
timeframesr7   r   rY   rZ   r   r   )r=   rA   r  rt   ru   rE   rV   r   rb   r]   rc   ohlcvs               r?   watch_ohlcvzbitmex.watch_ohlcv)  s      !!!!!!!!!V$$!T--doy)TTTckF4L0it$
 
 jjk4;;w3O3OQ\]]]]]]]]? 	2NN6511E))%q$GGGr@   c           
         |                      |d          }|                    dd          }|                     |          }|                     |          }|                     |dg           }i }t          dt          |                    D ]m}	||	         }
|                      |
d          }|                     |          }|d         }|dz   |d         z   }|                     |                      |
d	                    |d
z  z
  d | 	                    |
d          | 	                    |
d          | 	                    |
d          | 	                    |
d          g}|                     | j
        |i           | j
        |<   |                     | j
        |         |          }|;|                     | j        dd
          }t          |          }|| j
        |         |<   |                    |           |||<   ot          |                                          }t          dt          |                    D ]&}	||	         }|                    ||         |           'd S )Nr   r   rh   r   rA   rK   rL   r   r0   highlowclosevolumer3   )rj   replacefind_timeframeparse_timeframer   rT   rU   r   	parse8601
safe_floatohlcvsr   r8   r   rW   r   r   rm   )r=   rf   rn   r   intervalr  durationcandlesresultsr`   candlerp   rV   rA   rb   re   r   ru   r^   s                      r?   handle_ohlcvzbitmex.handle_ohlcvG  sK   D   '22==R00''11	''	22//'6266q#g,,'' 	* 	*AQZF''99H%%h//FH%F#+t4Kt//DDEESWW//..0011F #'//$+vr"J"JDK__T[%8)DDF~))$,dKK.u5517F#I.MM&!!!#)GK  W\\^^,,q#m,,-- 	> 	>A'*KNN7;/====	> 	>r@   c                    K   |                                   d {V  d}| j        d         d         }|                     ||           d {V S )N	heartbeatr.   r   )rC   r7   r   )r=   rE   eventr]   s       r?   watch_heartbeatzbitmex.watch_heartbeat  sf      !!!!!!!!!it$ZZU+++++++++r@   c           	         |                      |d          }|                      |d          }|d S |                     |dg           }|dk    r|                     |di           }|                     |d          }|d S |                     |          }|d         }	|dk    r|                                 | j        |	<   nI|dk    r|                     i d	          | j        |	<   n$|d
k    r|                     i d          | j        |	<   | j        |	         }
|	|
d<   t          dt          |                    D ]}|                     ||         d          }| 	                    |	|                      ||         d                    }|                      ||         d          }|                      ||         d          }|dk    rdnd}|
|         }|
                    |||g           |                      ||         d          }|                     |          |
d<   ||
d<   |dz   |	z   }|                    |
|           d S i }t          dt          |                    D ]`}|                     ||         d          }| d S ||vrd||<   |                     |d          ||<   |                     |          }|d         }	| j        |	         }
|                     ||         d          }|dk    rdn0| 	                    |	|                      ||         dd                    }|                      ||         d          }|                      ||         d          }|dk    rdnd}|
|         }|
                    |||g           |                      ||         d          }|                     |          |
d<   ||
d<   bt          |                                          }t          dt          |                    D ]R}||         }|                     |          }|d         }	|dz   |	z   }| j        |	         }
|                    |
|           Sd S )Nactionr   rh   partialfilterrA   r/   r   r   orderBook10r   r   pricesizerL   sideBuybidsasksr   datetimerK   r   delete0)rj   r   	safe_dictr   indexed_order_book
orderbooksrT   rU   r  convertFromRawQuantity
storeArrayr  rm   sumsafe_numberr   r   )r=   rf   rn   r  r   rh   r   rp   rV   rA   r   r`   r"  r#  rL   r$  booksider(  rb   numUpdatesByMarketIdr   s                        r?   handle_order_bookzbitmex.handle_order_book  s   d !!'844  '22=Fw33Y^^GXr::Fvx88H%%h//FH%F%%*.*A*A*C*C''****.*A*A"b*I*I''-''*.*A*A"b*I*I'/I"(Ih1c$ii(( 
1 
1Q992264;K;KDQRGU[;\;\]]%%d1gt44''Q88"&%--vvf$T?##UD"$5666++DG[AA)-)A)A	+&(0	*%%#+.KNN9k22222)+ 1c$ii(( 1 1??47H==#FF $88856(215:NPQ1R1R$X.))(33) OF3	((a'::#x//qqd6Q6QRXZ^ZjZjkopqkrtz|  [A  [A  7B  7B%%d1gt44''Q88"&%--vvf$T?##UD"$5666++DG[AA)-)A)A	+&(0	*%%1668899I1c)nn-- 7 7$Q<))(33)#ckF2 OF3	y+66667 7r@   c                     |S N r=   rf   rn   s      r?   handle_system_statuszbitmex.handle_system_status#  s	     r@   c                     |S r6  r7  r8  s      r?   handle_subscription_statusz!bitmex.handle_subscription_status3  s	     r@   c                    |                      |d          }||                     |di           }|                     |dg           }t          |          }|dk    rn|d         }| j        d         d         }|                     ||          }	d }
|	t          |          }
n ||	         |          }
|                    |
|           dS dS )	Nr   rc   rR   r   r   r5   FT)rj   r   rU   r9   find_broadly_matched_keyr   r   )r=   rf   rn   r   rc   rR   numArgsrb   r5   broadKey	exceptions              r?   handle_error_messagezbitmex.handle_error_message=  s    "   '22oogy"==G??7FB77D$iiG{{"1g-g688FF 	# -e 4 4II /h 6 6Ii555utr@   c                    |                      ||          r|                     |d          }| j        | j        | j        | j        | j        | j        | j        | j        | j        | j        | j        | j        | j	        | j
        d}|                     ||          }|M|                     |di           }|                     |d          }|dk    r|                     ||           d S d S  |||           d S d S )Nr   )r/   r   r!  rJ   r   
tradeBin1m
tradeBin5m
tradeBin1h
tradeBin1dr   r   r   r{   r   rc   rQ   r   )rA  rj   r4  rs   r   r  r   r   r   r   r   r   r   )r=   rf   rn   r   methodsmethodrc   rQ   s           r?   handle_messagezbitmex.handle_message`  s(   F $$VW55 	($$Wg66E#5"&"8#5"0+"/"/"/"/+!2-#6 1 G  __We44F~//'9bAA__Wd33)))66vwGGGGG *) vw'''''3	( 	(r@   )3__name__
__module____qualname__r   r<   r   r   rG   r   r   rD   r   rs   r
   r   r   ry   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  r4  r9  r;  r	   rA  rI  __classcell__)r>   s   @r?   r   r      s       -# - - - - - -^ 68   F     6:" $E $E7 $E $E $E $E $ELrF r r r rh BFTXac S Ss S3 Sc Shlmxhy S S S S UYgktv !b !bDI !bc !bad !b{  AL  |M !b !b !b !bF+T +T +T +T +TZ *, ] ] ] ] ] ]*g2V g2 g2 g2 g2RM0F M0 M0 M0 M0^ <@d[] S S SS S Sbfglbm S S S S )+    .6F 6 6 6 6 8<$]ajl _ _W _C _WZ _quv~q _ _ _ _<h2 h2 h2T 04$UYbd U U UC Us Uimnsit U U U U@oHF oH oH oH oHb 37TX\eg U UC Us URU Ulpqvlw U U U U@N@v N@ N@ N@ N@` @DB P PS P PS\ P P P P SW_a %! %!$s) %!C %!fo %! %! %! %!N OSaenp "S "Sd3i "S "S[^ "Suyz  vA "S "S "S "SH ?CQUdhqs H H H H3 H^a Hx|  ~B  yC H H H H<a>6 a> a> a> a>F ,. , , , ,q7 q7 q7 q7 q7f6         !6 !t ! ! ! !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   r   !ccxt.async_support.base.ws.clientr   typingr   ccxt.base.errorsr   r   r   async_supportr   r7  r@   r?   <module>rV     s       A  A  A  A  A  A  A  A  A  A  A  A  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 4 4 4 4 4 4       * * * * * * 0 0 0 0 0 0 . . . . . .K( K( K( K( K(T& K( K( K( K( K(r@   