
    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Jr  S SKJr  S SKJr  S SKJr  S SKJr   " S S	\R4                  R6                  5      rg)
    N)
ArrayCacheArrayCacheBySymbolByIdArrayCacheByTimestamp)AnyBalancesBoolIntOrder	OrderBookPosition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 4S jr	0 4S jr
0 S	4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SAS jrS	0 4S\S\4S jjrS rS\S\4S jrS 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#S	S	0 4S\S$\S\ S\ S\!\(   4
S% jjr)S\4S& jr*S	0 4S\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.0 4S\!\   S\4S* jjr/S+ r0S, r1S\4S- jr2S. r3S\4S/ jr4S	S	S	0 4S\S\ S\ S\!\5   4S0 jjr6S1 r7SAS2 jr8S\4S3 jr90 4S\:4S4 jjr;S\4S5 jr<S\4S6 jr=S7 r>S\4S8 jr?SAS9\S\4S: jjr@S\4S; jrAS\4S< jrBS\S\C4S= jrDS\4S> jrES\4S? jrFS@rGU =rH$ )Bkucoinfutures   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0E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+U R                  0S,.5      $ )-NwsTwatchLiquidationsFwatchLiquidatinsForSymbolswatchMyLiquidationswatchMyLiquidationsForSymbolswatchTickerwatchTickerswatchBidsAskswatchTrades
watchOHLCVwatchOrderBookwatchOrderswatchBalancewatchPositionwatchPositionswatchPositionForSymbolswatchTradesForSymbolswatchOrderBookForSymbols1min5min15min30min1hour2hour4hour8hour12hour1day1week1month)1m3m5m15m30m1h2h4h8h12h1d1w1Mfuturemargin)swapcross        )snapshotDelaysnapshotMaxRetries)fetchPositionSnapshotawaitPositionSnapshot)
timeframesaccountsByTypetradesLimitr%   r(   ping)hasoptions	streaming)deep_extendsuperr   describerT   )self	__class__s    P/home/james-whalen/.local/lib/python3.13/site-packages/ccxt/pro/kucoinfutures.pyrZ   kucoinfutures.describe   st   mT C Ed#U -e &t	
 0 t    t d !$ t    !%  *5!" (#$ +D%, !  ""!!!!# !"  %%#  $%'*+#
 .2-1"E&V 			yBH
 B B	    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_valuerV   spawnnegotiate_helper)r[   privateChannelparams	connectIdrc   rF   s         r]   	negotiatekucoinfutures.negotiateV   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)Nra   rb   datainstanceServersr   pingIntervalendpointtoken?)rq   rg   ri   rc   )futuresPrivatePostBulletPrivatefuturesPublicPostBulletPublicrd   safe_integersafe_string	urlencodeclient	keepAlive	ExceptionrV   reject)r[   rg   rh   responseri   rm   rn   firstInstanceServerro   rp   rq   resultrx   erF   s                  r]   rf   kucoinfutures.negotiate_helperd   sJ    !/IX	'	0!%!E!Ef!MM& "&!C!CF!KK??8VR8D"ood4ErJO"&///1"E,,-@.QL''(;ZHH$$T73E^dnn"0&6 ' F
 [[(F+MC N& L  	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      )sumru   rV   )r[   r   s     r]   
request_idkucoinfutures.request_id   s7    HHT..t||[!LaP	$-[!r_   c                    #    [        U R                  5       5      nUSUSS.nU R                  Xu5      nSU0n	Uc  U	nOU R                  X5      nU R                  XXU5      I S h  vN $  N7f)N	subscribeTidtypetopicr|   r   )strr   extendwatch)
r[   urlmessageHashsubscriptionHashsubscriptionrh   r   requestmessagesubscriptionRequests
             r]   r   kucoinfutures.subscribe   s{     )*	%	
 ++g.)%
 .L;;':ILZZ'\ZZZZs   A#A,%A*&A,c                    #    [        U R                  5       5      nUSUSS.nU R                  XU R                  X5      XE5      I S h  vN $  N7f)Nr   Tr   )r   r   watch_multipler   )	r[   r   messageHashesr   subscriptionHashessubscriptionArgsrh   r   r   s	            r]   subscribe_multiple kucoinfutures.subscribe_multiple   s\     )*		
 ((T[[=Y[m  A  A  	A  As   AAA	ANr   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unsubscribeTr   r   )r   r   r   rx   rangelensubscriptionsr   )r[   r   r   r   r   rh   r   r   r   r   rx   ir   s                r]   un_subscribe_multiple#kucoinfutures.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SUS'   U R                  U/U5      I Sh  vN nXA   $  N> 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/futures-trading/public-channels/get-ticker

: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   r    callerMethodName)load_marketsmarketwatch_tickers)r[   r   rh   r   tickerss        r]   watch_tickerkucoinfutures.watch_ticker   se      !!!V$!%2!"**F8V<< 	" =s!   AA5AA	AAsymbolsc                    #    U R                  5       I Sh  vN   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      $  NX N?7f)ax  
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
:returns dict: a `ticker structure <https://docs.ccxt.com/#/?id=ticker-structure>`
Nr!   z/contractMarket/ticker:r   )r   watch_multi_request
newUpdatesfilter_by_arrayr   r[   r   rh   tickerr   s        r]   r   kucoinfutures.watch_tickers   sq      !!!//@Y[bkk??G(.F8$%N##DLL(GDD 	"ks   A3A/A3A1>A31A3rx   c                     U R                  US0 5      nU R                  US5      nU R                  US S5      nU R                  X55      nX`R                  US   '   UR	                  X`R                  SUS   5      5        g )Nrm   r   -r   )rd   safe_marketparse_tickerr   resolveget_message_hash)r[   rx   r   rm   marketIdr   r   s          r]   handle_tickerkucoinfutures.handle_ticker   sy    , w3??42!!(D#6""40)/VH%&v44Xvh?OPQr_   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)ae  

https://www.kucoin.com/docs/websocket/futures-trading/public-channels/get-ticker-v2

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/contractMarket/tickerV2:Nr   )r   r   r   bidsasksr   s        r]   watch_bids_askskucoinfutures.watch_bids_asks  s]      //A\^enn??G(.F8$%N##DMM8WEE os   AA>AchannelNamec                   #    U R                  5       I S h  vN   U R                  USU5      u  pUS:H  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       HF  nX8   n	U R                  U	5      n
U(       a  S	OS
nUR                  U R                  XS   5      5        MH     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SU0nU R                  XU R!                  UU5      UU5      I S h  vN $  GNQ N{ N	7f)Nr   r"   FTd    z#() accepts a maximum of 100 symbolsr   bidaskr   r   ,r   r   r   )r   handle_param_stringmarket_symbolsr   r   r   r   r   appendr   rj   
market_idsjoinr   r   r   r   )r[   
methodNamer   r   rh   
isBidsAskslengthr   r   r   r   prefixr   	marketIdsjoinedr   r   r   s                     r]   r   !kucoinfutures.watch_multi_request  st    !!!!55f>PR\]
 O3
%%gtUD%HWC<#DGGcMJ$>Af$fggq#g,'AZF[[(F!+XF  !6!6vh?O!PQ	 (
 NN5))OOG,	)$)*	 )	
 )
 ((T[[RX=Y[hjvwww5 	" * xs5   E.E'CE..E*/A3E."E,#E.*E.,E.c                     U R                  U5      nUS   nX0R                  U'   UR                  X0R                  SU5      5        g )Nr   r   )parse_ws_bid_askr   r   r   )r[   rx   r   parsedTickerr   s        r]   handle_bid_askkucoinfutures.handle_bid_ask/  sC    " ,,W5h' ,f|%:%:8V%LMr_   c                    U R                  US0 5      nU R                  US5      nU R                  XB5      nU R                  US5      nU R                  US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      $ )
Nrm   r   tsư>bestAskPricebestAskSizebestBidPricebestBidSize)r   	timestampdatetimeask	askVolumebid	bidVolumeinfo)	safe_dictrv   r   safe_integer_productsafe_tickeriso8601safe_number)r[   r   r   rm   r   r   r   s          r]   r   kucoinfutures.parse_ws_bid_askE  s    ~~ffb1##D(3!!(3!!&(3--dD(C	"Y/##D.9))$>##D.9))$>	!
 	 		r_   c           
      b  #    Uc  [        U R                  S-   5      eU R                  5       I Sh  vN   U R                  S5      I Sh  vN nU R	                  U5      nSUS   -   nSS0nSUS   -   nU R                  U5      nU R                  X5        U R                  S	S
S5      n	U R                  S	SS5      n
U R                  U5      nU	(       a(  U
(       a!  Uc  UR                  SU-   5      I Sh  vN nU$ U R                  X7USU R                  Xb5      5      I Sh  vN $  N N N5 N
7f)aF  
watch open positions for a specific symbol

https://docs.kucoin.com/futures/#position-change-events

:param str|None symbol: unified market symbol
:param dict params: extra parameters specific to the exchange API endpoint
:returns dict: a `position structure <https://docs.ccxt.com/en/latest/manual.html#position-structure>`
Nz+ watchPosition() requires a symbol argumentTz/contract/position:r   rg   	position:r   r(   rO   rP   fetchPositionSnapshot:)r   r   r   rj   r   rx   set_position_cachehandle_optionget_current_positionrF   r   r   )r[   r   rh   r   r   r   r   r   rx   rO   rP   currentPositionsnapshots                r]   watch_positionkucoinfutures.watch_positionV  s0     >#DGG.[$[\\!!!NN4((V$%t4d
 "F8$44S!/ $ 2 2?D[]a b $ 2 2?D[]a b33F; %:?V#]]+Cf+LMMHO^^CeT4;;wC_```! 	"( N`sF   /D/D'D/
D)B*D/5D+6,D/"D-#D/)D/+D/-D/c                     U R                   c  g U R                   R                  nU R                  X!0 5      n[        UR	                  5       5      nU R                  US5      $ Nr   )	positionshashmaprd   listvalues)r[   r   cachesymbolCacher   s        r]   r   "kucoinfutures.get_current_positiont  sR    >>!&&ooeR8k((*+vq))r_   c                     U R                  SSS5      nU(       aD  SU-   nXAR                  ;  a/  UR                  U5        U R                  U R                  XU5        g g g )Nr(   rO   Fr   )r   futuresrF   re   load_position_snapshot)r[   rx   r   rO   r   s        r]   r    kucoinfutures.set_position_cache|  s\     $ 2 2?D[]b c 2V;K>>1k*

466VT 2 !r_   c                   #    U R                  U5      I S h  vN n[        5       U l        U R                  nUR                  U5        UR                  U   nUR                  U5        UR                  USU-   5        g  Nf7f)Nr   )fetch_positionr   r   r   r  r   )r[   rx   r   r   positionr   rF   s          r]   r  $kucoinfutures.load_position_snapshot  si     ,,V44/1X,uxv!56 5s   B A>A'B c                    U R                  USS5      nUR                  S5      nU R                  US5      nU R                  US S5      nU R                  nU R	                  U5      nSU-   n	U R                  US0 5      n
U R                  U
5      n[        UR                  5       5      n[        S[        U5      5       H  nX   nX   b  M  X	 M     U R                  X5      nUR                  U5        UR                  X5        g )Nr    :r   r   rm   r   )rv   splitsafe_symbolr   r   rd   parse_positionr   keysr   r   r   r   r   )r[   rx   r   r   partsr   r   r   r   r   rm   newPositionr  r   keyr	  s                   r]   handle_positionkucoinfutures.handle_position  s    z   '26C ##E1-!!(D"533F;!F*w3))$/K$$&'q#d)$A'C'$ % ;;<Xx-r_   sincelimitc                 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://docs.kucoin.com/futures/#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)r[   r   r  r  rh   s        r]   watch_tradeskucoinfutures.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                  S5      I Sh  vN nU R	                  U5      nU R                  U5      nSSR                  U5      -   n/ n	/ n
[        S[        U5      5       H3  nX   nX{   nU
R                  SU-   5        U	R                  SU-   5        M5     U R                  XjXSU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, GN Ne7f)av  
get the list of most recent trades for a particular symbol
: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/contractMarket/execution:r   trades:r   r   T)r   r   r   r   r   rj   r   r   r   r   r   r   rd   rv   getLimitfilter_by_since_limit)r[   r   r  r  rh   symbolsLengthr   r   r   r   r   r   r   r   tradesfirsttradeSymbols                    r]   r  &kucoinfutures.watch_trades_for_symbols  sa     GA#DGG.n$noo!!!%%g.NN5))%%g.OOG,	,sxx	/BBq#g,'AZF |H  V!34%%&BX&MN	 (
 ..s5^bdjkk??OOFA.E**5(;KOOK7E))&TRR% 	") ls6   =E3E+ )E3)E.*B!E3E1A E3.E31E3c                 D   #    U R                  U/U5      I Sh  vN $  N7f)a6  
unWatches trades stream

https://docs.kucoin.com/futures/#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)un_watch_trades_for_symbolsr[   r   rh   s      r]   un_watch_tradeskucoinfutures.un_watch_trades+  s"      55vhGGGG     c                   #    U R                  5       I Sh  vN   U R                  USS5      nU R                  S5      I Sh  vN nU R                  U5      nU R                  U5      n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                  X7XWX*5      I Sh  vN $  N N N7f)z
get the list of most recent trades for a particular symbol
: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#  T)r   subMessageHashesr   r   r   )	r   r   rj   r   r   r   r   r   r   )r[   r   rh   r   r   r   r   r   r   r   r   s              r]   r(  )kucoinfutures.un_watch_trades_for_symbols7  s      !!!%%gtU;NN5))%%g.OOG,	,sxx	/BBq#g,'AZF  !6!?@%%i&&89 (
 + 2
 //EZ`ooo' 	")" ps4   C2C,+C2C.B$C2'C0(C2.C20C2c                 P   U R                  US0 5      nU R                  U5      nUS   nU R                  U R                  U5      nUc6  U R                  U R                  SS5      n[        U5      nX`R                  U'   UR                  U5        SU-   nUR                  Xh5        U$ )Nrm   r   rS   rJ   r  )rd   parse_trader#  ru   rV   r   r   r   )	r[   rx   r   rm   trader   r#  r  r   s	            r]   handle_tradekucoinfutures.handle_tradeS  s    * w3  &xf5>%%dllM4HE&F"(KKe&(v+r_   r9   	timeframec                   #    U R                  5       I Sh  vN   U R                  U5      nU R                  S5      I Sh  vN nU R                  U5      nU R	                  U R
                  S5      nU R                  XU5      n	SU-   S-   U	-   n
SU-   S-   U-   nU R                  XkU
SU5      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`  

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

watches historical candlestick data containing the open, high, low, and close price, and the volume of a market
: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
NFrQ   z/contractMarket/limitCandle:_ohlcv::r   T)r   r   rj   	market_idr   rV   rv   r   r   r   r!  )r[   r   r5  r  r  rh   r   r   rQ   timeframeIdr   r   ohlcvs                r]   watch_ohlcvkucoinfutures.watch_ohlcvu  s      !!!V$NN5))>>&)^^DLL,?
&&ziH.9C?+M&(3.:nnSudFKK??NN61E))%q$GG 	") Ls4   C2C,)C2 C.A0C21C02;C2.C20C2c           	         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 R                  S5      nU R	                  XW5      nU R                  US5      n	U R                  U	5      n
SU
-   S-   U-   nU R                  US5      nU R                  US	5      U R                  US5      U R                  US
5      U R                  US5      U R                  US5      U R                  US5      /nU R                  U R                  U
0 5      U R                  U
'   XR                  U
   ;  a8  U R                  U R                  SS5      n[        U5      U R                  U
   U'   U R                  U
   U   nUR                  U5        UR                  X5        g )Nr   r7  r   rm   rQ   r   r8  candlesr      rL         
OHLCVLimitrJ   )rv   r  r   rV   find_timeframer  	safe_listru   r   ohlcvsr   r   r   )r[   rx   r   r   r  r:  rm   rQ   r5  r   r   r   r;  parsedr  storeds                   r]   handle_ohlcvkucoinfutures.handle_ohlcv  s   *   '2C &&ua0~~gv.^^DLL,?
''@	##D(3!!(+&(3.:tY/eQ'UA&UA&UA&UA&UA&
 #nnT[[&"EF[[00%%dllL$GE-B5-IDKK	*V$Y/fv+r_   c                 D   #    U R                  U/X#5      I Sh  vN $  N7f)a  
     watches information on open orders with bid(buy) and ask(sell) prices, volumes and other data
1. After receiving the websocket Level 2 data flow, cache the data.
2. Initiate a REST request to get the snapshot data of Level 2 order book.
3. Playback the cached Level 2 data flow.
4. Apply the new Level 2 data flow to the local snapshot to ensure that the sequence of the new Level 2 update lines up with the sequence of the previous Level 2 data. Discard all the message prior to that sequence, and then playback the change to snapshot.
5. Update the level2 full data based on sequence according to the size. If the price is 0, ignore the messages and update the sequence. If the size=0, update the sequence and remove the price of which the size is 0 out of level 2. For other cases, please update the price.
6. If the sequence of the newly pushed message does not line up to the sequence of the last message, you could pull through REST Level 2 message request to get the updated messages. Please note that the difference between the start and end parameters cannot exceed 500.

     https://docs.kucoin.com/futures/#level-2-market-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
     :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  rh   s       r]   watch_order_bookkucoinfutures.watch_order_book  s"     " 66xOOOOr,  c                   #    [        U5      nUS:X  a  [        U R                  S-   5      eUb$  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S	R                  U5      -   nS
U0n/ n	/ n
[        S[        U5      5       H3  nX   nX[   nU
R                  SU-   5        U	R                  SU-   5        M5     U R                  XjXyX5      I Sh  vN nUR                  5       $  N N N7f)a  
watches information on open orders with bid(buy) and ask(sell) prices, volumes and other data

https://docs.kucoin.com/futures/#level-2-market-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
: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 symbolsNrK   r   z8 watchOrderBook 'limit' argument must be None, 20 or 100F/contractMarket/level2:r   r  
orderbook:)r   r   r   r   r   r   r   rj   r   r   r   r   r  )r[   r   r  rh   r"  r   r   r   r   r   r   r   r   r   	orderbooks                  r]   rL  *kucoinfutures.watch_order_book_for_symbols  sD     GA#DGG.q$qrr%3,#DGG.h$hii!!!%%g.OOG,	NN5)))CHHY,??U"
  q#g,'AZF |H  !67%%&?(&JK	 (
 11#eaqzz	  ! 	" * {s7   A$D?&D9':D?!D;"BD?$D=%D?;D?=D?c                 D   #    U R                  U/U5      I Sh  vN $  N7f)a  
unWatches information on open orders with bid(buy) and ask(sell) prices, volumes and other data

https://docs.kucoin.com/futures/#level-2-market-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
: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      r]   un_watch_order_book!kucoinfutures.un_watch_order_book  s"      996(FKKKKr,  c                   #    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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                  XGXWX*5      I Sh  vN $  N N N7f)aa  
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
:returns dict: A dictionary of `order book structures <https://docs.ccxt.com/#/?id=order-book-structure>` indexed by market symbols
NFrP  r   r   zunsubscribe:orderbook:rQ  TrR  )r   r   r   r   r.  )	r   r   r   rj   r   r   r   r   r   )r[   r   rh   r   r   r   r   r   r   r   r   s              r]   rU  -kucoinfutures.un_watch_order_book_for_symbols   s      !!!%%g.OOG,	NN5)))CHHY,??q#g,'AZF  !9F!BC%%lV&;< (
 +  2
 //EZ`ooo% 	" * ps4   CC:CCBCCCCCc                    U R                  US5      US'   U R                  US5      nX1S'   U R                  U5      US'   U R                  US0 5      nUR                  S5      nU R	                  US5      nU R                  US5      nU R	                  US	5      nUS
:X  a  SOSn	Xh/n
U	S:X  a  US   nUR                  U
5        g US   nUR                  U
5        g )Nsequencenoncer   r   changer   r   r   r@  buybidsasks)ru   r   rd   r  r   rv   
storeArray)r[   rR  deltar   r]  splitChangepricesidequantityr   value
storedBids
storedAskss                r]   handle_deltakucoinfutures.handle_delta  s    !..ujA	'%%e[9	!*+ $Y 7	*"5ll3'  a0Q/##K3%-vf!6>"6*J!!%("6*J!!%(r_   c                 d    [        S[        U5      5       H  nU R                  XU   5        M     g r   )r   r   rj  )r[   booksidedeltasr   s       r]   handle_deltaskucoinfutures.handle_deltas.  s)    q#f+&Ahq	2 'r_   c                    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 S5      nSU-   nXpR                  ;  aN  U R                  UR                  U0 5      n	U R                  U	S5      n
U R                  0 U
5      U R                  U'   U R                  U   nU R                  US5      nU R                  US	5      nUGc2  [        UR                  5      nUR                  S5      nU R                  US5      nU R                  US
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Xz0 5        UR                  R%                  U5        g X:  a  g U R'                  X5        UR)                  X5        g )Nrm   r   r  r   r   rQ  r  r\  r[  r   r%   rM      )rd   rv   r  r  
orderbooksr   r   ru   
order_bookr   r   r   r  r   findr   re   load_order_bookr   rj  r   )r[   rx   r   rm   r   
topicPartsr   r   r   r   r  storedOrderBookr\  deltaEndcacheLengthtopicPartsNewtopicSymboltopicChannelr   r   r   r  rM   s                          r]   handle_order_bookkucoinfutures.handle_order_book2  s#     w/  '2[[%
##J2!!(D#6"V+//)#~~f.B.BE2N%%&6@E&*oob%&@DOOF#//&1!!/7;$$T:6=o334K!KK,M**=!<K++M1=L !5!5!:!:!<=ML1c-01#A&HH[)Q.SXXl5Kq5P#)#7#7#<L	 2
 %%lG<E ../?RSTMm+

4//fUWX!!((./04r_   c                    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XHS-
  :  d  M#  Us  $    [        U5      $ )Nr   r\  r[  r   )rd   ru   r   r   )	r[   rR  r   
firstDeltar\  firstDeltaStartr   rb  
deltaStarts	            r]   get_cache_indexkucoinfutures.get_cache_indexf  s    __UA.
!!)W5++J
CQ&&q#e*%AHE**5*=JA~%	 &
 5zr_   c                     U$ N r[   rx   r   s      r]   handle_system_status"kucoinfutures.handle_system_statuss  s	     r_   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b  U R                  U5      n	U	S   nUS-   U-   nU R                  XXUSU 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 orders made by the user

https://docs.kucoin.com/futures/#trade-orders-according-to-the-market

:param str symbol: unified market symbol of the market orders were made in
:param int [since]: the earliest time in ms to fetch orders for
:param int [limit]: the maximum number of order structures to retrieve
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict[]: a list of `order structures <https://docs.ccxt.com/#/?id=order-structure>`
NTz/contractMarket/tradeOrdersrg   ordersr   r  )r   rj   r   r   r   r   r   filter_by_symbol_since_limit)r[   r   r  r  rh   r   r   r   r   r   r  s              r]   watch_orderskucoinfutures.watch_orders  s      !!!NN4((-d
 [[(FH%F%+f4K~~ctT[[QXEabb??OOF2E00tTT 	"( cs2   CB=CB?ACC:C?CCc                 6    SSSSSS.nU R                  X!U5      $ )Nopenclosedcanceled)r  filledmatchupdater  )rv   )r[   statusstatusess      r]   parse_ws_order_status#kucoinfutures.parse_ws_order_status  s-    "
 &99r_   c                 p   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 R                  US5      n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	                  X5      nUS
   nU R                  US5      nU R                  0 SU_S
U_SU_SU_SU_SU R                  U5      _SS _SU_SS _SS _SU_SU_SS _SU_SS _SS _SU_S U
S S S.EU5      $ )NorderId	clientOid	orderTyperd  
filledSizesizer   	orderTimer   r   re  r   r   clientOrderIdr   r   lastTradeTimestamptimeInForcepostOnly	stopPriceamountcostaverager  )	remainingr  feer#  )rv   safe_string_lowerr  r   r   
safe_orderr   )r[   orderr   r   r  r  rd  r  r  rawTyper  r   r   r   re  s                  r]   parse_ws_orderkucoinfutures.parse_ws_order  s   $ eY/((<**5+>	  0!!%6!!%0""5&1++G4--e[(K	##E84!!(3!%%eV4  
E 
f 
 " 
 ]	 

  
 Y/ 
 !$ 
 I 
 4 
  
 D 
 U 
  
 f 
 D 
  t! 
" f# 
$ + 
, - 	r_   c                 |   SnU R                  US5      nU R                  U5      nU R                  US5      nU R                  US5      nUb  U R                  c-  U R	                  U R
                  SS5      n[        U5      U l        U R                  n	U R                  U	R                  U0 5      n
U R                  X5      nUb'  U R                  US5      nUb  XS'   US   S	:X  a  S	US'   U	R                  U5        UR                  U R                  U5        US
-   U-   nUR                  U R                  U5        g g )Nr  rm   r   r   ordersLimitrJ   r  r  r  r  )
rd   r  rv   r  ru   rV   r   r   r   r   )r[   rx   r   r   rm   rG  r   r  r  cachedOrdersr  r  r  symbolSpecificMessageHashs                 r]   handle_orderkucoinfutures.handle_order  s0   w/$$T*!!&(3""640{{"))$,,tL4U;;;L__\%9%962FFOOF4E  OOE;?	(*3;'?h.'/F8$'NN4;;4(3c(9F(B%NN4;;(AB# r_   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U R                  0nSnU R                  X&X5U R	                  XA5      5      I Sh  vN $  N] NF N7f)a7  
watch balance and get the amount of funds available for trading or funds locked in orders

https://docs.kucoin.com/futures/#account-balance-events

:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict: a `balance structure <https://docs.ccxt.com/#/?id=balance-structure>`
NTz/contractAccount/walletrg   methodbalance)r   rj   handle_balance_subscriptionr   r   )r[   rh   r   r   r   r   r   s          r]   watch_balancekucoinfutures.watch_balance  s      !!!NN4(()d
 d66
  ^^Ce4;;W^Kghhh 	"( is1   A:A4A:A6?A:/A80A:6A:8A:c                    U R                  US0 5      nX0R                  S'   U R                  US5      nU R                  U5      nU R	                  5       nU R                  US5      US'   U R                  US5      US'   X`R                  U'   U R                  U R                  5      U l        UR                  U R                  S5        g )	Nrm   r   currencyavailableBalancefreeholdBalanceusedr  )rd   r  rv   safe_currency_codeaccountsafe_balancer   )r[   rx   r   rm   
currencyIdcoder  s          r]   handle_balancekucoinfutures.handle_balance  s    " w3#V%%dJ7
&&z2,,.**41CD**4?$T((6t||Y/r_   c                 <    U R                  U R                  X5        g r  )re   fetch_balance_snapshot)r[   rx   r   r   s       r]   r  )kucoinfutures.handle_balance_subscription'  s    

4..@r_   c                 L  #    U R                  5       I S h  vN   U R                  5         SnU R                  U R                  SSS5      nSU0nU R	                  U5      I S h  vN n[        UR                  5       5      n[        S[        U5      5       HH  nXx   n	U	S:w  d  M  U	S:w  d  M  U	S	:w  d  M  U	S
:w  d  M'  U	S:w  d  M/  U	S:w  d  M7  Xi   U R                  U	'   MJ     U R                  US0 5      U R                  S'   UR                  U R                  U5        g  GN	 N7f)Nr  r'   defaultTyperH   r   r   r  r  totalr   r   r   )r   check_required_credentialssafe_string_2rV   fetch_balancer   r  r   r   r  rd   r   )
r[   rx   r   r   selectedTyperh   r   r  r   r  s
             r]   r  $kucoinfutures.fetch_balance_snapshot*  s    !!!'')))$,,W]^L
 ++F33D HMMO$q#d)$A7Dv~$&.TW_Q\I\aeisasx|  AG  yG%-^T" %  $xDVt||[1_ 	" 4sG   D$DAD$#D"$=D$%D$-D$5D$=D$D$AD$"D$c                 Z   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                  XC5      nUb	  U" X5        g g )Nsubject)level2r   zcandle.sticktickerV2zavailableBalance.changer  orderChangeorderUpdatedzposition.changezposition.settlementzposition.adjustRiskLimit)
rv   r~  r   rI  r   r  r3  r  r  rd   )r[   rx   r   r  methodsr  s         r]   handle_subjectkucoinfutures.handle_subject\  s     ""7I6,,(( --++'+':':&&,, --#33#'#7#7(,(<(<
 26# r_   elementNamec                 "    Ub  US-   U-   $ US-   $ )N@zs@allr  )r[   r  r   s      r]   r   kucoinfutures.get_message_hash{  s#    $v--((r_   c                 >    [        U R                  5       5      nUSS.$ )NrT   )r   r   )r   r   )r[   rx   r   s      r]   rT   kucoinfutures.ping  s%     "#
 	
r_   c                 0    U R                  5       Ul        U$ r  )millisecondslastPongr  s      r]   handle_pongkucoinfutures.handle_pong  s    ++-r_   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
)Nrm   r  ztoken is expiredrb   zconnectId=privater   ra   rc   r   T)rv   r   ru  rV   handle_errors)r[   rx   r   rm   r   s        r]   handle_error_message"kucoinfutures.handle_error_message  sw     4%%Dzz23q8 )-DLL &1b&**b"dRLr_   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 )Nr   r  r   Fr   r.  r   )	rv   r   rd   	safe_boolrE  r   r   clean_unsubscriptionclean_cache)r[   rx   r   r   r   r   r  isUnSubr   r.  r   r   subHashs                r]   handle_subscription_status(kucoinfutures.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 \* r_   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 )Nr   )welcomer   pongerrorack)rv   r  r  r  r  r  rd   )r[   rx   r   r   r  r  s         r]   handle_messagekucoinfutures.handle_message  sm    0 00**$$..22
 /6# r_   )r  r  r   r  )I__name__
__module____qualname____firstlineno__r   rZ   rj   rf   r   r   r   dictr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r	   r   r   r  r  r*  r(  r3  r   r<  rI  r   rM  rL  rV  rU  rj  ro  r~  r  r  r
   r  r  r  r  r   r  r  r  r  r  r   rT   r  r   r  r  r  __static_attributes____classcell__)r\   s   @r]   r   r      s   C# CJ 68  =? +Z
 XZ [$ pr A acy} hrv h$ 68  F " 6:" E7 E ERF R: 8<B FW F7 F" Z^fh x xw x:NV N," 26b a3 aX a<*U U U7m.f m.^ <@d[] S SS S Sbfglbm S OSaenp Sd3i S S[^ Suyz  vA S@ 9; 
HC 
Hs 
H LN pc pRU p8 6  D ?CQUdhqs H H H3 H^a Hx|  ~B  yC H4-,6 -,^ @DB PS P PS\ P& SW_a !!$s) !!C !!fo !!F =? 
L 
L3 
L PR pT#Y pVY p6)&325 25h6  04$UYbd U UC Us Uimnsit U8:5nC6 C2 *, i i*0V 08A& A02d$V $>)C ) )
6 
& 
6 t $+ +4$V $ $r_   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   r   !ccxt.async_support.base.ws.clientr   typingr   ccxt.base.errorsr   r   async_supportr   r  r_   r]   <module>r     sB     f f v v v v 4  * .z$D&&44 z$r_   