
    ":h                         d dl Zd dlmZmZmZmZ d dlZd dl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j:                  j<                        Zy)    N)
ArrayCacheArrayCacheBySymbolByIdArrayCacheBySymbolBySideArrayCacheByTimestamp)
AnyIntOrder	OrderBookPositionStrStringsTickerTickersTrade)Client)List)ExchangeError)AuthenticationError)ArgumentsRequiredc            
           e Zd Zdef 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
d
efdZd,dZdi fdededefdZdi fdee   dedefdZi fdZd
ef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ddi fdedededee   fdZddi fdeee      dedefdZd
efdZd,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e$   fdZ%d
efdZ&d
efd Z'd,d
edefd!Z(d" Z)d# Z*i fd$Z+d
efd%Z,d
efd&Z-d
efd'Z.d
efd(Z/d
efd)Z0d
efd*Z1d
efd+Z2 xZ3S )-apexreturnc                     | j                  t        t        |          dddddddddddddddddidd	d
didddddi | j                  ddd      S )NTF)wswatchTickerwatchTickerswatchOrderBookwatchOrderswatchTradeswatchTradesForSymbolswatchPositionswatchMyTradeswatchBalance
watchOHLCVz;https://omni.apex.exchange/assets/logo_content-CY9uyFbz.svgr   z2wss://quote.omni.apex.exchange/realtime_public?v=2z3wss://quote.omni.apex.exchange/realtime_private?v=2)publicprivatez5wss://qa-quote.omni.apex.exchange/realtime_public?v=2z6wss://qa-quote.omni.apex.exchange/realtime_private?v=2zhttps://apex.exchange/z"https://api-docs.pro.apex.exchangez`https://apex-pro.gitbook.io/apex-pro/apex-omni-live-now/trading-perpetual-contracts/trading-feesz https://omni.apex.exchange/trade)logoapitestwwwdocfeesreferraliPF  )ping	keepAlive)hasurlsoptions	streaming)deep_extendsuperr   describer.   )self	__class__s    G/var/www/html/turnos/venv/lib/python3.12/site-packages/ccxt/pro/apex.pyr6   zapex.describe   s    dD : <# $"&##)."&!% %" V"V#X "Y#[ 0;z>#& 		"E&?
 & &	    Nsymbolsincelimitc                 H   K   | j                  |g|||       d{   S 7 w)aC  
        watches information on multiple trades made in a market

        https://api-docs.pro.apex.exchange/#websocket-v3-for-omni-websocket-endpoint

        :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)watch_trades_for_symbols)r7   r;   r<   r=   paramss        r9   watch_tradeszapex.watch_trades>   s'      22F8UE6RRRRs   " "symbolsc                   K   | j                          d{    | j                  |      }t        |      }|dk(  rt        | j                  dz         t        | j                               }| j                  d   d   d   dz   |z   }g }g }	t        dt        |            D ]G  }
||
   }| j                  |      }d|d	   z   }|j                  |       d
|z   }|	j                  |       I | j                  ||	||       d{   }| j                  r6| j                  |d      }| j                  |d      }|j                  ||      }| j!                  |||dd      S 7 H7 ^w)a4  
        get the list of most recent trades for a list of symbols

        https://api-docs.pro.apex.exchange/#websocket-v3-for-omni-websocket-endpoint

        :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   z> watchTradesForSymbols() requires a non-empty array of symbolsr(   r   r%   &timestamp=zrecentlyTrade.H.id2trade:r;   	timestampT)load_marketsmarket_symbolslenr   idstrmillisecondsr1   rangemarketappendwatch_topics
newUpdates
safe_valuesafe_stringgetLimitfilter_by_since_limit)r7   rB   r<   r=   r@   symbolsLength	timeStampurltopicsmessageHashesir;   rO   topicmessageHashtradesfirsttradeSymbols                     r9   r?   zapex.watch_trades_for_symbolsL   sk     !!!%%g.GA#DGG.n$noo))+,	iit$X.>Jq#g,' 	.AQZF[[(F&6EMM% "V+K  -	. ((mVVLL??OOFA.E**5(;KOOK7E))&%TRR+ 	"  Ms#   E#EC+E#E!AE#!E#clientc                     | j                  |di       }| j                  |d      }|}|j                  d      }| j                  |d      }| j                  |d d       }|d   }	| j                  | j                  |	      }
|
7| j                  | j                  dd      }t        |      }
|
| j                  |	<   t        dt        |            D ](  }| j                  ||   |      }|
j                  |       * d	|	z   }|j                  |
|       y )
Ndatar]   .   r;   tradesLimit  r   rF   )rS   rT   splitsafe_marketr_   safe_integerr2   r   rN   rJ   parse_ws_traderP   resolve)r7   rb   messagerd   r]   r_   partsmarketIdrO   r;   storedr=   jparsedr^   s                  r9   handle_tradeszapex.handle_tradeso   s
   ( w3  '2C ##E1-!!(D$7!f5>%%dllM4HE&F"(DKKq#f+& 	"A((F;FMM&!	" $f,v{+r:   c                    | j                  |g d      }| j                  |ddg      }| j                  ||d       }|d   }| j                  |g d      }| j                  |ddg      }| j                  |ddg      }| j                  |g d	      }	| j	                  |||| j                  |      |d d |d ||	d d d
|      S )N)r\   rK   vsr;   )tT	createdAtSsidepprice)qrv   size)rK   inforG   datetimer;   ordertyper|   takerOrMakerr~   amountcostfee)safe_string_nrj   safe_integer_nsafe_string_lower_n
safe_tradeiso8601)
r7   traderO   rK   rp   r;   rG   r|   r~   r   s
             r9   rl   zapex.parse_ws_trade   s     '78%%ec8_=!!(FD9!''/FG	''V}=""53.9##E+=>"Y/  
  	r:   c                 F   K   | j                  |g||       d{   S 7 w)aK  
        watches information on open orders with bid(buy) and ask(sell) prices, volumes and other data

        https://api-docs.pro.apex.exchange/#websocket-v3-for-omni-websocket-endpoint

        :param str symbol: unified symbol of the market to fetch the order book for
        :param int [limit]: the maximum amount of order book entries to return.
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: A dictionary of `order book structures <https://docs.ccxt.com/#/?id=order-book-structure>` indexed by market symbols
        N)watch_order_book_for_symbols)r7   r;   r=   r@   s       r9   watch_order_bookzapex.watch_order_book   s%      66xOOOOs   !!c                 b  K   | j                          d{    t        |      }|dk(  rt        | j                  dz         | j	                  |      }t        | j                               }| j                  d   d   d   dz   |z   }g }g }t        dt        |            D ]Z  }	||	   }
| j                  |
      }|d}d	t        |      z   d
z   |d   z   }|j                  |       d|
z   }|j                  |       \ | j                  ||||       d{   }|j                         S 7 7 w)a.  
        watches information on open orders with bid(buy) and ask(sell) prices, volumes and other data

        https://api-docs.pro.apex.exchange/#websocket-v3-for-omni-websocket-endpoint

        :param str[] symbols: unified array of symbols
        :param int [limit]: the maximum amount of order book entries to return.
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: A dictionary of `order book structures <https://docs.ccxt.com/#/?id=order-book-structure>` indexed by market symbols
        Nr   zA watchOrderBookForSymbols() requires a non-empty array of symbolsr(   r   r%   rD      	orderBookz.H.rE   
orderbook:)rH   rJ   r   rK   rI   rL   rM   r1   rN   rO   rP   rQ   r=   )r7   rB   r=   r@   rW   rX   rY   rZ   r[   r\   r;   rO   r]   r^   	orderbooks                  r9   r   z!apex.watch_order_book_for_symbols   s>     !!!GA#DGG.q$qrr%%g.))+,	iit$X.>Jq#g,' 	.AQZF[[(F}#e*,u4ve}DEMM% &/K  -	. ++COO	  ' 	"$ Ps"   D/D*C>D/D-D/-D/c                 t   K   d|d}| j                  ||      }| j                  ||||       d {   S 7 w)N	subscribeopargs)extendwatch_multiple)r7   rY   r[   rZ   r@   requestrn   s          r9   rQ   zapex.watch_topics   sB     
 ++gv.((mWmTTTTs   /868c                    | j                  |d      }|dk(  }| j                  |di       }| j                  |d      }| j                  |d d       }|d   }| j                  |dd      }	|| j                  vr| j                         | j                  |<   | j                  |   }
|r'| j                  |||	dd	      }|
j                  |       ni| j                  |d	g       }| j                  |dg       }| j                  |
d
   |       | j                  |
d   |       |	|
d<   | j                  |	      |
d<   d|z   }|
| j                  |<   |j                  |
|       y )Nr   snapshotrd   rw   r;   tsMbP?baasksbidsrG   r   r   )rT   	safe_dictrj   safe_integer_product
orderbooks
order_bookparse_order_bookreset	safe_listhandle_deltasr   rm   )r7   rb   rn   r   
isSnapshotrd   rp   rO   r;   rG   r   r   r   r   r^   s                  r9   handle_order_bookzapex.handle_order_book   s^   D 0j(
~~gvr2##D#.!!(D$7!--gtUC	$//)&*oo&7DOOF#OOF+	,,T69c3OHOOH%>>$R0D>>$R0Dy0$7y0$7%.Ik"$(LL$;Ij!'&0"+y+.r:   c                 L    | j                  |dd      }|j                  |       y )Nr      )parse_bid_ask
storeArray)r7   booksidedeltabidAsks       r9   handle_deltazapex.handle_delta*  s$    ##E1a0F#r:   c                 b    t        dt        |            D ]  }| j                  |||           y )Nr   )rN   rJ   r   )r7   r   deltasr\   s       r9   r   zapex.handle_deltas.  s0    q#f+& 	3Ahq	2	3r:   c                 0  K   | j                          d{    | j                  |      }|d   }t        | j                               }| j                  d   d   d   dz   |z   }d|z   }d|d	   z   }|g}| j                  ||g||       d{   S 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://api-docs.pro.apex.exchange/#websocket-v3-for-omni-websocket-endpoint

        :param str symbol: unified symbol of the market to fetch the ticker for
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: a `ticker structure <https://docs.ccxt.com/#/?id=ticker-structure>`
        Nr;   r(   r   r%   rD   ticker:instrumentInfo.H.rE   )rH   rO   rL   rM   r1   rQ   )	r7   r;   r@   rO   rX   rY   r^   r]   rZ   s	            r9   watch_tickerzapex.watch_ticker2  s      !!!V$!))+,	iit$X.>J&((6%=8&&s[M66JJJ 	" Ks"   BBA6BBBBc                 8  K   | j                          d{    | j                  |dd      }g }t        | j                               }| j                  d   d   d   dz   |z   }g }t        dt        |            D ]G  }||   }| j                  |      }	d|	d	   z   }
|j                  |
       d
|z   }|j                  |       I | j                  ||||       d{   }| j                  ri }|||d   <   |S | j                  | j                  d|      S 7 7 ;w)a  
        watches a price ticker, a statistical calculation with the information calculated over the past 24 hours for all markets of a specific list

        https://api-docs.pro.apex.exchange/#websocket-v3-for-omni-websocket-endpoint

        :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>`
        NFr(   r   r%   rD   r   r   rE   r   r;   )rH   rI   rL   rM   r1   rN   rJ   rO   rP   rQ   rR   filter_by_arraytickers)r7   rB   r@   r[   rX   rY   rZ   r\   r;   rO   r]   r^   tickerresults                 r9   watch_tickerszapex.watch_tickersF  s0     !!!%%gtU;))+,	iit$X.>Jq#g,' 	.AQZF[[(F,ve}<EMM% #f,K  -	. ((mVVLL??F'-F6(#$M##DLL(GDD% 	" Ms"   DDCDD:DDc                    | j                  |dd      }| j                  |dd      }| j                  |di       }d }d }|dk(  r| j                  |      }|d   }n|dk(  r|j                  d      }t	        |      }	| j                  ||	d	z
        }
| j                  |
d d       }|d   }| j                  | j                  |i       }| j                  |d
i       }| j                  ||      }| j                  |      }| j                  |dd      }||d<   | j                  |      |d<   || j                  |<   d|z   }|j                  | j                  |   |       y )Nr]    r   rd   r   r;   r   re   r   r   r   r   rG   r   r   )rT   r   parse_tickerri   rJ   rj   r   r   r   r   rm   )r7   rb   rn   r]   
updateTyperd   r;   rs   
topicPartstopicLengthrp   rO   r   	rawTickermergedrG   r^   s                    r9   handle_tickerzapex.handle_tickerd  se   ,   '26%%gvr:
~~gvr2*$&&t,FH%F7"S)Jj/K''
K!ODH%%hd;FH%F^^DLL&"=Fvvr:I[[D1F&&v.F--gtUC	'{!\\)4z%V&(t||F+[9r:   1mc                 f   K   d|d<   | j                  ||gg|||       d{   }||   |   S 7 w)a  
        watches historical candlestick data containing the open, high, low, and close price, and the volume of a market

        https://api-docs.pro.apex.exchange/#websocket-v3-for-omni-websocket-endpoint

        :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
        r$   callerMethodNameN)watch_ohlcv_for_symbols)r7   r;   	timeframer<   r=   r@   r   s          r9   watch_ohlcvzapex.watch_ohlcv  sK      &2!"33fi5H4I5RWY_``f~i(( as    1/1symbolsAndTimeframesc                   K   | j                          d{    t        | j                               }| j                  d   d   d   dz   |z   }g }g }t	        dt        |            D ]  }	||	   }
| j                  |
d      }| j                  |      }|d   }| j                  |
dd	      }| j                  | j                  ||      }|j                  d
|z   dz   |z          |j                  d|z   dz   |z           | j                  ||||       d{   \  }}}| j                  r|j                  ||      }| j                  |||dd      }| j                  |||      S 7 N7 Qw)a  
        watches historical candlestick data containing the open, high, low, and close price, and the volume of a market

        https://api-docs.pro.apex.exchange/#websocket-v3-for-omni-websocket-endpoint

        :param str[][] symbolsAndTimeframes: array of arrays containing unified symbols and timeframes to fetch OHLCV data for, example [['BTC/USDT', '1m'], ['LTC/USDT', '5m']]
        :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 dict: A list of candles ordered, open, high, low, close, volume
        Nr(   r   r%   rD   r   rE   r   1zcandle.re   ohlcv::::T)rH   rL   rM   r1   rN   rJ   rT   rO   
timeframesrP   rQ   rR   rU   rV   create_ohlcv_object)r7   r   r<   r=   r@   rX   rY   	rawHashesr[   r\   rd   symbolStringrO   unfiedTimeframetimeframeIdr;   r   rq   filtereds                      r9   r   zapex.watch_ohlcv_for_symbols  s     !!!))+,	iit$X.>J	q#234 	TA'*D++D!4L[[.F!%=L"..tQ<O**4??O_]KY4s:\IJ  \!9D!@?!RS	T +/*;*;CPY[a*b$b!	6??OOFE2E--feUAtL''	8DD% 	" %cs#   E)E$C>E)E'AE)'E)c                 D   | j                  |di       }| j                  |d      }|j                  d      }t        |      }| j                  |d      }| j	                  |      }| j                  ||dz
        }	|j
                  j                  d      dkD  }
|
rdnd}| j                  |	d d |      }|d   }| j                  | j                  |      }|i | j                  |<   | j                  ||      8| j                  | j                  d	d
      }t        |      | j                  |   |<   | j                  |   |   }t        dt        |            D ]'  }| j                  ||         }|j                  |       ) d|z   dz   |z   }|||g}|j                  ||       y )Nrd   r]   re   r   spotcontractr;   
OHLCVLimitrh   r   r   r   )rS   rT   ri   rJ   find_timeframerY   findrj   ohlcvsrk   r2   r   rN   parse_ws_ohlcvrP   rm   )r7   rb   rn   rd   r]   r   r   r   r   rp   isSpot
marketTyperO   r;   ohlcvsByTimeframer=   rq   r\   rs   r^   resolveDatas                        r9   handle_ohlcvzapex.handle_ohlcv  s   . w3  '2[[%
*o&&z15''4	##Ja@(2-%V:
!!(D$
C! OODKK@$"$DKK??,i8@%%dllL$GE-B5-IDKK	*V$Y/q#d)$ 	"A((a1FMM&!	"  &(4/);y&1{K0r:   c           
          | j                  |d      | j                  |d      | j                  |d      | j                  |d      | j                  |d      | j                  |dd      gS )Nstartopenhighlowclosevolumeturnover)rk   safe_numbersafe_number_2)r7   ohlcvrO   s      r9   r   zapex.parse_ws_ohlcv  sp    " eW-UF+UF+UE*UG,uh
;
 	
r:   c                   K   d}| j                          d{    || j                  |      }|d|z   z  }t        | j                               }| j                  d   d   d   dz   |z   }| j                  |       d{    | j                  ||gdg|       d{   }| j                  r|j                  ||      }| j                  ||||d      S 7 7 W7 ;w)	av  
        watches information on multiple trades made by the user

        https://api-docs.pro.apex.exchange/#private-websocket

        :param str symbol: unified market symbol of the market orders were made in
        :param int [since]: the earliest time in ms to fetch orders for
        :param int [limit]: the maximum number of order structures to retrieve
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :param boolean [params.unifiedMargin]: use unified margin account
        :returns dict[]: a list of `order structures <https://docs.ccxt.com/#/?id=order-structure>`
        myTradesN:r(   r   r&   rD   T
rH   r;   rL   rM   r1   authenticaterQ   rR   rU   filter_by_symbol_since_limit)	r7   r;   r<   r=   r@   r^   rX   rY   r_   s	            r9   watch_my_tradeszapex.watch_my_trades  s      !!!![[(F3<'K))+,	iit$Y/-?)K$$$(({mj\6RR??OOFE2E00tTT 	" 	%Rs4   CCA'C CCC8CCCc                   K   | j                          d{    d}| j                  |      s%| j                  |      }ddj                  |      z   }t	        | j                               }| j                  d   d   d   dz   |z   }d	|z   }| j                  |      }| j                  |       d{    | j                  ||       | j                  }	|	.|j                  d
       d{   }
| j                  |
|||d      S d	g}| j                  ||g||       d{   }| j                  r|S | j                  |	|||d      S 7 17 7 a7 .w)a  

        https://api-docs.pro.apex.exchange/#private-websocket

        watch all open positions
        :param str[] [symbols]: list of unified market symbols
        :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   r   ,r(   r   r&   rD   	positionsfetchPositionsSnapshotT)rH   is_emptyrI   joinrL   rM   r1   rb   r   set_positions_cacher  futurefilter_by_symbols_since_limitrQ   rR   )r7   rB   r<   r=   r@   r^   rX   rY   rb   cacher   rZ   newPositionss                r9   watch_positionszapex.watch_positions'  s\     !!!}}W%))'2G'!22K))+,	iit$Y/-?)K!K/S!$$$  1=#]]+CDDH55hPUW[\\!..s[M66RR??11%%PTUU' 	" 	% E SsF   EEBE1E
28E*E+4EE (E
EEEc                   K   | j                          d{    d}|| j                  |      }|d|z   z  }t        | j                               }| j                  d   d   d   dz   |z   }| j                  |       d{    dg}| j                  ||g||       d{   }	| j                  r|	j                  ||      }| j                  |	|||d      S 7 7 Y7 ;w)	a,  
        watches information on multiple orders made by the user

        https://api-docs.pro.apex.exchange/#private-websocket

        :param str symbol: unified market symbol of the market orders were made in
        :param int [since]: the earliest time in ms to fetch orders for
        :param int [limit]: the maximum number of order structures to retrieve
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict[]: a list of `order structures <https://docs.ccxt.com/#/?id=order-structure>`
        Nordersr   r(   r   r&   rD   Tr   )
r7   r;   r<   r=   r@   r^   rX   rY   rZ   r  s
             r9   watch_orderszapex.watch_ordersH  s      !!![[(F3<'K))+,	iit$Y/-?)K$$$(({mVVLL??OOFE2E00tTT 	" 	%Ls4   CCA)C CC C!8CCCc                    | j                   -| j                  | j                  dd      }t        |      | _         | j                   }i }t	        dt        |            D ]5  }||   }d }| j                  |      }|d   }	d||	<   |j                  |       7 t        |j                               }
t	        dt        |
            D ]  }d|
|   z   }|j                  ||        d}|j                  ||       y )Nrg   rh   r   r;   Tz	myTrades:r   )r   rk   r2   r   rN   rJ   rl   rP   listkeysrm   )r7   rb   listsr=   r_   rB   r\   rawTraders   r;   r  currentMessageHashr^   s                r9   handle_my_tradeszapex.handle_my_tradesb  s    $ == %%dllM4HE259DMq#e*% 	"AQxHF((2FH%F"GFOMM&!	" GLLN#q#d)$ 	7A!,tAw!6NN6#56	7 !v{+r:   c                    | j                   -| j                  | j                  dd      }t        |      | _         | j                   }i }t	        dt        |            D ]3  }d }| j                  ||         }|d   }d||<   |j                  |       5 t        |j                               }	t	        dt        |	            D ]  }d|	|   z   }
|j                  ||
        d}|j                  ||       y )NordersLimitrh   r   r;   Tzorders:r  )r  rk   r2   r   rN   rJ   parse_orderrP   r  r  rm   )r7   rb   r  r=   r  rB   r\   rs   r;   symbolsArrayr  r^   s               r9   handle_orderzapex.handle_order  s    < ;;%%dllM4HE07DKq#e*% 	"AF%%eAh/FH%F"GFOMM&!	" GLLN+q#l+, 	7A!*\!_!<NN6#56	7 v{+r:   c                     | j                   y d}||j                  vr/|j                  |       | j                  | j                  ||       y y )Nr  )r  futuresr  spawnload_positions_snapshot)r7   rb   rB   r^   s       r9   r  zapex.set_positions_cache  sG    >>%.v~~-MM+&JJt33V[I .r:   c                   K   | j                  d       g}t        j                  |  d {   }t               | _        | j                  }t        dt        |            D ]7  }||   }t        dt        |            D ]  }||   }	|j                  |	        9 |j                  |   }
|
j                  |       |j                  |d       y 7 w)Nr   r  )
fetch_positionsasynciogatherr   r  rN   rJ   rP   r  rm   )r7   rb   r^   fetchFunctionspromisesr	  r\   r  iipositionr  s              r9   r  zapex.load_positions_snapshot  s        &
 !8813q#h-( 	'A IAs9~. '$R=X&'	' ,uuk* 9s   (CCB"Cc                    | j                   t               | _         | j                   }g }t        dt        |            D ]  }||   }| j	                  |      }| j                  |d      }|j                  |       ||dk(  r2d|d<   |j                  |       d|d<   |j                  |       d |d<   u|j                  |        | j                  |d      }	t        dt        |	            D ]f  }|	|   }
|
j                  d      }|d   }|j                  d	      }| j                  |d
|d      }| j                  |      rU|j                  ||
       h |j                  |d       y )Nr   r|   r   longshortzpositions::r   r   r  r;   Fr  )r  r   rN   rJ   parse_positionrT   rP   find_message_hashesri   r   r  rm   )r7   rb   r  r	  r
  r\   rawPositionr&  r|   r[   r^   ro   symbolsStringrB   r  s                  r9   handle_positionszapex.handle_positions  sn   . >>!57DNq#e*% 	'A(K**;7H##Hf5D )|trz $* X&#* X&#'  X&#	'$ 00Gq#m,- 	7A'*K%%d+E!!HM#))#.G,,\8WeTI==+y+6	7 	|[1r:   c           	      \  K   | j                          t        | j                               }d}d}||z   |z   }| j                  | j	                  |      | j	                  | j                  | j                              t        j                  d      }d}| j                  |      }	|	j                  |      }
| j                  |	j                  |      }|Mddg||| j                  | j                  ||d}dt        j                   |      gd}| j#                  ||||       |
 d {   S 7 w)	Nz/ws/accountsGETbase64authenticatedloginws_zk_accounts_v3)r   rZ   
httpMethodrequestPathapiKey
passphraserG   	signaturer   )check_required_credentialsrL   rM   hmacencodestring_to_base64secrethashlibsha256rb   r  rS   subscriptionsr7  passwordjsondumpswatch)r7   rY   r@   rG   request_pathhttp_methodmessageStringr9  r^   rb   r  r2  r   rn   s                 r9   r   zapex.authenticate
  s'    '')))+,	%"[0<?IIdkk-8$++dF[F[\`\g\gFh:ikrkyky  |D  E	%S!{+(<(<kJ   ./)+++"mm&&	G G,-G JJsK+>||s   D#D,%D*&D,c                 $   | j                  |g d      }	 ||dk7  r| j                  dz   | j                  |      z   }| j                  | j                  d   ||       | j                  |dd      }| j                  | j                  d   ||       t        |      | j                  |d      }|g|se| j                  |d      }| j                  |d	i       }| j                  |d
      }	|	dk(  rt        d|z         t        | j                  dz   |z         y# t        $ rn}
t        |
t              r0d}|j                  |
|       ||j                  v r2|j                  |= n$| j                  |d      }|j                  |
|       Y d }
~
yd }
~
ww xY w)N)coderet_coderetCode0 exactretMsgret_msgbroadsuccessr   r   authzAuthentication failed: Fr2  reqIdT)r   rK   rC  throw_exactly_matched_exception
exceptionssafe_string_2throw_broadly_matched_exceptionr   rS   rT   r   	Exception
isinstancerejectrA  )r7   rb   rn   rJ  feedbackmsgrS  rQ  r   r   errorr^   s               r9   handle_error_messagezapex.handle_error_message(  s   X !!'+JK	DCK77S=499W+==44T__W5MtU]^(((IF44T__W5MsT\]#H--oogy9G"7**7I>//'9bA%%gt4<-.G'.QRR'#(?@@ 		%!45-e[1&"6"66,,[9"..w@e[1		s   DD 	F!A$F

Fc                    | j                  ||      ry | j                  |ddd      }| j                  | j                  | j                  | j                  | j                  | j
                  | j
                  | j                  | j                  | j                  | j                  d}| j                  ||      }|
 |||       y t        |j                               }t        dt        |            D ].  }||   }|j                  ||         dk\  s ||   }	 |	||        y  | j                  |d      }
|
dk(  r| j                  ||       y y )Nr]   r   r   )r4  r   depthcandlekliner   instrumentInfor   recentlyTradepongrT  r   r   	AUTH_RESP)r`  rX  handle_accountr   r   r   rt   handle_ponghandle_authenticaterS   r  r  rN   rJ   r   rT   )r7   rb   rn   r]   methods
exacMethodr  r\   keymethodr   s              r9   handle_messagezapex.handle_messageq  sI   $$VW5""7GT2>!%!4!4//++''&&(("00''!//$$,,
 __We4
!vw'GLLN#q#d)$ 	Aq'Czz$q'"a' vw'	 0;$$VW5 r:   c                 @    t        | j                               }|gddS )Nr.   )r   r   )rL   rM   )r7   rb   rX   s      r9   r.   z	apex.ping  s'    ))+,	K
 	
r:   c                 4    | j                  |d      |_        |S )Nrg  )rk   lastPongr7   rb   rn   s      r9   rj  zapex.handle_pong  s     ++GV<r:   c                    | j                  |di       }| j                  |dg       }|| j                  ||       | j                  |dg       }|| j                  ||       | j                  |dg       }|| j	                  ||       y y )Ncontentsfillsr  r  )r   r   r  r.  r  )r7   rb   rn   rv  rw  r  r  s          r9   ri  zapex.handle_account  s    >>':r:x"5!!&%0NN8["=	 !!&)4(B7ff- r:   c                 l   | j                  |d      }| j                  |d      }d}|s|dk(  r/| j                  |j                  |      }|j                  d       |S t	        | j
                  dz   | j                  |      z         }|j                  ||       ||j                  v r|j                  |= |S )NrS  rL  r2  r   TrN  )	rS   rk   r  rm   r   rK   rC  r\  rA  )r7   rb   rn   rS  rJ  r^   r  r_  s           r9   rk  zapex.handle_authenticate  s     //'95  )4%dai__V^^[AFNN4  	 (#		'8J(JKEMM%-f222((5r:   c                     |S N rt  s      r9   handle_subscription_statuszapex.handle_subscription_status  s	     r:   rz  )4__name__
__module____qualname__r   r6   rL   r   r   r   rA   r?   r   rt   rl   r
   r   r   rQ   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`  rp  r.   rj  ri  rk  r|  __classcell__)r8   s   @r9   r   r      s1   '# 'R <@d[] S SS S Sbfglbm S OSaenp !Sd3i !S !S[^ !Suyz  vA !SF$,F $,L$L @DB PS P PS\ P SW_a !$s) !C !fo !@ EG U8/ 8/t$3 68 K KF K( 6:" E7 E E<-:F -:^ 8<$]ajl ) )C )WZ )quvzq{ )" aesw  AC E$tCy/ EZ] Emp E@.16 .1`
D 
2 37TX\eg UC Us URU Ulpqvlw U4 8<$]ajl VW VC VWZ Vquv~q VB 04$UYbd U UC Us Uimnsit U4$,v $,L.,6 .,`J& J7 J+$62p .0 <G6 GR6V 6B
6 
& 
.V 
.& , r:   r   )ccxt.async_supportccxt ccxt.async_support.base.ws.cacher   r   r   r   r!  r?  rC  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:   r9   <module>r     sQ     A  A    f f f 4  * 0 .E4"" Er:   