
    ":hn                     *   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 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" d dl#m$Z$  G d dejJ                  jL                        Z&y)    N)
ArrayCacheArrayCacheBySymbolByIdArrayCacheBySymbolBySideArrayCacheByTimestamp)AnyBalancesBoolIntMarketOrder	OrderBookPositionStrStringsTickerTickersTrade)Client)List)ExchangeError)AuthenticationError)ArgumentsRequired)
BadRequest)NotSupported)RateLimitExceeded)ChecksumError)UnsubscribeError)Precisec            
           e Zd Zdef fdZd Zi fdZi fdZi fdZi fdZ	i fdZ
d	efd
Zi fdZi fdedededefdZdefdZdDdeded	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	efdZdDdZdddi fdedededee   fdZdi fdedefd Z d	efd!Z!dDded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i fdee   dede#fd%Z&d	efd&Z'd	eded'efd(Z(d) Z)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-i fdedefd-Z.d	efd.Z/dDde+fd/Z0i fde1fd0Z2d	efd1Z3dddi fdedededee4   fd2Z5d	efd3Z6dDd4e7dede4fd5Z8dddi fdedededee9   fd6Z:defd7Z;d	efd8Z<dDd9Z=d	efd:Z>d	efd;Z?d	efd<Z@d	efd=ZAd	efd>ZBd	efd?ZCd	efd@ZDd	efdAZEd	efdBZFd	efdCZG xZHS )E	coincatchreturnc                 <   | j                  t        t        |          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ddddddd	ddidd| j                  idt
        t        t
        t        t        t        t        t        t        t        t
        t        di did      S )NTF)wswatchTradeswatchTradesForSymbolswatchOrderBookwatchOrderBookForSymbols
watchOHLCVwatchOHLCVForSymbolswatchOrderswatchMyTradeswatchTickerwatchTickerswatchBalancewatchPositionsapir#   z'wss://ws.coincatch.com/public/v1/streamz(wss://ws.coincatch.com/private/v1/stream)publicprivate     1m5m15m30m1H4H12H1D1W)	r5   r6   r7   r8   1h4h12h1d1wchecksum)tradesLimit
OHLCVLimittimeframesForWsr&   ping)300013000230003300043000530006300073001130012300133001430015)exactbroad)hasurlsoptions	streaming
exceptions)deep_extendsuperr    describerG   r   r   r   )self	__class__s    L/var/www/html/turnos/venv/lib/python3.12/site-packages/ccxt/pro/coincatch.pyr]   zcoincatch.describe   s    i ? A#)-"&,0"(-#!&# $ $"&  "K#M  $!   
$ #& 		 !+!4!+!4!4!2!2!4!4!4!+!4  [@D
 @ @	    c                    | j                  |d      }| j                  |d      }| j                  |      }|d   }|d   }d}|dk(  r
|dk(  rd}nd	}| j                  |      | j                  |      z   |z   }| j                  |      S )
NinstIdinstTypebaseIdquoteId_SPBLmcUSD_DMCBL_UMCBL)safe_stringparseSpotMarketIdsafe_currency_codesafeMarketCustom)	r^   entryrc   rd   baseAndQuotere   rf   suffixmarketIds	            r`   get_market_from_argzcoincatch.get_market_from_arg\   s    !!%2##E:6--f5h'y)t%!!**62T5L5LW5UUX^^$$X..ra   c                 T  K   | j                          | j                  d   d   d   }| j                  |      }d}|j                  |      }| j	                  |j
                  |      }|t        | j                               }|dz   dz   }| j                  | j                  |      | j                  | j                        t        j                  d      }	d}
|
| j                  | j                  ||	d	gd
}| j                  ||      }| j!                  ||||       | d {   S 7 w)Nr0   r#   r2   authenticatedGETz/user/verifybase64login)apiKey
passphrase	timestampsignopargs)check_required_credentialsrW   clientfuture
safe_valuesubscriptionsstrsecondshmacencodesecrethashlibsha256rz   passwordextendwatch)r^   paramsurlr   messageHashr   rv   r|   auth	signature	operationrequestmessages                r`   authenticatezcoincatch.authenticatek   s    '')iit$Y/S!%{+(<(<kJ DLLN+Iu$~5D		$++d"3T[[5Mw~~_ghII #'++&*mm%. )	
G kk'62GJJsK+>||s   DD(!D&"D(c                    K   | j                   d   d   d   }d|gd}| j                  ||      }| j                  ||||       d {   S 7 wNr0   r#   r1   	subscriber~   rW   r   r   r^   r   subscribeHashr   r   r   r   r   s           r`   watch_publiczcoincatch.watch_public   sZ     iit$X.F
 ++gv.ZZ['=IIII   AAAAc                    K   | j                   d   d   d   }d|gd}| j                  ||      }| j                  ||||       d {   S 7 w)Nr0   r#   r1   unsubscriber~   r   )r^   r   r   r   r   r   r   s          r`   un_watch_publiczcoincatch.un_watch_public   sZ     iit$X.F
 ++gv.ZZ[';GGGGr   c                    K   | j                          d {    | j                  d   d   d   }d|gd}| j                  ||      }| j                  ||||       d {   S 7 M7 wNr0   r#   r2   r   r~   )r   rW   r   r   r   s           r`   watch_privatezcoincatch.watch_private   su     !!!iit$Y/F
 ++gv.ZZ['=III 	" Js"   A(A$AA(A& A(&A(c                    K   | j                          d {    | j                  d   d   d   }d|d}| j                  ||      }| j                  ||||       d {   S 7 L7 wr   )r   rW   r   watch_multiple)r^   messageHashessubscribeHashesr   r   r   r   r   s           r`   watch_private_multiplez coincatch.watch_private_multiple   su     !!!iit$Y/
 ++gv.((mWoVVV 	" Ws"   A'A#AA'A%A'%A'r   c                 b    d}| j                  |j                  |      }|j                  d       y )Nrv   T)r   futuresresolve)r^   r   r   r   r   s        r`   handle_authenticatezcoincatch.handle_authenticate   s)     &=tra   c                    K   | j                   d   d   d   }d|d}| j                  ||      }| j                  ||||       d {   S 7 wr   )rW   r   r   )r^   r   r   	argsArrayr   r   r   r   s           r`   watch_public_multiplezcoincatch.watch_public_multiple   sZ     iit$X.
 ++gv.((mWoVVVVs   AAAAsymbolchannelmessageHashTopicc                    K   | j                          d {    | j                  |      }| j                  |      \  }}d|z   dz   |z   }|||d}	| j                  ||	|       d {   S 7 U7 w)Nzunsubscribe::rd   r   rc   )load_marketsmarketget_public_inst_type_and_idr   )
r^   r   r   r   r   r   rd   rc   r   r   s
             r`   un_watch_channelzcoincatch.un_watch_channel   s     !!!V$;;FC&$'77#=F 

 ))+tVDDD 	" E"   A0A,AA0'A.(A0.A0r   c                 x    |d   |d   z   }d }|d   rd}||gS |d   rd}||gS t        | j                  dz         )Nre   rf   spotSPswapMCz$ supports only spot and swap markets)r   id)r^   r   rc   rd   s       r`   r   z%coincatch.get_public_inst_type_and_id   sf    !F9$55&>H
 &!!	 F^H &!! tww)OOPPra   Nhash	timeframec                    |d   }| j                  ||      }| j                  | j                  |g       }d}|d|z   }t        dt	        |            D ]*  }	||	   }|d   }
||
z   |z   }| j                  ||      s(|c S  |S )Nr    r   r   r   )find_message_hashes	safe_listmarkets_by_idrangelenin_array)r^   r   r   r   r   rs   r   marketsWithCurrentIdrr   ir   r   s               r`   %handle_dmcbl_market_by_message_hashesz/coincatch.handle_dmcbl_market_by_message_hashes   s    $<00>  $~~d.@.@(BO 9_Fq#234 	A)!,FH%F-&0K}}[-8	 ra   c                    K   | j                          d{    | j                  |      }| j                  |      \  }}d}|dz   |z   }|||d}| j                  ||||       d{   S 7 U7 w)at  
        watches a price ticker, a statistical calculation with the information calculated over the past 24 hours for a specific market

        https://coincatch.github.io/github.io/en/spot/#tickers-channel

        :param str symbol: unified symbol of the market to fetch the ticker for
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :param str [params.instType]: the type of the instrument to fetch the ticker for, 'SP' for spot markets, 'MC' for futures markets(default is 'SP')
        :returns dict: a `ticker structure <https://docs.ccxt.com/#/?id=ticker-structure>`
        Ntickerr   r   )r   r   r   r   )	r^   r   r   r   rd   rc   r   r   r   s	            r`   watch_tickerzcoincatch.watch_ticker   s      !!!V$;;FC&mf, 

 &&{KvNNN 	" Or   c                 z   K   | j                          d{    | j                  |dd|       d{   S 7  7 w)aX  
        unsubscribe from the ticker channel

        https://coincatch.github.io/github.io/en/mix/#tickers-channel

        :param str symbol: unified symbol of the market to unwatch the ticker for
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns any: status of the unwatch request
        Nr   r   r   r^   r   r   s      r`   un_watch_tickerzcoincatch.un_watch_ticker   s=      !!!**68XvNNN 	"N   ;7;9;;symbolsc                   K   | j                          d{    || j                  }g }g }t        dt        |            D ]W  }||   }| j	                  |      }| j                  |      \  }}	|d|	d}
|j                  |
       |j                  d|z          Y | 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://coincatch.github.io/github.io/en/mix/#tickers-channel

        :param str[] symbols: unified symbol of the market to watch the tickers for
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: a `ticker structure <https://docs.ccxt.com/#/?id=ticker-structure>`
        Nr   r   r   ticker:r   )r   r   r   r   r   r   appendr   
newUpdatesfilter_by_arraytickers)r^   r   r   topicsr   r   r   r   rd   rc   r   r   results                r`   watch_tickerszcoincatch.watch_tickers
  s     !!!?llGq#g,' 
	5AQZF[[(F#??GHf$# D
 MM$  V!34
	5 22=-QWY_``??F(/F78$%M##DLL(GDD+ 	"  as"   C1C-BC13C/4:C1/C1c                    | j                  |di       }| j                  |      }|d   }d}|j                  d      dk\  r| j                  |||      }| j	                  |dg       }| j                  | j                  |di       |      }|d   }	|| j                  |	<   ||	z   }
|j                  | j                  |	   |
       y )Nargr   r   rj   r   datar   )	safe_dictrt   findr   r   parse_ws_tickerr   r   )r^   r   r   r   r   rs   r   r   r   r   r   s              r`   handle_tickerzcoincatch.handle_ticker+  s    2 nnWeR0))#.$<=="a'??fUF~~gvr2%%dnnT1b&A6J!%VVmt||F+[9ra   c           
         | j                  |d      }| j                  |dd      }| j                  i d|d   d|d| j                  |      d| j                  |d      d	| j                  |d
      d| j                  |d      d| j                  |d      d| j                  |d      d| j                  |d      dd d| j	                  |dd      d|d|dd dd dt        j                  | j                  |d      d      dd | j                  |d      | j                  |d      | j                  |d       | j                  |d!      |d"|      S )#Nlastts
systemTimer   r|   datetimehighhigh24hlowlow24hbidbestBid	bidVolumebidSzaskbestAsk	askVolumeaskSzvwapopenopen24hopenUtcclosepreviousClosechange
percentagechgUTC100average
baseVolumequoteVolume
indexPrice	markPrice)r  r  r  r	  info)rl   safe_integer_2safe_tickeriso8601safe_string_2r   
string_mulsafe_number)r^   r   r   r   r|   s        r`   r   zcoincatch.parse_ws_tickerQ  s   ^ /''lC	 !
fX&!
!
 Y/!
 D$$VY7	!

 4##FH5!
 4##FI6!
 ))&':!
 4##FI6!
 ))&':!
 D!
 D&&vy)D!
 T!
 D!
 T!
 d!
  ',,T-=-=fh-OQVW!!
" t#!
$ **6<@++FMB**6<@))&+>-!
. / 	ra   r5   sincelimitc                   K   | j                          d{    | j                  |      }| j                  d   }d| j                  ||      z   }| j	                  |      \  }	}
|	||
d}d|z   dz   |z   }| 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://coincatch.github.io/github.io/en/spot/#candlesticks-channel

        :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(not including)
        :param int [limit]: the maximum amount of candles to fetch(not including)
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :param bool [params.instType]: the type of the instrument to fetch the OHLCV data for, 'SP' for spot markets, 'MC' for futures markets(default is 'SP')
        :returns int[][]: A list of candles ordered, open, high, low, close, volume
        NrF   candler   ohlcv:r   r   T)	r   r   rX   rl   r   r   r   getLimitfilter_by_since_limit)r^   r   r   r  r  r   r   
timeframesr   rd   rc   r   r   ohlcvs                 r`   watch_ohlcvzcoincatch.watch_ohlcv  s      !!!V$\\"34
T--j)DD;;FC& 

 '#-	9''[$OO??NN651E))%q$GG 	" Ps"   CCA5CC8CCc                    K   | j                          d{    | j                  d   }| j                  ||      }d|z   }| j                  ||d|z   |       d{   S 7 I7 w)a  
        unsubscribe from the ohlcv channel

        https://www.bitget.com/api-doc/spot/websocket/public/Candlesticks-Channel

        :param str symbol: unified symbol of the market to unwatch the ohlcv for
 @param timeframe
        :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
        NrF   r  r  )r   rX   rl   r   )r^   r   r   r   r  intervalr   s          r`   un_watch_ohlcvzcoincatch.un_watch_ohlcv  sq      !!!\\"34
##J	:X%**67Hx<OQWXXX	 	" Ys"   A$A AA$A"A$"A$c                    | j                  |di       }| j                  |      }|d   }d}| j                  |dg       }| j                  |d      }|dd  }	| j	                  |	      }
|j                  d      dk\  r| j                  ||||
      }|d	   }|| j                  vri | j                  |<   |
| j                  |   vr8| j                  | j                  d
d      }t        |      | j                  |   |
<   | j                  |   |
   }t        dt        |            D ]8  }| j                  ||g       }| j                  ||      }|j                  |       : ||z   dz   |
z   }|j                  ||       y )Nr   r   r  r   r      rj   r   r   rE   r3   r   )r   rt   r   rl   find_timeframer   r   ohlcvssafe_integerrX   r   r   r   parse_ws_ohlcvr   r   )r^   r   r   r   r   rs   r   r   r   	klineTyper   r   r  storedr   r  parsedr   s                     r`   handle_ohlcvzcoincatch.handle_ohlcv  s   $ nnWeR0))#.$<~~gvr2""3	2ABK	''	2	=="a'??fV_`F!$++%"$DKKT[[00%%dllL$GE-B5-IDKK	*V$Y/q#d)$ 	"A^^D!R0F((8FMM&!	" Vmc)I5v{+ra   c           	          | j                  |d      | j                  |d      | j                  |d      | j                  |d      | j                  |d      | j                  |d      gS )Nr                  )r"  r  )r^   r  r   s      r`   r#  zcoincatch.parse_ws_ohlcv  sn     eQ'UA&UA&UA&UA&UA&
 	
ra   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://coincatch.github.io/github.io/en/spot/#depth-channel

        :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   r  r   s       r`   watch_order_bookzcoincatch.watch_order_book  s%      66xOOOOs   !!c                    K   | j                          d{    d}| j                  |d      }|dk(  s|dk(  r | j                  |d      }|t        |      z  }| j	                  ||||       d{   S 7 ^7 w)a  
        unsubscribe from the orderbook channel

        https://coincatch.github.io/github.io/en/spot/#depth-channel

        :param str symbol: unified symbol of the market to fetch the order book for
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :param int [params.limit]: orderbook limit, default is None
        :returns dict: A dictionary of `order book structures <https://docs.ccxt.com/#/?id=order-book-structure>` indexed by market symbols
        Nbooksr  r-     )r   r"  omitr   r   )r^   r   r   r   r  s        r`   un_watch_order_bookzcoincatch.un_watch_order_book  s      !!!!!&'2QJERKYYvw/Fs5z!G**67GVLLL 	" Ms"   A9A5AA90A71A97A9c                   K   | j                          d{    | j                  |      }d}g }g }t        dt        |            D ]Z  }||   }| j	                  |      }	| j                  |	      \  }
}|
||d}|j                  |       |j                  |dz   |z          \ | 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://coincatch.github.io/github.io/en/spot/#depth-channel

 @param symbols
        :param int [limit]: the maximum amount of order book entries to return
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: A dictionary of `order book structures <https://docs.ccxt.com/#/?id=order-book-structure>` indexed by market symbols
        Nr2  r   r   r   )	r   market_symbolsr   r   r   r   r   r   r  )r^   r   r  r   r   r   r   r   r   r   rd   rc   r   	orderbooks                 r`   r/  z&coincatch.watch_order_book_for_symbols(  s      !!!%%g.q#g,' 
	9AQZF[[(F#??GHf$" D
 MM$  3!78
	9 44]MSY[abb	  # 	"  cs"   CCB$C;C<CCc                    | j                  |di       }| j                  |      }|d   }d}|j                  d      dk\  r| j                  |||      }|d   }| j	                  |d      }||z   }	| j                  |dg       }
| j                  |
d      }| j                  |d	      }|}|r|| j                  vr%| j                  i       }||d<   || j                  |<   | j                  |   }| j                  |d
g       }| j                  |dg       }| j                  |d
   |       | j                  |d   |       ||d<   | j                  |      |d<   | j                  | j                  dd      }| j	                  |d      dk(  }|sF|rC|d
   }|d   }t        |      }t        |      }g }t        dd      D ]u  }||k  r4|j                  ||   d   d          |j                  ||   d   d          ||k  sB|j                  ||   d   d          |j                  ||   d   d          w dj!                  |      }| j#                  |d      }| j                  |d      }||k7  rc| j%                  | j&                  |||	       y | j)                  i       }| j+                  |||      }|j-                  |       || j                  |<   |j/                  | j                  |   |	       y )Nr   r   zbooks:rj   r   r   r   r   r   asksbidsr|   r   rC   Tactionsnapshot   r*  r)  r   )r   rt   r   r   rl   r   r"  
orderbookscounted_order_bookhandle_deltasr  	safe_boolrX   r   r   r   joincrc32spawnhandle_check_sum_error
order_bookparse_order_bookresetr   )r^   r   r   r   r   rs   r   r   r   r   r   rawOrderBookr|   incrementalBookobstoredOrderBookr:  r;  rC   
isSnapshot
storedAsks
storedBids
asksLength
bidsLengthpayloadArrayr   payloadcalculatedChecksumresponseChecksumr8  parsedOrderbooks                                  r`   handle_order_bookzcoincatch.handle_order_bookF  s     nnWeR0))#.$<=="a'??fUF!""3	2Vm~~gvr2~~dA.%%lD9	!doo-,,R0%8*,'"oof5O>>,;D>>,;Dv6=v6=+4OK(*.,,y*AOJ'~~dllJEH))'8<
JJ(,V4
,V4
 _
 _
!q" AA:~$++JqM!,<Q,?@$++JqM!,<Q,?@:~$++JqM!,<Q,?@$++JqM!,<Q,?@A ((<0%)ZZ%>"#'#4#4\:#N %)99JJt::FFKX+I"33L&)TOOOO,&/DOOF#tv.<ra   r   c                    K   | j                  |       d {    t        | j                  dz   | j                  |      z         }|j	                  ||       y 7 Aw)N )r5  r   r   orderbook_checksum_messagereject)r^   r   r   r   errors        r`   rF  z coincatch.handle_check_sum_error  sO     &&v...dggmd.M.Mf.UUVe[) 	/s   AAAAc                 n    | j                  |dd      }|j                  |       |j                  |       y )Nr   r)  )parse_bid_askr   
storeArray)r^   booksidedeltabidAsks       r`   handle_deltazcoincatch.handle_delta  s0    ##E1a0eF#ra   c                 b    t        dt        |            D ]  }| j                  |||           y )Nr   )r   r   rd  )r^   ra  deltasr   s       r`   rA  zcoincatch.handle_deltas  s0    q#f+& 	3Ahq	2	3ra   c                 H   K   | j                  |g|||       d{   S 7 w)a$  
        get the list of most recent trades for a particular symbol

        https://coincatch.github.io/github.io/en/spot/#trades-channel

        :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_symbols)r^   r   r  r  r   s        r`   watch_tradeszcoincatch.watch_trades  s'      22F8UE6RRRRs   " "c                   K   t        |      }|dk(  rt        | j                  dz         | j                          d{    | j	                  |      }g }g }t        dt        |            D ]W  }||   }	| j                  |	      }
| j                  |
      \  }}|d|d}|j                  |       |j                  d|	z          Y | j                  ||||       d{   }| j                  r6| j                  |d      }| j                  |d      }|j                  ||      }| j                  |||dd	      S 7 7 ]w)
a  
        watches information on multiple trades made in a market

        https://coincatch.github.io/github.io/en/spot/#trades-channel

 @param symbols
        :param int [since]: the earliest time in ms to fetch orders 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>`
        r   z> watchTradesForSymbols() requires a non-empty array of symbolsNtrader   trade:r   r|   T)r   r   r   r   r7  r   r   r   r   r   r   r   rl   r  r  )r^   r   r  r  r   symbolsLengthr   r   r   r   r   rd   rc   r   tradesfirsttradeSymbols                    r`   rh  z"coincatch.watch_trades_for_symbols  sL     GA#DGG.n$noo!!!%%g.q#g,' 
	4AQZF[[(F#??GHf$" D
 MM$  F!23
	4 11-PVX^__??NN61-E**5(;KOOK7E))&%TRR) 	" `s#   <D>D:BD>D<AD><D>c                 z   K   | j                          d{    | j                  |dd|       d{   S 7  7 w)aX  
        unsubscribe from the trades channel

        https://coincatch.github.io/github.io/en/spot/#trades-channel

        :param str symbol: unified symbol of the market to unwatch the trades for
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns any: status of the unwatch request
        Nrk  r   r   s      r`   un_watch_tradeszcoincatch.un_watch_trades  s=      !!!**67GVLLL 	"Lr   c                 p   | j                  |di       }| j                  |      }|d   }d}|j                  d      dk\  r| j                  |||      }|d   }|| j                  vr5| j                  | j                  dd      }t        |      | j                  |<   | j                  |   }	| j                  |d	g       }
|
a| j                  |
d      }
t        dt        |
            D ]7  }| j                  |
|      }| j                  ||      }|	j                  |       9 ||z   }|j                  |	|       y )
Nr   r   rl  rj   r   r   rD   r3   r   )r   rt   r   r   rn  r"  rX   r   r   sort_byr   r   parse_ws_trader   r   )r^   r   r   r   r   rs   r   r   r  r%  r   r   rk  r&  r   s                  r`   handle_tradeszcoincatch.handle_trades  s1    nnWeR0))#.$<=="a'??fUF!$++%%%dllM4HE",U"3DKKV$~~gvr2<<a(D1c$i( &tQ/,,UF;f%& Vmv{+ra   c                     | j                  |d      }| j                  d || j                  |      |d   | j                  |d      | j	                  |d      | j	                  |d      d d d d d |d|      S )Nr   r   r+  r)  r*  )r   r|   r   r   sidepriceamountcosttakerOrMakertypeorderfeer
  )r"  
safe_trader  safe_string_lowerrl   )r^   rk  r   r|   s       r`   ru  zcoincatch.parse_ws_trade  s     %%eQ/	"Y/X&**5!4%%eQ/&&ua0  
  	ra   c                    K   d}| j                  dd|      \  }}d}|dk(  rd}d}| j                  |dd|      \  }}||dd	}d
|j                         z   }| j                  ||||       d{   S 7 w)aK  
        watch balance and get the amount of funds available for trading or funds locked in orders

        https://coincatch.github.io/github.io/en/spot/#account-channel
        https://coincatch.github.io/github.io/en/mix/#account-channel

        :param dict [params]: extra parameters specific to the exchange API endpoint
        :param str [params.type]: 'spot' or 'swap'(default is 'spot')
        :param str [params.instType]: *swap only* 'umcbl' or 'dmcbl'(default is 'umcbl')
        :returns dict: a `balance structure <https://docs.ccxt.com/#/?id=balance-structure>`
        Nr.   spblr   umcblaccountrd   defaultr   balance:)handle_market_type_and_paramshandle_option_and_paramslowerr   )r^   r   r}  rd   r   r   r   s          r`   watch_balancezcoincatch.watch_balance  s      99.$PVWf6>H88Q[]ef& 

 !8>>#33''[$OOOOs   A+A4-A2.A4c                    | j                  |dg       }t        dt        |            D ]  }||   }| j                  |dd      }| j	                  |      }|| j
                  v r| j
                  |   n| j                         }d|v rdnd}	| j                  ||	      |d<   | j                  |d      |d	<   | j                  |d
      |d<   || j
                  |<    | j                  | j
                        | _        | j                  |d      }
| j                  |
d      }d|z   }|j                  | j
                  |       y )Nr   r   coinName
marginCoinmaxTransferOut	availablefreeequitytotalfrozenusedr   rd   r  )r   r   r   r  rn   balancer  rl   safe_balancer   r  r   )r^   r   r   r   r   
rawBalance
currencyIdcoder  	freeQueryr   rd   r   s                r`   handle_balancezcoincatch.handle_balance,  s6   J ~~gvr2q#d)$ 		)AaJ++J
LQJ**:6D-1T\\-Adll4(G-=-K(R]I"..z9EGFO#//
HEGG"..z8DGFO!(DLL		) ((6nnWe,))#z: 8+t||[1ra   c                   K   d}| j                          d{    d}d}|| j                  |      }|d   }|d   }d}| j                  |||      \  }}d}	|}
|dk(  r&|Xt        | j                  dz   |z   dz   |z   d	z         d
}
d}	|| j                  ||d|	      \  }	}n|j                  d      dk\  rd}	d}| j                  |d      }|rd}| j                  |d      }|	||
d}d}||d|z   z  }| j                  ||||       d{   }| j                  r|j                  ||      }| j                  ||||d      S 7 ,7 :w)a  
        watches information on multiple orders made by the user

        https://coincatch.github.io/github.io/en/spot/#order-channel
        https://coincatch.github.io/github.io/en/mix/#order-channel
        https://coincatch.github.io/github.io/en/mix/#plan-order-channel

        :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
        :param str [params.type]: 'spot' or 'swap'
        :param str [params.instType]: *swap only* 'umcbl' or 'dmcbl'(default is 'umcbl')
        :param bool [params.trigger]: *swap only* whether to watch trigger orders(default is False)
        :returns dict[]: a list of `order structures <https://docs.ccxt.com/#/?id=order-structure>`
        r*   Nr   r   r  r   rZ  z"() requires a symbol argument for z	 markets.r  r  rd   rj   r   dmcblorderstrigger
ordersAlgor   r   T)r   r   r  r   r   r  r   rB  r4  r   r   r  filter_by_symbol_since_limit)r^   r   r  r  r   
methodNamer   rs   
marketTyperd   rc   r   	isTriggerr   r   r  s                   r`   watch_orderszcoincatch.watch_ordersb  s    " #
!!![[(FH%Fd|H
!??
FTZ[
F~'#
(BEi(ilv(v  zE  )E  F  FFH~#'#@#@U_ai#j &==*a/&HNN695	"GYYvy1F 

 3<'K))+{D&QQ??OOFE2E00tTTM 	"F Rs"   E	EC3E	E8E	E	c                 p   | j                  |di       }| j                  |d      }| j                  |d      }d }|dk(  rd}nd}| j                  |dg       }| j                  -| j	                  | j
                  dd	      }t        |      | _        d
}	| j                  }
d }t        dt        |            D ]m  }||   }| j                  |d|      }| j                  |d d |      }| j                  ||      }|
j                  |       |d   }d|z   }|j                  |
|       o |j                  |
|	       y )Nr   rd   rc   r  r   r   r   ordersLimitr3   r  r   r   zorders:)r   rl   r   r  r"  rX   r   r   r   safe_marketparse_ws_orderr   r   )r^   r   r   r   rd   	argInstIdr  r   r  r   r%  r   r   r~  rs   r   r&  r   s                     r`   handle_orderzcoincatch.handle_order  sB   B nnWeR0##C4$$S(3	
vJJ~~gvr2;;%%dllM4HE07DKq#d)$ 	0AGE''xCH%%hdJGF((7FMM&!H%F#f,KNN6;/	0 	vt$ra   r~  c                 l   | j                  |d      }| j                  |d      }| j                  |||      }| j                  |d      }|d   }| j                  |d      }| j                  |dg       }| j	                  |d      }	t        j                  | j                  |	d      d	      }
| j                  |	d
      }| j                  | j                  |d            }| j                  | j                  |d            }||}| j                  |d      }| j                  i d| j                  |d      d| j                  |d      d|d| j                  |      dd d| j                  |d      d| j                  |      d|d|d| j                  | j                  |d            d| j                  |d      d|d| j                  |d      d| j                  |d      d| j                  |d       d!d d"d d d | j                  |d#      d ||
d$| j                  |d%      d |d&|      S )'Nrc   tgtCcycTimer   statusorderFeer   r  z-1feeCcypxavgPxordTyper   ordIdclientOrderIdclOrdIdr|   r   lastTradeTimestamplastUpdateTimestampuTimer}  timeInForceforcerx  ry  r  rz  szfilled	accFillSz	remainingtriggerPricenotional)currencyr{  r   )takeProfitPricestopLossPricer{  rn  r  
reduceOnlypostOnlyr
  )rl   ro   r"  r   r   r   r  	omit_zeror  
safe_orderr  parse_order_statusparseOrderTimeInForcerB  )r^   r~  r   rs   settleIdr|   r   	rawStatusr  r  feeCostfeeCurrencyry  priceAvgr}  s                  r`   r  zcoincatch.parse_ws_order  s   ` ##E84##E84&&xB%%eW5	!$$UH5	>>%R8nnXq)$$T%5%5c5%A4H&&sH5t//t<=>>$"2"25'"BC=E%%eY7  
$""5'2 
T--eY? 
  
 Y/	 

 !$ 
 "4#4#4UG#D 
 d--i8 
 f 
 D 
 455d6L6LUT[6\] 
 D**5&9 
 U 
 t''w7 
 d&&ud3 
 d&&uk: 
  ! 
" D# 
$  $!$$UJ7' ..69 
: ; 	ra   c                   K   | j                          d{    | j                  |d      }g }d}g }||t        dt        |            D ]c  }||   }	| j	                  |	      }
| j                  |
      }| j                  ||      s|j                  |       |j                  |dz   |	z          e nddg}|j                  |       g }g }t        dt        |            D ]5  }||   }||dd	}|j                  |dz   |z          |j                  |       7 | j                  ||||       d{   }| j                  r|S | j                  ||||d
      S 7 @7 *w)a  
        watch all open positions

        https://coincatch.github.io/github.io/en/mix/#positions-channel

        :param str[]|None symbols: list of unified market symbols
 @param since
 @param 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   	positionsr   ::r  r  r  r   T)r   r7  r   r   r   get_private_inst_typer   r   r   r   filter_by_symbols_since_limit)r^   r   r  r  r   r   r   	instTypesr   r   r   rd   r   r   r   newPositionss                   r`   watch_positionszcoincatch.watch_positionsU  s     !!!%%gv6	1c'l+ ; V,55f=}}Xy9$$X.$$TD[6%9:; !'*I  &q#i.) 	A |H$#C
 ""4$;#9:KK	 "88Y]_eff??11,PUW[\\= 	"6 gs"   EEDE.E/(EEc                 8    |d   }|j                  d      dk\  ryy)Nr   rj   r   r  r  )r   )r^   r   rs   s      r`   r  zcoincatch.get_private_inst_type  s"    $<=="a'ra   c                    | j                   t               | _         | j                   }| j                  |dg       }t        |      }|dk(  ry g }g }t	        dt        |            D ]N  }||   }	| j                  |	      }
|j                  |
d          |j                  |
       |j                  |
       P d}| j                  ||      }t	        dt        |            D ]}  }||   }|j                  d      }|d   }| j                  ||      s1g }t	        dt        |            D ]!  }||   }
|
d   |k(  s|j                  |
       # |j                  ||        |j                  ||       y )Nr   r   r   r  r  r)  )r  r   r   r   r   parse_ws_positionr   r   splitr   r   )r^   r   r   cacherawPositions
dataLengthr  r   r   rawPositionpositionr   r   r   partsr   positionsForSymboljs                     r`   handle_positionszcoincatch.handle_positions  s~   F >>!57DNw;&
?q#l+, 	#A&q/K--k:HNN8H-.)LL"	# 00>q#m,- 
	@A'*K%%d+E1XF}}VW-%'"q#l"34 <A+AH)V3*11(;< 1;?
	@ 	|T*ra   c                    | j                  |d      }| j                  |d      }| j                  |||      }| j                  |d      }| j                  |d      }| j                  |ddd      }d }| j                  |d      }	|	d	k(  rd
}n|	dk(  rd}| j                  |d      }
t	        j
                  |
d      }| j                  |d      }| j                  i d|d   dd d|d| j                  |      d| j                  |d      dd d| j                  |d      d|d| j                  |d      d| j                  |d      d| j                  |d      dd d| j                  |d       d!| j                  |d!      d"| j                  |d#      d|d$|d | j                  |d%      |d | j                  |d&      | j                  |d'      d d d ||d(      S ))Nr   r  r  
marginModecrossisolated)crossedr  holdModedouble_holdTsingle_holdFuplRater  marginr   r|   r   	contractsr  contractSizerx  holdSider  leverageunrealizedPnluplrealizedPnlachievedProfits
collateral
entryPriceaverageOpenPricer	  liquidationPriceliqPxhedgedkeepMarginRate
marginRater  )maintenanceMarginmaintenanceMarginPercentageinitialMargininitialMarginPercentagemarginRatior  	lastPricer  r  r  r
  )
rl   ro   r"  get_supported_mappingr   r  r  safe_positionr  r  )r^   r  r   rs   r  r|   marginModeIdr  isHedgedr  percentageDecimalr  r  s                r`   r  zcoincatch.parse_ws_position  s   8 ##Hh7##Hl;&&xB%%h8	'',?//"?
 
 ##Hj9}$H&H ,,XyA''(95A
!!(H5!! #
fX&#
$#
 #
 Y/	#

 ))(G<#
 D#
 D**8Z@#
 #
 ))(J?#
 T--h>#
 4++H6GH#
 $#
 $**85GH#
 ))(K@#
  0 07 C#
  *!#
" h##
$ "&+/+;+;HFV+W#'+++HlC#'#4#4Xw#G!#$9#
  	ra   c                 0   | j                  |d      }	 |dk(  r| j                  |d      }| j                  dz   | j                  |      z   }| j                  | j                  d   d   ||       | j                  |dd      }| j                  | j                  d   d	   ||       t        |      y
# t        $ r[}t        |t              r0d}|j                  ||       ||j                  v r|j                  |= n|j                  |       Y d }~yd }~ww xY w)Neventr]  r  rZ  r#   rT   msgr   rU   Frv   T)rl   r   jsonthrow_exactly_matched_exceptionrZ   throw_broadly_matched_exceptionr   	Exception
isinstancer   r\  r   )	r^   r   r   r  r  feedbackr  er   s	            r`   handle_error_messagezcoincatch.handle_error_message  s      '2	''877S=499W+==44T__T5J75SUY[cd&&wr:44T__T5J75SUXZbc#H-- 	!01-a-&"6"66,,[9a 	s   BB1 1	D:ADDc                 6   | j                  ||      ry | j                  |d      }|dk(  r| j                  ||       y |dk(  r| j                  ||       y | j                  |d      }|dk(  r| j                  ||       y |dk(  r| j	                  ||       y |dk(  r| j                  ||       y | j                  |di       }| j                  |d      }|d	k(  r| j                  ||       |j                  d
      dk\  r| j                  ||       |j                  d      dk\  r| j                  ||       |dk(  r| j                  ||       |dk(  r| j                  ||       |dk(  s|dk(  r| j                  ||       |dk(  r| j                  ||       y y )Nr   pongr  ry   r   r   r   r   r   r  r   r2  rk  r  r  r  r  )r  rl   handle_pongr   handle_subscription_statushandle_un_subscription_statusr   r   r   r'  rX  rv  r  r  r  )r^   r   r   contentr  r   r   s          r`   handle_messagezcoincatch.handle_message-  s   $$VW5""7I6fVW-fVW-  '2G$$VW5K++FG<M!..vw?~~gub1""43hvw/<<!Q&fg.<< A%""673gvw/i0xW%<fg.k!!!&'2 "ra   c                      y)NrG    )r^   r   s     r`   rG   zcoincatch.pingS  s    ra   c                 0    | j                         |_        |S N)millisecondslastPongr^   r   r   s      r`   r  zcoincatch.handle_pongV  s    ++-ra   c                     |S r!  r  r$  s      r`   r  z$coincatch.handle_subscription_statusZ  s    ra   c                    | j                  |d      }|| j                  |di       g}t        dt        |            D ]  }||   }| j	                  |d      }|dk(  r| j                  ||       2|dk(  r| j                  ||       J|dk(  r| j                  ||       b|j                  d      st| j                  ||        |S )	Nr   r   r   r   r2  rk  r   r  )
r   r   r   r   rl   !handle_order_book_un_subscriptionhandle_trades_un_subscriptionhandle_ticker_un_subscription
startswithhandle_ohlcv_un_subscription)r^   r   r   argsListr   r   r   s          r`   r  z'coincatch.handle_un_subscription_status]  s    >>'62wr:;Hq#h-( 
	CA1+C&&sI6G'!66vwGG#2267CH$2267C##H-11&'B
	C ra   c                    | j                  |di       }| j                  |d      }|dk(  rdnd}| j                  |d      }| j                  |d d |      }|d   }d|d   z   }	d	|z   }
|| j                  v r| j                  |= |
|j
                  v r|j
                  |
= |	|j
                  v r|j
                  |	= t        | j                  d
z   |z         }|j                  ||
       |j                  d|	       y )Nr   rd   spr   r   rc   r   zunsubscribe:orderbook:z
orderbook:z orderbook T)
r   r  rl   r  r?  r   r   r   r\  r   r^   r   r   r   rd   r}  rc   r   r   r   subMessageHashr]  s               r`   r'  z+coincatch.handle_order_book_un_subscriptionn  s   nnWeR0))#z:"d*v!!#x0!!&$d;!.1AA%.T__$'V111$$^4&...$$[1 =!86!ABe^,t[)ra   c                    | j                  |di       }| j                  |d      }|dk(  rdnd}| j                  |d      }| j                  |d d |      }|d   }d|d   z   }	d	|z   }
|| j                  v r| j                  |= |
|j
                  v r|j
                  |
= |	|j
                  v r|j
                  |	= t        | j                  d
z   |z         }|j                  ||
       |j                  d|	       y )Nr   rd   r.  r   r   rc   r   zunsubscribe:trade:rl  z trades T)
r   r  rl   r  rn  r   r   r   r\  r   r/  s               r`   r(  z'coincatch.handle_trades_un_subscription  s   nnWeR0))#z:"d*v!!#x0!!&$d;!*VH-==!F*T[[ F#V111$$^4&...$$[1 :!5!>?e^,t[)ra   c                    | j                  |di       }| j                  |d      }|dk(  rdnd}| j                  |d      }| j                  |d d |      }|d   }d|d   z   }	d	|z   }
|| j                  v r| j                  |= |
|j
                  v r|j
                  |
= |	|j
                  v r|j
                  |	= t        | j                  d
z   |z         }|j                  ||
       |j                  d|	       y )Nr   rd   r.  r   r   rc   r   zunsubscribe:ticker:r   z ticker T)
r   r  rl   r  r   r   r   r   r\  r   r/  s               r`   r)  z'coincatch.handle_ticker_un_subscription  s   nnWeR0))#z:"d*v!!#x0!!&$d;!+fX.>>"V+T\\!V$V111$$^4&...$$[1 :!5!>?e^,t[)ra   c                    | j                  |di       }| j                  |d      }|dk(  rdnd}| j                  |d      }| j                  |d      }|j                  dd	      }| j                  | j                  d
      }	| j                  ||	      }
| j                  |d d |      }|d   }d|
z   dz   |d   z   }d|z   dz   |
z   }|| j                  v r!|
| j                  |   v r| j                  |   |
= | j                  |||       y )Nr   rd   r.  r   r   rc   r   r  r   rF   r   zunsubscribe:ohlcv:r   r  )	r   r  rl   replacerX   r   r  r!  clean_unsubscription)r^   r   r   r   rd   r}  rc   r   r  r  r   r   r   r   r0  s                  r`   r+  z&coincatch.handle_ohlcv_un_subscription  s   nnWeR0))#z:"d*v!!#x0""3	2??8R0^^DLL2CD
''*=	!!&$d;!*Y6<vh?OO!F*S09<T[[ DKK//KK'	2!!&.+Fra   r!  )I__name__
__module____qualname__r   r]   rt   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r
   r   listr  r  r'  r#  r   r0  r5  r/  rX  rF  rd  rA  r   ri  rh  rr  rv  ru  r   r  r  r   r  r  dictr  r   r  r  r  r  r  r  rG   r  r  r  r'  r(  r)  r+  __classcell__)r_   s   @r`   r    r       sZ   A# AF/ )+ 6 KM J ?A H LN J Y[ W&  ]_ W _a 
ES 
E3 
ERU 
Eeh 
E	"& 	"F # W] jm $ 68 O OF O. 9; OC Os O 6:" E7 E EB$:F $:LHT 8<$]ajl H HC HWZ Hquvzq{ H< ;?r Y3 Yc Y"(,6 (,T
F 
d 
( @DB PS P PS\ P =? M M3 M& SW_a !$s) !C !fo !<C= C=J*6 *3 *UX *
$
3 <@d[] S SS S Sbfglbm S OSaenp #Sd3i #S #S[^ #Suyz  vA #SJ 9; MC Ms M,F ,<E 4 *, P P642V 42l 04$UYbd 8U 8UC 8Us 8Uimnsit 8UtY%6 Y%v\D \& \E \| 8<$]ajl *]W *]C *]WZ *]quv~q *]XF ?+v ?+BKZ6 0$3V $3L6 &  F "* *&*F *&*F *&G6 Gra   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   r   r   r   r   r   ccxt.base.preciser   async_supportr    r  ra   r`   <module>rE     sb     A  A  ~ ~ ~ ~ 4  * 0 . ' ) . * - %`G"",, `Gra   