
    ei                          d dl Zd dlmZmZmZm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  G d dej        j                  ZdS )	    N)
ArrayCacheArrayCacheBySymbolByIdArrayCacheBySymbolBySideArrayCacheByTimestamp)AnyBalancesIntMarketOrder	OrderBookPositionStrStringsTickerTickersTrade)Client)List)NotSupportedc                   d    e Zd Zdef fdZdefdZd Zd Zddi fde	d	e	d
e	de
dee	         f
dZddi i fde	de	d	e	d
e	de	de
dee	         defdZi fde	defdZi fde	defdZdi fdedefdZdi fdedefdZdddi fde	de	dededee         f
dZdi fde	de	dee         fdZddi fde	dededee         fdZi fde	dee         fdZdi fde	dedefdZi fde	defdZdddi fd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"i f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& Z)d' Z*d$e'd(e+fd)Z,d$e'd(e+fd*Z-d$e'd(e+fd+Z.d$e'd(e+fd,Z/d$e'd(e+fd-Z0d;d.e+de
fd/Z1d;d0e+de
fd1Z2d$e'd(e+fd2Z3d$e'd(e+fd3Z4d$e'd(e+fd4Z5d$e'fd5Z6d$e'fd6Z7d7 Z8d$e'd8e+fd9Z9d$e'd(e+fd:Z: xZ;S )<xt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dddiidddddiddidddddd| j        dd d           S )!NwsT
watchOHLCVunWatchOHLCVwatchOrderBookunWatchOrderBookwatchTickerunWatchTickerwatchTickersunWatchTickerswatchTradesunWatchTradeswatchTradesForSymbolsFwatchBalancewatchOrderswatchMyTradeswatchPositionsapizwss://stream.xt.comzwss://fstream.xt.com/ws)spotcontract  methodtickertickersswap)typefetchPositionsSnapshotawaitPositionsSnapshot)tradesLimitordersLimit
OHLCVLimitr   r!   r)   i N  )	keepAliveping)hasurlsoptions	streamingtoken)deep_extendsuperr   describer9   )self	__class__s    I/var/www/html/volatility/venv/lib/python3.11/site-packages/ccxt/pro/xt.pyrA   zxt.describe   sx   b$ 8 8 : :dd  !$	
 #D t    !$ t   (  t    !$!&  5$=   $#"h  i! #.2.2# # " #	  _0=
 0=
 0 0 0	    
isContractc                 
  K   |                                   |rdnd}| j        d         d         |         }|s|dz   }|                     |          }|                     |j        d          }||r9|                                  d{V }|                     |d          |j        d<   nN|                                  d{V }|                     |d          }|                     |d	          |j        d<   |j        d         S )
a  
 @ignore
        required for private endpoints
        :param str isContract: True for contract trades

        https://doc.xt.com/#websocket_privategetToken
        https://doc.xt.com/#futures_user_websocket_v2base

        :returns str: listen key / access token
        r,   r+   r*   r   z/privater>   NresultaccessToken)check_required_credentialsr;   clientsafe_stringsubscriptions)privateLinearGetFutureUserV1UserListenKeyprivateSpotPostWsToken	safe_dict)rB   rF   	tradeTypeurlrK   r>   responserH   s           rD   get_listen_keyzxt.get_listen_keyC   s/      	'')))",8JJ&	it$Y/ 	#
"CS!!  !5w??= X!%!O!O!Q!QQQQQQQ 150@0@80T0T$W--!%!<!<!>!>>>>>>> (;;040@0@0W0W$W-#G,,rE   c                 N   |                      |d          }|                     |d          }|                     |dd          }||dz
  k     rdS t          dt	          |                    D ]+}||         }|                     |dd          }||k    r|c S ,t	          |          S )Nnoncer   iu   )safe_integer
safe_valuesafe_integer_2rangelen)	rB   	orderbookcacherV   
firstDeltafirstDeltaNoncerW   delta
deltaNonces	            rD   get_cache_indexzxt.get_cache_indexr   s    !!)W55__UA..
--j#sCC?Q&&&2q#e**%% 	 	A!HE,,UC==JU"" #5zzrE   c                 ^   |                      |dd          |d<   |                     |dg           }|                     |dg           }|d         }|d         }t          dt          |                    D ]L}||         }|                     |d          }	|                     |d	          }
|                    |	|
           Mt          dt          |                    D ]L}||         }|                     |d          }	|                     |d	          }
|                    |	|
           Md S )
NrW   rX   rV   abbidsasksr   rY   )r]   	safe_listr^   r_   safe_numberstore)rB   r`   rd   obAsksobBidsrj   rk   rW   bidpricequantityasks               rD   handle_deltazxt.handle_delta   s8   !00SAA	'sB//sB//  q#f++&& 	( 	(A)C$$S!,,E''Q//HJJuh''''q#f++&& 	( 	(A)C$$S!,,E''Q//HJJuh''''		( 	(rE   Nnameaccess
methodNamemarketsymbolsc                   K   |dk    }d}|                      |||          \  }}|dk    }	|                     |                                           |z   }
|	rdnd|
d}|rQ|	s%|g|d<   |                     |	           d{V |d<   n0|                     |	           d{V }|d	z   |z   }|g|d<   n|g|d<   |	rd
nd}|dz   |z   }||dz   d                    |          z   }|                     ||          }|}|	r|rdnd}d|
i}| j        d         d         |         dz   |z   }|                     |||||           d{V S )a)  
 @ignore
        Connects to a websocket channel

        https://doc.xt.com/#websocket_privaterequestFormat
        https://doc.xt.com/#futures_market_websocket_v2base

        :param str name: name of the channel
        :param str access: public or private
        :param str methodName: the name of the CCXT class method
        :param dict [market]: CCXT market
        :param str[] [symbols]: unified market symbols
        :param dict params: extra parameters specific to the xt api
        :returns dict: data from the websocket stream
        privateNr+   	SUBSCRIBE	subscriber.   idparams	listenKey@r,   ::,userry   r   r*   r   /)handle_market_type_and_paramsnumber_to_stringmillisecondsrT   joinextendr;   watch)rB   rv   rw   rx   ry   rz   r   privateAccessr2   rF   r   r~   r   paramrQ   messageHashrequesttailsubscriptionrR   s                       rD   r~   zxt.subscribe   s       )+99*ffUUffn
""4#4#4#6#677$>%/@kk[
 
	  		) .'+f	(#/3/B/B:/N/N)N)N)N)N)N)N	+&&"&"5"5j"A"AAAAAAA	s
Y.',g	(###'&Ih",8JJ&	TkI-%,sxx/@/@@K++i00 	9*866D"
 it$Y/#5<ZZ[';UUUUUUUUUrE   r   topicc
           
        K   |dk    }
d}|                      |||          \  }}|dk    }|                     |                                           |z   }|rdnd|d}|
rQ|s%|g|d<   |                     |           d{V |d<   n0|                     |           d{V }|d	z   |z   }|g|d<   n|g|d<   |rd
nd}|dz   |z   }|                     ||          }|}|r|
rdnd}| j        d         d         |         dz   |z   }d||g|g||d}|                     |	d          }|||d<   |                     |	d          }	|                     |||                     ||          ||                     ||	                     d{V S )a  
 @ignore
        Connects to a websocket channel

        https://doc.xt.com/#websocket_privaterequestFormat
        https://doc.xt.com/#futures_market_websocket_v2base

        :param str messageHash: the message hash of the subscription
        :param str name: name of the channel
        :param str access: public or private
        :param str methodName: the name of the CCXT class method
        :param str topic: topic of the subscription
        :param dict [market]: CCXT market
        :param str[] [symbols]: unified market symbols
        :param dict params: extra parameters specific to the xt api
        :param dict subscriptionParams: extra parameters specific to the subscription
        :returns dict: data from the websocket stream
        r|   Nr+   UNSUBSCRIBEunsubscriber   r   r   r   r,   r   r   ry   r*   r   r   T)r   r   subMessageHashesmessageHashesrz   r   symbolsAndTimeframes)	r   r   r   rT   r   r;   rl   omitr   )rB   r   rv   rw   rx   r   ry   rz   r   subscriptionParamsr   r2   rF   r   r   r   r   rQ   subMessageHashr   r   rR   r   r   s                           rD   un_subscribezxt.un_subscribe   sT     & )+99*ffUUffn
""4#4#4#6#677$>'1Dmm}
 
  		+ 0)-H%151D1DZ1P1P+P+P+P+P+P+PK(("&"5"5j"A"AAAAAAA	s
Y.).H%%%)FK!",8JJ&	y0++k622 	9*866Dit$Y/#5<!/ 0)]
 
  $~~.@BXYY+3GL/0!%+=?U!V!VZZ[$++gv2N2NP[]a]h]hiu  xJ  ^K  ^K  L  L  L  L  L  L  L  L  	LrE   symbolc                 R  K   |                                   d{V  |                     |          }|                     | j        d          }|                     |dd          }|                     |d|          }|dz   |d         z   }|                     |dd|d|           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://doc.xt.com/#websocket_publictickerRealTime
        https://doc.xt.com/#futures_market_websocket_v2tickerRealTime
        https://doc.xt.com/#futures_market_websocket_v2aggTickerRealTime

        :param str symbol: unified symbol of the market to fetch the ticker for
        :param dict params: extra parameters specific to the xt api endpoint
        :param str [params.method]: 'agg_ticker'(contract only) or 'ticker', default = 'ticker' - the endpoint that will be streamed
        :returns dict: a `ticker structure <https://docs.ccxt.com/en/latest/manual.html#ticker-structure>`
        Nr   r.   r/   r   r   public)load_marketsry   rP   r<   rL   r~   )rB   r   r   ry   r<   defaultMethodr.   rv   s           rD   watch_tickerzxt.watch_ticker   s       !!!!!!!!!V$$..}==(((HEE!!&(MBB|fTl*^^D(M64QWXXXXXXXXXrE   c           
      `  K   |                                   d{V  |                     |          }|                     | j        d          }|                     |dd          }|                     |d|          }|dz   |d         z   }d|z   }|                     ||dd||d|           d{V S )	a  
        stops watching a price ticker, a statistical calculation with the information calculated over the past 24 hours for a specific market

        https://doc.xt.com/#websocket_publictickerRealTime
        https://doc.xt.com/#futures_market_websocket_v2tickerRealTime
        https://doc.xt.com/#futures_market_websocket_v2aggTickerRealTime

        :param str symbol: unified symbol of the market to fetch the ticker for
        :param dict params: extra parameters specific to the xt api endpoint
        :param str [params.method]: 'agg_ticker'(contract only) or 'ticker', default = 'ticker' - the endpoint that will be streamed
        :returns dict: a `ticker structure <https://docs.ccxt.com/en/latest/manual.html#ticker-structure>`
        Nr    r.   r/   r   r   unsubscribe::r   )r   ry   rP   r<   rL   r   )	rB   r   r   ry   r<   r   r.   rv   r   s	            rD   un_watch_tickerzxt.un_watch_ticker  s       !!!!!!!!!V$$..??(((HEE!!&(MBB|fTl*%,&&{D(OUbdjlprxyyyyyyyyyrE   c                   K   |                                   d{V  |                     | j        d          }|                     |dd          }|                     |d|          }d}||                     |d                   }|                     |dd|||           d{V }| j        r|S |                     | j        d|          S )a  
        watches a price ticker, a statistical calculation with the information calculated over the past 24 hours for a specific market

        https://doc.xt.com/#websocket_publicallTicker
        https://doc.xt.com/#futures_market_websocket_v2allTicker
        https://doc.xt.com/#futures_market_websocket_v2allAggTicker

        :param str [symbols]: unified market symbols
        :param dict params: extra parameters specific to the xt api endpoint
        :param str [params.method]: 'agg_tickers'(contract only) or 'tickers', default = 'tickers' - the endpoint that will be streamed
        :returns dict: a `ticker structure <https://docs.ccxt.com/en/latest/manual.html#ticker-structure>`
        Nr!   r.   r0   r   r   r   )	r   rP   r<   rL   ry   r~   
newUpdatesfilter_by_arrayr0   )rB   rz   r   r<   r   rv   ry   r0   s           rD   watch_tickerszxt.watch_tickers*  s       !!!!!!!!!..~>>(((IFF-@@[[,,FtX~vwX^________? 	N##DL(GDDDrE   c           
        K   |                                   d{V  |                     | j        d          }|                     |dd          }|                     |d|          }|t	          | j        dz             d|z   }|                     ||dddd||           d{V }| j        r|S |                     | j	        d	|          S )
a  
        stops watching a price ticker, a statistical calculation with the information calculated over the past 24 hours for a specific market

        https://doc.xt.com/#websocket_publicallTicker
        https://doc.xt.com/#futures_market_websocket_v2allTicker
        https://doc.xt.com/#futures_market_websocket_v2allAggTicker

        :param str [symbols]: unified market symbols
        :param dict params: extra parameters specific to the xt api endpoint
        :param str [params.method]: 'agg_tickers'(contract only) or 'tickers', default = 'tickers' - the endpoint that will be streamed
        :returns dict: a `ticker structure <https://docs.ccxt.com/en/latest/manual.html#ticker-structure>`
        Nr"   r.   r0   zc unWatchTickers() does not support symbols argument, unsubscribtion is for all tickers at once onlyr   r   r/   r   )
r   rP   r<   rL   r   r   r   r   r   r0   )rB   rz   r   r<   r   rv   r   r0   s           rD   un_watch_tickerszxt.un_watch_tickersC  s      !!!!!!!!!../?@@(((IFF-@@tw  *O   O  P  P  P%,))+tXGWYacgiprxyyyyyyyy? 	N##DL(GDDDrE   1m	timeframesincelimitc                 2  K   |                                   d{V  |                     |          }d|d         z   dz   |z   }|                     |dd|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://doc.xt.com/#websocket_publicsymbolKline
        https://doc.xt.com/#futures_market_websocket_v2symbolKline

        :param str symbol: unified symbol of the market to fetch OHLCV data for
        :param str timeframe: 1m, 3m, 5m, 15m, 30m, 1h, 2h, 4h, 6h, 8h, 12h, 1d, 3d, 1w, or 1M
        :param int [since]: not used by xt watchOHLCV
        :param int [limit]: not used by xt watchOHLCV
        :param dict params: extra parameters specific to the xt api endpoint
        :returns int[][]: A list of candles ordered, open, high, low, close, volume
        Nkline@r   r   r   r   r   Tr   ry   r~   r   getLimitfilter_by_since_limit)	rB   r   r   r   r   r   ry   rv   ohlcvs	            rD   watch_ohlcvzxt.watch_ohlcv\  s       !!!!!!!!!V$$&,&,y8nnT8\64QWXXXXXXXX? 	2NN6511E))%q$GGGrE   c                    K   |                                   d{V  |                     |          }d|d         z   dz   |z   }d|z   }|d         |gg}|                     ||ddd	||g|d
|i	  	         d{V S )aU  
        stops watching historical candlestick data containing the open, high, low, and close price, and the volume of a market

        https://doc.xt.com/#websocket_publicsymbolKline
        https://doc.xt.com/#futures_market_websocket_v2symbolKline

        :param str symbol: unified symbol of the market to fetch OHLCV data for
        :param str timeframe: 1m, 3m, 5m, 15m, 30m, 1h, 2h, 4h, 6h, 8h, 12h, 1d, 3d, 1w, or 1M
        :param dict params: extra parameters specific to the xt api endpoint
        :returns int[][]: A list of candles ordered, open, high, low, close, volume
        Nr   r   r   r   r   r   r   r   r   r   ry   r   )rB   r   r   r   ry   rv   r   r   s           rD   un_watch_ohlcvzxt.un_watch_ohlcvr  s       !!!!!!!!!V$$&,&,y8%,!'!19 =>&&{D(NT[]cflemou  yO  Qe  xf  g  g  g  g  g  g  g  g  	grE   c                 $  K   |                                   d{V  |                     |          }d|d         z   }|                     |dd|d|           d{V }| j        r|                    ||          }|                     |||d          S )a`  
        get the list of most recent trades for a particular symbol

        https://doc.xt.com/#websocket_publicdealRecord
        https://doc.xt.com/#futures_market_websocket_v2dealRecord

        :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 xt api endpoint
        :returns dict[]: a list of `trade structures <https://docs.ccxt.com/en/latest/manual.html?#public-trades>`
        Ntrade@r   r   r#   	timestampr   )rB   r   r   r   r   ry   rv   tradess           rD   watch_tradeszxt.watch_trades  s       !!!!!!!!!V$$&,&~~dHmVTSYZZZZZZZZ? 	3OOFE22E))&%LLLrE   c           
         K   |                                   d{V  |                     |          }d|d         z   }d|z   }|                     ||ddd||g|           d{V S )a  
        stops watching the list of most recent trades for a particular symbol

        https://doc.xt.com/#websocket_publicdealRecord
        https://doc.xt.com/#futures_market_websocket_v2dealRecord

        :param str symbol: unified symbol of the market to fetch trades for
        :param dict params: extra parameters specific to the xt api endpoint
        :returns dict[]: a list of `trade structures <https://docs.ccxt.com/en/latest/manual.html?#public-trades>`
        Nr   r   r   r   r$   r   r   )rB   r   r   ry   rv   r   s         rD   un_watch_tradeszxt.un_watch_trades  s       !!!!!!!!!V$$&,&%,&&{D(OU]_ehngoqwxxxxxxxxxrE   c                 `  K   |                                   d{V  |                     |          }|                     |d          }|                     |d          }d|d         z   }|d|d         z   dz   |z   }|                     |dd|d|           d{V }|                                S )	a  
        watches information on open orders with bid(buy) and ask(sell) prices, volumes and other data

        https://doc.xt.com/#websocket_publiclimitDepth
        https://doc.xt.com/#websocket_publicincreDepth
        https://doc.xt.com/#futures_market_websocket_v2limitDepth
        https://doc.xt.com/#futures_market_websocket_v2increDepth

        :param str symbol: unified symbol of the market to fetch the order book for
        :param int [limit]: not used by xt watchOrderBook
        :param dict params: extra parameters specific to the xt api endpoint
        :param int [params.levels]: 5, 10, 20, or 50
        :returns dict: A dictionary of `order book structures <https://docs.ccxt.com/en/latest/manual.html#order-book-structure>` indexed by market symbols
        Nlevelsdepth_update@r   depth@r   r   r   )r   ry   rL   r   r~   r   )rB   r   r   r   ry   r   rv   r`   s           rD   watch_order_bookzxt.watch_order_book  s       !!!!!!!!!V$$!!&(3368,,-fTl*S069D..x9I6SWY_````````	   rE   c           
      H  K   |                                   d{V  |                     |          }|                     |d          }|                     |d          }d|d         z   }|d|d         z   dz   |z   }d|z   }|                     ||dd	d
||g|           d{V S )a  
        stops watching information on open orders with bid(buy) and ask(sell) prices, volumes and other data

        https://doc.xt.com/#websocket_publiclimitDepth
        https://doc.xt.com/#websocket_publicincreDepth
        https://doc.xt.com/#futures_market_websocket_v2limitDepth
        https://doc.xt.com/#futures_market_websocket_v2increDepth

        :param str symbol: unified symbol of the market to fetch the order book for
        :param dict params: extra parameters specific to the xt api endpoint
        :param int [params.levels]: 5, 10, 20, or 50
        :returns dict: A dictionary of `order book structures <https://docs.ccxt.com/en/latest/manual.html#order-book-structure>` indexed by market symbols
        Nr   r   r   r   r   r   r   r   r`   )r   ry   rL   r   r   )rB   r   r   ry   r   rv   r   s          rD   un_watch_order_bookzxt.un_watch_order_book  s       !!!!!!!!!V$$!!&(3368,,-fTl*S069D%,&&{D(DVXcekntmuw}~~~~~~~~~rE   c                   K   |                                   d{V  d}d}||                     |          }|                     |dd|d|           d{V }| j        r|                    ||          }|                     |||d          S )a-  
        watches information on multiple orders made by the user

        https://doc.xt.com/#websocket_privateorderChange
        https://doc.xt.com/#futures_user_websocket_v2order

        :param str [symbol]: unified market symbol
        :param int [since]: not used by xt watchOrders
        :param int [limit]: the maximum number of orders to return
        :param dict params: extra parameters specific to the xt api endpoint
        :returns dict[]: a list of `order structures <https://docs.ccxt.com/en/latest/manual.html#order-structure>`
        Norderr|   r'   r   r   )rB   r   r   r   r   rv   ry   orderss           rD   watch_orderszxt.watch_orders  s       !!!!!!!!![[((F~~dI}fdTZ[[[[[[[[? 	3OOFE22E))&%LLLrE   c                   K   |                                   d{V  d}d}||                     |          }|                     |dd|d|           d{V }| j        r|                    ||          }|                     |||d          S )aZ  
        watches information on multiple trades made by the user

        https://doc.xt.com/#websocket_privateorderDeal
        https://doc.xt.com/#futures_user_websocket_v2trade

        :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  orde structures to retrieve
        :param dict params: extra parameters specific to the kucoin api endpoint
        :returns dict[]: a list of `trade structures <https://docs.ccxt.com/?id=trade-structure>`
        Ntrader|   r(   r   r   )rB   r   r   r   r   rv   ry   r   s           rD   watch_my_tradeszxt.watch_my_trades  s       !!!!!!!!![[((F~~dIPTV\]]]]]]]]? 	3OOFE22E))&%LLLrE   c                 ~   K   |                                   d{V  d}|                     |dddd|           d{V S )av  
        watches information on multiple orders made by the user

        https://doc.xt.com/#websocket_privatebalanceChange
        https://doc.xt.com/#futures_user_websocket_v2balance

        :param dict params: extra parameters specific to the xt api endpoint
        :returns dict[]: a list of `balance structures <https://docs.ccxt.com/?id=balance-structure>`
        Nbalancer|   r&   )r   r~   )rB   r   rv   s      rD   watch_balancezxt.watch_balance
  s`       !!!!!!!!!^^D)^T4QWXXXXXXXXXrE   c                 X  K   |                                   d{V  | j        d         d         d         dz   dz   }|                     |          }|                     |           |                     ddd	          }|                     dd
d	          }| j        }	|rK|rI|                     |	          r4|                    d           d{V }
|                     |
|||d	          S d}| 	                    |dddd|           d{V }| j
        r|S |                     |	|||d	          S )a  

        https://doc.xt.com/#futures_user_websocket_v2position

        watch all open positions
        :param str[]|None symbols: list of unified market symbols
        :param number [since]: since timestamp
        :param number [limit]: limit
        :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>`
        Nr*   r   r,   r   r   r)   r3   Tr4   positionr|   )r   r;   rK   set_positions_cachehandle_option	positionsis_emptyfuturefilter_by_symbols_since_limitr~   r   )rB   rz   r   r   r   rR   rK   r3   r4   ra   snapshotrv   newPositionss                rD   watch_positionszxt.watch_positions  sx      !!!!!!!!!it$Z036?S!!  (((!%!3!34DF^`d!e!e!%!3!34DF^`d!e!e! 	]&< 	]uAUAU 	]#]]+CDDDDDDDDH55hPUW[\\\!^^D)=MtUY[abbbbbbbb? 	 11%%PTUUUrE   rK   c                     | j         t                      | _         |                     dd          }|r>d}||j        vr5|                    |           |                     | j        ||           d S d S d S )Nr)   r3   )r   r   r   futuresr   spawnload_positions_snapshot)rB   rK   r3   r   s       rD   r   zxt.set_positions_cache4  s    >!577DN!%!3!34DF^!_!_! 	N2K6>11k***

47MMMMM		N 	N11rE   c                   K   |                      d            d {V }t                      | _        | j        }t          dt	          |                    D ]<}||         }|                     |dd          }|dk    r|                    |           =||j        v r:|j        |         }|                    |           |                    |d           d S d S )Nr   	contractsposition::contract)	fetch_positionsr   r   r^   r_   rm   appendr   resolve)	rB   rK   r   r   ra   rW   r   r   r   s	            rD   r   zxt.load_positions_snapshot>  s      ..t44444444	133q#i..)) 	' 	'A |H((;BBI1}}X&&&&.((^K0FNN5!!!NN5"677777 )(rE   c                 `   | j         t                      | _         | j         }|                     |di           }|                     |          }|                    |           |                     |d          }t          dt          |                    D ]}||         }|                    d          }	|	d         }
|
                    d          }| 	                    |gd|d          }| 
                    |          s|                    ||           |                    |gd           d S )	Ndatar   r   r   rY   r   r   F)r   r   rP   parse_positionr   find_message_hashesr^   r_   splitr   r   r   )rB   rK   messagera   r   r   r   rW   r   partssymbolsStringrz   r   s                rD   handle_positionzxt.handle_positionM  s+   > >!577DN~~gvr22&&t,,X009MNNq#m,,-- 	7 	7A'*K%%d++E!!HM#))#..G,,hZ7ERRI==++ 7y+666z#788888rE   r   c                 X   |                      |d          }|                     |d          }|{|                     |d          }|d u}|                     |          }|d         }|| j        |<   |                     |d          }	|rdnd}
|	dz   |
z   }|                    ||           |S )	Nr   scvr   eventr+   r,   r   )rP   rL   parse_tickerr0   r   )rB   rK   r   r   marketIdr   isSpotr/   r   r   messageHashTailr   s               rD   handle_tickerzxt.handle_ticker}  s    z ~~gv..##D#..!!$--Bt^F&&t,,FH%F#)DL $$Wg66E(.>ffJO$,8KNN6;///rE   c                    |                      |dg           }|                     |d          }|                     |dd          }|dnd}g }t          dt	          |                    D ]F}||         }	|                     |	          }
|
d         }|
| j        |<   |                    |
           G|                     |d          d	z   |z   }| 	                    ||d	z             }t          dt	          |                    D ]}||         }|
                    d	          }|d
         }|
                    d          }|                     |d|          }t          |                                          }t	          |          }|dk    r|                    ||           |                    | j        |           |S )Nr   r   r   aqr+   r,   r   r   r      r   )rl   rP   safe_string_2r^   r_   r   r0   r   rL   r   r   r   listkeysr   )rB   rK   r   r   firstTickerspotTestrQ   
newTickersrW   
tickerDatar/   r   messageHashStartr   r   r   r   rz   r0   tickersSymbols
numTickerss                        rD   handle_tickerszxt.handle_tickers  s   H ~~gvr22nnT1--%%k4>>'3FF*	
q#d))$$ 	& 	&AaJ&&z22FH%F#)DL f%%%%++GW==DyP009ID9PQQq#m,,-- 		5 		5A'*K%%d++E!!HM#))#..G**:xIIG!',,..11N^,,JA~~w444t|%5666rE   c                    |                      |di           }|                     |d          }||                     |d          }d|v rdnd}|                     |d d |          }|d         }|                     ||          }	|                      | j        |i           | j        |<   |                     | j        |         |          }
|
;|                     | j        dd	          }t          |          }
|
| j        |         |<   |
	                    |	           |                     |d
          }|dz   |z   }|
                    |
|           |S )Nr   r   rW   qr+   r,   r   r7   r-   r   r   )rP   rL   safe_marketparse_ohlcvohlcvsr\   r[   r<   r   r   r   )rB   rK   r   r   r   r   rQ   ry   r   parsedstoredr   r   r   s                 rD   handle_ohlcvzxt.handle_ohlcv&  sU   L ~~gvr22##D#..((s33I#&$;;ZI%%hdIFFFH%F%%dF33F"&..fb"I"IDK__T[%8)DDF~))$,dKK.u5517F#I.MM&!!!$$Wg66E$,2KNN6;///rE   c                 :   |                      |d          }|                     |d          }||                     |          }|                     |d          }|dnd}|                     |d d |          }|d         }	|                     |d          }
|                     | j        |	          }|5|                     | j        dd	          }t          |          }|| j        |	<   |
                    |           |
d
z   |z   }|                    ||           |S )Nr   r   rW   r+   r,   r   r   r5   r-   r   )rP   safe_string_lowerparse_traderL   r  r\   r   r[   r<   r   r   r   )rB   rK   r   r   r   r   rW   rQ   ry   r   r   tradesArrayr5   r   s                 rD   handle_tradezxt.handle_trade`  s'   > ~~gv..))$44$$T**E  s++A#$=zI%%hdIFFFH%F$$Wg66E//$+v>>K""//mTRR(55&1F#u%%%$,2KNN;444rE   c                    |                      |d          }|                     |d          }||                     |d          }|                    d          }|                     |d          }d|v rdnd}|                     |d d |          }|d	         }	|                     |d
          }
|                     |d          }|dz   |z   }|	| j        vrP|                      |j        |i           }|                     |d          }|                     i |          | j        |	<   | j        |	         }|                     |d          }|jt          |j
                  }|                     ddd          }||k    r|                     | j        |||	           |j
                            |           d S |
r|d         }t          dt          |
                    D ]L}|
|         }|                     |d          }|                     |d          }|                    ||           M|r|d         }t          dt          |                    D ]L}||         }|                     |d          }|                     |d          }|                    ||           M|                     |d          }|                     |dd          |d<   ||d<   |                     |          |d<   |	|d	<   |                    ||           d S d S )Nr   r   r   r   r   fur,   r+   r   rh   ri   r   r   rV   r   snapshotDelay   rk   rY   rj   trW   rX   r   datetime)rP   rL   r   r  rl   
orderbooksrM   r[   
order_bookr_   ra   r   r   load_order_bookr   r^   rm   rn   r]   iso8601r   )rB   rK   r   r   r   r   
splitEventrQ   ry   r   ro   rp   r   r   r   r`   rV   cacheLengthr  rk   rW   rt   rr   rs   rj   rq   r   s                              rD   handle_order_bookzxt.handle_order_book  s*   x ~~gv..##D#..$$Wg66ES))J$$Z33E'+t||

&I%%hdIFFFH%F^^D#..F^^D#..F$,2Kdo--#~~f.BKQSTT)),@@*.//"e*D*D'/I%%i99E}!)/22 $ 2 23C_VX Y Y-//JJt3V[&QQQ&&t,,,! (q#f++.. 0 0A )C ,,S!44E#//Q77HJJuh////! (q#f++.. 0 0A )C ,,S!44E#//Q77HJJuh////))$44I!%!4!4T3!D!DIg%.Ik"$(LL$;$;Ij!"(IhNN9k22222U  rE   r   c                 8   |                      |d          }d|v rdnd}|                     ||d |          }|                      |d          }|                     |d ||                     |          |d         |                      |dd          |                     |                      |dd	                    |                     |d
d          d |                     |d          |                      |d          d d |                     |d          d dd|          S )Nr   r   r,   r+   r  rW   orderIdststatesd	orderSiderr   origQtyf)currencycostrate)infor   r   r  r   r   r2   sidetakerOrMakerrr   amountr,  fee)rL   r  
safe_trader  parse_order_statusr  rm   )rB   r   ry   r   rQ   r   s         rD   parse_ws_order_tradezxt.parse_ws_order_trade  s<   B ##E3//#+u#4#4JJ6	!!(FD)DD$$UC00	"Y//X&%%eS)<<++D,<,<UD',R,RSS**5$DD %%eW55&&ui88 ((44  
  
$ %  	rE   r   c                 L   |                      |dd          }d|v rdnd}|                     ||d |          }|                     |dd          }|                     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          d|                      |dd           |                     |d!          d |                      |d"d#          |                     |                     |d$d%                    d | 	                    |d&          d'd d(|          S ))Nr   r   r,   r+   ct
createTimer.  r   rW   r$  clientOrderIdcir   r  lastTradeTimestampr2   timeInForcepostOnlyr/  r'  r(  rr   p	stopPricestopLoss
takeProfitr1  oqr)  filledeqexecutedQtylqapavgPricer%  r&  r*  )r+  r,  )	remainingr,  averagestatusr2  r   )
r   r  r]   
safe_orderr  safe_string_lower_2safe_number_2rL   r4  rm   )rB   r   ry   r   rQ   r   s         rD   parse_ws_orderzxt.parse_ws_order5  sL   V %%eS(;;#+u#4#4JJ6	!!(FD)DD''t\BB	  
E 
$$$UC;; 
 T//t_MM 
 	 

 Y// 
 !$ 
 fX& 
 F6N 
 4 
  
 D,,UD+FF 
 T''sG<< 
  
  
 $ 
  d((i@@! 
" d((mDD# 
$ ))%66))%zBB--d.>.>udG.T.TUU ((44  5 
  
  
6 7  	rE   c                    | j         }|1|                     | j        d          }t          |          }|| _         |                     |di           }|                     |dd          }|dd|v rdnd}|                     |d d |          }|                     ||          }	|                    |	           |	                    |d|z              |S )Nr6   r   r   r   r,   r+   zorder::)
r   r[   r<   r   rP   r   r  rO  r   r   )
rB   rK   r   r   r   r   r   rQ   ry   r  s
             rD   handle_orderzxt.handle_order  s    V >%%dlMBBE+E22F DKw33%%eS(;;'/5'8'8

vI%%hdIFFF((77FMM&!!!NN69y#8999rE   c                    |                      |di           }|                     |dd          }|                     |          }|                                 }|                     |d          |d<   |                     |d          |d<   |                     |dd	          |d
<   || j        |<   |                     | j                  | _        d|v rdnd}|                    | j        d|z              d S )Nr   ccoinavailableBalancefreer*  usedri   walletBalancetotalr,   r+   z	balance::)rP   r   safe_currency_codeaccountrL   r   safe_balancer   )rB   rK   r   r   
currencyIdcoder[  rQ   s           rD   handle_balancezxt.handle_balance  s    F ~~gvr22''c6::
&&z22,,..**41CDD**455--dCII$T((66#)T>>JJ	t|[9%<=====rE   c                 ~   |                      |di           }| j        }|2|                     | j        dd          }t	          |          }|| _        |                     |          }|                     |d                   }|                    |           |d         rdnd}|                    |d|z              d S )Nr   r5   r-   r   r,   r+   ztrade::)	rP   myTradesr[   r<   r   r  ry   r   r   )	rB   rK   r   r   r  r   parsedTradery   rQ   s	            rD   handle_my_tradeszxt.handle_my_trades  s    F ~~gvr22>%%dlM4HHE+E22F"DM&&t,,[233k""""("4@JJ&	vy9455555rE   c                    |                      |d          }|dk    r|                                 d S ||                      |d          }| j        | j        | j        | j        | j        | j        | j        | j        | j        | j        d
}| 	                    ||          }|dk    r-| 
                    |d          }d|v sd|v r| j        }n| j        }| |||           d S d S |                     ||           d S )	Nr   pongr   )
klinedepthdepth_updater/   
agg_tickerr0   agg_tickersr   r   r   r   r   oir$  )rL   onPongr  r"  r   r  r_  rQ  r   r\   rP   rc  r  handle_subscription_status)rB   rK   r   r   r   methodsr.   r   s           rD   handle_messagezxt.handle_message  s0     '22F??MMOOOOO$$Wg66E*/ $ 6,"0.#2.* 0 G __We44F~~gv66DLLi4&7&7!2FF!.F!vw''''' "! ++FG<<<<<rE   c                 8    |                                  |_        dS )Nr9   )r   lastPong)rB   rK   s     rD   r9   zxt.ping7  s    ++--vrE   c                     |                      |d          }|                     |j        d          }d}|F|                     ||i           }|                     |dd          }|r|                     ||           |S )Nr   Fr   )rL   index_byrM   rP   	safe_boolhandle_un_subscription)rB   rK   r   r   subscriptionsByIdr   r   s          rD   rm  zxt.handle_subscription_status;  s      gt,, MM&*>EE>>>*;RDDL..}eLLK B++FLAAArE   r   c                    |                      |dg           }|                      |dg           }t          dt          |                    D ])}||         }||         }|                     |||           *|                     |           d S )Nr   r   r   )rl   r^   r_   clean_unsubscriptionclean_cache)rB   rK   r   r   r   j	unsubHashsubHashs           rD   ru  zxt.handle_un_subscriptionU  s    |_bII>>,8JBOOq#m,,-- 	B 	BA%a(I&q)G%%fgyAAAA&&&&&rE   c                     |                      |d          }|dk    s|dk    r!d |j        d<   |                     d           d S |                    |           d S )Nmsginvalid_listen_keyztoken expirer>   T)rL   rM   rT   reject)rB   rK   r   r~  s       rD   handle_error_messagezxt.handle_error_message^  sm     w..'''SN-B-B,0F )%%%FgrE   )N)<__name__
__module____qualname__r   rA   boolrT   rf   ru   strr
   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   r   r   r   dictr   r  r  r  r"  r5  rO  rQ  r_  rc  ro  r9   rm  ru  r  __classcell__)rC   s   @rD   r   r      sM       1# 1 1 1 1 1 1f--t -- -- -- --^  ( ( (& Y]sw  AC /V /VC /V /V# /Vv /Vgklogp /V /V /V /Vb z~  UY  bd  y{ 9L 9Lc 9L 9Lc 9LWZ 9Lcf 9Lpv 9L  IM  NQ  IR 9L  @C 9L 9L 9L 9Lv 68 Y Y YF Y Y Y Y* 9; z zC zv z z z z, 6:" E E7 E E E E E2 9=R E Eg EG E E E E2 ?CQUdhqs H H H H3 H^a Hx|  ~B  yC H H H H, BFb g g3 g3 gUYZ^U_ g g g g& <@d[] M M MS M Mbfglbm M M M M* 9; y yC ytE{ y y y y" @DB ! !S ! !S\ ! ! ! !2 =?   9    0 04$UYbd M M MC Ms Mimnsit M M M M. 37TX\eg M MC Ms MRU Mlpqvlw M M M M. *, Y Y Y Y Y Y 8<$]ajl V VW VC VWZ Vquv~q V V V V8N& N N N N8 8 8.9 .9 .9`IF IT I I I IV\V \d \ \ \ \|86 8D 8 8 8 8t06 0D 0 0 0 0dh3 h3 h3 h3 h3 h3T7 7$ 7 7 7 7 7rJ JD J& J J J JX86 8D 8 8 8 8t->V ->d -> -> -> ->^-6v -6 -6 -6 -6 -6^=V = = = =<6      4'V '4 ' ' ' '6 D        rE   r   )ccxt.async_supportccxt ccxt.async_support.base.ws.cacher   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   async_supportr    rE   rD   <module>r     s)       A  A  A  A  A  A  A  A  A  A  A  A x x x x x x x x x x x x x x x x x x x x x x x x x x x x 4 4 4 4 4 4       ) ) ) ) ) )] ] ] ] ]		 ] ] ] ] ]rE   