
    eif                         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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j        j                   Z dS )    N)
ArrayCacheArrayCacheBySymbolByIdArrayCacheBySymbolBySideArrayCacheByTimestamp)AnyBoolIntOrder	OrderBookPositionStrStringsTickerTickersTrade)Client)List)ExchangeError)AuthenticationError)ArgumentsRequired)NetworkErrorc                       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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de,fd&Z-d
efd'Z.d
efd(Z/d) Z0d
efd*Z1d
efd+Z2d
efd,Z3d
efd-Z4d
efd.Z5 xZ6S )0apexreturnc                     |                      t          t          |                                           dd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watchOrderBookForSymbolswatchOrderswatchTradeswatchTradesForSymbolswatchPositionswatchMyTradeswatchBalance
watchOHLCVwatchOHLCVForSymbolsz;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   describer2   )self	__class__s    K/var/www/html/volatility/venv/lib/python3.11/site-packages/ccxt/pro/apex.pyr:   zapex.describe   s    dD 1 1 : : < <# $"&,0##)."&!% %"(,   V"V#X  "Y#[  0;z># & 	" I(?
 (?
 ( ( (	    Nsymbolsincelimitc                 D   K   |                      |g|||           d{V S )aA  
        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)r;   r?   r@   rA   paramss        r=   watch_tradeszapex.watch_tradesA   s6       22F8UE6RRRRRRRRRr>   symbolsc                 2  K   |                                   d{V  |                     |          }t          |          }|dk    rt          | j        dz             t          |                                           }| j        d         d         d         dz   |z   }g }g }	t          dt          |                    D ]Y}
||
         }| 	                    |          }d|d	         z   }|
                    |           d
|z   }|	
                    |           Z|                     ||	||           d{V }| j        rB|                     |d          }|                     |d          }|                    ||          }|                     |||dd          S )a2  
        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millisecondsr5   rangemarketappendwatch_topics
newUpdates
safe_valuesafe_stringgetLimitfilter_by_since_limit)r;   rF   r@   rA   rD   symbolsLength	timeStampurltopicsmessageHashesir?   rS   topicmessageHashtradesfirsttradeSymbols                     r=   rC   zapex.watch_trades_for_symbolsO   s      !!!!!!!!!%%g..GA#DG.n$nooo))++,,	it$X.>Jq#g,,'' 	. 	.AQZF[[((F&6EMM%   "V+K  ----((mVVLLLLLLLL? 	8OOFA..E**5(;;KOOK77E))&%TRRRr>   clientc                    |                      |di           }|                     |d          }|}|                    d          }|                     |d          }|                     |d d           }|d         }	|                      | j        |	          }
|
5|                     | j        dd          }t          |          }
|
| j        |	<   t          |          }t          d|          D ];}||z
  d	z
  }| 
                    ||         |          }|
                    |           <d
|	z   }|                    |
|           d S )Ndatara   .   r?   tradesLimit  r      rJ   )rW   rX   splitsafe_marketrc   safe_integerr6   r   rN   rR   parse_ws_traderT   resolve)r;   rf   messagerh   ra   rc   partsmarketIdrS   r?   storedrA   lengthjindexparsedrb   s                    r=   handle_tradeszapex.handle_tradesr   sI   * w33  '22C  ##E1--!!(D$77!f55>%%dlM4HHE&&F"(DKVq&!! 	" 	"AQJNE((??FMM&!!!!#f,v{+++++r>   c                    |                      |g d          }|                      |ddg          }|                     ||d           }|d         }|                     |g d          }|                     |ddg          }|                      |ddg          }|                      |g d	          }	|                     ||||                     |          |d d |d ||	d d d
|          S )N)r`   rO   vsr?   )tT	createdAtSsidepprice)qr}   size)rO   inforK   datetimer?   ordertyper   takerOrMakerr   amountcostfee)safe_string_nro   safe_integer_nsafe_string_lower_n
safe_tradeiso8601)
r;   traderS   rO   ru   r?   rK   r   r   r   s
             r=   rq   zapex.parse_ws_trade   s    '7'7'788%%ec8_==!!(FD99!''/F/F/FGG	''V}==""53.99##E+=+=+=>>"Y//  
  
   	r>   c                 B   K   |                      |g||           d{V S )aI  
        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)r;   r?   rA   rD   s       r=   watch_order_bookzapex.watch_order_book   s4       66xOOOOOOOOOr>   c                   K   |                                   d{V  t          |          }|dk    rt          | j        dz             |                     |          }t          |                                           }| j        d         d         d         dz   |z   }g }g }t          dt          |                    D ]p}	||	         }
| 	                    |
          }|d}d	t          |          z   d
z   |d         z   }|
                    |           d|
z   }|
                    |           q|                     ||||           d{V }|                                S )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)   rH      	orderBookz.H.rI   
orderbook:)rL   rN   r   rO   rM   rP   rQ   r5   rR   rS   rT   rU   rA   )r;   rF   rA   rD   r[   r\   r]   r^   r_   r`   r?   rS   ra   rb   	orderbooks                  r=   r   z!apex.watch_order_book_for_symbols   s}      !!!!!!!!!GA#DG.q$qrrr%%g..))++,,	it$X.>Jq#g,,'' 	. 	.AQZF[[((F}#e**,u4ve}DEMM%   &/K  ----++COOOOOOOO	   r>   c                 x   K   d|d}|                      ||          }|                     ||||           d {V S )N	subscribeopargs)extendwatch_multiple)r;   r]   r_   r^   rD   requestrs   s          r=   rU   zapex.watch_topics   sY      
 
 ++gv..((mWmTTTTTTTTTr>   c                    |                      |d          }|dk    }|                     |di           }|                      |d          }|                     |d d           }|d         }|                     |dd          }	|| j        vr|                                 | j        |<   | j        |         }
|r/|                     |||	dd	          }|
                    |           n|                     |d	g           }|                     |dg           }| 	                    |
d
         |           | 	                    |
d         |           |	|
d<   | 
                    |	          |
d<   d|z   }|
| j        |<   |                    |
|           d S )Nr   snapshotrh   r~   r?   tsMbP?baasksbidsrK   r   r   )rX   	safe_dictro   safe_integer_product
orderbooks
order_bookparse_order_bookreset	safe_listhandle_deltasr   rr   )r;   rf   rs   r   
isSnapshotrh   ru   rS   r?   rK   r   r   r   r   rb   s                  r=   handle_order_bookzapex.handle_order_book   s   D 00j(
~~gvr22##D#..!!(D$77!--gtUCC	$/))&*oo&7&7DOF#OF+	 		<,,T69c3OOHOOH%%%%>>$R00D>>$R00Dy0$777y0$777%.Ik"$(LL$;$;Ij!'&0"+y+.....r>   c                 ^    |                      |dd          }|                    |           d S )Nr   rm   )parse_bid_ask
storeArray)r;   booksidedeltabidAsks       r=   handle_deltazapex.handle_delta0  s3    ##E1a00F#####r>   c                 ~    t          dt          |                    D ]}|                     |||                    d S )Nr   )rR   rN   r   )r;   r   deltasr`   s       r=   r   zapex.handle_deltas4  sH    q#f++&& 	3 	3Ahq	2222	3 	3r>   c                 X  K   |                                   d{V  |                     |          }|d         }t          |                                           }| j        d         d         d         dz   |z   }d|z   }d|d	         z   }|g}|                     ||g||           d{V S )
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)   rH   ticker:instrumentInfo.H.rI   )rL   rS   rP   rQ   r5   rU   )	r;   r?   rD   rS   r\   r]   rb   ra   r^   s	            r=   watch_tickerzapex.watch_ticker8  s       !!!!!!!!!V$$!))++,,	it$X.>J&((6%=8&&s[M66JJJJJJJJJr>   c                 ~  K   |                                   d{V  |                     |dd          }g }t          |                                           }| j        d         d         d         dz   |z   }g }t          dt          |                    D ]Y}||         }|                     |          }	d|	d	         z   }
|                    |
           d
|z   }|                    |           Z| 	                    ||||           d{V }| j
        ri }|||d         <   |S |                     | j        d|          S )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)   rH   r   r   rI   r   r?   )rL   rM   rP   rQ   r5   rR   rN   rS   rT   rU   rV   filter_by_arraytickers)r;   rF   rD   r_   r\   r]   r^   r`   r?   rS   ra   rb   tickerresults                 r=   watch_tickerszapex.watch_tickersL  sh      !!!!!!!!!%%gtU;;))++,,	it$X.>Jq#g,,'' 	. 	.AQZF[[((F,ve}<EMM%   #f,K  ----((mVVLLLLLLLL? 	F'-F6(#$M##DL(GDDDr>   c                 (   |                      |dd          }|                      |dd          }|                     |di           }d }d }|dk    r|                     |          }|d         }n|dk    r|                    d          }t	          |          }	|                      ||	d	z
            }
|                     |
d d           }|d         }|                     | j        |i           }|                     |d
i           }|                     ||          }|                     |          }|                     |dd          }||d<   | 	                    |          |d<   || j        |<   d|z   }|
                    | j        |         |           d S )Nra    r   rh   r   r?   r   ri   rm   r   r   r   rK   r   r   )rX   r   parse_tickerrn   rN   ro   r   r   r   r   rr   )r;   rf   rs   ra   
updateTyperh   r?   rz   
topicPartstopicLengthru   rS   r   	rawTickermergedrK   rb   s                    r=   handle_tickerzapex.handle_tickerj  s   ,   '266%%gvr::
~~gvr22*$$&&t,,FH%FF7""S))Jj//K''
K!ODDH%%hd;;FH%F^^DL&"==Fvvr::I[[D11F&&v..F--gtUCC	'{!\\)44z%V&(t|F+[99999r>   1m	timeframec                 n   K   d|d<   |                      ||gg|||           d{V }||         |         S )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)r;   r?   r   r@   rA   rD   r   s          r=   watch_ohlcvzapex.watch_ohlcv  sW       &2!"33fi5H4I5RWY_````````f~i((r>   symbolsAndTimeframesc                 @  K   |                                   d{V  t          |                                           }| j        d         d         d         dz   |z   }g }g }t	          dt          |                    D ]}	||	         }
|                     |
d          }|                     |          }|d         }|                     |
dd	          }|                     | j        ||          }|	                    d
|z   dz   |z              |	                    d|d         z   dz   |z              | 
                    ||||           d{V \  }}}| j        r|                    ||          }|                     |||dd          }|                     |||          S )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)   rH   r   rI   rm   1zcandle.ri   ohlcv::r?   ::T)rL   rP   rQ   r5   rR   rN   rX   rS   
timeframesrT   rU   rV   rY   rZ   create_ohlcv_object)r;   r   r@   rA   rD   r\   r]   	rawHashesr_   r`   rh   symbolStringrS   unfiedTimeframetimeframeIdr?   r   rv   filtereds                      r=   r   zapex.watch_ohlcv_for_symbols  s      !!!!!!!!!))++,,	it$X.>J	q#23344 	X 	XA'*D++D!44L[[..F!%=L"..tQ<<O**4?O_]]KY4s:\IJJJ  VH-=!=!D!VWWWW*.*;*;CPY[a*b*b$b$b$b$b$b$b!	6? 	3OOFE22E--feUAtLL''	8DDDr>   c                    |                      |di           }|                     |d          }|                    d          }t          |          }|                     |d          }|                     |          }|                     ||dz
            }	|j                            d          dk    }
|
rdnd}|                     |	d d |          }|d         }|| j        vr
i | j        |<   || j        |         vr9| 	                    | j
        d	d
          }t          |          | j        |         |<   | j        |         |         }t          dt          |                    D ]2}|                     ||                   }|                    |           3d|z   dz   |z   }|||g}|                    ||           d S )Nrh   ra   ri   rm   spotcontractr?   
OHLCVLimitrl   r   r   r   )rW   rX   rn   rN   find_timeframer]   findro   ohlcvsrp   r6   r   rR   parse_ws_ohlcvrT   rr   )r;   rf   rs   rh   ra   r   r   r   r   ru   isSpot
marketTyperS   r?   rA   rv   r`   rz   rb   resolveDatas                       r=   handle_ohlcvzapex.handle_ohlcv  s   . w33  '22[[%%
*oo&&z155''44	##Ja@@((2-%5VV:
!!(D$
CC!$+%%"$DKT[000%%dlL$GGE-B5-I-IDK	*V$Y/q#d))$$ 	" 	"A((a11FMM&!!!!&(4/);y&1{K00000r>   c           
         |                      |d          |                     |d          |                     |d          |                     |d          |                     |d          |                     |dd          gS )Nstartopenhighlowclosevolumeturnover)rp   safe_numbersafe_number_2)r;   ohlcvrS   s      r=   r   zapex.parse_ws_ohlcv  s    " eW--UF++UF++UE**UG,,uh
;;
 	
r>   c                   K   d}|                                   d{V  ||                     |          }|d|z   z  }t          |                                           }| j        d         d         d         dz   |z   }|                     |           d{V  |                     ||gdg|           d{V }| j        r|                    ||          }| 	                    ||||d          S )	at  
        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*   rH   T
rL   r?   rP   rQ   r5   authenticaterU   rV   rY   filter_by_symbol_since_limit)	r;   r?   r@   rA   rD   rb   r\   r]   rc   s	            r=   watch_my_tradeszapex.watch_my_trades  s      !!!!!!!!!![[((F3<'K))++,,	it$Y/-?)K$$$$$$$$$(({mj\6RRRRRRRR? 	3OOFE22E00tTTTr>   c                   K   |                                   d{V  d}|                     |          s-|                     |          }dd                    |          z   }t	          |                                           }| j        d         d         d         dz   |z   }d	|z   }|                     |          }|                     |           d{V  | 	                    ||           | j
        }	|	4|                    d
           d{V }
|                     |
|||d          S d	g}|                     ||g||           d{V }| j        r|S |                     |	|||d          S )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*   rH   	positionsfetchPositionsSnapshotT)rL   is_emptyrM   joinrP   rQ   r5   rf   r  set_positions_cacher  futurefilter_by_symbols_since_limitrU   rV   )r;   rF   r@   rA   rD   rb   r\   r]   rf   cacher   r^   newPositionss                r=   watch_positionszapex.watch_positions,  s      !!!!!!!!!}}W%% 	3))'22G'!2!22K))++,,	it$Y/-?)K!K/S!!$$$$$$$$$  111=#]]+CDDDDDDDDH55hPUW[\\\!..s[M66RRRRRRRR? 	 11%%PTUUUr>   c                   K   |                                   d{V  d}||                     |          }|d|z   z  }t          |                                           }| j        d         d         d         dz   |z   }|                     |           d{V  dg}|                     ||g||           d{V }	| j        r|	                    ||          }| 	                    |	|||d          S )	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*   rH   Tr  )
r;   r?   r@   rA   rD   rb   r\   r]   r^   r  s
             r=   watch_orderszapex.watch_ordersM  s"      !!!!!!!!![[((F3<'K))++,,	it$Y/-?)K$$$$$$$$$(({mVVLLLLLLLL? 	3OOFE22E00tTTTr>   c                 <   | j         0|                     | j        dd          }t          |          | _         | j         }i }t	          dt          |                    D ]C}||         }d }|                     |          }|d         }	d||	<   |                    |           Dt          |	                                          }
t	          dt          |
                    D ]#}d|
|         z   }|
                    ||           $d}|
                    ||           d S )Nrk   rl   r   r?   Tz	myTrades:r   )r   rp   r6   r   rR   rN   rq   rT   listkeysrr   )r;   rf   listsrA   rc   rF   r`   rawTraderz   r?   r  currentMessageHashrb   s                r=   handle_my_tradeszapex.handle_my_tradesg  s"   $ = %%dlM4HHE2599DMq#e**%% 	" 	"AQxHF((22FH%F"GFOMM&!!!!GLLNN##q#d))$$ 	7 	7A!,tAw!6NN6#56666 v{+++++r>   c                 8   | j         0|                     | j        dd          }t          |          | _         | j         }i }t	          dt          |                    D ]A}d }|                     ||                   }|d         }d||<   |                    |           Bt          |	                                          }	t	          dt          |	                    D ]#}d|	|         z   }
|
                    ||
           $d}|
                    ||           d S )NordersLimitrl   r   r?   Tzorders:r  )r  rp   r6   r   rR   rN   parse_orderrT   r  r  rr   )r;   rf   r  rA   r  rF   r`   rz   r?   symbolsArrayr  rb   s               r=   handle_orderzapex.handle_order  s   < ;%%dlM4HHE077DKq#e**%% 	" 	"AF%%eAh//FH%F"GFOMM&!!!!GLLNN++q#l++,, 	7 	7A!*\!_!<NN6#56666v{+++++r>   c                     | j         d S d}||j        vr3|                    |           |                     | j        ||           d S d S )Nr  )r  futuresr  spawnload_positions_snapshot)r;   rf   rF   rb   s       r=   r  zapex.set_positions_cache  sZ    >%F.v~--MM+&&&JJt3V[IIIII .-r>   c                   K   |                      d           g}t          j        |  d {V }t                      | _        | j        }t          dt          |                    D ]G}||         }t          dt          |                    D ]}||         }	|                    |	            H||j        v r:|j        |         }
|
	                    |           |	                    |d           d S d S )Nr   r  )
fetch_positionsasynciogatherr   r  rR   rN   rT   r!  rr   )r;   rf   rb   fetchFunctionspromisesr  r`   r  iipositionr  s              r=   r#  zapex.load_positions_snapshot  s        &&
 !8888888133q#h--(( 	' 	'A IAs9~~.. ' '$R=X&&&&' &.((^K0FNN5!!!NN5+..... )(r>   c                 \   | j         t                      | _         | j         }g }t          dt          |                    D ]}||         }|                     |          }|                     |d          }|                    |           ||dk    r:d|d<   |                    |           d|d<   |                    |           d |d<   |                    |           |                     |d          }	t          dt          |	                    D ]}|	|         }
|
                    d          }|d         }|                    d	          }| 	                    |d
|d          }| 
                    |          s|                    ||
           |                    |d           d S )Nr   r   r   longshortzpositions::r   rm   r  r?   Fr  )r  r   rR   rN   parse_positionrX   rT   find_message_hashesrn   r   r	  rr   )r;   rf   r  r  r  r`   rawPositionr+  r   r_   rb   rt   symbolsStringrF   r  s                  r=   handle_positionszapex.handle_positions  s   . >!577DNq#e**%% 	' 	'A(K**;77H##Hf55D )))|trzz $* X&&&#* X&&&#'   X&&&&00GGq#m,,-- 	7 	7A'*K%%d++E!!HM#))#..G,,\8WeTTI==++ 7y+666|[11111r>   c           	      v  K   |                                   t          |                                           }d}d}||z   |z   }|                     |                     |          |                     |                     | j                            t          j        d          }d}| 	                    |          }	|	
                    |          }
|                     |	j        |          }|Fddg||| j        | j        ||d}dt          j        |          gd}|                     ||||           |
 d {V S )	Nz/ws/accountsGETbase64authenticatedloginws_zk_accounts_v3)r   r^   
httpMethodrequestPathapiKey
passphraserK   	signaturer   )check_required_credentialsrP   rQ   hmacencodestring_to_base64secrethashlibsha256rf   reusableFuturerW   subscriptionsr<  passwordjsondumpswatch)r;   r]   rD   rK   request_pathhttp_methodmessageStringr>  rb   rf   r  r7  r   rs   s                 r=   r  zapex.authenticate  sY     '')))))++,,	%"[0<?IIdkk-88$++dF[F[\`\gFhFh:i:ikrky  |D  E  E	%S!!&&{33(<kJJ   ./)++"m&&	 	G G,,- G JJsK+>>>||||||r>   c                    |                      |g d          }	 ||dk    r| j        dz   |                     |          z   }|                     | j        d         ||           |                     |dd          }|                     | j        d         ||           t          |          |                     |d          }|w|su| 	                    |d          }|                     |d	i           }| 	                    |d
          }	|	dk    rt          d|z             t          | j        dz   |z             dS # t          $ rv}
t          |
t                    r*d}|                    |
|           ||j        v r|j        |= n,| 	                    |d          }|                    |
|           Y d }
~
dS d }
~
ww xY w)N)coderet_coderetCode0 exactretMsgret_msgbroadsuccessr   r   authzAuthentication failed: Fr7  reqIdT)r   rO   rI  throw_exactly_matched_exception
exceptionssafe_string_2throw_broadly_matched_exceptionr   rW   rX   r   	Exception
isinstancerejectrG  )r;   rf   rs   rP  feedbackmsgrY  rW  r   r   errorrb   s               r=   handle_error_messagezapex.handle_error_message.  s   X !!'+J+J+JKK	DCKK7S=499W+=+==44T_W5MtU]^^^(((IFF44T_W5MsT\]]]#H---oogy99G"7"**7I>>//'9bAA%%gt44<<-.G'.QRRR'#(?@@@5 		 		 		%!455 2-e[111&"666,[9"..w@@e[11144444		s   D!D= =
F=A+F88F=c                    |                      ||          rd S |                     |ddd          }| j        | j        | j        | j        | j        | j        | j        | j        | j        | j        | j        | j	        d}| 
                    ||          }| |||           d S t          |                                          }t          dt          |                    D ]@}||         }|                    ||                   dk    r||         }	 |	||            d S A|                     |d          }
|
dk    r|                     ||           d S d S )Nra   r   r   )r9  r   depthcandlekliner   instrumentInfor   recentlyTradepongrZ  r2   r   r   	AUTH_RESP)rf  r^  handle_accountr   r   r   r{   handle_ponghandle_authenticatehandle_pingrW   r  r  rR   rN   r   rX   )r;   rf   rs   ra   methods
exacMethodr  r`   keymethodr   s              r=   handle_messagezapex.handle_messagew  s   $$VW55 	F""7GT2>>!%!4/+'&("0'!/$,$
 
 __We44
!Jvw'''FGLLNN##q#d))$$ 	 	Aq'Czz$q'""a'' vw''' (
 00;$$VW55555 r>   c                 ^    |                                  }||_        t          |          gddS )Nr2   r   r   )rQ   lastPongrP   )r;   rf   r\   s      r=   r2   z	apex.ping  s8    %%''	#^^$
 
 	
r>   c                 @  K   |                                  }	 |                    t          |          gdd           d {V  d S # t          $ rM}t	          | j        dz   |                     |          z             }|                    |           Y d }~d S d }~ww xY w)Nrm  ry  z handlePing failed with error )rQ   sendrP   r`  r   rO   exception_messager   )r;   rf   rs   r\   ere  s         r=   rm  z	apex.pong  s       %%''		 ++I'7vFFGGGGGGGGGGG 	  	  	  +K!KdNdNdefNgNg!ghhELL	 s   ,A 
BABBc                 b    |                      |d|                                           |_        |S )Nrm  )rp   rQ   rz  r;   rf   rs   s      r=   rp  zapex.handle_pong  s.     ++GVT=N=N=P=PQQr>   c                 >    |                      | j        ||           d S N)r"  rm  r  s      r=   rr  zapex.handle_ping  s     

49fg.....r>   c                 R   |                      |di           }|                     |dg           }||                     ||           |                     |dg           }||                     ||           |                     |dg           }||                     ||           d S d S )Ncontentsfillsr  r  )r   r   r  r3  r  )r;   rf   rs   r  r  r  r  s          r=   ro  zapex.handle_account  s    >>':r::x"55!!&%000NN8["==	 !!&)444(B77ff----- r>   c                 |   |                      |d          }|                     |d          }d}|s|dk    r1|                      |j        |          }|                    d           nTt	          | j        dz   |                     |          z             }|                    ||           ||j        v r|j        |= |S )NrY  rR  r7  r   TrT  )	rW   rp   r!  rr   r   rO   rI  rb  rG  )r;   rf   rs   rY  rP  rb   r  re  s           r=   rq  zapex.handle_authenticate  s     //'955  )44% 	6daii__V^[AAFNN4    '#		'8J8J(JKKEMM%---f222(5r>   c                     |S r   r  s      r=   handle_subscription_statuszapex.handle_subscription_status  s	     r>   r  )7__name__
__module____qualname__r   r:   rP   r	   r   r   rE   rC   r   r{   rq   r   r   r   rU   r   r   r   r   r   r   r   r   r   r  r   r   r   r   r   r  r   r  r
   r  r  r  r  r#  r3  r  r   rf  rw  r2   rm  rp  rr  ro  rq  r  __classcell__)r<   s   @r=   r   r      s       )# ) ) ) ) ) )V <@d[] S S SS S Sbfglbm S S S S OSaenp !S !Sd3i !S !S[^ !Suyz  vA !S !S !S !SF',F ', ', ', ',R$ $ $ $L @DB P PS P PS\ P P P P SW_a ! !$s) !C !fo ! ! ! !@ EG U U U U8/ 8/ 8/ 8/ 8/t$ $ $3 3 3 68 K K KF K K K K( 6:" E E7 E E E E E<-:F -: -: -: -:^ ?CQUdhqs ) ) ) )3 )^a )x|  ~B  yC ) ) ) )" aesw  AC E E$tCy/ EZ] Emp E E E E@-16 -1 -1 -1 -1^
 
D 
 
 
 
2 37TX\eg U UC Us URU Ulpqvlw U U U U4 8<$]ajl V VW VC VWZ Vquv~q V V V VB 04$UYbd U U UC Us Uimnsit U U U U4$,v $, $, $, $,L.,6 ., ., ., .,`J J& J7 J J J J/ / /&62 62 62p .0    <G6 Gt G G G GR 6V  6  6  6  6D
6 
 
 
 
	  	  	 &    /& / / / /
.V 
. 
. 
. 
.&    ,        r>   r   )!ccxt.async_supportccxt ccxt.async_support.base.ws.cacher   r   r   r   r&  rD  rI  ccxt.base.typesr   r   r	   r
   r   r   r   r   r   r   r   !ccxt.async_support.base.ws.clientr   typingr   ccxt.base.errorsr   r   r   r   async_supportr   r  r>   r=   <module>r     st       A  A  A  A  A  A  A  A  A  A  A  A    l l l l l l l l l l l l l l l l l l l l l l l l l l 4 4 4 4 4 4       * * * * * * 0 0 0 0 0 0 . . . . . . ) ) ) ) ) )Z Z Z Z Z4" Z Z Z Z Zr>   