
    ":h                        d dl Zd dl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 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j4                  j6                        Zy)    N)
ArrayCacheArrayCacheBySymbolByIdArrayCacheByTimestamp)
AnyBalancesIntOrder	OrderBookStrStringsTickerTickersTrade)Client)List)AuthenticationError)ArgumentsRequired)NotSupportedc            
           e Zd Zdef fdZ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Zdi fde
defdZdefdZd:dZi fdZi fdZi fdZi fdZdddi fdedededee   fdZdefdZd:defdZdi fdededefdZdefdZd ZdefdZ d Z!d Z"ddi fdedededee#   fdZ$defd Z%dddi fde&dededee#   fd!Z'd:defd"Z(d:d#Z)dddi fde&dededee*   fd$Z+defd%Z,d:d&Z-d:d'Z.d( Z/d) Z0i fde1fd*Z2defd+Z3i fdedefd,Z4di fde
defd-Z5di fde
defd.Z6di fdedefd/Z7i fdedefd0Z8i fdedefd1Z9ded2ee   fd3Z:i fd4Z;i fd5Z<defd6Z=defd7Z>defd8Z?defd9Z@ xZAS );mexc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dddddddddd	dddddiidddddddd d!d"d#d$
d%d&d'd d(| j                  d)d*i d+      S ),NwsTcancelAllOrdersWsFcancelOrdersWscancelOrderWscreateOrderWseditOrderWsfetchBalanceWsfetchOpenOrdersWsfetchOrderWsfetchTradesWswatchBalancewatchMyTrades
watchOHLCVwatchOrderBookwatchOrderswatchTickerwatchTickers)	watchBidsAskswatchTradeswatchTradesForSymbolsunWatchTickerunWatchTickersunWatchBidsAsksunWatchOHLCVunWatchOrderBookunWatchTradesapizwss://wbs.mexc.com/wszwss://contract.mexc.com/edge)spotswapO Min1Min5Min15Min30Min60Hour4Hour8Day1Week1Month1)
1m5m15m30m1h4h8h1d1w1M      )snapshotDelaysnapshotMaxRetries)listenKeyRefreshRate
timeframesr&   	listenKeyi@  )ping	keepAlive)hasurlsoptions	streaming
exceptions)deep_extendsuperr   describerR   )self	__class__s    G/var/www/html/turnos/venv/lib/python3.12/site-packages/ccxt/pro/mexc.pyr[   zmexc.describe   s   dD : <d#U !%  	
   u !% $U       d !$ t  t!" #$ "&#).!%"&#' $$(!%5:  7 > )0 ! ""!!! !" &(*+# "', 		!}@?
 @ @	    symbolc                 b  K   | j                          d{    | j                  |      }d|d   z   }|d   rKd}| j                  |dd      \  }}d}|rd|d	   z   d
z   }nd|d	   z   }| j                  |||       d{   S d}d|d	   i}| j	                  ||||       d{   S 7 7 +7 w)a  
        watches a price ticker, a statistical calculation with the information calculated over the past 24 hours for a specific market

        https://mexcdevelop.github.io/apidocs/spot_v3_en/#individual-symbol-book-ticker-streams
        https://mexcdevelop.github.io/apidocs/contract_v1_en/#public-channels
        https://mexcdevelop.github.io/apidocs/spot_v3_en/#miniticker

        :param str symbol: unified symbol of the market to fetch the ticker for
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :param boolean [params.miniTicker]: set to True for using the miniTicker endpoint
        :returns dict: a `ticker structure <https://docs.ccxt.com/#/?id=ticker-structure>`
        Nticker:r`   r4   Fr(   
miniTickerspot@public.miniTicker.v3.api@id@UTC+8spot@public.bookTicker.v3.api@z
sub.ticker)load_marketsmarkethandle_option_and_paramswatch_spot_publicwatch_swap_public)r\   r`   paramsri   messageHashrc   channelrequestParamss           r^   watch_tickerzmexc.watch_tickerV   s      !!!V$&"22&>J!%!>!>v}Vb!cJG:VD\IHT:VD\I//fMMM"G&,#M //mU[\\\# 	" N ]s4   B/B)A(B/?B+ $B/$B-%B/+B/-B/clientc                    | j                  ||       | j                  |dd      }| j                  |dd      }| j                  |d      }| j	                  |      }|d   }d }|d   r,| j                  ||      }||d<   | j                  |      |d<   n| j                  ||      }|| j                  |<   d	|z   }	|j                  ||	       y )
Nddatasr`   tr4   	timestampdatetimerb   )
handle_bid_asksafe_dict_2safe_string_2safe_integersafe_marketparse_ws_tickeriso8601parse_tickertickersresolve)
r\   rr   message	rawTickermarketIdrx   ri   r`   tickerrn   s
             r^   handle_tickerzmexc.handle_tickerv   s    H 	FG,$$Wc6:	%%gsH=%%gs3	!!(+!&>)))V<F"+F;!%i!8F:&&y&9F%V&(v{+r_   Nsymbolsc                 :  K   | j                          d{    | j                  |d      }g }| j                  |d      }d}|| j                  |      }d}| j	                  d||      \  }}|dk(  }|r| j
                  d   d   d   n| j
                  d   d   d   }i }	|rd}
| j                  |dd	      \  }
}g }|
sx|t        | j                  d
z         | j                  |      }t        dt        |            D ]4  }||   }|j                  d||   z          d|z   }|j                  |       6 nV|j                  d       ||j                  d       n1t        dt        |            D ]  }|j                  d||   z           d|	d<   ||	d<   nd|	d<   i |	d<   |j                  d       | j                  ||| j                  |	|      |       d{   }|r| j                  ri }|||d   <   |S | j!                  | j"                  d|      S 7  7 >w)a  
        watches a price ticker, a statistical calculation with the information calculated over the past 24 hours for all markets of a specific list

        https://mexcdevelop.github.io/apidocs/spot_v3_en/#individual-symbol-book-ticker-streams
        https://mexcdevelop.github.io/apidocs/contract_v1_en/#public-channels
        https://mexcdevelop.github.io/apidocs/spot_v3_en/#minitickers

        :param str[] symbols: unified symbol of the market to fetch the ticker for
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :param boolean [params.miniTicker]: set to True for using the miniTicker endpoint
        :returns dict: a `ticker structure <https://docs.ccxt.com/#/?id=ticker-structure>`
        Nr   r)   r4   r3   r   r5   Frc   B watchTickers required symbols argument for the bookTicker channelrb   rg   $spot@public.miniTickers.v3.api@UTC+8zspot:tickerSUBSCRIPTIONmethodrm   zsub.tickersr   r`   )rh   market_symbolssafe_stringri   handle_market_type_and_paramsrU   rj   r   re   
market_idsrangelenappendwatch_multipleextend
newUpdatesfilter_by_arrayr   )r\   r   rm   messageHashesfirstSymbolri   typeisSpoturlrequestrc   topics	marketIdsir   ro   r   results                     r^   watch_tickerszmexc.watch_tickers   sT     !!!%%gt4&&w2"[[-F99.&RXYf&.17diit$V,dii>Nt>TU[>\J!%!>!>v~Wc!dJF?+DGG6z,z{{ OOG4	q#i.1 +A(|H!((WQZ)?@>IGMM'*	+ DE?!((7"1c'l3 E%,,Y-CDE .GH &GH -GH "GH  ***3t{{7TZ?[]jkkdooF'-F6(#$M##DLL(GDDW 	"L ls"   HHGHH<HHc                    | j                  |dd      }| j                  |dd      }| j                  |d      }| j                  |      }|j                  d      }|d u }|r|n|d   }	d}
|	r|
nd}|dz   }g }t	        d	t        |            D ]k  }||   }d }|	r| j                  ||      }n| j                  |      }|d
   }|| j                  |<   |j                  |       d|z   }|j                  ||       m |j                  ||       y )Nru   rt   c rv   r4   zspot:r   r   r`   rb   )safe_list_2r   r~   
startswithr   r   r   r   r   r   r   )r\   rr   r   ru   ro   r   ri   channelStartsWithSpotmarketIdIsUndefinedr   
spotPrefixmessageHashPrefixtopicr   r   entryr   r`   rn   s                      r^   handle_tickerszmexc.handle_tickers  s0   z 5""7C4##GS1!!(+ ' 2 26 :&$.*=&6&>
*0Jb!H,q#d)$ 	0AGEF--eV<**51H%F#)DLL MM&!#f,KNN6;/	0 	vu%r_   c           	      b   | j                  |d      }| j                  |d      }| j                  |d      }| j                  i d|d| j                  ||      d|d| j	                  |      dd d	| j                  |d
      d| j                  |d      d|d|d| j                  |d      d| j                  |d      d| j                  |d      d| j                  |d      dd dd dd d| j                  |d      d | j                  |d      | j                  |d      d|      S )Nrv   rw   pinfor`   rx   ry   openhighhlowlcloselastbidb	bidVolumeBaska	askVolumeAvwappreviousClosechange
percentagetrvq)average
baseVolumequoteVolume)r   r}   safe_tickersafe_symbolr   safe_number)r\   r   ri   r   rx   prices         r^   r   zmexc.parse_ws_ticker[  s   8 ##FC0%%fc2	  - !
F!
d&&x8!
 !
 Y/	!

 D!
 D$$VS1!
 4##FC0!
 U!
 E!
 4##FC0!
 ))&#6!
 4##FC0!
 ))&#6!
 D!
 T!
  d!!
" $**648#!
$ **637++FC8)!
* + 	r_   c                   K   | j                          d{    | j                  |dddd      }d}|t        | j                  dz         | j	                  |      }| j                  d|d   |      \  }}|dk(  }|st        | j                  dz         g }g }t        dt        |            D ]F  }|r+| j                  ||         }	|j                  d	|	d
   z          |j                  d||   z          H | j                  d   d   d   }
d|d}| j                  |
|| j                  ||      |       d{   }| j                  ri }|||d   <   |S | j                  | j                   d|      S 7 \7 <w)a  

        https://mexcdevelop.github.io/apidocs/spot_v3_en/#individual-symbol-book-ticker-streams

        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>`
        NTF( watchBidsAsks required symbols argumentr*   r   r4   ' watchBidsAsks only support spot marketrg   re   bidask:r3   r   r   r   rm   r`   )rh   r   r   re   markets_for_symbolsr   r   r   r   ri   r   rU   r   r   r   r   bidsasks)r\   r   rm   
marketTypemarketsr   r   r   r   ri   r   r   r   r   s                 r^   watch_bids_askszmexc.watch_bids_asks  s     !!!%%gtT5$G
?#DGG.X$XYY**73!??QXYZQ[]cd
Fv%tww)RRSSq#g,' 	9AWQZ0>MN  WQZ!78		9
 iit$V,$
 **3t{{7TZ?[]jkk??G(.GF8$%N##DMM8WEE7 	", ls"   E7E2D!E78E59:E75E7c                     | j                  |      }| j                  |d      }|y || j                  |<   d|z   }|j                  ||       y )Nr`   r   )parse_ws_bid_askr   r   r   )r\   rr   r   parsedTickerr`   rn   s         r^   rz   zmexc.handle_bid_ask  sT     ,,W5!!,9> ,f&(|[1r_   c                    | j                  |d      }| j                  |d      }| j                  ||      }| j                  |d      }| j                  |d      }| j	                  ||| j                  |      | j                  |d      | j                  |d      | j                  |d      | j                  |d      |d	|      S )
Nrt   rv   r`   rw   r   r   r   r   )r`   rx   ry   r   r   r   r   r   )	safe_dictr   r~   r}   r   r   r   )r\   r   ri   ru   r   r`   rx   s          r^   r   zmexc.parse_ws_bid_ask  s    ~~fc*##FC0!!(F3!!&(3%%fc2	"Y/##D#.))$4##D#.))$4	!
 	 		r_   c                    K   | j                  |dd      }| j                  |dg      }| j                  d   d   d   }|rdnd}||gd}| j                  ||| j	                  ||      |       d {   S 7 w)	NunsubscribedFr3   r   r4   UNSUBSCRIPTIONr   r   )	safe_boolomitrU   watchr   )r\   ro   rn   rm   r   r   r   r   s           r^   rk   zmexc.watch_spot_public  s     ~~fneD6N#34iit$V,&2!i
 ZZ[$++gv2NP[\\\\s   A/A81A62A8c                    K   | j                          | j                  |       d {   }| j                  d   d   d   dz   |z   }d|gd}| j                  ||| j	                  ||      |       d {   S 7 Q7 w)Nr3   r   r4   ?listenKey=r   r   )check_required_credentialsauthenticaterU   r   r   )r\   ro   rn   rm   rQ   r   r   s          r^   watch_spot_privatezmexc.watch_spot_private  s     '')++G44	iit$V,}<yH$i
 ZZ[$++gv2NPWXXX 5 Ys"   %A=A9AA=4A;5A=;A=c                    K   | j                   d   d   d   }||d}| j                  ||      }| j                  ||||       d {   S 7 w)Nr3   r   r5   r   param)rU   r   r   )r\   ro   rn   rp   rm   r   r   r   s           r^   rl   zmexc.watch_swap_public  sX     iit$V,"
 ++gv.ZZ[';GGGGs   AAAAc                   K   | j                          d}| j                  d   d   d   }t        | j                               }| j                  |z   }| j                  | j                  |      | j                  | j                        t        j                        }|| j                  ||dd}| j                  ||      }	| j                  |||	|       d {   S 7 w)Nloginr3   r   r5   )apiKey	signaturereqTimer   )r   rU   strmillisecondsr   hmacencodesecrethashlibsha256r   r   )
r\   rn   rm   ro   r   rx   payloadr   r   r   s
             r^   watch_swap_privatezmexc.watch_swap_private  s     '')iit$V,))+,	++	)IIdkk'2DKK4Lgnn]	++&$
 ++gv.ZZ['7CCCCs   CCCCrA   sincelimitc                   K   | j                          d{    | j                  |      }|d   }| j                  | j                  di       }| j	                  ||      }d|z   dz   |z   }	d}
|d   r*d|d   z   d	z   |z   }| j                  ||	|       d{   }
n&d
}|d   |d}| j                  ||	||       d{   }
| j                  r|
j                  ||      }| j                  |
||dd      S 7 7 `7 ;w)a  

        https://mexcdevelop.github.io/apidocs/spot_v3_en/#kline-streams

        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
        Nr`   rP   candles::r4   spot@public.kline.v3.api@re   @z	sub.kliner`   intervalr   T)
rh   ri   
safe_valuerV   r   rk   rl   r   getLimitfilter_by_since_limit)r\   r`   	timeframer   r   rm   ri   rP   timeframeIdrn   ohlcvro   rp   s                r^   watch_ohlcvzmexc.watch_ohlcv  s     !!!V$!__T\\<D
&&z9= 6)C/);&>1F4L@3FTG00+vNNE!G ,'#M 00+}V\]]E??NN651E))%q$GG' 	" O ^s4   C;C5A?C;C7&C;=C9>8C;7C;9C;c                    | j                  |ddi       }| j                  |d|      }| j                  |dd      }| j                  | j                  di       }| j	                  ||      }| j                  |dd      }| j                  |      }	|	d   }
d	|
z   d
z   |z   }| j                  ||	      }| j                  | j                  |
i       | j                  |
<   | j                  | j                  |
   |      }|:| j                  | j                  dd      }t        |      }|| j                  |
   |<   |j                  |       |j                  ||       y )Nrt   ru   kr   r  rP   rv   r`   r   r   
OHLCVLimit  )safe_value_2r  r|   rV   find_timeframer~   parse_ws_ohlcvohlcvsr}   r   r   r   )r\   rr   r   rt   rawOhlcvr	  rP   r  r   ri   r`   rn   parsedstoredr   s                  r^   handle_ohlcvzmexc.handle_ohlcv1  sI   ^ gsFB7??1c1-((3
C__T\\<D
''Z@	%%gsH=!!(+! 6)C/);$$Xv6"oodkk62FFV!4i@>%%dllL$GE*51F-3DKK	*fv{+r_   c           
          | j                  |d      | j                  |d      | j                  |d      | j                  |d      | j                  |d      | j                  |dd      gS )Nrw   or   r   r   r   r   )safe_timestampr   safe_number_2)r\   r
  ri   s      r^   r  zmexc.parse_ws_ohlcvs  sq    D s+UC(UC(UC(UC(uc3/
 	
r_   c                 Z  K   | j                          d{    | j                  |      }|d   }d|z   }d}|d   r3d|d   z   }| j                  |||       d{   }|j	                         S d}d|d   i}| j                  ||||       d{   }|j	                         S 7 7 K7 w)a  

        https://mexcdevelop.github.io/apidocs/spot_v3_en/#diff-depth-stream
        https://mexcdevelop.github.io/apidocs/contract_v1_en/#public-channels

        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
        Nr`   
orderbook:r4   "spot@public.increase.depth.v3.api@re   z	sub.depth)rh   ri   rk   rl   r   )	r\   r`   r   rm   ri   rn   	orderbookro   rp   s	            r^   watch_order_bookzmexc.watch_order_book  s      !!!V$!"V+	&>:VD\IG"44Wk6RRI    "G&,#M #44Wk=Z`aaI   	" S bs4   B+B%AB+B'4B+B)B+'B+)B+c                     | j                  |d      }|j                  d      }| j                  |d      }| j                  |      }| j                  i       | j                  |<   y )Nmsgr     )r   splitr   
order_book
orderbooks)r\   rr   r   r"  partsr   r`   s          r^   handle_order_book_subscriptionz#mexc.handle_order_book_subscription  s\     w.		###E1-!!(+"&//""5r_   c                    | j                  |d      }| j                  |d      }| j                  |dd      }||dz
  k  ryt        dt	        |            D ]#  }||   }| j                  |dd      }||k\  s!|c S  t	        |      S )Nnoncer   rversion   )r}   r  safe_integer_2r   r   )	r\   r  cacher*  
firstDeltafirstDeltaNoncer   delta
deltaNonces	            r^   get_cache_indexzmexc.get_cache_index  s    !!)W5__UA.
--j#yI?Q&&q#e*% 	A!HE,,UCCJU"		
 5zr_   c                 R   | j                  |dd      }| j                  |dd      }| j                  |      }d|z   }| j                  |j                  |      }| j                  |d      }|| j                  vr| j                         | j                  |<   | j                  |   }	| j                  |	d      }
|
it        |	j                        }| j                  dd	d
      }||k(  r | j                  | j                  ||||i        |	j                  j                  |       y 	 | j                  |	|       | j                  |dd      }||	d<   | j!                  |      |	d<   |j'                  |	|       y # t"        $ r)}|j                  |= |j%                  ||       Y d }~@d }~ww xY w)Nrt   ru   rv   r`   r  r   r*  r&   rM   rK   rw   tsrx   ry   )r  r|   r   r  subscriptionsr}   r&  r%  r   r0  handle_optionspawnload_order_bookr   handle_deltar/  r   	Exceptionrejectr   )r\   rr   r   ru   r   r`   rn   subscriptionr   storedOrderBookr*  cacheLengthrM   rx   es                  r^   handle_order_bookzmexc.handle_order_book  s   X   #v6%%gsH=!!(+"V+v';';[I!!,8$//)&*oo&7DOOF#//&1!!/7;=o334K ../?RTUMm+

4//feUWX!!((.	*ot4++GS$?I+4OK(*.,,y*AOJ' 	4  	*$$[1MM![))	*s   #>E4 4	F&=F!!F&c                     t        dt        |            D ]_  }||   }t        |t              r|j	                  |       *| j                  |d      }| j                  |d      }|j                  ||       a y )Nr   r   r   )r   r   
isinstancelist
storeArray
safe_floatstore)r\   booksidebidasksr   bidaskr   amounts          r^   handle_bookside_deltazmexc.handle_bookside_delta  sm     q#g,' 	.AQZF&$'##F+45uf-	.r_   c                    | j                  |d      }| j                  |dd      }||k  ry ||d<   | j                  |dg       }| j                  |dg       }|d   }|d   }| j                  ||       | j                  ||       y )Nr*  r+  r,  asksbids)r}   r/  	safe_listrN  )	r\   r  r3  existingNoncer4  rP  rQ  asksOrderSidebidsOrderSides	            r^   r<  zmexc.handle_delta(  s    )))W=((Y?
% '	'~~eVR0~~eVR0!&)!&)""=$7""=$7r_   c                   K   | j                          d{    | j                  |      }|d   }d|z   }d}|d   r$d|d   z   }| j                  |||       d{   }n%d}d|d   i}	| j                  |||	|       d{   }| j                  r|j                  ||      }| j                  |||dd	      S 7 7 _7 ;w)
at  

        https://mexcdevelop.github.io/apidocs/spot_v3_en/#trade-streams
        https://mexcdevelop.github.io/apidocs/contract_v1_en/#public-channels

        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>`
        Nr`   trades:r4   spot@public.deals.v3.api@re   zsub.dealrx   T)rh   ri   rk   rl   r   r  r  )
r\   r`   r   r   rm   ri   rn   tradesro   rp   s
             r^   watch_tradeszmexc.watch_trades7  s      !!!V$!&(&>1F4L@G11';OOF G&,#M  11';W]^^F??OOFE2E))&%TRR! 	" P _s4   B?B9AB?B;%B?B=8B?;B?=B?c                 6   | j                  |dd      }| j                  |      }|d   }d|z   }| j                  | j                  |      }|7| j	                  | j
                  dd      }t        |      }|| j                  |<   | j                  |dd      }	| j                  |	d|	g      }
t        d	t        |
            D ]E  }d }|d
   r| j                  |
|   |      }n| j                  |
|   |      }|j                  |       G |j                  ||       y )Nrv   r`   rW  tradesLimitr  rt   ru   dealsr   r4   )r|   r~   r  rY  r}   rV   r   r  r   r   parse_ws_tradeparse_trader   r   )r\   rr   r   r   ri   r`   rn   r  r   rt   rY  jparsedTrades                r^   handle_tradeszmexc.handle_tradesV  s   @ %%gsH=!!(+!&(f5>%%dllM4HE&F"(DKKgsF3GaS1q#f+& 	'AKf~"11&)VD"..vay&AMM+&	' 	v{+r_   c                   K   | j                          d{    d}d}|| j                  |      }|d   }|dz   |z   }d}| j                  d||      \  }}d}|dk(  rd}	| j                  |	||       d{   }n| j	                  ||       d{   }| j
                  r|j                  ||      }| j                  ||||d      S 7 7 T7 ;w)	a  

        https://mexcdevelop.github.io/apidocs/spot_v3_en/#spot-account-deals
        https://mexcdevelop.github.io/apidocs/contract_v1_en/#private-channels

        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myTradesr`   r   r$   r4   zspot@private.deals.v3.apiT)rh   ri   r   r   r   r   r  filter_by_symbol_since_limit)
r\   r`   r   r   rm   rn   ri   r   rY  ro   s
             r^   watch_my_tradeszmexc.watch_my_trades  s      !!! [[(FH%F%+f4K99/6SYZf6>1G227KPPF22;GGF??OOFE2E00tTT# 	" QGs4   CC	AC6C7CC8CCCc                    d}| j                  |dd      }| j                  |d      }| j                  |d|      }| j                  |      }|d   }	d }
|d   r| j                  ||      }
n| j	                  ||      }
| j
                  }|/| j                  | j                  dd      }t        |      }|| _        |j                  |
       |j                  ||       |d	z   |	z   }|j                  ||       y )
Nrd  rt   ru   r`   rv   r4   r\  r  r   )r  r   r~   r^  r_  rd  r}   rV   r   r   r   )r\   rr   r   r?  rn   ru   futuresMarketIdr   ri   r`   traderY  r   symbolSpecificMessageHashs                 r^   handle_my_tradezmexc.handle_my_trade  s    & !  #v6**4:##GS/B!!(+!&>''f5E$$T62E>%%dllM4HE+E2F"DMev{+$/#$5$>!v89r_   c                 *   | j                  |d      }| j                  |d      }|| j                  |d      }d }| j                  |d      }| j                  |d      }| j                  |d      }|dk(  rdnd}| j                  |d	      }	| j                  |d
      }
| j                  |d      }| j                  ||| j                  |d      || j	                  |      | j                  d |      d ||	rdnd||d |
| j                  |      dd|      S )NTrw   r   r   S1buysellmnNr   makertaker)costcurrency)r   re   orderrx   ry   r`   r   sidetakerOrMakerr   rM  rw  fee)r}   r   r   
safe_trader   r   safe_currency_code)r\   ri  ri   rx   tradeIdpriceStringamountStringrawSiderz  isMaker	feeAmountfeeCurrencyIds               r^   r^  zmexc.parse_ws_trade  s;   J %%eS1	""5#.))%5IG&&uc2''s3""5#. Cuf##E3/$$UC0	((4%%eS1"Y/&&tV4(/Gg "! 33MB 
" # 	r_   c                   K   | j                          d{    | j                  |d      }d}d}|| j                  |      }|d   }|dz   |z   }d}| j                  d||      \  }}d}|dk(  r!|dz   }	| j	                  |	||       d{   }n| j                  ||       d{   }| j                  r|j                  ||      }| j                  ||||d	      S 7 7 T7 ;w)
a  

        https://mexcdevelop.github.io/apidocs/spot_v3_en/#spot-account-orders
        https://mexcdevelop.github.io/apidocs/spot_v3_en/#margin-account-orders

        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|None params['type']: the type of orders to retrieve, can be 'spot' or 'margin'
        :returns dict[]: a list of `order structures <https://docs.ccxt.com/#/?id=order-structure>`
        Nr   ordersr`   r   r'   r4   z@private.orders.v3.apiT)	rh   r   ri   r   r   r   r   r  re  )
r\   r`   r   r   rm   rn   ri   r   r  ro   s
             r^   watch_orderszmexc.watch_orders  s     !!!66*[[(FH%F%+f4K99-QWXf6>55G227KPPF22;GGF??OOFE2E00tTT% 	" QGs4   C$CA4C$C C$&C"'8C$ C$"C$c                    d}| j                  |dd      }| j                  |d      }| j                  |d|      }| j                  |      }|d   }d }	|d   r| j                  ||      }	n| j	                  ||      }	| j
                  }
|
/| j                  | j                  dd      }t        |      }
|
| _        |
j                  |	       |j                  |
|       |d	z   |z   }|j                  |
|       y )
Nr  rt   ru   r`   rv   r4   ordersLimitr  r   )r  r   r~   parse_ws_orderparse_orderr  r}   rV   r   r   r   )r\   rr   r   rn   ru   rh  r   ri   r`   r  r  r   rj  s                r^   handle_orderzmexc.handle_order8  s    D   #v6**4:##GS/B!!(+!&>((v6F%%dF3F>%%dllM4HE+E2F DKfv{+$/#$5$>!v89r_   c                 (   | j                  |d      }| j                  |d      }| j                  |d      }| j                  |d      }d }| j                  |d      }||d d}| j                  i d| j                  |d      d	| j                  |d
      d|d| j                  |      dd d| j	                  ||      d| j                  d |      d| j                  |      d| j                  |      d|dk(  rdndd| j                  |d      dd d| j                  |d      d| j                  |d      d| j                  |d      d| j                  |d       d!| j                  |d"      | j                  |d#      |d |d$|      S )%NOrn  rv   r  rt  )rx  rw  re   r   clientOrderIdr   rx   ry   lastTradeTimestampstatusr`   r   timeInForcerz  ro  rp  rq  r   r   	stopPricetriggerPricePr   aprM  r   rw  r   filledcvV)	remainingr|  rY  r   )	r}   r   
safe_orderr   parse_ws_order_statusr   parse_ws_order_typeparse_ws_time_in_forcer   )	r\   ry  ri   rx   rz  r  r   r|  feeCurrencys	            r^   r  zmexc.parse_ws_order  s%   f %%eS1	s+!!%-s+&&uc2"'C   
$""5#. 
T--eS9 
  
 Y/	 

 !$ 
 d00@ 
 d&&tV4 
 D,,T2 
 466t< 
 dckE 
 T%%eS1 
  
 D,,UC8 
 t''t4 
 d&&uc2 
  D$$UC0! 
" d&&ud3# 
$ ))%5+ 
, - 	r_   c           
      @    dddddddddd	}| j                  |||      S )Nr   closedcanceledrejected)	ro  2345NEWCANCELEDEXECUTEDFAILEDr   )r\   r  ri   statusess       r^   r  zmexc.parse_ws_order_status  s;    "  

 &&99r_   c                 8    dd d d ddd}| j                  ||      S )Nr   ri   ro  r  r  r  r  100r  )r\   r   typess      r^   r  zmexc.parse_ws_order_type  s0    
 t,,r_   c                 8    ddddddd}| j                  ||      S )NGTCPOIOCFOKr  r  )r\   r  timeInForceIdss      r^   r  zmexc.parse_ws_time_in_force  s0     
 <<r_   c                    K   | j                          d{    d}| j                  dd|      \  }}d|z   }|dk(  rd}| j                  |||       d{   S | j                  ||       d{   S 7 ]7  7 w)am  

        https://mexcdevelop.github.io/apidocs/spot_v3_en/#spot-account-upadte

        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>`
        Nr#   balance:r4   zspot@private.account.v3.api)rh   r   r   r   )r\   rm   r   rn   ro   s        r^   watch_balancezmexc.watch_balance	  s      !!!99.$PVWf 4'6>3G00+vNNN00fEEE 	" OEs3   A:A4>A:A6A:/A80A:6A:8A:c                    | j                  |d      }|dnd}d|z   }| j                  |dd      }| j                  |d      }| j                  |d|      }|| j                  vri | j                  |<   || j                  |   d<   || j                  |   d	<   | j	                  |      | j                  |   d
<   | j                  |dd      }	| j                  |	      }
| j                         }| j                  |dd      |d<   | j                  |dd      |d<   || j                  |   |
<   | j                  | j                  |         | j                  |<   |j                  | j                  |   |       y )Nr   r5   r4   r  rt   ru   r7  r   rx   ry   r   rx  favailableBalancefreer   frozenBalanceused)
r   r  r}   balancer   r|   r~  accountsafe_balancer   )r\   rr   r   r   r   rn   ru   futuresTimestamprx   
currencyIdcoder  s               r^   handle_balancezmexc.handle_balance  sp   @ Wc*)v& 4'  #v6,,Wd;%%dC1AB	$!#DLL%)T6"*3T;')-i)@T:&''c:>
&&z2,,.,,T38JK,,T3H#*T4 !..t||D/ABTt||D);7r_   c                   K   | j                          d{    | j                  |      }d|d   z   }d}d}|d   r\d}| j                  |dd      \  }}|rd|d	   z   d
z   }nd|d	   z   }| j                  d   d   d   }d|d<   | j	                  |||       n2d}d|d	   i}| j                  d   d   d   }| j                  ||||       | j                  |      }	| j                  |	|g       y7 ٭w)a  
        unWatches a price ticker, a statistical calculation with the information calculated over the past 24 hours for all markets of a specific list
        :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>`
        Nunsubscribe:ticker:r`   r4   Fr(   rc   rd   re   rf   rg   r3   r   Tr   zunsub.tickerr5   )rh   ri   rj   rU   rk   rl   rr   handle_unsubscriptions)
r\   r`   rm   ri   rn   r   ro   rc   rp   rr   s
             r^   un_watch_tickerzmexc.un_watch_tickerP  s*     !!!V$+fX.>>&>J!%!>!>v}Vb!cJ:VD\IHT:VD\I))E"4(0C%)F>"""7K@$G&,#M ))E"4(0C""7KOS!##F[M:1 	"s   C2C0CC2c                    K   | j                          d{    | j                  |d      }g }| j                  |d      }d}|| j                  |      }d}| j	                  d||      \  }}|dk(  }|r| j
                  d   d   d   n| j
                  d   d   d   }i }	|rd}
| j                  |dd	      \  }
}g }|
sx|t        | j                  d
z         | j                  |      }t        dt        |            D ]4  }||   }|j                  d||   z          d|z   }|j                  |       6 nV|j                  d       ||j                  d       n1t        dt        |            D ]  }|j                  d||   z           d|	d<   ||	d<   nd|	d<   i |	d<   |j                  d       | j                  |      }| j                  ||| j                  |	|      |       | j!                  ||       y7 w)a  
        unWatches 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   r)   r4   r3   r   r5   Frc   r   r  rg   r   zunsubscribe:spot:tickerr   r   rm   zunsub.tickerszunsubscribe:ticker)rh   r   r   ri   r   rU   rj   r   re   r   r   r   r   rr   r   r   r  )r\   r   rm   r   r   ri   r   r   r   r   rc   r   r   r   r   ro   rr   s                    r^   un_watch_tickerszmexc.un_watch_tickersq  s/     !!!%%gt4&&w2"[[-F99.&RXYf&.17diit$V,dii>Nt>TU[>\J!%!>!>v~Wc!dJF?+DGG6z,z{{ OOG4	q#i.1 +A(|H!(()>)KL>IGMM'*	+ DE?!(()BC"1c'l3 Q%,,-BWQZ-OPQ 0GH &GH /GH "GH  !56S!CGV0Lm\##FM:S 	"s   G>G;G%G>c                   K   | j                          d{    | j                  |dddd      }d}|t        | j                  dz         | j	                  |      }| j                  d|d   |      \  }}|dk(  }|st        | j                  dz         g }g }t        dt        |            D ]F  }|r+| j                  ||         }	|j                  d	|	d
   z          |j                  d||   z          H | j                  d   d   d   }
d|d}| j                  |
      }| j                  |
|| j                  ||      |       | j                  ||       y7 Cw)a9  
        unWatches 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>`
        NTFr   r*   r   r4   r   rg   re   unsubscribe:bidask:r3   r   r   r   )rh   r   r   re   r   r   r   r   r   ri   r   rU   rr   r   r   r  )r\   r   rm   r   r   r   r   r   r   ri   r   r   rr   s                r^   un_watch_bids_askszmexc.un_watch_bids_asks  s{     !!!%%gtT5$G
?#DGG.X$XYY**73!??QXYZQ[]cd
Fv%tww)RRSSq#g,' 	EAWQZ0>MN  !6!CD		E
 iit$V,&
 S!CGV0Lm\##FM:3 	"s   EEEEc                   K   | j                          d{    | j                  |      }|d   }| j                  | j                  di       }| j	                  ||      }d|z   dz   |z   }d}|d   r<| j
                  d   d   d   }d	|d
   z   dz   |z   }	d|d<   | j                  |	||       n3| j
                  d   d   d   }d}	|d
   |d}
| j                  |	||
|       | j                  |      }| j                  ||g       y7 w)aJ  
        unWatches 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 dict [params]: extra parameters specific to the exchange API endpoint
        :param dict [params.timezone]: if provided, kline intervals are interpreted in that timezone instead of UTC, example '+08:00'
        :returns int[][]: A list of candles ordered, open, high, low, close, volume
        Nr`   rP   zunsubscribe:candles:r   r4   r3   r   r  re   r  Tr   r5   zunsub.kliner  )
rh   ri   r  rV   r   rU   rk   rl   rr   r  )r\   r`   r  rm   ri   rP   r	  rn   r   ro   rp   rr   s               r^   un_watch_ohlcvzmexc.un_watch_ohlcv  s2     !!!V$!__T\\<D
&&z9=,v5;iG&>))E"4(0C1F4L@3FTG%)F>"""7K@))E"4(0C#G ,'#M ""7KOS!##F[M:- 	"s   DDC0Dc                   K   | j                          d{    | j                  |      }|d   }d|z   }d}|d   r6| j                  d   d   d   }d|d   z   }d	|d
<   | j                  |||       n2| j                  d   d   d   }d}d|d   i}| j	                  ||||       | j                  |      }| j                  ||g       y7 w)a  
        unWatches information on open orders with bid(buy) and ask(sell) prices, volumes and other data
        :param str symbol: 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
        Nr`   unsubscribe:orderbook:r4   r3   r   r  re   Tr   r5   zunsub.depthrh   ri   rU   rk   rl   rr   r  	r\   r`   rm   ri   rn   r   ro   rp   rr   s	            r^   un_watch_order_bookzmexc.un_watch_order_book  s      !!!V$!.7&>))E"4(0C:VD\IG%)F>"""7K@))E"4(0C#G&,#M ""7KOS!##F[M:' 	"   CC
B4Cc                   K   | j                          d{    | j                  |      }|d   }d|z   }d}|d   r6| j                  d   d   d   }d|d   z   }d	|d
<   | j                  |||       n2| j                  d   d   d   }d}d|d   i}| j	                  ||||       | j                  |      }| j                  ||g       y7 w)a  
        unsubscribes from the trades channel
        :param str symbol: unified symbol of the market to fetch trades for
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :param str [params.name]: the name of the method to call, 'trade' or 'aggTrade', default is 'trade'
        :returns dict[]: a list of `trade structures <https://docs.ccxt.com/#/?id=public-trades>`
        Nr`   unsubscribe:trades:r4   r3   r   rX  re   Tr   r5   z
unsub.dealr  r  s	            r^   un_watch_tradeszmexc.un_watch_trades  s      !!!V$!+f4&>))E"4(0C1F4L@G%)F>"""7K@))E"4(0C"G&,#M ""7KOS!##F[M:' 	"r  r   c                 p   t        dt        |            D ]  }||   }|j                  dd      }| j                  |||       |j	                  d      dk\  r|j                  dd      }|j	                  d      dk\  rNt        | j                  j                               }t        dt        |            D ]  }| j                  ||   =  || j                  v s| j                  |= |j	                  d      dk\  r1|j                  dd      }|| j                  v s	| j                  |= |j	                  d	      dk\  rh|j                  d
      }	| j                  |	d      }t        |	      dkD  r|d
| j                  |	d      z   z  }|| j                  v s| j                  |= |j	                  d      dk\  r1|j                  dd      }|| j                  v s| j                  |= |j	                  d      dk\  s|j                  dd      }|| j                  v s| j                  |=   y )Nr   zunsubscribe:r   r   r  unsubscriberL  r  candlesr   r#     rL   r  r  rY  r  )r   r   replaceclean_unsubscriptionfindrF  r   keysr   r$  r   r  r&  rY  )
r\   rr   r   r   rn   subMessageHashr`   r   r`  splitHashess
             r^   r  zmexc.handle_unsubscriptions  s   q#m,- 	,A'*K(00DN%%fnkJ)Q.$,,-BBG;;}-2"4<<#4#4#67G"1c'l3 5 LL45t||+V,!!(+q0$,,-BBGT]]*f-!!),1)//4))+q9{#a'cD$4$4[!$DDDFT[[(F+!!+.!3$,,-ErJT__,/!!(+q0$,,-BBGT[[(F+?	,r_   c                 <  K   | j                  | j                  d      }||S | j                  |       d {   }| j                  |d      }|| j                  d<   | j                  | j                  dd      }| j	                  || j
                  ||       |S 7 bw)NrQ   rO   r6   )r   rV   spotPrivatePostUserDataStreamr}   delaykeep_alive_listen_key)r\   subscriptionHashrm   rQ   responserO   s         r^   r   zmexc.authenticateA  s     $$T\\;?	 ;;FCC $$X{;	$-[!#00?UW^_

')C)CYPVW Ds   5BBA#Bc                   K   |y d|i}	 | j                  | j                  ||             d {    | j                  | j                  dd      }| j	                  || j
                  ||       y 7 @# t        $ rc}| j                  d   d   d   dz   |z   }| j                  |      }d | j                  d<   |j                  |       | j                  |= Y d }~y d }~ww xY ww)NrQ   rO   r6   r3   r   r4   r   )spotPrivatePutUserDataStreamr   r}   rV   r  r  r=  rU   rr   r>  clients)r\   rQ   rm   r   rO   errorr   rr   s           r^   r  zmexc.keep_alive_listen_keyR  s     
		"33DKK4PQQQ#'#4#4T\\CY[b#c JJ+T-G-GTZ[ R  	"))E"4(0=@9LC[[%F(,DLL%MM% S!	"s?   C!$A2 A0?A2 /C!0A2 2	C;ACC!CC!c                 0    | j                         |_        |S N)r   lastPong)r\   rr   r   s      r^   handle_pongzmexc.handle_pongc  s    ++-r_   c                 "   | j                  |dd      }|dk(  r| j                  ||       y |j                  d      dkD  rP|j                  d      }| j                  |d      }d| j                  i}| j                  ||      }|
 |||       y y y )Nr"  r   PONGr  r.  r-  public.increase.depth.v3.api)r   r  r  r$  r(  r  )r\   rr   r   r"  r'  ro   methodsr   s           r^   handle_subscription_statuszmexc.handle_subscription_statusg  s     wr2&=VW-XXc]RIIcNE&&ua0G.0S0SG __Wg6F!vw' "  r_   c                 H   t        |t              r/|dk(  r)t        | j                  dz         }|j	                  |       y d|v r| j                  ||       y | j                  |d      }d }|| j                  |d      }n#|j                  d      }| j                  |d      }i d| j                  d	| j                  d
| j                  d| j                  d| j                  d| j                  d| j                  d| j                  d| j                  d| j                  d| j                  d| j                  d| j                  d| j                  d| j                  d| j                  d| j                  d| j                   i}||v r||   } |||       y y )NzInvalid listen keyz invalid listen keyr"  r   ro   r  r-  zpublic.deals.v3.apiz	push.dealzpublic.kline.v3.apiz
push.klinezpublic.bookTicker.v3.apizpublic.miniTicker.v3.apizpublic.miniTickers.v3.apizpush.tickerzpush.tickersr  z
push.depthzprivate.orders.v3.apizpush.personal.orderzprivate.account.v3.apizpush.personal.assetzprivate.deals.v3.apizpush.personal.order.dealpong)rE  r   r   re   r>  r  r   r$  rb  r  r   r   rC  r  r  rk  r  )	r\   rr   r   r  r   ro   r'  r  r   s	            r^   handle_messagezmexc.handle_message|  s
   gs#..+DGG6K,KLe$G++FG<Wc*9&&w	:GGGCLE&&ua0G
!4#5#5
++
 "4#4#4
 $++	

 '(:(:
 '(:(:
 ()<)<
 4--
 D//
 +D,B,B
 $00
 $T%6%6
 "4#4#4
 %d&9&9
 "4#6#6
  #D$8$8!
" '(<(<#
$ D$$%
( gW%F67# r_   c                 
    ddiS )Nr   rR    )r\   rr   s     r^   rR   z	mexc.ping  s    &!!r_   r  )B__name__
__module____qualname__r   r[   r   r   rq   r   r   r   r   r   r   r   r   rz   r   rk   r   rl   r   r   r   rF  r  r  r  r
   r   r(  r5  rC  rN  r<  r   rZ  rb  r   rf  rk  r^  r	   r  r  r  r  r  r  r   r  r  r  r  r  r  r  r  r  r   r  r  r  r  rR   __classcell__)r]   s   @r^   r   r      s   A# AF 68 ] ]F ]@S,F S,j 6:" 8E7 8E 8EtT&V T&l4l 8<B %FW %F7 %FN2V 2," DF 	] EG Y SU H <> D$ 8<$]ajl  H  HC  HWZ  Hquvzq{  HD@,6 @,D(
D (
T @DB !S ! !S\ !86V 6E5 E5N. 8 <@d[] S SS S Sbfglbm S>2,F 2,h 37TX\eg UC Us URU Ulpqvlw U@&:f &:PBH 04$UYbd  U  UC  Us  Uimnsit  UDU:6 U:nTl:	-	= *, F F&28V 28h 9; C s B 9=R 0g 0C 0d ;?r    c  D ;?r 3 c B =?  3 8 9; C s : ,V  ,DI  ,D ;= " =? ""& ( (*&$V &$P"6 "r_   r   )ccxt.async_supportccxt ccxt.async_support.base.ws.cacher   r   r   r   ccxt.base.typesr   r   r   r	   r
   r   r   r   r   r   !ccxt.async_support.base.ws.clientr   typingr   ccxt.base.errorsr   r   r   async_supportr   r  r_   r^   <module>r     sE     f f  f f f 4  0 . )T"4"" T"r_   