
    ":h]                    &   d dl Zd dlmZmZmZmZ d dlZd dlm	Z	m
Z
mZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZ d dlmZ d dlmZ d dlm Z  d dlm!Z! d dlm"Z" d d	lm#Z# d d
lm$Z$ d dlm%Z% d dl&m'Z'  G d dejP                  jR                        Z)y)    N)
ArrayCacheArrayCacheBySymbolByIdArrayCacheBySymbolBySideArrayCacheByTimestamp)AnyBalancesIntLiquidationMarket
MarketTypeNumOrder	OrderBookOrderRequest	OrderSide	OrderTypePositionStrStringsTickerTickersTrade)Client)List)ExchangeError)AuthenticationError)ArgumentsRequired)
BadRequest)NotSupported)ChecksumError)Precisec                       e Zd Zdef fdZdi fdedededede	f
d	Z
i fd
ee   fdZdi fdefdZdi fdedefdZddi fdedededede	de	fdZdi fdedefdZdddi fdedededee   fdZdddi fdedededee   fdZdddi fdedededefdZdi fdededefdZi fdedefdZdefdZdefdZd Zd Zd Z i fdede!fdZ"di fd e#de$fd!Z%defd"Z&di fd e#de$fd#Z'defd$Z(ddi fd e#d%ede$fd&Z)d'edefd(Z*ddi fdedededee+   fd)Z,ddi fd ee   dededee+   fd*Z-i fd ee   defd+Z.i fdedefd,Z/defd-Z0d.ddi fdedededee1   fd/Z2defd0Z3dddi fdedededee+   fd1Z4defd2Z5i fde6fd3Z7defd4Z8dddi fd e#dededee9   fd5Z:dUded e#fd6Z;d7 Z<d8 Z=dddi fdedededee   fd9Z>defd:Z?ddi fdedededee@   fd;ZAdddi fd ee   dededee@   fd<ZBdefd=ZCdUd>ZDdefd?ZEdUdefd@ZFdefdAZGdefdBZHdCeIfdDZJdefdEZKdF ZLdGeMfdHZNdVdeOfdIZPdJefdKZQdL ZRi dfdMZSi fdNZTi fdOZUdP ZVdefdQZWdUdRefdSZXdVdTZY xZZS )Wgatereturnc                    | j                  t        t        |          i ddddddddddddd	dd
dddddddddddddddddddi ddddddddddddddddddddddddd dd!dd"dd#dd$ddddd%d&d'd(d)d*d+d,d*d-d.d*d/d0d1d*d0d1d*d2d3d*d4d5d6d6i i i d7d8id9d:d;dd<d=d>d?ddd@dA	dt        t        t
        t
        t
        dBi dCidD      S )ENwsTcancelAllOrdersWscancelOrderWscreateMarketBuyOrderWithCostWscreateMarketOrderWscreateMarketOrderWithCostWsFcreateMarketSellOrderWithCostWscreateOrderWscreateOrdersWscreatePostOnlyOrderWscreateReduceOnlyOrderWscreateStopLimitOrderWscreateStopLossOrderWscreateStopMarketOrderWscreateStopOrderWscreateTakeProfitOrderWscreateTriggerOrderWseditOrderWsfetchOrderWsfetchOrdersWsfetchOpenOrdersWsfetchClosedOrdersWswatchOrderBookwatchBidsAskswatchTickerwatchTickerswatchTradeswatchTradesForSymbolswatchMyTrades
watchOHLCVwatchBalancewatchOrderswatchLiquidationswatchLiquidationsForSymbols)watchMyLiquidationswatchMyLiquidationsForSymbolswatchPositionszwss://ws.gate.io/v4zwss://api.gateio.ws/ws/v4/z wss://fx-ws.gateio.ws/v4/ws/usdtzwss://fx-ws.gateio.ws/v4/ws/btc)usdtbtcz)wss://fx-ws.gateio.ws/v4/ws/delivery/usdtz(wss://fx-ws.gateio.ws/v4/ws/delivery/btcz"wss://op-ws.gateio.live/v4/ws/usdtz!wss://op-ws.gateio.live/v4/ws/btc)r&   spotswapfutureoptionz(wss://fx-ws-testnet.gateio.ws/v4/ws/usdtz'wss://fx-ws-testnet.gateio.ws/v4/ws/btcz*wss://op-ws-testnet.gateio.live/v4/ws/usdtz)wss://op-ws-testnet.gateio.live/v4/ws/btc)rN   rO   rP   )apitest  nametickers100ms
      )intervalsnapshotDelaysnapshotMaxRetrieschecksumrK   zspot.balances)settlerM   )fetchPositionsSnapshotawaitPositionsSnapshot)	tradesLimit
OHLCVLimitwatchTradesSubscriptionswatchTickerSubscriptionswatchOrderBookSubscriptionsr>   r<   rD   rJ   )124611)exactbroad)hasurlsoptions
exceptions)deep_extendsuperr#   describer   r   )self	__class__s    G/var/www/html/turnos/venv/lib/python3.12/site-packages/ccxt/pro/gate.pyrr   zgate.describe   s   dD : <&d&#T&  & 1$	&
 &t& .u& 25&  & !$& (& *4& )$& (& *5& $T&  *4!&" '#&$ t%&& '&(  )&* $T+&, &t-&. !$/&0  1&2 t3&4 5&6 t7&8 (9&:  ;&< d=&> ?&@ tA&B $UC&D .uE&F (,15"&K&R 08 B@
 !LI
 !EB$ !KH
 !KH
 !MJ#B  $",.,./1I  !(%'*+ $	# %+!
 /3.2#'2 ''001  	Al?
 l l	    Nsymboltypesideamountpricec                   K   | j                          d{    | j                  |      }|d   }| j                  |      }|dz   }	| j                  |      }
d|d<   | j	                  ||||||      }| j                  |
|       d{    | j                  |
||	       d{   }| j                  ||      }|S 7 7 57 w)a	  

        https://www.gate.io/docs/developers/apiv4/ws/en/#order-place
        https://www.gate.io/docs/developers/futures/ws/en/#order-place

        Create an order on the exchange
        :param str symbol: Unified CCXT market symbol
        :param str type: 'limit' or 'market' *"market" is contract only*
        :param str side: 'buy' or 'sell'
        :param float amount: the amount of currency to trade
        :param float [price]: *ignored in "market" orders* the price at which the order is to be fulfilled at in units of the quote currency
        :param dict [params]:  extra parameters specific to the exchange API endpoint
        :param float [params.stopPrice]: The price at which a trigger order is triggered at
        :param str [params.timeInForce]: "GTC", "IOC", or "PO"
        :param float [params.stopLossPrice]: The price at which a stop loss order is triggered at
        :param float [params.takeProfitPrice]: The price at which a take profit order is triggered at
        :param str [params.marginMode]: 'cross' or 'isolated' - marginMode for margin trading if not provided self.options['defaultMarginMode'] is used
        :param int [params.iceberg]: Amount to display for the iceberg order, Null or 0 for normal orders, Set to -1 to hide the order completely
        :param str [params.text]: User defined information
        :param str [params.account]: *spot and margin only* "spot", "margin" or "cross_margin"
        :param bool [params.auto_borrow]: *margin only* Used in margin or cross margin trading to allow automatic loan of insufficient amount if balance is not enough
        :param str [params.settle]: *contract only* Unified Currency Code for settle currency
        :param bool [params.reduceOnly]: *contract only* Indicates if self order is to reduce the size of a position
        :param bool [params.close]: *contract only* Set to close the position, with size set to 0
        :param bool [params.auto_size]: *contract only* Set side to close dual-mode position, close_long closes the long side, while close_short the short one, size also needs to be set to 0
        :param int [params.price_type]: *contract only* 0 latest deal price, 1 mark price, 2 index price
        :param float [params.cost]: *spot market buy only* the quote quantity that can be used alternative for the amount
        :returns dict|None: `An order structure <https://docs.ccxt.com/#/?id=order-structure>`
        Nrw   z.order_placeTtextIsRequired)load_marketsmarketget_type_by_marketget_url_by_marketcreate_order_requestauthenticaterequest_privateparse_order)rs   rw   rx   ry   rz   r{   paramsr   messageTypechannelurlrequestrawOrderorders                 ru   create_order_wszgate.create_order_ws   s     < !!!V$!--f5.$$V,#' ++FD$vV[111--c7GDD  62 	" 	2Ds4   CB<A1CB>	C#C $C>C Cordersc                   K   | j                          d{    | j                  ||      }|d   }| j                  |d         }|d   durt        | j                  dz         | j                  |      }|dz   }| j                  |      }| j                  ||       d{    | j                  |||       d{   }	| j                  |	|      S 7 7 37 w)a  
        create a list of trade orders

        https://www.gate.io/docs/developers/futures/ws/en/#order-batch-place

        :param Array orders: list of orders to create, each object should contain the parameters required by createOrder, namely symbol, type, side, amount, price and params
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: an `order structure <https://docs.ccxt.com/#/?id=order-structure>`
        Nr   rw   rN   Tz1 createOrdersWs is not supported for swap marketsz.order_batch_place)
r~   createOrdersRequestr   r   idr   r   r   r   parse_orders)
rs   r   r   r   
firstOrderr   r   r   r   	rawOrderss
             ru   create_orders_wszgate.create_orders_ws   s      !!!**66:AY
Z12&>%tww)\\]]--f5 44$$V,[111..sGWEE	  F33 	" 	2Es4   CCB
C!C"C<C=CCCc                 r  K   | j                          d{    |dn| j                  |      }| j                  |dd      }| j                  |      }|dz   }| j	                  |dd|      \  }}| j                  |      }| j                  |ddg      }| j                  d||      \  }}	|dk(  r| j                  |||	      n| j                  |||	      \  }
}| j                  ||       d{    | j                  || j                  |
|      |       d{   }| j                  ||      S 7 7 D7 w)	a  
        cancel all open orders

        https://www.gate.io/docs/developers/futures/ws/en/#cancel-all-open-orders-matched
        https://www.gate.io/docs/developers/apiv4/ws/en/#order-cancel-all-with-specified-currency-pair

        :param str symbol: unified market symbol, only orders in the market of self symbol are cancelled when symbol is not None
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :param str [params.channel]: the channel to use, defaults to spot.order_cancel_cp or futures.order_cancel_cp
        :returns dict[]: a list of `order structures <https://docs.ccxt.com/#/?id=order-structure>`
        Nstoptriggerz.order_cancel_cpr'   r   cancelAllOrdersrM   )r~   r   safe_bool_2r   handle_option_and_paramsr   omithandle_market_type_and_paramsmultiOrderSpotPrepareRequestprepareRequestr   r   extendr   )rs   rw   r   r   r   r   r   r   rx   queryr   requestParamsr   s                ru   cancel_all_orders_wszgate.cancel_all_orders_ws   sc     !!! .t{{6/B""669=--f5 2277@SU^`gh$$V,6FI#67889JFTZ[e_cgm_m!B!B67TY!Ztx  uH  uH  IO  QU  W\  u][111..sDKK4WY`aa	  F33 	" 	2as4   D7D0CD7.D3/*D7D5D73D75D7r   c                 n  K   | j                          d{    |dn| j                  |      }| j                  |g dd      }| j                  |g d      }| j	                  d||      \  }}|dk(  s|dk(  r| j                  |||      n| j                  |||      \  }}	| j                  |      }
|
dz   }| j                  |      }| j                  ||
       d{    t        |      |d<   | j                  || j                  ||	      |       d{   }| j                  ||      S 7 7 R7 w)	a  
        Cancels an open order

        https://www.gate.io/docs/developers/apiv4/ws/en/#order-cancel
        https://www.gate.io/docs/developers/futures/ws/en/#order-cancel

        :param str id: Order id
        :param str symbol: Unified market symbol
        :param dict [params]: Parameters specified by the exchange api
        :param bool [params.trigger]: True if the order to be cancelled is a trigger order
        :returns: An `order structure <https://docs.ccxt.com/#/?id=order-structure>`
        N)is_stop_orderr   r   FcancelOrderrM   marginz.order_cancelorder_id)r~   r   safe_value_nr   r   spotOrderPrepareRequestr   r   r   r   strr   r   r   )rs   r   rw   r   r   r   rx   r   r   r   r   r   r   ress                 ru   cancel_order_wszgate.cancel_order_ws   s\     !!! .t{{6/B##F,PRWX6#GH88PVWeZ^bhZhlpt|l|!=!=fgu!U  DH  DW  DW  X^  `d  fk  Dl--f5/$$V,[111!"g
((dkk'=.QSZ[[V,, 	" 	2[s4   D5D.CD5D18D5D3D51D53D5c           	      r  K   | j                          d{    | j                  |      }| j                  |||||||      }	| j                  |      }
|
dz   }| j	                  |      }| j                  ||
       d{    | j                  ||	|       d{   }| j                  ||      S 7 7 37 w)a`  
        edit a trade order, gate currently only supports the modification of the price or amount fields

        https://www.gate.io/docs/developers/apiv4/ws/en/#order-amend
        https://www.gate.io/docs/developers/futures/ws/en/#order-amend

        :param str id: order id
        :param str symbol: unified symbol of the market to create an order in
        :param str type: 'market' or 'limit'
        :param str side: 'buy' or 'sell'
        :param float amount: how much of the currency you want to trade in units of the base currency
        :param float [price]: the price at which the order is to be fulfilled, in units of the quote currency, ignored in market orders
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: an `order structure <https://docs.ccxt.com/#/?id=order-structure>`
        Nz.order_amend)r~   r   edit_order_requestr   r   r   r   r   )rs   r   rw   rx   ry   rz   r{   r   r   extendedRequestr   r   r   r   s                 ru   edit_order_wszgate.edit_order_ws   s       !!!V$11"fdD&RWY_`--f5.$$V,[111--c?GLL&11 	" 	2Ls4   B7B1A(B7?B3 B7B5B73B75B7c                   K   | j                          d{    |dn| j                  |      }| j                  |||      \  }}| j                  |      }|dz   }| j	                  |      }	| j                  |	|       d{    | j                  |	| j                  ||      |       d{   }
| j                  |
|      S 7 7 C7 w)a  
        Retrieves information on an order

        https://www.gate.io/docs/developers/apiv4/ws/en/#order-status
        https://www.gate.io/docs/developers/futures/ws/en/#order-status

        :param str id: Order id
        :param str symbol: Unified market symbol, *required for spot and margin*
        :param dict [params]: Parameters specified by the exchange api
        :param bool [params.trigger]: True if the order being fetched is a trigger order
        :param str [params.marginMode]: 'cross' or 'isolated' - marginMode for margin trading if not provided self.options['defaultMarginMode'] is used
        :param str [params.type]: 'spot', 'swap', or 'future', if not provided self.options['defaultMarginMode'] is used
        :param str [params.settle]: 'btc' or 'usdt' - settle currency for perpetual swap and future - market settle currency is used if symbol is not None, default="usdt" for swap and "btc" for future
        :returns: An `order structure <https://docs.ccxt.com/#/?id=order-structure>`
        Nz.order_status)	r~   r   fetchOrderRequestr   r   r   r   r   r   )rs   r   rw   r   r   r   r   r   r   r   r   s              ru   fetch_order_wszgate.fetch_order_ws  s       !!! .t{{6/B!%!7!7FF!K--f5/$$V,[111--c4;;w3VX_``&11 	" 	2`s4   C
CA+C
C*C
-C.C
C
C
sincelimitc                 H   K   | j                  d||||       d{   S 7 w)a  
        fetch all unfilled currently open orders

        https://www.gate.io/docs/developers/futures/ws/en/#order-list

        :param str symbol: unified market symbol
        :param int [since]: the earliest time in ms to fetch open orders for
        :param int [limit]: the maximum number of  open orders structures to retrieve
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns Order[]: a list of `order structures <https://docs.ccxt.com/#/?id=order-structure>`
        openNfetch_orders_by_status_wsrs   rw   r   r   r   s        ru   fetch_open_orders_wszgate.fetch_open_orders_ws1  s(      33FFE5RXYYYY   " "c                 H   K   | j                  d||||       d{   S 7 w)a<  
        fetches information on multiple closed orders made by the user

        https://www.gate.io/docs/developers/futures/ws/en/#order-list

        :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 Order[]: a list of `order structures <https://docs.ccxt.com/#/?id=order-structure>`
        finishedNr   r   s        ru   fetch_closed_orders_wszgate.fetch_closed_orders_ws?  s(      33JuV\]]]]r   statusc                 2  K   | j                          d{    d}|5| j                  |      }|d   }|d   durt        | j                  dz         | j	                  |||||      \  }}| j                  |dg      }	| j                  |      }
|
dz   }| j                  |      }| j                  ||
       d{    | j                  || j                  |	|      |       d{   }| j                  ||      }| j                  ||||      S 7 7 W7 .w)a
  

        https://www.gate.io/docs/developers/futures/ws/en/#order-list

        fetches information on multiple orders made by the user by status
        :param str status: requested order status
        :param str symbol: unified market symbol of the market orders were made in
        :param int|None [since]: the earliest time in ms to fetch orders for
        :param int|None [limit]: the maximum number of order structures to retrieve
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :param int [params.orderId]: order id to begin at
        :param int [params.limit]: the maximum number of order structures to retrieve
        :returns dict[]: a list of `order structures <https://docs.ccxt.com/#/?id=order-structure>`
        Nrw   rN   TzX fetchOrdersByStatusWs is only supported by swap markets. Use rest API for other marketsr]   z.order_list)r~   r   r   r   prepareOrdersByStatusRequestr   r   r   r   r   r   r   filter_by_symbol_since_limit)rs   r   rw   r   r   r   r   r   r   
newRequestr   r   r   r   r   s                  ru   r   zgate.fetch_orders_by_status_wsM  s7     !!![[(FH%Ff~T)"477  .H  $H  I  I!%!B!B66SXZ_ag!hYYw
3
--f5-$$V,[111..sDKK
M4Z\cdd	""9f500NN 	" 	2ds4   DDB$D;D<*D&D'+DDDc                   K   | j                          d{    | j                  |      }|d   }|d   }| j                  |ddd      \  }}| j                  |      }|dz   }	d|z   }
| j	                  |      }||g}|d	}|d
   rt        |      }|j                  |       ||d}| j                  ||
||	||       d{   }|j                         S 7 7 w)a  
        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
        :returns dict: A dictionary of `order book structures <https://docs.ccxt.com/#/?id=order-book-structure>` indexed by market symbols
        Nrw   r   r<   rY   rV   .order_book_update
orderbook:d   contract)rw   r   )	r~   r   r   r   r   r   appendsubscribe_publicr   )rs   rw   r   r   r   marketIdrY   r   r   r   messageHashr   payloadstringLimitsubscription	orderbooks                   ru   watch_order_bookzgate.watch_order_bookm  s     !!!V$!$<77@PR\^ef%--f5 44'&0$$V,X&=E*e*KNN;'
 //['7TY[ghh	  ) 	"& is"   CCB)C CCCc           
        K   | j                          d{    | j                  |      }|d   }|d   }d}| j                  |dd|      \  }}| j                  |      }|dz   }d|z   }d	|z   }	| j	                  |      }
||g}| j                  |d
d      }|d   rt        |      }|j                  |       | j                  |
d|g|	g|g|||       d{   S 7 7 w)a  
        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
        :returns dict: A dictionary of `order book structures <https://docs.ccxt.com/#/?id=order-book-structure>` indexed by market symbols
        Nrw   r   rV   r<   rY   r   r   zunsubscribe:orderbook:r   r   r   r   )	r~   r   r   r   r   safe_integerr   r   un_subscribe_public_multiple)rs   rw   r   r   r   rY   r   r   subMessageHashr   r   r   r   r   s                 ru   un_watch_order_bookzgate.un_watch_order_book  s$     !!!V$!$<88AQS]_gh&--f5 44*V33f<$$V,X&!!&'37*e*KNN;'66sK&T_S`cqbrt{  ~E  GM  N  N  	N! 	"  Ns"   CCB?CCCCclientc                     | j                  |d      }| j                  |d      }| j                  i |      | j                  |<   y )Nrw   r   )safe_stringr   
order_book
orderbooks)rs   r   messager   rw   r   s         ru   handle_order_book_subscriptionz#gate.handle_order_book_subscription  s?    !!,9!!,8"&//"e"<rv   c                 *   | j                  |d      }|j                  d      }| j                  |d      }|dk(  }|rdnd}| j                  |d      }| j                  |d      }	| j                  |d      }
| j                  |d	      }| j	                  |d d
|      }d|z   }| j                  | j
                  || j                  i             }| j                  |d      }|d}|t        |j                        }| j                  ddd      }|r|nd}||k(  rA|j                  |   }| j                  |d      }| j                  | j                  ||||i        |j                  j                  |       y ||
k\  ry ||	dz
  k\  r| j                  ||       nk|j                  |= | j
                  |= | j                  ddd      }|r<t        | j                   dz   | j#                  |      z         }|j%                  ||       |j'                  ||       y )Nr   .r   rM   r   resultUus_r   noncer<   rZ   rW   r      r\   T )r   split
safe_valuer   safe_symbolr   r   lencachehandle_optionsubscriptionsspawnload_order_bookr   handle_deltar    r   orderbook_checksum_messagerejectresolve)rs   r   r   r   channelPartsrawMarketTypeisSpot
marketTypedelta
deltaStartdeltaEndr   rw   r   storedOrderBookr   cacheLengthrZ   
waitAmountr   r   r\   errors                          ru   handle_order_bookzgate.handle_order_book  s   l ""7I6}}S)((q9&(%V:
2&&uc2
$$UC0##E3/!!(D#zB"V+//$//64??SUCVW!!/7;=K*!/"7"78 ../?RTUM*0aJj(%33K@)),@

4//feUWX!!((/hj1n$ou5$$[1'))*:JMH%dggmd6U6UV\6]&]^e[14rv   c                     | j                  |d      }|d   }| j                  |d      }||k  ryt        dt        |            D ]=  }||   }| j                  |d      }| j                  |d      }	||dz
  k\  s5||	k  s;|c S  t        |      S )Nr   r   r   r   r   )r   ranger   )
rs   	orderBookr   r   
firstDeltafirstDeltaStartir   r   r   s
             ru   get_cache_indexzgate.get_cache_index  s    !!)W51X
++J<?"q#e*% 	A!HE**5#6J((4Ha'eh.>	 5zrv   c                    t        dt        |            D ]n  }||   }t        |t              r!|j	                  | j                  |             9| j                  |d      }| j                  |d      }|j                  ||       p y )Nr   pr   )r  r   
isinstancelist
storeArrayparse_bid_ask
safe_floatstore)rs   bookSidebidAsksr	  bidAskr{   rz   s          ru   handle_bid_askszgate.handle_bid_asks  sv    q#g,' 	.AQZF&$'##D$6$6v$>?45uf-	.rv   c                 ,   | j                  |d      }||d<   | j                  |      |d<   | j                  |d      |d<   | j                  |dg       }| j                  |dg       }|d   }|d	   }| j                  ||       | j                  ||       y )
Nt	timestampdatetimer   r   babidsasks)r   iso8601r   r  )rs   r   r   r  r  r  
storedBids
storedAskss           ru   r   zgate.handle_delta  s    %%eS1	!*	+ $Y 7	*!..uc:	'uc2.uc2.v&
v&
Z.Z.rv   c                    K   | j                          d{    | j                  |      }|d   }d|d<   | j                  |g|       d{   }| j                  ||      S 7 L7 w)a  

        https://www.gate.io/docs/developers/apiv4/ws/en/#tickers-channel

        watches a price ticker, a statistical calculation with the information calculated over the past 24 hours for a specific market
        :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>`
        Nrw   r>   callerMethodName)r~   r   watch_tickersr   )rs   rw   r   r   r   s        ru   watch_tickerzgate.watch_ticker*  sp      !!!V$!%2!"))6(F;;vv.. 	" <s!   A'A#5A'A%A'%A'symbolsc                 h   K   | j                  |d| j                  ddi|             d{   S 7 w)a  

        https://www.gate.io/docs/developers/apiv4/ws/en/#tickers-channel

        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>`
        r?   methodrU   N%subscribe_watch_tickers_and_bids_asksr   rs   r&  r   s      ru   r$  zgate.watch_tickers;  sM      ??Y]YdYdfnpyez  }C  ZD  E  E  	E  E   )202c                 *    | j                  d||       y )Ntickerhandle_ticker_and_bid_askrs   r   r   s      ru   handle_tickerzgate.handle_tickerG  s    & 	&&xArv   c                 h   K   | j                  |d| j                  ddi|             d{   S 7 w)a  

        https://www.gate.io/docs/developers/apiv4/ws/en/#best-bid-or-ask-price
        https://www.gate.io/docs/developers/apiv4/ws/en/#order-book-channel

        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=   r(  book_tickerNr)  r+  s      ru   watch_bids_askszgate.watch_bids_asks\  sM      ??Z^ZeZegoq~f  BH  [I  J  J  	J  Jr,  c                 *    | j                  d||       y )Nbidaskr/  r1  s      ru   handle_bid_askzgate.handle_bid_aski  s    $ 	&&xArv   r#  c                   K   | j                          d {    | j                  |d|      \  }}| j                  |d d      }| j                  |d         }| j	                  |      }| j                  |      }d }| j                  ||d      \  }}| j                  |      }|dz   |z   }	|j                  d      dk\  }
|
rdnd}g }t        dt        |            D ]  }||   }|j                  |d	z   |z            | j                  ||||	|       d {   }| j                  ri }|||d
   <   |S |
r| j                  n| j                  }| j!                  |d
|d      S 7 J7 Mw)Nr#  Fr   r(  r   r>   r.  r7  :rw   T)r~   handle_param_stringmarket_symbolsr   r   
market_idsr   r   findr  r   r   subscribe_public_multiple
newUpdatesrU   bidsasksfilter_by_array)rs   r&  r#  r   r   r   	marketIdschannelNamer   r   isWatchTickersprefixmessageHashesr	  rw   tickerOrBidAskitemsr   s                     ru   r*  z*gate.subscribe_watch_tickers_and_bids_asks}  s    !!!#'#;#;FDVXh#i &%%gtU;WQZ(--f5OOG,	";;FDTV^_V$$V,#k1)..}=B+q#g,' 	8AQZF  #!67	8  $==c=R[]dflmm??E.<E.*+L!/T]]##FHgtDD- 	"  ns#   E%E C>E%E#AE%#E%
objectNamec                 T   | j                  |d      }|j                  d      }| j                  |d      }|dk(  rdnd}| j                  |d      }g }	t        |t              r| j                  |dg       }	n| j                  |di       }
|
g}	|dk(  }t        dt        |	            D ]  }|	|   }
| j                  |
d	      }| j                  |d d
|      }| j                  |
|      }|d   }|r|| j                  |<   n|| j                  |<   |dz   |z   }|j                  ||        y )Nr   r   r   futuresr   rM   r   r.  r   r   rw   r:  )r   r   r   r  r  	safe_list	safe_dictr  r   safe_marketparse_tickerrU   rA  r   )rs   rJ  r   r   r   partsr   r   r   results	rawTickerisTickerr	  r   r   
parsedItemrw   r   s                     ru   r0  zgate.handle_ticker_and_bid_ask  s9   ""7I6c"((2$1Y$>ZV
(3fd#nnWh;Gw"=I kG(*q#g,' 	4A
I''	37H%%hc:FF**9f=J)F'1V$(2f%$s*V3KNN:{3	4rv   c                 H   K   | j                  |g|||       d{   S 7 w)a  
        get the list of most recent trades for a particular symbol
        :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_symbolsr   s        ru   watch_tradeszgate.watch_trades  s'      22F8UE6RRRRr   c                 P  K   | j                          d{    | j                  |      }| j                  |      }| j                  |d         }| j	                  |      }|dz   }g }	t        dt        |            D ]  }
||
   }|	j                  d|z           | j                  |      }| j                  ||	|||       d{   }| j                  r6| j                  |d      }| j                  |d      }|j                  ||      }| j                  |||dd      S 7 7 ^w)a  
        get the list of most recent trades for a particular symbol
        :param str[] symbols: unified symbol of the market to fetch trades for
        :param int [since]: timestamp in ms of the earliest trade to fetch
        :param int [limit]: the maximum amount of trades to fetch
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict[]: a list of `trade structures <https://docs.ccxt.com/#/?id=public-trades>`
        Nr   .tradestrades:rw   r  T)r~   r<  r=  r   r   r  r   r   r   r?  r@  r   r   getLimitfilter_by_since_limit)rs   r&  r   r   r   rC  r   r   r   rG  r	  rw   r   tradesfirsttradeSymbols                   ru   rW  zgate.watch_trades_for_symbols  s,     !!!%%g.OOG,	WQZ(--f5	)q#g,' 	5AQZF  V!34	5 $$V,55c=)U\^dee??OOFA.E**5(;KOOK7E))&%TRR! 	" fs#   D&D!B.D&D$AD&$D&c           
        K   | j                          d{    | j                  |      }| j                  |      }| j                  |d         }| j	                  |      }|dz   }g }g }t        dt        |            D ]/  }	||	   }
|j                  d|
z          |j                  d|
z          1 | j                  |      }| j                  |d||||||       d{   S 7 7 w)aR  
        get the list of most recent trades for a particular symbol
        :param str[] symbols: 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>`
        Nr   rZ  r[  zunsubscribe:trades:r^  )
r~   r<  r=  r   r   r  r   r   r   r   )rs   r&  r   rC  r   r   r   subMessageHashesrG  r	  rw   r   s               ru   un_watch_trades_for_symbolsz gate.un_watch_trades_for_symbols  s     !!!%%g.OOG,	WQZ(--f5	)q#g,' 	AAQZF##I$67  !6!?@	A $$V,66sHg}^npy  |C  EK  L  L  	L 	" Ls"   C'C#CC'C%C'%C'c                 D   K   | j                  |g|       d{   S 7 w)aO  
        get the list of most recent trades for a particular symbol
        :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)rc  )rs   rw   r   s      ru   un_watch_tradeszgate.un_watch_trades  s#      55vhGGGGs     c                    | j                  |d      }t        |t              s|g}| j                  |      }t	        dt        |            D ]  }||   }|d   }| j                  | j                  |      }|7| j                  | j                  dd      }	t        |	      }|| j                  |<   |j                  |       d|z   }
|j                  ||
        y )Nr   r   rw   r`   rS   r[  )r   r  r  parse_tradesr  r   r^  r   rn   r   r   r   )rs   r   r   r   parsedTradesr	  traderw   cachedTradesr   hashs              ru   handle_tradeszgate.handle_trades  s    " (3&$'XF((0q#l+, 
	/A OE8_F??4;;?L#))$,,tL)%0&2F#&v%DNN<.
	/rv   1mc                   K   | j                          d{    | j                  |      }|d   }|d   }| j                  | j                  ||      }| j	                  |      }	|	dz   }
d|z   dz   |d   z   }| j                  |      }||g}| j                  ||||
|       d{   }| j                  r|j                  ||      }| j                  |||dd      S 7 7 9w)	aQ  
        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
        Nrw   r   z.candlestickscandles:r:  r   T)
r~   r   r   
timeframesr   r   r   r@  r\  r]  )rs   rw   	timeframer   r   r   r   r   rY   r   r   r   r   r   ohlcvs                  ru   watch_ohlcvzgate.watch_ohlcv  s      !!!V$!$<##DOOY	J--f5/ 8+c1F84DD$$V,X&++CgwPVWW??NN651E))%q$GG 	" Xs"   C CBC $C%8C C c                 `   | j                  |d      }|j                  d      }| j                  |d      }|dk(  rdnd}| j                  |d      }t        |t              s|g}i }t        dt        |            D ]&  }	||	   }
| j                  |
dd      }|j                  d	      }| j                  |d      }| j                  |      }|d	z   }|j                  |d      }| j                  |d d	|      }| j                  |
      }| j                  | j                  |i       | j                  |<   | j                  | j                  |   |      }|:| j                  | j                  d
d      }t        |      }|| j                  |   |<   |j                  |       |||<   ) t	        |j!                               }t        dt        |            D ]Y  }	||	   }||   }| j                  |      }d|z   dz   |z   }| j                  | j                  |   |      }|j#                  ||       [ y )Nr   r   r   rM   r   r   n r   ra   rS   ro  r:  )r   r   r   r  r  r  r   find_timeframereplacer   parse_ohlcvohlcvsr   rn   r   r   keysr   )rs   r   r   r   r   r   r   r   rC  r	  rr  r   rQ  rq  timeframeIdrF  r   rw   parsedstoredr   r{  rY   rk  s                           ru   handle_ohlcvzgate.handle_ohlcv/  s'   " ""7I6}}S)((q9 - 7Vj
(3&$'XF	q#f+& 	*A1IE++E3;L &&s+E((2I--i8K_F#++FB7H%%hc:FF%%e,F"&//$++vr"JDKK__T[[%8)DF~))$,,dK.u539F#K0MM&! )If#	*$ INN$%q#d)$ 	)A!WF!&)I**95H"X-3f<D__T[[%8(CFNN64(	)rv   c           	        K   | j                          d{    d}d}d}d}|| j                  |      }|d   }| j                  d||      \  }}| j                  d||      \  }}| j	                  |dddddd      }	|	d	z   }
d
}||d|z   z  }|dk(  }| j                  ||      }|g}|dk7  }| j                  ||||
||       d{   }| j                  r|j                  ||      }| j                  ||||d      S 7 7 9w)a  
        watches information on multiple trades made by the user
        :param str symbol: unified market symbol of the market trades were made in
        :param int [since]: the earliest time in ms to fetch trades for
        :param int [limit]: the maximum number of trade structures to retrieve
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict[]: a list of `trade structures <https://docs.ccxt.com/#/?id=trade-structure>`
        N!allr   rB   rM   rL  rn   rM   r   rO   rN   rP   z.usertradesmyTradesr:  inverseT)
r~   r   r   handle_sub_type_and_paramsget_supported_mappingget_url_by_market_typesubscribe_privater@  r\  r   )rs   rw   r   r   r   subTyperx   r   r   r   r   r   	isInverser   r   requiresUidr^  s                    ru   watch_my_tradeszgate.watch_my_tradesc  sO     !!![[(Fd|H99/6SYZf99/6SYZ008
  - 3<'K	)	))$	:*v~--c;RXZeff??OOFE2E00tTT; 	"4 gs"   DD B1DD	8DDc                 "   | j                  |dg       }t        |      }|dk(  ry | j                  }|/| j                  | j                  dd      }t        |      }|| _        | j                  |      }i }t        dt        |            D ]"  }	||	   }
|j                  |
       |
d   }d||<   $ t        |j                               }t        dt        |            D ]  }	||	   }d|z   }|j                  ||         |j                  |d       y )	Nr   r   r`   rS   rw   Tz	myTrades:r  )r   r   r  r   rn   r   rg  r  r   r  r{  r   )rs   r   r   r   tradesLengthrj  r   r}  rC  r	  ri  rw   r{  r   rk  s                  ru   handle_my_tradeszgate.handle_my_trades  s   * (B76{1}}%%dllM4HE1%8L(DM""6*	q#f+& 	%A1IE&8_F $If		%
 INN$%q#d)$ 	/A!WF'DNN<.	/ 	|Z0rv   c           	      Z  K   | j                          d{    d}d}| j                  dd|      \  }}| j                  dd|      \  }}|dk(  }| j                  ||      }|dk7  }| j	                  |dddddd      }|dz   }|d	z   }	| j                  ||	d|||       d{   S 7 7 w)
a  
        watch balance and get the amount of funds available for trading or funds locked in orders
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: a `balance structure <https://docs.ccxt.com/#/?id=balance-structure>`
        NrD   r  rM   rL  rn   r  z	.balances.balance)r~   r   r  r  r  r  )
rs   r   rx   r  r  r   r  channelTyper   r   s
             ru   watch_balancezgate.watch_balance  s      !!!99.$PVWf99.$PVW	)	))$	:v~008
  +Z'++CdGVU`aaa# 	"" bs"   B+B'BB+"B)#B+)B+c                    | j                  |dg       }| j                  |d      }|| j                  d<   || j                  d<   | j                  |      | j                  d<   t	        dt        |            D ]u  }||   }| j                         }| j                  |dd      }| j                  |      }	| j                  |d	      |d
<   | j                  |dd      |d<   || j                  |	<   w | j                  |d      }
|
j                  d      }| j                  |d      }| j                  |dddd      }|dz   }| j                  | j                        | _        |j                  | j                  |       y )Nr   time_msinfor  r  r   currencyUSDT	availablefreetotalbalancer   r   rM   rN   rP   )rM   rL  rn   r  )r   r   r  r  r  r   accountr   safe_currency_codesafe_string_2r   r  safe_balancer   )rs   r   r   r   r  r	  
rawBalancer  
currencyIdcoder   rQ  rawTyper  r   s                  ru   handle_balancezgate.handle_balance  ss   z (B7%%gy9	%V$-[!#'<<	#:Z q#f+& 	)AJllnG))*j&IJ**:6D"..z;GGFO#11*gyQGG!(DLL	) ""7I6c"""5!,00;
 
 "J.((6t||[1rv   c                   K   | j                          d{    d}| j                  |      }dg}| j                  |      s| j                  |      }d}d}| j	                  d||      \  }}|dk(  rd}| j                  |dddd      }	|d	z   }
| j                  |      s|
d
dj                  |      z   z  }
|	dz   }d}| j                  d||      \  }}|dk(  }| j                  ||      }| j                  |      }| j                  |||       | j                  ddd      }| j                  ddd      }| j                  | j                  |      }|r |r||j                  |dz          d{   S | j                  ||
|||d       d{   }| j                   r|S | j#                  | j                  |   |||d      S 7 7 U7 9w)a  

        https://www.gate.io/docs/developers/futures/ws/en/#positions-subscription
        https://www.gate.io/docs/developers/delivery/ws/en/#positions-subscription
        https://www.gate.io/docs/developers/options/ws/en/#positions-channel

        watch all open positions
        :param str[] [symbols]: list of unified market symbols to watch positions for
        :param int [since]: the earliest time in ms to fetch positions for
        :param int [limit]: the maximum number of positions to retrieve
        :param dict params: extra parameters specific to the exchange API endpoint
        :returns dict[]: a list of `position structure <https://docs.ccxt.com/en/latest/manual.html#position-structure>`
        Nr  rJ   rM   rN   rL  rn   rO   rN   rP   
:positions::,z
.positionsr  r^   Tr_   :fetchPositionsSnapshot)r~   r<  is_emptyget_market_from_symbolsr   r  joinr  r  r   set_positions_cacher   r   	positionsrO   r  r@  filter_by_symbols_since_limit)rs   r&  r   r   r   r   r   rx   r   typeIdr   r   r  r  r   r   r^   r_   r   r  s                       ru   watch_positionszgate.watch_positions&  s     !!!%%g.-}}W%11':F889I6SYZe6>D++D3
 
 \)}}W%4#((7"333K<'889I6SXY	)	))$	:S!  w7!%!3!34DF^`d!e!%!3!34DF^`d!e5!&<t.G'GHHH00k7GUZ\`aa	??11$..2FQVX]_cddE 	"< Ias4   GGEG.G/GG5GGGc                 (   | j                   i | _         || j                   v ry | j                  ddd      }|rD|dz   }||j                  vr0|j                  |       | j	                  | j
                  |||       y y t               | j                   |<   y )NrJ   r^   Fr  )r  r   rL  rO   r   load_positions_snapshotr   )rs   r   rx   r&  r^   r   s         ru   r  zgate.set_positions_cacheX  s    >>!DN4>>!!%!3!34DF^`e!f!!::K6>>1k*

477dS 2 $<#=DNN4 rv   c                   K   | j                  d d|i       d {   }t               | j                  |<   | j                  |   }t        dt	        |            D ]1  }||   }| j                  |dd      }|dkD  s!|j                  |       3 |j                  |   }	|	j                  |       |j                  ||dz          y 7 w)Nrx   r   	contractsz	:position)	fetch_positionsr   r  r  r   safe_numberr   rL  r   )
rs   r   r   rx   r  r   r	  positionr  rO   s
             ru   r  zgate.load_positions_snapshotf  s     ..tfd^DD	79tt$q#i.) 	'A |H((;BI1}X&		' ,uud[01 Es   CCAC;A
Cc                 &   | j                  |j                        }| j                  |dg       }| j                  |   }g }t	        dt        |            D ]  }||   }| j                  |      }	| j                  |	d      }
| j                  |	d      }|| j                  ||
dz         }|*|d   |	d<   |j                  |	       |j                  |	       | j                  ||
dz         }|*|d   |	d<   |j                  |	       |j                  |	       ||d|	d<   |j                  |	       |j                  |	       |j                  |	       |j                  |	        | j                  ||dz         }t	        dt        |            D ]f  }||   }|j                  d      }|d	   }|j                  d
      }| j                  |d|d      }| j                  |      rU|j                  ||       h |j                  ||dz          y )Nr   r   rw   ry   longshortz:positions::r  r   r  Fr  )get_market_type_by_urlr   r   r  r  r   parse_positionr   rN  r   find_message_hashesr   rB  r  r   )rs   r   r   rx   datar   newPositionsr	  rawPositionr  rw   ry   prevLongPositionprevShortPositionrG  r   rQ  symbolsStringr&  r  s                       ru   handle_positionszgate.handle_positionst  s   > **6::6w"5t$q#d)$ 	'Aq'K**;7H%%h9F##Hf5D|#'>>%&#I #/'7'?HV$ ''1LL*$(NN5&7:J$K!$0'8'@HV$ ''1LL*#+0A0I'-HV$ ''1LL*##H-X&1	'2 009NOq#m,- 	7A'*K%%d+E!!HM#))#.G,,\8WeTI==+y+6	7 	|TL%89rv   c           	        K   | j                          d{    d}|| j                  |      }|d   }d}d}| j                  d||      \  }}| j                  |dddddd      }|dz   }	d	}
d
g}||
d|d   z   z  }
|d   g}d}| j	                  d||      \  }}|dk(  }| j                  ||      }|dk7  }| j                  ||
||	||       d{   }| j                  r|j                  ||      }| j                  |||dd      S 7 7 9w)a  
        watches information on multiple orders made by the user
        :param str symbol: unified market symbol of the market orders were made in
        :param int [since]: the earliest time in ms to fetch orders for
        :param int [limit]: the maximum number of order structures to retrieve
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :param str [params.type]: spot, margin, swap, future, or option. Required if listening to all symbols.
        :param boolean [params.isInverse]: if future, listen to inverse or linear contracts
        :returns dict[]: a list of `order structures <https://docs.ccxt.com/#/?id=order-structure>`
        Nrw   rE   rM   rL  rn   r  z.ordersr   r  r:  r   r  r  T)
r~   r   r   r  r  r  r  r@  r\  r]  )rs   rw   r   r   r   r   rx   r   r  r   r   r   r  r  r   r  r   s                    ru   watch_orderszgate.watch_orders  s`     !!![[(FH%F88PVWe++D3
  9$-3--Kd|nG88PUV	)	))$	:v~--c;RWYdee??OOFE2E))&%TRR= 	"6 fs"   DD	B:DD8DDc                 2   | j                  |dg       }| j                  | j                  dd      }| j                  t	        |      | _        | j                  }i }| j                  |      }t        dt        |            D ]  }||   }	| j                  |	d      }
| j                  |
d      }|dk(  s|dk(  rd	|	d
<   n7|dk(  r2| j                  |	d
      }|| j                  |
d      }|dk(  rdnd|	d
<   |j                  |	       |	d   }| j                  |      }d||d   <    t        |j                               }t        dt        |            D ]&  }d||   z   }|j                  | j                  |       ( |j                  | j                  d       y )Nr   ordersLimitrS   r   r  eventputupdater   r   finishleftclosedcanceledrw   Tr   zorders:r   )r   r   rn   r   r   r   r  r   r   r   r   r  r{  r   )rs   r   r   r   r   r~  rC  parsedOrdersr	  r}  r  r  r   r  rw   r   r{  r   s                     ru   handle_orderzgate.handle_order  s   H (B7!!$,,tD;;07DK	((0q#l+, 	+A!!_F??662D$$T73E~(!2#)x ("))&(;>,,T6:D48AIxJF8$MM&!H%F[[(F&*IfTl#	+  INN$%q#d)$ 	5A#d1g-KNN4;;4	5 	t{{H-rv   c                 4   K   | j                  |g|||      S w)a  
        watch the public liquidations of a trading pair

        https://www.gate.io/docs/developers/futures/ws/en/#liquidates-api
        https://www.gate.io/docs/developers/delivery/ws/en/#liquidates-api
        https://www.gate.io/docs/developers/options/ws/en/#liquidates-channel

        :param str symbol: unified CCXT market symbol
        :param int [since]: the earliest time in ms to fetch liquidations for
        :param int [limit]: the maximum number of liquidation structures to retrieve
        :param dict [params]: exchange specific parameters for the bitmex api endpoint
        :returns dict: an array of `liquidation structures <https://github.com/ccxt/ccxt/wiki/Manual#liquidation-structure>`
        )!watch_my_liquidations_for_symbolsr   s        ru   watch_my_liquidationszgate.watch_my_liquidations'  s!      55vhufUUs   c                   K   | j                          d{    | j                  |ddd      }| j                  |      }d}d}| j                  d||      \  }}| j	                  |dddd      }d}	| j                  d||      \  }	}|	dk(  }
| j                  ||
      }g }d}| j                  |      r3|dk7  r|
st        | j                  d	z         d
}|j                  d       nDt        |      }|dk7  rt        | j                  dz         d|d   z   }|j                  |d          |dz   }| j                  |||||d       d{   }| j                  r|S | j                  | j                  |||d      S 7 e7 4w)a  
        watch the private liquidations of a trading pair

        https://www.gate.io/docs/developers/futures/ws/en/#liquidates-api
        https://www.gate.io/docs/developers/delivery/ws/en/#liquidates-api
        https://www.gate.io/docs/developers/options/ws/en/#liquidates-channel

        :param str[] symbols: unified CCXT market symbols
        :param int [since]: the earliest time in ms to fetch liquidations for
        :param int [limit]: the maximum number of liquidation structures to retrieve
        :param dict [params]: exchange specific parameters for the gate api endpoint
        :returns dict: an array of `liquidation structures <https://github.com/ccxt/ccxt/wiki/Manual#liquidation-structure>`
        NTrI   rL  rn   r  r  rv  z watchMyLiquidationsForSymbols() does not support listening to all symbols, you must call watchMyLiquidations() instead for each symbol you wish to watch.myLiquidationsznot allr   z watchMyLiquidationsForSymbols() only allows one symbol at a time. To listen to several symbols call watchMyLiquidationsForSymbols() several times.myLiquidations::r   r   z.liquidates)r~   r<  r  r   r  r  r  r  r   r   r   r   r  r@  r  liquidations)rs   r&  r   r   r   r   rx   r   r  r  r  r   r   r   symbolsLengthr   newLiquidationss                    ru   r  z&gate.watch_my_liquidations_for_symbols7  s     !!!%%gtT4@--g6889XZ`bhie++D3
 
 889XZ`bgh	)	))$	:==!"9   ,H  "H  I  I*KNN9%LM!   ,A  "A  B  B,wqz9KNN6$<(=( $ 6 6sKRY[`bf gg??""11$2C2CWeUZ\`aaA 	": hs"   F E;D2F 	E>
2F >F c                    | j                  |dg       }g }t        dt        |            D ]  }||   }| j                  |      }| j	                  |d      }| j                  | j                  |      }	|	(| j                  | j                  dd      }
t        |
      }	|	j                  |       |	| j                  |<   |j                  |	d|z           |j                  |d       y )Nr   r   rw   liquidationsLimitrS   r  r  )rM  r  r   parse_ws_liquidationr   r   r  r   rn   r   r   r   )rs   r   r   rawLiquidationsr  r	  rawLiquidationliquidationrw   r  r   s              ru   handle_liquidationzgate.handle_liquidationg  s    X ..(B?q#o./ 
	FA,Q/N33NCK%%k8<F??4+<+<fEL#))$,,8KTR)%0,(4Df%NN<);f)DE
	F 	(89rv   c                    | j                  |d      }| j                  ||      }| j                  |d      }| j                  |d      }| j                  |d      }t        j                  t        j
                  ||            }| j                  || j                  ||      | j                  |      | j                  |d      | j                  |d      d d || j                  |      d	      S )Nr   r  sizer  contractSize
fill_price)	r  rw   r  r  r{   	baseValue
quoteValuer  r  )r   rO  r   r!   
string_abs
string_subsafe_liquidationr   parse_numberr  r  )rs   r  r   r   r  originalSizer  rz   s           ru   r  zgate.parse_ws_liquidation  s    : ##K<!!(F3%%k9=	''V<V4##G$6$6|T$JK$$&&x8**62 ,,V^D%%k<@"Y/
&
 
 
	rv   c                    | j                  |d      }| j                  |d      }| j                  |d|      }| j                  |dd      }| j                  |g d      }|| j                  |j                  |      }	 | j                  | j                  d   d   || j                  |             | j                  | j                  d   || j                  |             | j                  |d	| j                  |d	            }	| j                  | j                  d   d
   |	| j                  |             t        | j                  |            y# t        $ r}
|j                  |
|       |||j                  v r|j                  |= | j                  |d      }||j                  d      dkD  r|j                  d      }| j                  |dg       }t        dt!        |            D ]K  }|d   dk(  }| j#                  ||   d d|      }|d   dz   |z   }|0||j                  v s?|j                  |= M Y d }
~
nd }
~
ww xY w|||j                  v r|j                  |= y)Nr  errsr  r  label)r   	requestId
request_idr&   rj   r   rk   r   r   r   r   rN   r   r   r:  TF)rN  r  safe_string_nr   r   throw_exactly_matched_exceptionro   jsonthrow_broadly_matched_exceptionr   	Exceptionr   r>  r   rM  r  r   r   )rs   r   r   r  r  r  r  r   r   errorMessageer   parsedChannelr   r	  r   rw   messageHashSymbols                     ru   handle_error_messagezgate.handle_error_message  sq   h ~~gv.~~dF+w6!!%9)JK**6+?+?DKH44T__T5J75SUY[_[d[del[mn44T__W5MtUYU^U^_cUde#//y$BRBRSWYbBcd44T__T5J75SUacgclclmtcuv#DIIg$677& %  Ha-++AUAU2U,,[9**7I>'gll3.?!.C$+MM#$6M"nnWiDG"1c'l3 H%21%5%?
!%!1!1'!*dC!T,9!,<s,BV,K)-9@QU[UiUi@i & 4 45F GHH R6+?+?%?((,s&   >CE 	I B?H;H;'H;;I c                     i | _         y N)r  )rs   r   r   r   s       ru   handle_balance_subscriptionz gate.handle_balance_subscription&  s	    rv   c                 b   | j                  |d      }| j                  | j                  | j                  d}| j                  |d      }||v rG| j                  |j                  |      }| j	                  |j                  |      }||   } ||||       ||j                  v r|j                  |= y y )Nr   )r  zspot.order_book_updatezfutures.order_book_updater   )r   r  r   r   r   )	rs   r   r   r   methodsr   subscriptionHashr   r(  s	            ru   handle_subscription_statuszgate.handle_subscription_status)  s    ""7I677&*&I&I)-)L)L

 gt,g#//0D0DbI??6+?+?AQRLW%F67L1%%%$$R( &rv   c                    | j                  |d      }t        |j                  j                               }t	        dt        |            D ]  }||   }||j                  vr|j                  d      s)|j                  |   }| j                  |d      }||k7  rP| j                  |dg       }	| j                  |dg       }
t	        dt        |	            D ]  }|	|   }|
|   }| j                  |||       ! | j                  |        y )Nr   r   unsubscriberG  rb  )
r   r  r   r{  r  r   
startswithrM  clean_unsubscriptionclean_cache)rs   r   r   r   r{  r	  r   r   subIdrG  rb  j	unsubHashsubHashs                 ru   handle_un_subscribezgate.handle_un_subscribe9  s   & gt,F((--/0q#d)$ 	/Aq'K6#7#77%%m4%33K@((t<; $|_b Q#'>>,@RTV#W q#m"45 JA -a 0I.q1G--fgyIJ   .!	/rv   r   c                    | j                  |dd      }| j                  |dg       }t        |      }|dk(  rg| j                  |dg       }t        dt        |            D ];  }||   }| j                  |d      }| j                  |d      }	| j                  |   |	= = y |dkD  ret        dt        |            D ]L  }||   }|j                  d      r| j                  |= '|d	k(  r| j                  |= :|d
k(  s@| j                  |= N y |j                  d      rNt        | j                  j                               }
t        dt        |
            D ]  }| j                  |
|   =  y y )Ntopicrv  r&  rr  symbolsAndTimeframesr   r   r^  r   r.  )r   rM  r   r  rz  endswithr^  r   rU   r  r{  )rs   r   r  r&  r  symbolsAndTimeFramesr	  symbolAndTimeFramerw   rq  r{  s              ru   r
  zgate.clean_cache`  so     w;..y"=GG#'>>,@VXZ#[ 1c"678 3%9!%<"))*<a@ ,,-?C	KK'	2	3
 Q1c'l+ - >>(+F+k)/h&V,- ~~h' DKK,,./q#d), -ADG,-	 (rv   c                 *   | j                  ||      ry | j                  |d      }|dk(  r| j                  ||       y |dk(  r| j                  ||       y | j                  |dd      }|j	                  d      }| j                  |d      }| j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  d
}| j                  ||      }|	 |||       | j                  |d	      }	|	d
k(  r| j!                  ||       y |	N| j#                  |d      }
| j                  |
d      }| j%                  |d      }|dur|j'                  ||	       y y y )Nr  	subscriber  r   rv  r   r   )

usertradescandlesticksr   r  rU   r4  r^  order_book_updatebalances
liquidatesr  authenticatedr  r   ackT)r  r   r  r  r   r   r  r  r  r  r2  r8  rl  r  r  r  handle_authentication_messagerN  	safe_boolr   )rs   r   r   r  r   r   r  	v4Methodsr(  r  r  r   r  s                ru   handle_messagezgate.handle_message|  s   t $$VW5  '2K++FG<M!$$VW5""7Ir:}}S)oolA6// --''..))..((!%!7!7++11
	 K867#$$Wl;	'..vw? >>'62D__T84F..%0C$vy1  !rv   c                 X    | j                   d   |d      }|d   r|d   r|d   S |d   S |S )NrQ   rx   r   linearrK   rL   rm   )rs   r   baseUrls      ru   r   zgate.get_url_by_market  s@    ))E"6&>2*&,X&676?JGENJNrv   r   c                     |d   ry|d   ryy)NrM   rP   rn   rL   )rs   r   s     ru   r   zgate.get_type_by_market  s    &>Hrv   c                 Z    | j                   d   }||   }|dk(  s|dk(  r|r|d   S |d   S |S )NrQ   rN   rO   rL   rK   r%  )rs   rx   r  rQ   r   s        ru   r  zgate.get_url_by_market_type  sA    ii$iFN 0!*3u:;F;Jrv   r   c                     dddd}t        |j                               }t        dt        |            D ]$  }||   }||   }|j	                  |      dk\  s"|c S  y)NrP   rO   rN   )zop-deliveryfxr   rM   )r  r{  r  r   r>  )rs   r   findByr{  r	  keyvalues          ru   r  zgate.get_market_type_by_url  sj     

 FKKM"q#d)$ 	Aq'C3KExx}!		
 rv   c                 ~    | j                  | j                  | j                  dd      d      }|| j                  d<   |S )Nreqidr   r   )sumr   rn   )rs   r1  s     ru   r  zgate.request_id"  s8    **4<<!DaH %Wrv   c                 2  K   | j                         }| j                         }|||d|d}	|9| j                  |      }
||
j                  vrt	        |      }||
j                  |<   | j                  |	|      }| j                  |||||       d {   S 7 wNr  r   timer   r  r   )r  secondsr   r   r   r   watch)rs   r   r   r   r   r   r   r  r6  r   r   tempSubscriptionHashr   s                ru   r   zgate.subscribe_public(  s     OO%	||~ 
 #[[%F6#7#77'*9~$=H$$%9:++gv.ZZ[';UUUUs   BBBBc                    K   | j                         }| j                         }|||d|d}| j                  ||      }	| j                  |||	|       d {   S 7 wr4  )r  r7  r   watch_multiple)
rs   r   rG  r   r   r   r  r6  r   r   s
             ru   r?  zgate.subscribe_public_multiple:  sb     OO%	||~ 
 ++gv.((mWmTTTTs   AAAAc	                    K   | j                         }	| j                         }
|	|
|d|d}t        |	      |d|||d}| j                  ||      }| j	                  |||||       d {   S 7 w)Nr  r5  T)r   r  r  rG  rb  r&  )r  r7  r   r   r;  )rs   r   r  r&  rG  rb  r   r   r   r  r6  r   subr   s                 ru   r   z!gate.un_subscribe_public_multipleG  s     OO%	||~"
 i.* 0
 ++gv.((mWmUXYYYYs   A%A.'A,(A.c                    K   |dz   }| j                  |      }d}|j                  |      }| j                  |j                  |      }|| j	                  |i ||       d {   S |S 7 w)Nz.loginr  )r   rO   r   r   r   )rs   r   r   r   r   r   rO   r  s           ru   r   zgate.authenticate\  sr     (S!%{+(<(<kJ --c2wLLL Ms   AA*!A("A*c                 b    d}| j                  |j                  |      }|j                  d       y )Nr  T)r   rL  r   )rs   r   r   r   rO   s        ru   r  z"gate.handle_authentication_messagef  s'    %=trv   r  c                    K   | j                          d}|| j                         }t        |      }|}| j                         }dj	                  ||| j                  |      t        |      g      }	| j                  | j                  |	      | j                  | j                        t        j                  d      }
|t        |      | j                  |
|d}|dk(  s|dk(  rddi|d	<   |||||d
}| j                  |||||       d {   S 7 w)NrQ   
hex)req_idr  api_key	signature	req_paramzspot.order_placezfutures.order_placezX-Gate-Channel-Idccxt
req_headerr5  )check_required_credentialsr  r   r7  r  r  hmacencodesecrethashlibsha512apiKeyr8  )rs   r   	reqParamsr   r  r  reqIdr   r6  signatureStringrE  r   r   s                ru   r   zgate.request_privatek  s    '')OO%EE
I||~))UGTYYy5I3t9$UVIIdkk/:DKK<TV]VdVdfkl	T{{""
 ))w:O/O#V%GL! 
 ZZ[';	RRRRs   DDDDc                   K   | j                          |r`| j                  t        | j                        dk(  rt        | j                  dz         | j                  g}||}n| j                  ||      }| j                         }d}	d|z   dz   dz   |	z   dz   dz   t        |      z   }
| j                  | j                  |
      | j                  | j                        t        j                  d      }d	| j                  |d
}| j                         }||||	|d}|||d<   | j                  |      }||j                   vrt        |      }||j                   |<   | j#                  ||      }| j%                  |||||       d {   S 7 w)Nr   z requires uid to subscriber  zchannel=&zevent=ztime=rB  rD  )r(  KEYSIGN)r   r6  r   r  authr   )rI  uidr   r   r   array_concatr7  r   rJ  rK  rL  rM  rN  rO  r  r   r   r   r8  )rs   r   r   r   r   r   r  idArrayr6  r  signaturePayloadrE  rW  r  r   r   r9  r   s                     ru   r  zgate.subscribe_private  s    '')xx3txx=A#5'2N(NOOxxjG!++GW=||~%/#5@5H3NQXX[^_c[ddIIdkk*:;T[[=UW^WeWeglm	;;

 OO%	
 !(GIS!v333#&y> 9DF  !56++gv.ZZ[';TTTTs   FF
FF
r   )F)[__name__
__module____qualname__r   rr   r   r   r   floatr   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$  r2  r5  r8  r*  r0  r   rX  rW  rc  re  rl  r  rs  r  r  r  r   r  r  r   r  r  r  r  r  r  r
   r  r  r  r  r  r  r  r  dictr
  r"  r   r   r   r   r  r  r  r   r?  r   r   r  r   r  __classcell__)rt   s   @ru   r#   r#      s=   m# m^ pt|~ )C )y )	 )[` )il )V IK 4T,-? 4. 8<B 4 44 <@ - -S -6 im{  IK 2c 23 2i 2y 2be 2ux 24 ;?r 2s 2C 24 8<$]ajl Z ZC ZWZ Zquv{q| Z :>D_cln ^3 ^c ^Y\ ^swx}s~ ^ JN\`os|~ Oc O3 OVY Oil O@ @DB !S ! !S\ !< =? N N3 N2=V =
[5 [5z.
/ 68 / /F /" 6:" 
E7 
E 
EBF B* 8<B JW J7 JBV B( NRkoxz E7 Eeh E  @G E24C 4 44 <@d[] 	S 	SS 	S 	Sbfglbm 	S OSaenp Sd3i S S[^ Suyz  vA S6 LN Lc LRU L, 9; HC Hs H/F /B 8<$]ajl H HC HWZ Hquvzq{ H22)6 2)h 37TX\eg &UC &Us &URU &Ulpqvlw &UP*1v *1X *, b b2T2V T2l 8<$]ajl 0eW 0eC 0eWZ 0equv~q 0ed>& > >2E:N 04$UYbd )S )SC )Ss )Simnsit )SV?.6 ?.B EIW[df V# Vc VQT Vkop{k| V  LP^bqu  A .btCy .bX[ .bkn .b  FJ  KV  FW .b`9: 9:v-^S6 Sj& ) ) %/& %/N- -82V 2B : #  QSae V$ \^ U AC Z*F 
S S@$Urv   r#   )*ccxt.async_supportrG   ccxt.async_support.base.ws.cacher   r   r   r   rM  ccxt.base.typesr   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   !ccxt.async_support.base.ws.clientr   typingr   ccxt.base.errorsr   r   r   r   r   r    ccxt.base.preciser!   async_supportr#   r(  rv   ru   <module>rj     sn     A  A  {  {  {  {  { 4  * 0 . ' ) * %ZU4"" ZUrv   