
    ":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 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ArrayCacheByTimestamp)AnyBalancesIntOrder	OrderBookStrTrade)Client)List)AuthenticationError)NetworkErrorc            
           e Zd Zdef 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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i fded
e	defdZd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efdZd Zd ZdefdZi f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efdZ"d'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#Z(d$ Z)defd%Z*i fd&Z+ xZ,S )(ascendexreturnc                     | j                  t        t        |          dddddddddddddiddd	did
ddddddddd      S )NTF)wswatchBalance
watchOHLCVwatchOrderBookwatchOrderswatchTickerwatchTradeswatchTradesForSymbolsr   z(wss://ascendex.com:443/api/pro/v2/streamz7wss://ascendex.com:443/{accountGroup}/api/pro/v2/stream)publicprivatez9wss://api-test.ascendex-sandbox.com:443/api/pro/v2/streamzHwss://api-test.ascendex-sandbox.com:443/{accountGroup}/api/pro/v2/stream)apitest  spotswapmargin)cashfuturesr$   )tradesLimitordersLimit
OHLCVLimitcategoriesAccount)hasurlsoptions)deep_extendsuperr   describe)self	__class__s    K/var/www/html/turnos/venv/lib/python3.12/site-packages/ccxt/pro/ascendex.pyr0   zascendex.describe   s    h > @ $""&#$#)-	 "L#\ "]#m  $#""%&&		3#C
 # #	    c                    K   | j                   d   d   d   }| j                         }t        |      dd}| j                  ||      }| j	                  ||||       d {   S 7 wNr   r   r   sub)idop)r,   noncestrextendwatch)r1   messageHashparamsurlr8   requestmessages          r3   watch_publiczascendex.watch_public8   sg     iit$X.ZZ\b'
 ++gv.ZZ[';GGGG   AA&A$ A&c                    K   | j                   d   d   d   }| j                         }t        |      dd}| j                  ||      }| j	                  ||||       d {   S 7 wr6   )r,   r:   r;   r<   watch_multiple)r1   messageHashesr?   r@   r8   rA   rB   s          r3   watch_public_multiplezascendex.watch_public_multipleB   si     iit$X.ZZ\b'
 ++gv.((mWmTTTTrD   c                   K   | j                          d {    | j                  | j                  d      }| j                  d   d   d   }| j	                  |d|i      }| j                         }t        |      d|d}| j                  ||      }| j                  ||       d {    | j                  ||||       d {   S 7 7 "7 w)Nzaccount-groupr   r   r   accountGroupr7   )r8   r9   ch)
load_accountssafe_stringr-   r,   implode_paramsr:   r;   r<   authenticater=   )	r1   channelr>   r?   rJ   r@   r8   rA   rB   s	            r3   watch_privatezascendex.watch_privateL   s       """''oFiit$Y/!!#'EFZZ\b'

 ++gv.V,,,ZZ['7CCC 	# 	-Cs4   CCBC&C	'CCC	CC1mNsymbolsincelimitc                 x  K   | j                          d{    | j                  |      }|d   }||dkD  rd}| j                  | j                  ||      }d|z   dz   |d   z   }d|i}| j	                  ||       d{   }	| j
                  r|	j                  ||      }| j                  |	||d	d
      S 7 7 9w)a  
        watches historical candlestick data containing the open, high, low, and close price, and the volume of a market

        https://ascendex.github.io/ascendex-pro-api/#channel-bar-data

        :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
        NrS   i  d   zbar::r8   rK   r   T)load_marketsmarketrM   
timeframesrC   
newUpdatesgetLimitfilter_by_since_limit)
r1   rS   	timeframerT   rU   r?   rZ   intervalrP   ohlcvs
             r3   watch_ohlcvzascendex.watch_ohlcv[   s      !!!V$!Mut|E##DOOY	J(3.='
 ''88??NN651E))%q$GG 	" 9s"   B:B6A'B:>B8?8B:8B:clientc                    | j                  |d      }| j                  |      }| j                  |d      }| j                  |di       }| j                  |d      }|dz   |z   dz   |z   }| j                  |      }	| j	                  |      }
| j                  ||
      }| j                  | j                  |i       | j                  |<   | j                  | j                  |   |	      }|:| j                  | j                  dd      }t        |      }|| j                  |   |	<   |j                  |       |j                  ||       |S )NsmdatairX   r)   r!   )rM   safe_symbol
safe_valuefind_timeframerZ   parse_ohlcvohlcvssafe_integerr-   r   appendresolve)r1   rc   rB   marketIdrS   rP   rg   r`   r>   r_   rZ   parsedstoredrU   s                 r3   handle_ohlcvzascendex.handle_ohlcvw   s5     ##GS1!!(+""7C0w3##D#.mh.4x?''1	V$!!'62"oodkk62FFV!4i@>%%dllL$GE*51F-3DKK	*fv{+r4   c                 H   K   | j                  |g|||       d{   S 7 w)a)  
        get the list of most recent trades for a particular symbol

        https://ascendex.github.io/ascendex-pro-api/#channel-market-trades

        :param str symbol: unified symbol of the market to fetch trades for
        :param int [since]: timestamp in ms of the earliest trade to fetch
        :param int [limit]: the maximum amount of trades to fetch
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict[]: a list of `trade structures <https://docs.ccxt.com/#/?id=public-trades>`
        N)watch_trades_for_symbols)r1   rS   rT   rU   r?   s        r3   watch_tradeszascendex.watch_trades   s'      22F8UE6RRRRs   " "symbolsc                 ^  K   | j                          d{    | j                  |dddd      }g }g }|Yt        dt        |            D ]A  }| j	                  ||         }|j                  |d          |j                  d|d   z          C ddj                  |      z   }	| j                  |d|	i      }| j                  ||       d{   }
| j                  r6| j                  |
d      }| j                  |d	      }|
j                  ||      }| j                  |
||d
d      S 7 7 ^w)a  
        get the list of most recent trades for a list of symbols

        https://ascendex.github.io/ascendex-pro-api/#channel-market-trades

        :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
        :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>`
        NFTr   r8   ztrades:,rK   rS   	timestamp)rY   market_symbolsrangelenrZ   ro   joinr<   rH   r\   rj   rM   r]   r^   )r1   rx   rT   rU   r?   	marketIdsrG   rh   rZ   rP   tradesfirsttradeSymbols                r3   rv   z!ascendex.watch_trades_for_symbols   s;     !!!%%gtUD$G	1c'l+ ?WQZ0  .$$Y%=>? chhy11V'&
  11-HH??OOFA.E**5(;KOOK7E))&%TRR% 	" Is#   D-D(B5D-D+AD-+D-c                    | j                  |d      }| j                  |      }| j                  |d      }|dz   |z   }| j                  |      }| j                  |d      }|g }| j	                  ||      }	| j                  | j
                  |      }
|
(| j                  | j                  dd      }t        |      }
t        dt        |	            D ]  }|
j                  |	|           |
| j
                  |<   |j                  |
|       y )NrS   rf   rX   rg   r'   r!   r   )rM   ri   rZ   rj   parse_tradesr   rn   r-   r   r}   r~   ro   rp   )r1   rc   rB   rq   rS   rP   r>   rZ   rawDatar   tradesArrayrU   rh   s                r3   handle_tradeszascendex.handle_trades   s     ##GX6!!(+""7C0mh.V$//'62?G""7F3oodkk6:%%dllM4HE$U+Kq#f+& 	*Avay)	*)F{K0r4   c                    K   | j                          d{    | j                  |      }d|d   z   }| j                  |d|i      }| j                  ||       d{   }|j	                         S 7 [7 w)aM  
        watches information on open orders with bid(buy) and ask(sell) prices, volumes and other data

        https://ascendex.github.io/ascendex-pro-api/#channel-level-2-order-book-updates

        :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
        Nzdepth:r8   rK   rY   rZ   r<   rC   rU   )r1   rS   rU   r?   rZ   rP   	orderbooks          r3   watch_order_bookzascendex.watch_order_book   s      !!!V$&,.V'&
  ++GV<<	   	" =s"   A6A2AA6A4A64A6c                   K   | j                          d {    | j                  |      }d}|dz   |d   z   }| j                  ||d|d   idd      }| j                  ||       d {   }|j	                         S 7 g7 w)Ndepth-snapshotrX   r8   rS   req)actionargsr9   r   )r1   rS   rU   r?   rZ   r   rP   r   s           r3   watch_order_book_snapshotz"ascendex.watch_order_book_snapshot   s     !!!V$!3,-V&, &
  ++GV<<	   	" =s"   BA>AB)B *B Bc                    K   | j                  |||       d {   }|| j                  vr| j                         | j                  |<   | j                  |   }|j                  |       |S 7 QwN)fetch_rest_order_book_safe
orderbooks
order_bookreset)r1   rS   rU   r?   restOrderBookr   s         r3   fetch_order_book_snapshotz"ascendex.fetch_order_book_snapshot  sd     "==feVTT$//)&*oo&7DOOF#OOF+	& Us   A-A+AA-c                    | j                  |d      }| j                  |      }| j                  |d      }|dz   |z   }| j                  |   }| j                  |d      }| j	                  ||      }	| j                  |d      |	d<   |j                  |	       |j                  }
t        dt        |
            D ]  }|
|   }| j                  |||        || j                  |<   |j                  ||       y )NrS   rf   rX   rg   seqnumr:   r   )rM   ri   r   rj   parse_order_bookrn   r   cacher}   r~   handle_order_book_messagerp   )r1   rc   rB   rq   rS   rP   r>   r   rg   snapshotmessagesrh   messageItems                r3   handle_order_book_snapshotz#ascendex.handle_order_book_snapshot  s    & ##GX6!!(+""7C0mf,OOF+	w/((v6 --dH=!??q#h-( 	KA"1+K**6;	J	K #,y+.r4   c                    | j                  |d      }| j                  |d      }| j                  |      }|dz   |z   }|| j                  vr| j                  i       | j                  |<   | j                  |   }|d   |j                  j                  |       y | j                  |||       |j                  ||       y )Nrf   rS   rX   r:   )rM   ri   r   r   r   ro   r   rp   )r1   rc   rB   rP   rq   rS   r>   r   s           r3   handle_order_bookzascendex.handle_order_book:  s     ""7C0##GX6!!(+mh.$//)&*oob&9DOOF#OOF+	W%OO""7+**67IFNN9k2r4   c                 p    | j                  |d      }| j                  |d      }|j                  ||       y )Nr      )
safe_floatstore)r1   booksidedeltapriceamounts        r3   handle_deltazascendex.handle_deltaT  s2     q)*uf%r4   c                 b    t        dt        |            D ]  }| j                  |||           y )Nr   )r}   r~   r   )r1   r   deltasrh   s       r3   handle_deltaszascendex.handle_deltas\  s0    q#f+& 	3Ahq	2	3r4   c                 `   | j                  |di       }| j                  |d      }||d   kD  r| j                  |dg       }| j                  |dg       }| j                  |d   |       | j                  |d   |       ||d<   | j                  |d      }||d<   | j                  |      |d<   |S )	Nrg   r   r:   asksbidstsr{   datetime)rj   rn   r   iso8601)	r1   rc   rB   r   rg   seqNumr   r   r{   s	            r3   r   z"ascendex.handle_order_book_message`  s    ( w3""42Ig&&??44D??44Dy0$7y0$7!'Ig))$5I%.Ik"$(LL$;Ij!r4   c                 ^  K   | j                          d{    | j                  dd|      \  }}d}d}|dk(  s|dk(  rK| j                  | j                  di       }| j	                  ||d      }|j                         }d|z   }d|z   }nd	}d
}| j                  |||       d{   S 7 7 w)an  
        watch balance and get the amount of funds available for trading or funds locked in orders

        https://ascendex.github.io/ascendex-pro-api/#channel-order-and-balance

        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: a `balance structure <https://docs.ccxt.com/#/?id=balance-structure>`
        Nr   r"   r$   accountCategoriesr%   order:balance:futures-account-updatezbalance:swap)rY   handle_market_type_and_paramsrj   r-   rM   upperrQ   )r1   r?   typequeryrP   r>   r   accountCategorys           r3   watch_balancezascendex.watch_balance  s      !!!88vVeFN 0 $>QSU V"../@$OO-335O0G$t+K.G(K''eDDD 	" Es"   B-B)BB-$B+%B-+B-c                    | j                  |d      }d }d }|dk(  s|dk(  r| j                  |d      }| j                  |d      }| j                  |      }| j                         }	| j                  |d      |	d<   | j                  |d      |	d	<   | j                         }
| j                  |d
      |
d<   | j                  |d      |
d	<   |d   r d}| j                  | j                  |i       }n"|d   }| j                  | j                  |i       }|	||d   <   |
||d   <   n| j                  |dd      }| j                  | j                  d      }| j                  ||d      }| j                  | j                  |i       }| j                  |d      }d }|| j                  |d      }n|g}t        dt        |            D ]h  }||   }| j                  | j                  |d            }| j                         }| j                  |d      |d<   | j                  |dd      |d	<   |||<   j d|z   }|j                  | j                  |      |       y )Nrf   orderfutures-orderrg   re   babfreebtbtotalqabqtbcontractr#   r   basequoteacatr*   r"   colr   aabtbbr   )rM   rj   safe_marketaccountbalancesafe_string_lower_2r-   r}   r~   safe_currency_codesafe_string_2rp   safe_balance)r1   rc   rB   rP   resultr   rg   rq   rZ   baseAccountquoteAccountaccountTypecategoriesAccountsbalancesrh   r   coder   r>   s                      r3   handle_balancezascendex.handle_balance  s`   d ""7C0wG$>??7F3D''c2H%%h/F,,.K"&"2"24"?K#'#3#3D%#@K <<>L#'#3#3D%#@L $($4$4T5$AL!j!tR@f~tR@%0F6&>"&2F6'?#227D$GK!%?R!S##$6VLD__T\\4<F??7F3DH|??7E: 61c(m, '"1+..t/?/?/MN,,."&"2"27D"A#'#5#5gtS#I &t' &,t((0+>r4   c                   K   | j                          d{    d}|| j                  |      }|d   }| j                  d||      \  }}d}d}	|dk7  r
|dk7  rd}	d}nG| j                  | j                  di       }
| j                  |
|d	      }|j                         }d
|z   }|}	||dz   |z   }| j                  |	||       d{   }| j                  r|j                  ||      }| j                  ||||d      S 7 7 9w)a=  

        https://ascendex.github.io/ascendex-pro-api/#channel-order-and-balance

        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
        :returns dict[]: a list of `order structures <https://docs.ccxt.com/#/?id=order-structure>`
        NrS   r   r"   r$   r   zorder:FUTURESr   r%   r   rX   T)rY   rZ   r   rj   r-   rM   r   rQ   r\   r]   filter_by_symbol_since_limit)r1   rS   rT   rU   r?   rZ   r   r   r>   rP   r   r   orderss                r3   watch_orderszascendex.watch_orders  s+     !!![[(FH%F88PVWe6>dh.%G)K $>QSU V"../@$OO-335O'/9K!G%+f4K))';FF??OOFE2E00tTT- 	"& Gs"   DC=B.DC?8D?Dc                    | j                  |d      }d|z   }| j                  |d|      }| j                  |      }| j                  -| j	                  | j
                  dd      }t        |      | _        | j                  }|j                  |       |dz   |d   z   }	|j                  ||	       |j                  ||       y )Nr   r   rg   r(   r!   rX   rS   )	rM   rj   parse_ws_orderr   rn   r-   r   ro   rp   )
r1   rc   rB   r   r>   rg   r   rU   r   symbolMessageHashs
             r3   handle_orderzascendex.handle_order  s    T &&w5,w8##D);;%%dllM4HE07DKe'#-h?v01v{+r4   c           	      f   | j                  | 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      }d }|(| j                  |d      }| j                  |      }||d}| j                  | j                  | j                  |d                  }| j                  i d|d|dd d|d| j                  |      d|d|d|dd dd d|d|d|d|d|	dd d |
|d ||d d!|      S )"Nstre   t/pqapcfqorderIdotsdcffa)costcurrencyspinfor8   clientOrderIdr{   r   lastTradeTimestamprS   r   timeInForcepostOnlysider   	stopPricetriggerPricer   r   average)filled	remainingstatusfeer   )parse_order_statusrM   rn   ri   safe_string_lowersafe_numberr   parse_number	omit_zero
safe_orderr   )r1   r   rZ   r	  rq   r{   rS   r   r   r   r  r  r8   r   r  feeCostr
  feeCurrencyIdfeeCurrencyCoder  s                       r3   r   zascendex.parse_ws_orderP  sb   v (()9)9%)FG##E3/%%eS1	!!(FC8!..uc:  ,!!%-""5$/!!%/eY/%%eT2%%eT2""5$/ ,,UD9M"55mDO+C %%dnnT5E5EeT5R&ST	  
E 
" 
 T 
 	 

 Y/ 
 !"4 
 f 
 D 
 4 
  
 D 
 U 
  
 I 
 f 
  D! 
" w# 
$ - 
. / 	r4   c                    | j                  |d      }	 |u| j                  dz   | j                  |      z   }| j                  | j                  d   ||       | j                  |d      }| | j                  | j                  d   ||       y# t        $ r[}t        |t              r0d}|j                  ||       ||j                  v r|j                  |= n|j                  |       Y d }~yd }~ww xY w)	Nr    exactrB   broadFauthenticatedT)rn   r8   jsonthrow_exactly_matched_exception
exceptionsrj   throw_broadly_matched_exception	Exception
isinstancer   rejectsubscriptions)r1   rc   rB   	errorCodefeedbackmessageStringer>   s           r3   handle_error_messagezascendex.handle_error_message  s     %%gv6		$77S=499W+==44T__W5MyZbc $ C ,889QS`bjk 	!01-a-&"6"66,,[9a 	s   A7B 	C0AC++C0c                 ,    d}|j                  ||       y )Nr  )rp   )r1   rc   rB   r>   s       r3   handle_authenticatezascendex.handle_authenticate  s     &w,r4   c           
         | j                  ||      ry | j                  |d      }| j                  | j                  | j                  | j
                  | j                  | j                  | j                  | j                  | j                  d	}| j                  ||      }|	 |||       |dk(  s|dk(  r+| j                  ||       |dk(  r| j                  ||       y y y )Nrf   )	pingauthr7   depthr   r   barr   r   r   r   )r%  rM   handle_pingr'  handle_subscription_statusr   r   r   rt   r   rj   r   )r1   rc   rB   subjectmethodsmethods         r3   handle_messagezascendex.handle_message  s    $$VW5b ""7C0$$,,22++"==(($$**&*&9&9

 '267#wG$> fg.'!##FG4 " %?r4   c                     | j                  |dd      }|j                  d      dkD  r&|j                  d      dkD  s| j                  ||       |S )NrK    r+  r   )rM   findhandle_order_book_subscription)r1   rc   rB   rP   s       r3   r.  z#ascendex.handle_subscription_status  sP     ""7D"5<< 2%w||<L/MPR/R//@r4   c                    | j                  |d      }|j                  d      }|d   }| j                  |      }|d   }|| j                  v r| j                  |= | j	                  i       | j                  |<   | j
                  d   dk(  s|d   r| j                  | j                  |       y | j                  | j                  |       y )NrK   rX   r   rS   defaultTyper#   r   )	rM   splitr   r   r   r-   spawnr   r   )r1   rc   rB   rP   partsrq   rZ   rS   s           r3   r7  z'ascendex.handle_order_book_subscription  s    ""7D1c"8!!(+!T__$'"&//""5<<&&0F:4FJJt55v>JJt55v>r4   c                   K   	 |j                  d| j                  |d      d       d {    y 7 # t        $ rE}t        | j                  dz   | j                  |      z         }|j                  |       Y d }~y d }~ww xY ww)Nponghp)r9   r?  z handlePing failed with error )sendrn   r  r   r8   r  r   )r1   rc   rB   r$  errors        r3   r>  zascendex.pong  so     	 ++V43D3DWd3STUUU 	  +K!KdiiXYl!Z[ELL	 s6   B'3 13 B3 	B;A<7B<BBc                 >    | j                  | j                  ||       y r   )r;  r>  )r1   rc   rB   s      r3   r-  zascendex.handle_ping  s    

499fg.r4   c                   K   | j                          d}| j                  |      }| j                  |j                  |      }|t	        | j                               }|j                  d      }t        |      }| j                  ||dz
        }	| j                  ||dz
        }
|dz   |
z   dz   |	z   }| j                  | j                        }| j                  | j                  |      |t        j                  d      }dt	        | j                               || j                   |d}| j#                  ||| j%                  ||      |       d {   }||j                  |<   |S 7 w)	Nr  r   r      +base64r*  )r9   r8   r   keysig)check_required_credentialsrc   rj   r   r;   millisecondsr:  r~   rM   base64_to_binarysecrethmacencodehashlibsha256r:   apiKeyr=   r<   )r1   r@   r?   r>   rc   futurer{   urlPartspartsLengthpathversionr*  rL  	signaturerA   s                  r3   rO   zascendex.authenticate  sG    '')%S!!5!5{C>D--/0Iyy~Hh-K##HkAo>D&&xqAGs?W,s2T9D**4;;7F		$++d"3VW^^XVI$**,'{{ G  ::c;GV8TVabbF06F  - cs   EE&E$E&r   )-__name__
__module____qualname__r   r0   rC   rH   rQ   r;   r   r   listrb   r   rt   r   rw   rv   r   r
   r   r   r   r   r   r   r   r   r   r   r   r   r	   r   r   r   r%  r'  r2  r.  r7  r>  r-  rO   __classcell__)r2   s   @r3   r   r      s_   $# $L 68 H AC U @B D 8<$]ajl H HC HWZ Hquvzq{ H8!6 !F <@d[] S SS S Sbfglbm S OSaenp Sd3i S S[^ Suyz  vA SB 1F  1D @DB !S ! !S\ !( IMUW !c !# ! IMUW c # "/ "/H3 34&3 B *, E E0Z?V Z?x 04$UYbd "U "UC "Us "Uimnsit "UH5,6 5,nhT6 8-& -h5V h5T	 	?V ? /& / .0 r4   r   )ccxt.async_supportccxt ccxt.async_support.base.ws.cacher   r   r   rO  ccxt.base.typesr   r   r   r	   r
   r   r   !ccxt.async_support.base.ws.clientr   typingr   ccxt.base.errorsr   r   async_supportr    r4   r3   <module>rf     sB     f f  L L L 4  0 )tt!!** tr4   