
    E#i                         S SK rS SKJrJrJr  S SKJrJrJ	r	J
r
JrJrJrJrJrJrJr  S SKJr  S SKJr  S SKJr  S SKJr   " S S	\R2                  R4                  5      rg)
    N)
ArrayCacheArrayCacheBySymbolByIdArrayCacheByTimestamp)AnyBalancesBoolIntOrder	OrderBookStrStringsTickerTickersTrade)Client)List)ExchangeError)ArgumentsRequiredc                     ^  \ rS rSrS\4U 4S jjr0 4S jr0 4S jrS r0 S4S jr	0 S4S	\
4S
 jjr0 S4S jr0 S4S	\
4S jjr0 4S\S\4S jjr0 4S\S\4S jjrS0 4S\S\4S jjrS\4S jrS0 4S\S\4S jjrS0 4S\S\4S jjrS\4S jrS?S jrSSS0 4S\S\S\S\S\\   4
S jjrS0 4S\S\S\\   4S jjrS\4S jr SS0 4S\S\S\S\\!   4S  jjr"SS0 4S\\   S\S\S\\!   4S! jjr#0 4S\\   S\4S" jjr$0 4S\S\4S# jjr%S\4S$ jr&S0 4S\S\S\'4S% jjr(0 4S\S\4S& jjr)S0 4S\\   S\S\'4S' jjr*0 4S\\   S\4S( jjr+S\4S) jr,S* r-S+ r.S, r/S\4S- jr0S\4S. jr1S\4S/ jr2SSS0 4S\3S\S\S\\4   4S0 jjr5S1 r6S?S2 jr7S\4S3 jr8SSS0 4S\3S\S\S\\!   4S4 jjr9S\4S5 jr:S?S6 jr;0 4S\<4S7 jjr=S\4S8 jr>S\4S9 jr?S\4S: jr@S\4S; jrAS\S\B4S< jrCS\4S= jrDS>rEU =rF$ )@kucoin   returnc                    > U R                  [        [        U ]  5       0 SS_SS_SS_SS_SS_SS_S	S_S
S_SS_SS_SS_SS_SS_SS_SS_SS_SS_SSSSSSSS.ESSS0SSSS.SS0S.SU R                  0S .5      $ )!NwsTcreateOrderWsFeditOrderWsfetchOpenOrdersWsfetchOrderWscancelOrderWscancelOrdersWscancelAllOrdersWswatchBidsAskswatchOrderBookwatchOrderswatchMyTradeswatchTickerswatchTickerwatchTradeswatchTradesForSymbolswatchOrderBookForSymbols)watchBalance
watchOHLCVunWatchTickerunWatchOHLCVunWatchOrderBookunWatchTradesunWatchhTradesForSymbols  namezmarket/snapshot      /market/level2)snapshotDelaysnapshotMaxRetriesmethodr9   /spotMarket/tradeOrders)tradesLimitr'   r#   r%   ping)hasoptions	streaming)deep_extendsuperr   describer<   )self	__class__s    I/home/james-whalen/.local/lib/python3.13/site-packages/ccxt/pro/kucoin.pyrB   kucoin.describe   sT   fd < >d u $U	
    !% $U   !$ t    t t  (!" +D#$ !%"!% $$(!%,016  $-  &'*+.# 7"$ 			S/A
 / /	    c                   #    U(       a  SOSnU R                  U R                  S0 5      nU R                  XC5      nUb
  UI S h  vN $ U R                  U R                  X5      XC'   X@R                  S'   XC   nUI S h  vN $  N> N7f)Nprivatepublicurls)
safe_valuer>   spawnnegotiate_helper)rC   privateChannelparams	connectIdrK   futures         rE   	negotiatekucoin.negotiateC   s     !/IX	t||VR81< **T%:%:NS#V|   s$   ABB9B>B?BBc                   #    S nU(       a  SOSn U(       a  U R                  U5      I S h  vN nOU R                  U5      I S h  vN nU R                  US0 5      nU R                  US/ 5      nU R                  US5      nU R                  US5      nU R	                  US5      n	U R	                  US5      n
U	S	-   U R                  U
UUS
.5      -   nU R                  U5      nXl        U$  N N! [         aJ  nU R                  U R                  S   U5      nUR                  U5        U R                  S   U	  S nAg S nAff = f7f)NrI   rJ   datainstanceServersr   pingIntervalendpointtoken?)rZ   rO   rQ   rK   )privatePostBulletPrivatepublicPostBulletPublicrL   safe_integersafe_string	urlencodeclient	keepAlive	Exceptionr>   reject)rC   rO   rP   responserQ   rV   rW   firstInstanceServerrX   rY   rZ   resultra   erR   s                  rE   rN   kucoin.negotiate_helperQ   sJ    !/IX	'	0!%!>!>v!FF& "&!<!<V!DD??8VR8D"ood4ErJO"&///1"E,,-@.QL''(;ZHH$$T73E^dnn"0&6 ' F
 [[(F+MC G& E  	0__T\\&%99EFMM!V$Y/		0sR   EC1 C-C1 C/B%C1 ,E-C1 /C1 1
E;A E ;E EEc                 |    U R                  U R                  U R                  SS5      S5      nXR                  S'   U$ )N	requestIdr      )sumr^   r>   )rC   rk   s     rE   
request_idkucoin.request_id~   s7    HHT..t||[!LaP	$-[!rG   Nc                   #    [        U R                  5       5      nUSUSS.nU R                  Xt5      nU R                  U5      n	X9R                  ;  a  X9R                  U'   U R                  XXU5      I S h  vN $  N7f)N	subscribeTidtypetopicre   )strrn   extendra   subscriptionswatch)
rC   urlmessageHashsubscriptionHashrP   subscriptionrk   requestmessagera   s
             rE   rq   kucoin.subscribe   sz     )*	%	
 ++g.S! $8$88.>  +ZZ'\ZZZZs   A6A?8A=9A?r}   c                 H   #    U R                  X/X4/XV5      I S h  vN $  N7fN)un_subscribe_multiple)rC   rz   r{   ru   r|   rP   r}   s          rE   un_subscribekucoin.un_subscribe   s&     //]EK]_etttts   " "c                 F  #    [        U R                  5       5      nUSUSS.nU R                  X5      n	U R                  U5      n
[	        S[        U5      5       H&  nXK   nXR                  ;  d  M  XR                  U'   M(     U R                  XXU5      I S h  vN $  N7f)Nrq   Trr   r   rv   rn   rw   ra   rangelenrx   watch_multiplerC   rz   messageHashesru   subscriptionHashesrP   r}   rk   r~   r   ra   ir|   s                rE   subscribe_multiplekucoin.subscribe_multiple   s     )*		
 ++g.S!q#012A14$(<(<<2B$$Y/ 3 ((WZfggggs   A,B!2(B!BB!c                 T  #    [        U R                  5       5      nUSUSS.nU R                  X5      n	Ub  XvU'   U R                  U5      n
[	        S[        U5      5       H&  nXK   nXR                  ;  d  M  XR                  U'   M(     U R                  XXU5      I S h  vN $  N7f)NunsubscribeTrr   r   r   r   s                rE   r   kucoin.un_subscribe_multiple   s     )*	!	
 ++g.#&/#S!q#012A14$(<(<<2B$$Y/ 3 ((WZfggggs   A3B(9(B(!B&"B(symbolc                 $  #    U R                  5       I Sh  vN   U R                  U5      nUS   nU R                  S5      I Sh  vN nU R                  USSS5      u  pVUS-   US   -   nS	U-   nU R	                  XHXv5      I Sh  vN $  Ns NF N7f)
a  
watches a price ticker, a statistical calculation with the information calculated over the past 24 hours for a specific market

https://www.kucoin.com/docs/websocket/spot-trading/public-channels/market-snapshot

:param str symbol: unified symbol of the market to fetch the ticker for
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict: a `ticker structure <https://docs.ccxt.com/#/?id=ticker-structure>`
Nr   Fr'   r9   /market/snapshot:rs   ticker:)load_marketsmarketrS   handle_option_and_paramsrq   )	rC   r   rP   r   rz   r9   queryru   r{   s	            rE   watch_tickerkucoin.watch_ticker   s      !!!V$!NN5))55fmXWijvd|+&(^^CeCCC 	" * Ds3   BB
.BB?BBBBBc                 J  #    U R                  5       I Sh  vN   U R                  U5      nUS   nU R                  S5      I Sh  vN nSnU R                  USSS5      u  pRUS-   US   -   nS	U-   nS
U-   nU/U/SSU/S.n	U R	                  XGXhX)5      I Sh  vN $  N NY N7f)a  
unWatches a price ticker, a statistical calculation with the information calculated over the past 24 hours for a specific market

https://www.kucoin.com/docs/websocket/spot-trading/public-channels/market-snapshot

:param str symbol: unified symbol of the market to fetch the ticker for
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict: a `ticker structure <https://docs.ccxt.com/#/?id=ticker-structure>`
Nr   Fr'   r9   r   r   rs   zunsubscribe:ticker:r   tradesTr   subMessageHashesru   r   symbols)r   r   rS   r   r   )
rC   r   rP   r   rz   r9   ru   r{   subMessageHashr}   s
             rE   un_watch_tickerkucoin.un_watch_ticker   s      !!!V$!NN5))66v}hXjkvd|++f4"V+)]!/ 0x
 &&sPVeee! 	" * fs4   B#B.B#BAB#B!B#B#!B#r   c                   #    U R                  5       I Sh  vN   U R                  U5      nSnSnU R                  USSS5      u  pB/ n/ nUb_  [        S[	        U5      5       HF  nX   nUR                  SU-   5        U R                  U5      n	UR                  US-   U	S	   -   5        MH     U R                  S
5      I Sh  vN n
SnUc3  US-   nU R                  XX5      I Sh  vN nU R                  (       a  U$ O_U R                  U5      nUS-   SR                  U5      -   nU R                  XXU5      I Sh  vN nU R                  (       a  0 nXUS   '   U$ U R                  U R                  SU5      $  GNc N N ND7f)a*  

https://www.kucoin.com/docs/websocket/spot-trading/public-channels/ticker

watches a price ticker, a statistical calculation with the information calculated over the past 24 hours for all markets of a specific list
:param str[] symbols: unified symbol of the market to fetch the ticker for
:param dict [params]: extra parameters specific to the exchange API endpoint
:param str [params.method]: either '/market/snapshot' or '/market/ticker' default is '/market/ticker'
:returns dict: a `ticker structure <https://docs.ccxt.com/#/?id=ticker-structure>`
Ntickersr&   r9   z/market/tickerr   r   r   rs   Fz:all,r   )r   market_symbolsr   r   r   appendr   rS   rq   
newUpdates
market_idsjoinr   filter_by_arrayr   )rC   r   rP   r{   r9   r   topicsr   r   r   rz   r   allTopic	marketIdssymbolsTopicnewDicts                   rE   watch_tickerskucoin.watch_tickers   s     !!!%%g.66v~xYij1c'l+ $$Y%78V,fslVD\9:	 ,
 NN5))?H NN3XNNG  0I!C<#((9*==L 33C^deeG "-4)*##DLL(GDD9 	" * O fsG   FE9B)F E<#F$E>%AF;F <>F<F>F Fra   c                    U R                  US5      nS nUbR  UR                  S5      nU R                  US5      nS nUS:X  a  U R                  US5      nOUnU R                  XtS5      nU R                  US0 5      nU R                  USU5      n	U R	                  X5      n
U
S   nXR
                  U'   S	U-   nUR                  X5        0 nXU'   UR                  US
5        g )Nru   r   rl   allsubject-rV   r   r   r   )r_   splitsafe_marketrL   parse_tickerr   resolve)rC   ra   r   ru   r   partsfirstmarketIdrV   	rawTickertickerr   r{   
allTickerss                 rE   handle_tickerkucoin.handle_ticker  s    n   '2KK$E$$UA.EH~++GY? %%h<Fw3OOD&$7	""95!%V&(v+
#6z9-rG   c                    #    U R                  SSX5      I Sh  vN nU R                  (       a  0 nX4US   '   U$ U R                  U R                  SU5      $  N=7f)ak  

https://www.kucoin.com/docs/websocket/spot-trading/public-channels/level1-bbo-market-data

watches best bid & ask for symbols
:param str[] symbols: unified symbol of the market to fetch the ticker for
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict: a `ticker structure <https://docs.ccxt.com/#/?id=ticker-structure>`
r"   z/spotMarket/level1:Nr   )watch_multi_helperr   r   bidsasks)rC   r   rP   r   r   s        rE   watch_bids_askskucoin.watch_bids_asks\  s]      ..@UW^gg??G(.F8$%N##DMM8WEE hs   AA>AchannelNamec                 t  #    U R                  5       I S h  vN   U R                  US SSS5      n[        U5      nUS:  a  [        U R                  S-   U-   S-   5      e/ n[        S[        U5      5       H/  nX7   nU R                  U5      n	UR                  SU	S   -   5        M1     U R                  S5      I S h  vN n
U R                  U5      nS	R                  U5      n[        U R                  5       5      nUS
X,-   SS.nU R                  X5      nU R                  XX5      I S h  vN $  GN Nv N	7f)NFTd    z#() accepts a maximum of 100 symbolsr   bidask@r   r   rq   rr   )r   r   r   r   rs   r   r   r   rS   r   r   rv   rn   rw   r   )rC   
methodNamer   r   rP   lengthr   r   r   r   rz   r   joinedrk   r~   r   s                   rE   r   kucoin.watch_multi_helperm  s0    !!!%%gtUD%HWC<#DGGcMJ$>Af$fggq#g,'AZF[[(F  VH-=!=> ( NN5))OOG,	)$)*	 )	
 ++g.((WTTT+ 	" * Us5   D8D1B&D8=D4>A.D8,D6-D84D86D8c                 x    U R                  U5      nUS   nX0R                  U'   SU-   nUR                  X55        g )Nr   r   )parse_ws_bid_askr   r   )rC   ra   r   parsedTickerr   r{   s         rE   handle_bid_askkucoin.handle_bid_ask  s@     ,,W5h' ,f&(|1rG   c                    U R                  US5      nUR                  S5      nUS   nU R                  XR5      nU R                  US5      nU R                  US0 5      nU R	                  US/ 5      nU R	                  US/ 5      n	U R                  US5      n
U R                  UU
U R                  U
5      U R                  US	5      U R                  US5      U R                  U	S	5      U R                  U	S5      US
.U5      $ )Nru   r   rl   r   rV   asksbids	timestampr   )r   r   datetimeask	askVolumebid	bidVolumeinfo)	r_   r   r   	safe_dict	safe_listr^   safe_tickeriso8601safe_number)rC   r   r   ru   r   r   r   rV   r   r   r   s              rE   r   kucoin.parse_ws_bid_ask  s     1C 8!!(3!!&(3~~ffb1nnT62.nnT62.%%dK8	"Y/##C+))#q1##C+))#q1	!
 	 		rG   1m	timeframesincelimitc                   #    U R                  5       I Sh  vN   U R                  S5      I Sh  vN nU R                  U5      nUS   nU R                  U R                  X"5      nSUS   -   S-   U-   n	SU-   S-   U-   n
U R                  XjX5      I Sh  vN nU R                  (       a  UR                  X5      nU R                  XUS	S
5      $  N N N>7f)a]  
watches historical candlestick data containing the open, high, low, and close price, and the volume of a market

https://www.kucoin.com/docs/websocket/spot-trading/public-channels/klines

:param str symbol: unified symbol of the market to fetch OHLCV data for
:param str timeframe: the length of time each candle represents
:param int [since]: timestamp in ms of the earliest candle to fetch
:param int [limit]: the maximum amount of candles to fetch
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns int[][]: A list of candles ordered, open, high, low, close, volume
NFr   /market/candles:rs   _candles:r   r   T)	r   rS   r   r_   
timeframesrq   r   getLimitfilter_by_since_limit)rC   r   r   r   r   rP   rz   r   periodru   r{   ohlcvs               rE   watch_ohlcvkucoin.watch_ohlcv  s      !!!NN5))V$!!!$//9H"VD\1C7&@ 6)C/);nnSuEE??NN61E))%q$GG 	") Fs2   CCCCA$CC;CCCc                 p  #    U R                  5       I Sh  vN   U R                  S5      I Sh  vN nU R                  U5      nUS   nU R                  U R                  X"5      nSUS   -   S-   U-   nSU-   S-   U-   nS	U-   S-   U-   n	U/U	/S
SU/S.n
U R                  XHXxX:5      I Sh  vN $  N N N7f)a  
unWatches historical candlestick data containing the open, high, low, and close price, and the volume of a market

https://www.kucoin.com/docs/websocket/spot-trading/public-channels/klines

:param str symbol: unified symbol of the market to fetch OHLCV data for
:param str timeframe: the length of time each candle represents
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns int[][]: A list of candles ordered, open, high, low, close, volume
NFr   r   rs   r   zunsubscribe:candles:r   r   r   Tr   )r   rS   r   r_   r   r   )rC   r   r   rP   rz   r   r   ru   r{   r   r}   s              rE   un_watch_ohlcvkucoin.un_watch_ohlcv  s      !!!NN5))V$!!!$//9H"VD\1C7&@,v5;iG#f,s2Y>)]!/ 0x
 &&sVbbb 	") cs2   B6B0B6B2A;B6+B4,B62B64B6c                    U R                  US0 5      nU R                  US5      nU R                  US/ 5      nU R                  US5      nUR                  S5      nU R                  US5      nU R                  U5      n	U R	                  U5      n
U
S   nSU-   S-   U	-   nU R                  U R
                  U0 5      U R
                  U'   U R                  U R
                  U   U	5      nUc9  U R                  U R                  S	S
5      n[        U5      nXR
                  U   U	'   U R                  XZ5      nUR                  U5        UR                  X5        g )NrV   r   candlesru   r   rl   r   r   
OHLCVLimitr2   )rL   r_   r   find_timeframer   ohlcvsr^   r>   r   parse_ohlcvr   r   )rC   ra   r   rV   r   r   ru   r   intervalr   r   r   r{   storedr   r   s                   rE   handle_ohlcvkucoin.handle_ohlcv  sB   * w3##D(3//$	26  '2C ##E1-''1	!!(+! 6)C/);"oodkk62FFV!4i@>%%dllL$GE*51F-3KK	*  1ev+rG   c                 F   #    U R                  U/X#U5      I Sh  vN $  N7f)a  
get the list of most recent trades for a particular symbol

https://www.kucoin.com/docs/websocket/spot-trading/public-channels/match-execution-data

: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)rC   r   r   r   rP   s        rE   watch_tradeskucoin.watch_trades  s$      22F8U6RRRRs   !!c                   #    [        U5      nUS:X  a  [        U R                  S-   5      eU R                  5       I Sh  vN   U R	                  U5      nU R                  U5      nU R                  S5      I Sh  vN n/ n/ n	SSR                  U5      -   n
[        S[        U5      5       H3  nX   nUR                  SU-   5        Xk   nU	R                  SU-   5        M5     U R                  XxXU5      I Sh  vN nU R                  (       a6  U R                  US5      nU R                  US5      nUR                  UU5      nU R                  XUS	S
5      $  GN N Nd7f)a  
get the list of most recent trades for a particular symbol

https://www.kucoin.com/docs/websocket/spot-trading/public-channels/match-execution-data

:param str[] symbols:
: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>`
r   z> watchTradesForSymbols() requires a non-empty array of symbolsNF/market/match:r   trades:r   r   T)r   r   rs   r   r   r   rS   r   r   r   r   r   rL   r_   r   r   )rC   r   r   r   rP   symbolsLengthr   rz   r   r   ru   r   r   r   r   r   tradeSymbols                    rE   r  kucoin.watch_trades_for_symbols  sP     GA#DGG.n$noo!!!%%g.OOG,	NN5)) 388I#66q#g,'AZF  V!34 |H%%&6&AB	 (
 ..s5^dee??OOFA.E**5(;KOOK7E))&TRR# 	" * fs6   =E E :E :E;A>E 9E:A E E E c                   #    U R                  5       I Sh  vN   U R                  USS5      nU R                  U5      nU R                  S5      I Sh  vN n/ n/ nSSR	                  U5      -   n[        S[        U5      5       H/  nX   n	UR                  SU	-   5        UR                  SU	-   5        M1     UUSS	US
.n
U R                  XEXuX*5      I Sh  vN $  N N N7f)a.  
unWatches trades stream

https://www.kucoin.com/docs/websocket/spot-trading/public-channels/match-execution-data

:param str symbols:
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict[]: a list of `trade structures <https://docs.ccxt.com/#/?id=public-trades>`
NFr  r   r   zunsubscribe:trades:r  r   Tr   )	r   r   r   rS   r   r   r   r   r   )rC   r   rP   r   rz   r   r   ru   r   r   r}   s              rE   un_watch_trades_for_symbols"kucoin.un_watch_trades_for_symbols>  s      !!!%%gtU;OOG,	NN5)) 388I#66q#g,'AZF  !6!?@%%i&&89 (
 + 2
 //EZ`ooo% 	" * ps4   C!C<C!CBC!CC!C!C!c                 D   #    U R                  U/U5      I Sh  vN $  N7f)a^  
unWatches trades stream

https://www.kucoin.com/docs/websocket/spot-trading/public-channels/match-execution-data

:param str symbol: unified symbol of the market to fetch trades for
: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)r  rC   r   rP   s      rE   un_watch_tradeskucoin.un_watch_trades\  s"      55vhGGGG     c                 N   U R                  US0 5      nU R                  U5      nUS   nSU-   nU R                  U R                  U5      nUc6  U R                  U R                  SS5      n[        U5      nXpR                  U'   UR                  U5        UR                  Xv5        g )NrV   r   r  r;   r2   )rL   parse_trader   r^   r>   r   r   r   )	rC   ra   r   rV   trader   r{   r   r   s	            rE   handle_tradekucoin.handle_tradeh  s    ( w3  &x&(f5>%%dllM4HE&F"(KKev+rG   c                 D   #    U R                  U/X#5      I Sh  vN $  N7f)a  

https://www.kucoin.com/docs/websocket/spot-trading/public-channels/level1-bbo-market-data
https://www.kucoin.com/docs/websocket/spot-trading/public-channels/level2-market-data
https://www.kucoin.com/docs/websocket/spot-trading/public-channels/level2-5-best-ask-bid-orders
https://www.kucoin.com/docs/websocket/spot-trading/public-channels/level2-50-best-ask-bid-orders

watches information on open orders with bid(buy) and ask(sell) prices, volumes and other data
: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
:param str [params.method]: either '/market/level2' or '/spotMarket/level2Depth5' or '/spotMarket/level2Depth50' default is '/market/level2'
: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)rC   r   r   rP   s       rE   watch_order_bookkucoin.watch_order_book  s"     < 66xOOOOr  c                 D   #    U R                  U/U5      I Sh  vN $  N7f)a~  

https://www.kucoin.com/docs/websocket/spot-trading/public-channels/level1-bbo-market-data
https://www.kucoin.com/docs/websocket/spot-trading/public-channels/level2-market-data
https://www.kucoin.com/docs/websocket/spot-trading/public-channels/level2-5-best-ask-bid-orders
https://www.kucoin.com/docs/websocket/spot-trading/public-channels/level2-50-best-ask-bid-orders

unWatches information on open orders with bid(buy) and ask(sell) prices, volumes and other data
: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 str [params.method]: either '/market/level2' or '/spotMarket/level2Depth5' or '/spotMarket/level2Depth50' default is '/market/level2'
:returns dict: A dictionary of `order book structures <https://docs.ccxt.com/#/?id=order-book-structure>` indexed by market symbols
N)un_watch_order_book_for_symbolsr  s      rE   un_watch_order_bookkucoin.un_watch_order_book  s"      996(FKKKKr  c                 4  #    [        U5      nUS:X  a  [        U R                  S-   5      eUb0  US:w  a*  US:w  a$  US:w  a  US:w  a  [        U R                  S-   5      eU R	                  5       I Sh  vN   U R                  U5      nU R                  U5      nU R                  S	5      I Sh  vN nSnU R                  US
SS5      u  psUS:X  d  US:X  a  S[        U5      -   nUS-   SR                  U5      -   n/ n	/ n
[        S[        U5      5       H6  nX   nU	R                  SU-   5        X[   nU
R                  US-   U-   5        M8     0 nUS:X  a  U R                  UUS.nU R                  XiXX>5      I Sh  vN nUR                  5       $  GN N N7f)a  

https://www.kucoin.com/docs/websocket/spot-trading/public-channels/level1-bbo-market-data
https://www.kucoin.com/docs/websocket/spot-trading/public-channels/level2-market-data
https://www.kucoin.com/docs/websocket/spot-trading/public-channels/level2-5-best-ask-bid-orders
https://www.kucoin.com/docs/websocket/spot-trading/public-channels/level2-50-best-ask-bid-orders

watches information on open orders with bid(buy) and ask(sell) prices, volumes and other data
:param str[] symbols: unified array of symbols
:param int [limit]: the maximum amount of order book entries to return
:param dict [params]: extra parameters specific to the exchange API endpoint
:param str [params.method]: either '/market/level2' or '/spotMarket/level2Depth5' or '/spotMarket/level2Depth50' default is '/market/level2'
:returns dict: A dictionary of `order book structures <https://docs.ccxt.com/#/?id=order-book-structure>` indexed by market symbols
r   zA watchOrderBookForSymbols() requires a non-empty array of symbolsN   r   2   r4   z? watchOrderBook 'limit' argument must be None, 5, 20, 50 or 100Fr#   r9   r6   /spotMarket/level2Depthr   r   
orderbook:)r9   r   r   )r   r   rs   r   r   r   r   rS   r   rv   r   r   r   handle_order_book_subscriptionr   r   )rC   r   r   rP   r  r   rz   r9   ru   r   r   r   r   r   r}   	orderbooks                   rE   r  #kucoin.watch_order_book_for_symbols  s     GA#DGG.q$qrr%3,Ub[uPQz#DGG.o$opp!!!%%g.OOG,	NN5))66v?OQY[klQJERK.U;Fsxx	22q#g,'AZF  !67 |H%%fslX&=>	 (
 %%=="L
 11#eagvv	  1 	" *( ws7   A0F2F3:F-F.CF<F=FFFc                 x  #    U R                  US5      nU R                  US5      nU R                  5       I Sh  vN   U R                  USS5      nU R	                  U5      nU R                  S5      I Sh  vN nSnU R                  USSS5      u  pbUS:X  d  US:X  a  S	[        U5      -   nUS
-   SR                  U5      -   n/ n/ n	[        S[        U5      5       H/  n
X   nUR                  SU-   5        U	R                  SU-   5        M1     UUSSU	S.nU R                  XXXxX,5      I Sh  vN $  N N N7f)aa  

https://www.kucoin.com/docs/websocket/spot-trading/public-channels/level1-bbo-market-data
https://www.kucoin.com/docs/websocket/spot-trading/public-channels/level2-market-data
https://www.kucoin.com/docs/websocket/spot-trading/public-channels/level2-5-best-ask-bid-orders
https://www.kucoin.com/docs/websocket/spot-trading/public-channels/level2-50-best-ask-bid-orders

unWatches information on open orders with bid(buy) and ask(sell) prices, volumes and other data
:param str[] symbols: unified array of symbols
:param dict [params]: extra parameters specific to the exchange API endpoint
:param str [params.method]: either '/market/level2' or '/spotMarket/level2Depth5' or '/spotMarket/level2Depth50' default is '/market/level2'
:returns dict: A dictionary of `order book structures <https://docs.ccxt.com/#/?id=order-book-structure>` indexed by market symbols
r   NFr#   r9   r6   r4   r'  r(  r   r   r   zunsubscribe:orderbook:r)  Tr+  )r   r   r   ru   r   )r^   omitr   r   r   rS   r   rv   r   r   r   r   r   )rC   r   rP   r   r   rz   r9   ru   r   r   r   r   r}   s                rE   r"  &kucoin.un_watch_order_book_for_symbols  sQ     !!&'267+!!!%%gtU;OOG,	NN5))66v?OQY[klQJERK.U;Fsxx	22q#g,'AZF  !9F!BC%%lV&;< (
 +  2
 //EZ`ooo- 	" *& ps4   8D:D4<D:7D68B7D:/D80D:6D:8D:c                 D   U R                  US5      nU R                  US5      nU R                  US5      nUR                  S5      nU R                  US5      nU R                  US5      nU R                  USU5      n	U R                  U	S S5      n
S	U
-   nUS
:X  a_  XR                  ;  a  U R                  5       U R                  U
'   OU R                  U
   nUR                  5         XR                  U
   S'   GOgXR                  ;  a  U R                  5       U R                  U
'   U R                  U
   nU R                  US5      nU R                  USS5      nUc  [        UR                  5      n[        UR                  R                  5       5      nS n[        S[        U5      5       HE  nUU   nUR                  U5      S:  d  M  UR                  U5      S:  d  M6  UR                  U   n  O   U R                  US5      nU R!                  SSS5      nUU:X  a  U R#                  U R$                  XU
U0 5        UR                  R'                  U5        g X:  a  g U R)                  U R                  U
   U5        UR+                  U R                  U
   U5        g )NrV   r   ru   r   rl   r   r   r   r)  level2noncesequenceEndr   r   r#   r7   r4   )rL   r_   r   safe_symbol
orderbooks
order_bookresetr^   safe_integer_2r   cachelistrx   keysr   findhandle_optionrM   load_order_bookr   handle_deltar   )rC   ra   r   rV   r   ru   
topicPartstopicSymboltopicChannelr   r   r{   r+  r2  deltaEndcacheLengthrx   r}   r   keyr   r7   s                         rE   handle_order_bookkucoin.handle_order_book  sb   R w/""7I6  '2[[%
&&z15''
A6##D(K@!!(D#6"V+hoo-*.//*;' OOF3	!06OOF#H-oo-*.//*;'/I%%i9E**4LH}!)//2 $V%9%9%>%>%@ A#q#m"45A'*C-2,9OST9T'-';';C'@	 6
 )),@ $ 2 23C_VW X-/JJt33V&RWY[\&&t,"$//&148tv.<rG   c                 D   U R                  US5      nU R                  US5      nU R                  US5      nXES-
  :  a  g[        S[        U5      5       H>  nX&   nU R                  US5      nU R                  US5      n	XHS-
  :  d  M5  XI:  d  M<  Us  $    [        U5      $ )Nr   r2  sequenceStartrl   r3  )rL   r^   r   r   )
rC   r+  r9  
firstDeltar2  firstDeltaStartr   delta
deltaStartrC  s
             rE   get_cache_indexkucoin.get_cache_indexc  s    __UA.
!!)W5++JHQ&&q#e*%AHE**5/BJ((>Ha'e.> & 5zrG   c                 P   U R                  USS5      nU R                  USU5      US'   X1S'   U R                  U5      US'   U R                  USU5      nU R                  US/ 5      nU R                  US/ 5      nUS   nUS   nU R	                  Xu5        U R	                  X5        g )	Ntimer   r3  r2  r   changesr   r   )r8  r^   r   rL   handle_bid_asks)	rC   r+  rM  r   rS  r   r   
storedBids
storedAskss	            rE   r?  kucoin.handle_deltaq  s    ''v{C	!..umYO	'!*+ $Y 7	*//%E:w3w3v&
v&
Z.Z.rG   c                     [        S[        U5      5       H'  nU R                  X#   5      nUR                  U5        M)     g )Nr   )r   r   parse_bid_ask
storeArray)rC   bookSidebidAsksr   bidAsks        rE   rT  kucoin.handle_bid_asks~  s7    q#g,'A''
3F' (rG   c                 4   U R                  US5      nU R                  US5      nUc2  U R                  US5      nU R                  0 U5      U R                  U'   g [        S[        U5      5       H&  nXW   nU R                  0 U5      U R                  U'   M(     g )Nr   r   r   r   )r^   rL   r_   r6  r5  r   r   )rC   ra   r   r}   r   r   r   r   s           rE   r*  %kucoin.handle_order_book_subscription  s    !!,8//,	:?%%lH=F&*oob%&@DOOF#1c'l+ *.//"e*D' ,rG   c                    U R                  US5      nX1R                  ;  a  g U R                  UR                  U5      nU R                  UR                  U5      nUR                  U	 U R                  US5      nUb	  U" XU5        U R                  USS5      nU(       an  U R	                  US/ 5      nU R	                  US/ 5      n	[        S[        U5      5       H  n
X   nX   nU R                  XU5        M     U R                  U5        g g )Nrs   r9   r   Fr   r   r   )	r_   rx   rL   	safe_boolr   r   r   clean_unsubscriptionclean_cache)rC   ra   r   rs   r|   r}   r9   isUnSubr   r   r   r{   subHashs                rE   handle_subscription_status!kucoin.handle_subscription_status  s
    gt,***++F,@,@"Ev';';=MN  $x86L1..}eD NN<"MM#~~l<NPRS1c-01+.*-))&;G 2 \* rG   c                     U$ r    rC   ra   r   s      rE   handle_system_statuskucoin.handle_system_status  s	     rG   c           	        #    U R                  5       I Sh  vN   U R                  USS5      nU R                  USS/5      nU R                  S5      I Sh  vN nU(       a  SOSnSS0nSn	Ub  U R	                  U5      n
U
S	   nU	S
-   U-   n	U R                  XiXpR                  X5      5      I Sh  vN nU R                  (       a  UR                  X5      nU R                  XX#S5      $  N N N=7f)a  
watches information on multiple orders made by the user

https://www.kucoin.com/docs/websocket/spot-trading/private-channels/private-order-change
https://www.kucoin.com/docs/websocket/spot-trading/private-channels/stop-order-event

: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 boolean [params.trigger]: trigger orders are watched if True
:returns dict[]: a list of `order structures <https://docs.ccxt.com/#/?id=order-structure>`
NstoptriggerTz/spotMarket/advancedOrdersr:   rO   ordersr   r   )
r   safe_value_2r.  rS   r   rq   rw   r   r   filter_by_symbol_since_limit)rC   r   r   r   rP   rp  rz   ru   r~   r{   r   rq  s               rE   watch_orderskucoin.watch_orders  s      !!!##FFI>6FI#67NN4((07,=Vd
 [[(FH%F%+f4K~~c{{7?[\\??OOF2E00tTT! 	" ) ]s4   C1C+?C1C-AC11C/2:C1-C1/C1c                 :    SSSSSSSS.nU R                  X!U5      $ )Nopenclosedcanceled	triggered)rw  filledmatchupdatery  cancel	TRIGGERED)r_   )rC   statusstatusess      rE   parse_ws_order_statuskucoin.parse_ws_order_status  s3    " $
 &99rG   c           	         U R                  US5      nU R                  U5      nU R                  USS5      nU R                  US5      nU R                  Xb5      nU R                  US5      nU R	                  US5      nUSL=(       a    US Ln	US:X  a	  U	(       a  S	nU R                  0 S
U_SUS   _SU R                  US5      _SU R                  US5      _SU_SU R                  U5      _SS _SU R                  US5      _SS _SS _SU R                  US5      _SU R                  USS5      _SU_SU_SU R                  US5      _SS _SS _U R                  US5      S US S S.EU5      $ )Nrt   	orderTime	createdAtr   	stopPricetriggerSuccessTrz  ry  r   rs   orderIdclientOrderId	clientOidr   r   lastTradeTimestamp	orderTypetimeInForcepostOnlysideprice
orderPricetriggerPriceamountsizecostaverage
filledSize)r{  	remainingr  feer   )	r_   r  r8  r   rL   
safe_orderr   safe_string_lowersafe_string_2)
rC   orderr   rawTyper  r   r   r  r  triggerFails
             rE   parse_ws_orderkucoin.parse_ws_order  s   L ""5&1++G4''{KH	##E84!!(3''{;0@A%T1Sd8Rk!{F  
E 
fX& 
 $""5)4 
 T--e[A	 

  
 Y/ 
 !$ 
 D**5+> 
 4 
  
 D**5&9 
 T''wE 
  
 L 
 d&&uf5 
  D! 
" t# 
$ &&ul;- 
. / 	rG   c                    SnU R                  US5      nU R                  US5      nUb  U R                  X5        U R                  U5      nU R                  US5      nU R                  US5      nU R                  US5      n	U	S Ln
U R                  c=  U R                  U R                  SS5      n[        U5      U l        [        U5      U l        U
(       a  U R                  OU R                  nU R                  UR                  U0 5      nU R                  X5      nUb  US	   S
:X  a  S
US	'   UR                  U5        UR                  X5        US-   U-   nUR                  X5        g )Nrq  rV   tradeIdr   rs   r  ordersLimitr2   r  rx  r   )rL   r_   handle_my_trader  rq  r^   r>   r   triggerOrdershashmapr   r   )rC   ra   r   r{   rV   r  parsedr   r  r  isTriggerOrderr   cachedOrdersrq  r  symbolSpecificMessageHashs                   rE   handle_orderkucoin.handle_order.  sS   * w/""43  1$$T*!!&(3""640v~>&d2;;%%dllM4HE07DK!7!>D-;t))!5!5vrB0X(*#+x F#|1$/#$5$>!|?rG   c           	        #    U R                  5       I Sh  vN   U R                  S5      I Sh  vN nSnU R                  USSS5      u  pdSS0nSnUb!  U R                  U5      n	U	S   nUS	-   U	S   -   nU R	                  XXX`R                  Xt5      5      I Sh  vN n
U R                  (       a  U
R                  X5      nU R                  XX#S5      $  N N N=7f)
a  
watches information on multiple trades made by the user

https://www.kucoin.com/docs/websocket/spot-trading/private-channels/private-order-change

:param str symbol: unified market symbol of the market trades were made in
:param int [since]: the earliest time in ms to fetch trades for
:param int [limit]: the maximum number of trade structures to retrieve
:param dict [params]: extra parameters specific to the exchange API endpoint
:param str [params.method]: '/spotMarket/tradeOrders' or '/spot/tradeFills' default is '/spotMarket/tradeOrders'
:returns dict[]: a list of `trade structures <https://docs.ccxt.com/#/?id=trade-structure>`
NTr%   r9   r:   rO   myTradesr   r   )	r   rS   r   r   rq   rw   r   r   rs  )rC   r   r   r   rP   rz   ru   r~   r{   r   r   s              rE   watch_my_tradeskucoin.watch_my_trades]  s      !!!NN4((55foxYrsd
 ![[(FH%F%+fX.>>K~~c{{7?[\\??OOF2E00tTT 	"( ]s2   CCCCA*CC:CCCc                    U R                   c-  U R                  U R                  SS5      n[        U5      U l         U R	                  US5      nU R                  U5      nU R                   nUR                  U5        SnUR                  U R                   U5        US-   US   -   nUR                  U R                   U5        g )Nr;   r2   rV   r  r   r   )r  r^   r>   r   r   parse_ws_trader   r   )	rC   ra   r   r   rV   r  r  r{   r  s	            rE   r  kucoin.handle_my_trade{  s    : == %%dllM4HE259DM~~gv.$$T*== t}}k2$/#$5x8H$H!t}}&?@rG   c                 z   U R                  US5      nU R                  X2S5      nUS   nU R                  US5      nU R                  US5      nU R                  US5      nU R                  US5      nU R                  US5      n	Uc$  U R                  US5      nU R                  US	5      n	U R                  US
5      n
U R                  USSS5      nUS   nU R                  US5      nU R                  US5      nU R                  UUU R	                  U5      UUU
UU R                  US5      UUU	S UUUS.S.U5      $ )Nr   r   r  r  r  
matchPrice	matchSizer  r  r  tsrR  gư>quotefeeRater  	liquidity)r  ratecurrency)r   r   r   r   rs   r  rt   takerOrMakerr  r  r  r  r  )r_   r   safe_integer_product_2
safe_trader   )rC   r  r   r   r   rt   r  r  r  r  r  r   feeCurrencyr  feeCosts                  rE   r  kucoin.parse_ws_trade  sn   R ##E84!!(C8!{3v.""5)4  5!!%5=$$UG4E%%eV4F  	2//tVXN	Wo""5)4""5%0"Y/ ,,UK@' 
$ % 	rG   c           	         #    U R                  5       I Sh  vN   U R                  S5      I Sh  vN nSnSS0nSnU R                  X%X0R                  XA5      5      I Sh  vN $  NN N7 N7f)aZ  
watch balance and get the amount of funds available for trading or funds locked in orders

https://www.kucoin.com/docs/websocket/spot-trading/private-channels/account-balance-change

:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict: a `balance structure <https://docs.ccxt.com/#/?id=balance-structure>`
NTz/account/balancerO   balance)r   rS   rq   rw   )rC   rP   rz   ru   r~   r{   s         rE   watch_balancekucoin.watch_balance  sm      !!!NN4(("d
  ^^Ce[[=YZZZ 	"( [s1   A+A%A+A'0A+ A)!A+'A+)A+c                    U R                  US0 5      nSnU R                  US5      nU R                  US5      nS nUb#  UR                  S5      nU R                  US5      nU R                  U R                  SSS	5      n	U R                  U R                  S
5      n
U R                  XS	5      nXR
                  ;  a  0 U R
                  U'   X0R
                  U   S'   U R                  US5      nXR
                  U   S'   U R                  U5      U R
                  U   S'   U R                  U5      nU R                  5       nU R                  US5      US'   U R                  US5      US'   U R                  US5      US'   XR
                  U   U'   U R                  U R
                  U   5      U R
                  U'   X:X  a   UR                  U R
                  U   U5        g g )NrV   r  r  relationEvent.r   r+   defaultTyper  accountsByTyper   rR  r   r   	availablefreeholdusedtotal)rL   r_   r   r  r>   r  r^   r   safe_currency_codeaccountsafe_balancer   )rC   ra   r   rV   r{   
currencyIdr  requestAccountTyperelationEventPartsselectedTyper  uniformTyper   coder  s                  rE   handle_balancekucoin.handle_balance  s   0 w3%%dJ7
((?!$!.!4!4S!9!%!1!12Da!H))$,,W^_7GH&&~7S||+(*DLL%,0[!&)%%dF3	1:[!+.04Y0G[!*-&&z2,,.**4=**48++D':*1[!$'$($5$5dll;6O$P[!&NN4<<4kB 'rG   c                    U R                  US5      nUS:X  a  U R                  X5        g U R                  US5      nU R                  U R                  U R                  U R                  U R                  U R                  U R
                  U R                  U R                  U R                  U R                  S.nU R                  XT5      nUb	  U" X5        g g )Nru   z/market/ticker:allr   )level1r1  ztrade.l2updateztrade.tickerztrade.snapshotztrade.l3matchztrade.candles.updatezaccount.balanceorderChange	stopOrderz/spot/tradeFills)
r_   r   r   rF  r  r  r  r  r  rL   )rC   ra   r   ru   r   methodsr9   s          rE   handle_subjectkucoin.handle_subject7  s    "   '2((v/""7I6)),,"44 .."00!..$($5$5#22,,** $ 4 4
 26# rG   c                 >    [        U R                  5       5      nUSS.$ )Nr<   )rs   rt   )rv   rn   )rC   ra   rs   s      rE   r<   kucoin.ping^  s%     "#
 	
rG   c                 .    U R                  5       Ul        g r   )millisecondslastPongrk  s      rE   handle_pongkucoin.handle_pongh  s    ++-rG   c                     U R                  USS5      nUS:X  a5  SnUR                  R                  S5      S:  a  SnS U R                  S   U'   U R	                  S	SUR                  S0 X20 0 5	        g
)NrV    ztoken is expiredrJ   zconnectId=privater   rI   rK   rl   F)r_   rz   r<  r>   handle_errors)rC   ra   r   rV   rt   s        rE   handle_error_messagekucoin.handle_error_messagel  sw     4%%Dzz23q8 )-DLL &1b&**b"dRLrG   c                     U R                  US5      nU R                  U R                  U R                  U R                  U R
                  S.nU R                  XC5      nUb	  U" X5        g g )Nrt   )welcomeackr   pongerror)r_   rl  rg  r  r  r  rL   )rC   ra   r   rt   r  r9   s         rE   handle_messagekucoin.handle_message~  sm    0 0022**$$..
 /6# rG   )r  rq  r  r   )G__name__
__module____qualname____firstlineno__r   rB   rS   rN   rn   rq   dictr   r   r   rv   r   r   r   r   r   r   r   r   r   r   r   r   r	   r   r:  r   r   r  r   r  r  r  r  r  r   r  r#  r  r"  rF  rO  r?  rT  r*  rg  rl  r   r
   rt  r  r  r  r  r  r  r   r  r  r  r<   r  r   r  r  __static_attributes____classcell__)rD   s   @rE   r   r      sb   0# 0d 68  =? +Z
 JLZ^ [ TVlp uei u ^`nr h  acy} hrv h$ 68 D DF D& 9; fC fv f8 6:" 'E7 'E 'ERL.F L.\ 8<B FW F7 F" Y]eg U Ug U02V 2** ?CQUdhqs H H H3 H^a Hx|  ~B  yC H2 BFb c3 c3 cUYZ^U_ c8(,6 (,T <@d[] S SS S Sbfglbm S OSaenp  Sd3i  S  S[^  Suyz  vA  SD LN pc pRU p< 9; 
HC 
Hs 
H,6 ,@ @DB PS P PS\ P@ =? L L3 L  SW_a -!$s) -!C -!fo -!^ PR &pT#Y &pVY &pPR= R=h/(
	EV 	E+ +46  04$UYbd U UC Us Uimnsit U@
:GR-@6 -@^ 37TX\eg UC Us URU Ulpqvlw U<'Af 'ARL\ *, [ [$1CV 1Cf%$V %$N
6 
.& .6 t $$V $ $rG   r   )ccxt.async_supportccxt ccxt.async_support.base.ws.cacher   r   r   ccxt.base.typesr   r   r   r	   r
   r   r   r   r   r   r   !ccxt.async_support.base.ws.clientr   typingr   ccxt.base.errorsr   r   async_supportr   rj  rG   rE   <module>r     sB     f f l l l l 4  * .{$T&& {$rG   