
    ":h                         d dl Zd dlmZmZmZ d dlZd dlmZm	Z	m
Z
mZmZmZmZmZmZmZ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 y)    N)
ArrayCacheArrayCacheBySymbolByIdArrayCacheByTimestamp)AnyBalancesIntNumOrder	OrderBook	OrderSide	OrderTypeStrStringsTickerTickersTrade)Client)List)ExchangeError)ArgumentsRequired)
BadRequest)Precisec                   $    e Zd Zdef fdZd Zi fd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efdZd>dZdefdZi fde
defdZdi fdedefdZi fde
defdZdefdZd>dZi f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efdZdefdZ d>dZ!defdZ"d>dZ#d Z$d Z%defdZ&di fde
d
ede'fd Z(defd!Z)d" Z*defd#Z+d$ Z,d% Z-d&ddi fde
d	ed
edee.   fd'Z/defd(Z0defd)Z1defd*Z2defd+Z3di fd,e
defd-Z4dddi fded	ed
efd.Z5di fde
d/e6d0e7d1e8d2e9defd3Z:ddi fd,e
de
d/e6d0e7d1e9d2e9defd4Z;di fd,e
defd5Z<di fd6ee
   defd7Z=defd8Z>defd9Z?defd:Z@defd;ZAdefd<ZBi fd=ZC xZDS )?cexreturnc           	          | j                  t        t        |          i ddddddddddddd	dd
ddddddd ddddddddddddddddddiidi ii i d      S )NwsTwatchBalancewatchTickerwatchTickerswatchTradeswatchTradesForSymbolsFwatchMyTradeswatchOrderswatchOrderBook
watchOHLCVwatchPositioncreateOrderWseditOrderWscancelOrderWscancelOrdersWsfetchOrderWsfetchOpenOrdersWs)fetchTickerWsfetchBalanceWsapizwss://ws.cex.io/ws	orderbook)hasurlsoptions	streaming
exceptions)deep_extendsuperr   describe)self	__class__s    F/var/www/html/turnos/venv/lib/python3.12/site-packages/ccxt/pro/cex.pyr9   zcex.describe   s-   c4 9 ;d t 	
 t (   t !$ d     t   !$  !" $T#$ "&"&', . RA">
 " "	    c                     | j                  | j                  | j                  dd      d      }|| j                  d<   t        |      S )N	requestIdr      )sumsafe_integerr4   str)r:   r?   s     r<   
request_idzcex.request_id9   s=    HHT..t||[!LaP	$-[!9~r=   c                   K   | j                  |       d{    | j                         }| j                  d   d   }di | j                         d}| j                  ||      }| j	                  |||||       d{   S 7 i7 w)aP  
        watch balance and get the amount of funds available for trading or funds locked in orders

        https://cex.io/websocket-api#get-balance

        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: a `balance structure <https://docs.ccxt.com/#/?id=balance-structure>`
        Nr0   r   get-balanceedataoid)authenticaterD   r3   r7   watch)r:   paramsmessageHashurl	subscriberequests         r<   watch_balancezcex.watch_balance>   s      '''oo'iit$??$
	
 ""9f5ZZ[';PPP 	( Qs"   BBA$B<B=BBclientc                    | j                  |di       }| j                  |di       }| j                  |di       }d|i}t        |j                               }t        dt	        |            D ]W  }||   }	| j                         }
| j                  ||	      |
d<   | j                  ||	      |
d<   | j                  |	      }|
||<   Y | j                  |      | _	        | j                  |d      }|j                  | j                  |       y )	NrI   balanceobalanceinfor   freeusedrJ   )
safe_valuelistkeysrangelenaccountsafe_stringsafe_currency_codesafe_balancerU   resolve)r:   rS   messagerI   freeBalanceusedBalanceresultcurrencyIdsi
currencyIdr_   coderN   s                r<   handle_balancezcex.handle_balanceR   s   * w3oodIr:oodJ;D
 ;++-.q#k*+ 	#A$QJllnG"..{JGGFO"..{JGGFO**:6D"F4L	# ((0&&w6t||[1r=   Nsymbolsincelimitc                   K   | j                          d{    | j                  |      }|d   }| j                  d   d   }d}d|z   }|| j                  d<   | j	                  | j
                  |      }	|	jt        |	j                  j                               }
t        dt        |
            D ]/  }|
|   }||k(  r|dd	 }|d
k(  st        | j                  dz          dd|d   z   dz   |d   z   gd}| j                  ||      }| j                  ||||       d{   }t        dt        |            D ]
  }|||   d<    | j                  |||dd      S 7 E7 >w)a=  
        get the list of most recent trades for a particular symbol. Note: can only watch one symbol at a time.

        https://cex.io/websocket-api#old-pair-room

        :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>`
        Nrm   r0   r   tradeszold:currentWatchTradeSymbolr      oldz; watchTrades() only supports watching one symbol at a time.rP   pair-base-quoterH   rooms	timestampT)load_marketsmarketr3   r4   rZ   clientsr[   subscriptionsr\   r]   r^   r   idr7   rL   filter_by_since_limit)r:   rm   rn   ro   rM   r}   rO   rN   subscriptionHashrS   subscriptionKeysri   subscriptionKeyrd   rQ   rq   s                   r<   watch_tradeszcex.watch_tradesy   s     !!!V$!iit$!F?28./s3#F$8$8$=$=$?@1c"234 q"21"5"&66"1!A"6"e+'2o(oppq v.4vgFG
 ""7F3zz#{G=MNNq#f+& 	)A"(F1Ih	)))&%TRR5 	", Os)   E EB0E AE E <E E c                    | j                  |dg       }| j                  | j                  dd      }t        |      }| j	                  | j                  d      }|y | j                  |      }t        |      }t        d|      D ]2  }	|dz
  |	z
  }
||
   }| j                  ||      }|j                  |       4 d}|| _
        |j                  | j                  |       y )NrI   tradesLimit  rr   r   r@   rq   )	safe_listrB   r4   r   r`   r}   r^   r]   parse_ws_old_tradeappendrq   rc   )r:   rS   rd   rI   ro   storedrm   r}   
dataLengthri   indexrawTradeparsedrN   s                 r<   handle_trades_snapshotzcex.handle_trades_snapshot   s     ~~gvr2!!$,,tDE"!!$,,0IJ>V$Y
q*% 	"ANQ&EE{H,,Xv>FMM&!		"
 t{{K0r=   c                 v   t        |t              s|j                  d      }| j                  |d      }| j	                  |d      }| j                  |d      }| j                  |d      }| j                  |d      }| j                  |||| j                  |      | j                  |d      d |d d ||d d d|      S )	N:r   r@      rs      rm   )rW   r   r{   datetimerm   typesideordertakerOrMakerpriceamountcostfee)
isinstancer[   splitr`   rB   
safe_tradeiso8601)r:   trader}   r   r{   r   r   r   s           r<   r   zcex.parse_ws_old_trade   s     %&KK$Eq)%%eQ/	!!%+  *eQ'"Y/&&vx8  
  	r=   c                 "   | j                  |dg       }| j                  }t        |      }t        d|      D ]1  }|dz
  |z
  }||   }| j	                  |      }	|j                  |	       3 d}
|| _        |j                  | j                  |
       y )NrI   r   r@   rq   )rZ   rq   r^   r]   r   r   rc   )r:   rS   rd   rI   r   r   ri   r   r   r   rN   s              r<   handle_tradezcex.handle_trade   s     w3Y
q*% 	"ANQ&EE{H,,X6FMM&!		"
 t{{K0r=   c                   K   | j                          d{    | j                  |      }|d   }| j                  d   d   }d|z   }| j                  |dd      }dd	gd
}d	}|dk(  r9| j	                          d{    d|d   |d   g| j                         d}d|z   }| j                  ||      }	| j                  |||	|       d{   S 7 7 U7 w)a  

        https://cex.io/websocket-api#ticker-subscription

        watches a price ticker, a statistical calculation with the information calculated over the past 24 hours for a specific market
        :param str symbol: unified symbol of the market to fetch the ticker for
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :param str [params.method]: public or private
        :returns dict: a `ticker structure <https://docs.ccxt.com/#/?id=ticker-structure>`
        Nrm   r0   r   ticker:methodprivaterP   tickersry   tickerbaseIdquoteIdrG   )r|   r}   r3   r`   rK   rD   r7   rL   )
r:   rm   rM   r}   rO   rN   r   rd   r   rQ   s
             r<   watch_tickerzcex.watch_ticker   s     !!!V$!iit$&(!!&(I>
 %Y##%%%8$fY&7 (G  )61""7F3ZZ[';KLLL1 	" & Ms5   CCA$C;C<AC
CCCCsymbolsc                   K   | j                          d{    | j                  |      }| j                  d   d   }d}ddgd}| j                  ||      }| j	                  ||||       d{   }|d   }|,| j                  ||      s| j                  ||       d{   S | j                  r	i }	||	|<   |	S | j                  | j                  d|      S 7 7 k7 :w)a  

        https://cex.io/websocket-api#ticker-subscription

        watches price tickers for multiple markets, statistical information calculated over the past 24 hours for each market
        :param str[]|None symbols: unified symbols of the markets to fetch the ticker for, all market tickers are returned if not assigned
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: a dictionary of `ticker structures <https://docs.ccxt.com/#/?id=ticker-structure>`
        Nr0   r   r   rP   ry   rm   )
r|   market_symbolsr3   r7   rL   in_arraywatch_tickers
newUpdatesfilter_by_arrayr   )
r:   r   rM   rO   rN   rd   rQ   r   tickerSymbolrg   s
             r<   r   zcex.watch_tickers  s      !!!%%g.iit$
 ""7F3zz#{G[IIh't}}\7'K++GV<<<??F#)F< M##DLL(GDD' 	" J =s4   CCAC/C02C"C#7CCCc                   K   | j                          d{    | j                  |      }| j                  d   d   }| j                         }| j	                  d||d   |d   gd|      }| j                  ||||       d{   S 7 q7 w)a  

        https://docs.cex.io/#ws-api-ticker-deprecated

        fetches a price ticker, a statistical calculation with the information calculated over the past 24 hours for a specific market
        :param str symbol: unified symbol of the market to fetch the ticker for
        :param dict [params]: extra parameters specific to the cex api endpoint
        :returns dict: a `ticker structure <https://docs.ccxt.com/#/?id=ticker-structure>`
        Nr0   r   r   rv   rx   rH   rJ   rI   )r|   r}   r3   rD   extendrL   )r:   rm   rM   r}   rO   rN   rQ   s          r<   fetch_ticker_wszcex.fetch_ticker_ws5  s      !!!V$iit$oo'++F^VG_5
 	
 ZZ[';GGG 	" Hs"   BBA,BB
B
Bc                    | j                  |di       }| j                  |      }|d   }|y || j                  |<   d|z   }|j                  ||       |j                  |d       | j	                  |d      }||j                  ||       y y )NrI   rm   r   r   rJ   )rZ   parse_ws_tickerr   rc   r`   )r:   rS   rd   rI   r   rm   rN   s          r<   handle_tickerzcex.handle_tickerJ  s     w3%%d+!>%V&(v{+vy)&&w6"NN6;/ #r=   c           	         | j                  |dg       }| j                  |d      }|| j                  |d      }| j                  |d      }|| j                  |d      }| j                  |      }| j                  |      }|dz   |z   }| j                  |d      }	|	|	dz  }	| j	                  i d	|d|	d
| j                  |	      d| j                  |d      d| j                  |d      d| j                  |d      dd d| j                  |d      dd dd d| j                  |d      dd d| j                  |dd      dd d| j                  |d      d| j                  |d      dd d | j                  |d      |d|      S )Npairsymbol1r   symbol2r@   /r{   r   rm   r   highlowbid	bidVolumeask	askVolumevwapopenopen24closelastr   previousClosechangepriceChange
percentagepriceChangePercentageaveragevolume)
baseVolumequoteVolumerW   )rZ   r`   ra   rB   safe_tickerr   safe_string_2)
r:   r   r}   r   r   r   rv   rx   rm   r{   s
             r<   r   zcex.parse_ws_tickerd  s.   0 vvr2!!&)4>%%dA.F""695?&&tQ/G&&v.''0e#%%fk:	 !D(I !
f!
!
 Y/!
 D$$VV4	!

 4##FE2!
 4##FE2!
 !
 4##FE2!
 !
 D!
 D$$VX6!
 T!
 D&&vw?!
 T!
 d&&v}=!
  $**63JK!!
" t#!
$ ++FH=)!
* + 	r=   c                   K   | j                          d{    | j                          d{    | j                  d   d   }| j                         }| j	                  d|d|      }| j                  ||||       d{   S 7 o7 Y7 w)aN  

        https://docs.cex.io/#ws-api-get-balance

        query for balance and get the amount of funds available for trading or funds locked in orders
        :param dict [params]: extra parameters specific to the cex api endpoint
        :returns dict: a `balance structure <https://docs.ccxt.com/#/?id=balance-structure>`
        Nr0   r   rF   )rH   rJ   )r|   rK   r3   rD   r   rL   )r:   rM   rO   rN   rQ   s        r<   fetch_balance_wszcex.fetch_balance_ws  s      !!!!!!iit$oo'++
  ZZ[';GGG 	"! Hs2   BBBBABB
BB
Bc                   K   |t        | j                  dz         | j                          d{    | j                  |       d{    | j                  d   d   }| j                  |      }|d   }d|z   }dd|d	   |d
   gi|d}| j                  ||      }	| j                  |||	||	       d{   }
| j                  r|
j                  ||      }| j                  |
|||d      S 7 7 7 ;w)a  
        get the list of orders associated with the user. Note: In CEX.IO system, orders can be present in trade engine or in archive database. There can be time periods(~2 seconds or more), when order is done/canceled, but still not moved to archive database. That means, you cannot see it using calls: archived-orders/open-orders.

        https://docs.cex.io/#ws-api-open-orders

        :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>`
        Nz) watchOrders() requires a symbol argumentr0   r   rm   orders:open-ordersr   r   r   rG   T)r   r   r|   rK   r3   r}   r7   rL   r   getLimitfilter_by_symbol_since_limit)r:   rm   rn   ro   rM   rO   r}   rN   rd   rQ   orderss              r<   watch_orderszcex.watch_orders  s     >#DGG.Y$YZZ!!!'''iit$V$!&(8$9% 	
 ""7F3zz#{G['RR??OOFE2E00tTT) 	"'  Ss4   .C3C-C3	C/
A+C35C168C3/C31C3c                   K   |t        | j                  dz         | j                          d{    | j                  |       d{    | j                  d   d   }| j                  |      }d|d   z   }d|d   z   }dd	|d
   |d   gi|d   d}	| j                  |	|      }
| j                  |||
||
       d{   }| j                  ||d   ||      S 7 7 7 w)a  
        get the list of trades associated with the user. Note: In CEX.IO system, orders can be present in trade engine or in archive database. There can be time periods(~2 seconds or more), when order is done/canceled, but still not moved to archive database. That means, you cannot see it using calls: archived-orders/open-orders.

        https://docs.cex.io/#ws-api-open-orders

        :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>`
        Nz+ watchMyTrades() requires a symbol argumentr0   r   	myTrades:rm   r   r   r   r   r   rG   )	r   r   r|   rK   r3   r}   r7   rL   r   )r:   rm   rn   ro   rM   rO   r}   rN   r   rd   rQ   r   s               r<   watch_my_tradeszcex.watch_my_trades  s     >#DGG.[$[\\!!!'''iit$V$!F8$44$vh'778$9% (#	
 ""7F3zz#{G=MwWW009I5RWXX% 	"'  Xs4   .C CC 	C
A4C >C?C C C c                     | j                  |d      }| j                  |d      }|y | j                  ||       | j                  ||       y )NrI   r   )rZ   r`   handle_order_updatehandle_my_trades)r:   rS   rd   rI   r   s        r<   handle_transactionzcex.handle_transaction  sL    w/""43?  1fg.r=   c                    | j                  |di       }| j                  }|/| j                  | j                  dd      }t	        |      }|| _        | j                  |      }|j                  |       d|d   z   }|j                  ||       y )NrI   r   r   r   rm   )rZ   myTradesrB   r4   r   parse_ws_trader   rc   )r:   rS   rd   rI   r   ro   r   rN   s           r<   r   zcex.handle_my_trades  s    V w3>%%dllM4HE+E2F"DM##D)e!E(O3v{+r=   c                 P   | j                  |d      }| j                  |d      }| j                  |d      }| j                  |d      }| j                  |d      }| j                  |      }| j                  |      }	|dz   |	z   }
| j                  |d      }|dk(  r|	dz   |z   }
t        j                  ||      }| j                  |d	      | j                  |d
      || j	                  |      ||
d |d |d |d d}| j                  |d      }|	||	d d|d<   | j                  ||      S )Nr   r   timerm   r   r   r   sellr   r   )r   r   rW   r{   r   rm   r   r   r   r   r   r   r   
fee_amountr   currencyrater   )r`   ra   r   
string_div	parse8601r   )r:   r   r}   r   r   r   r   r   rv   rx   rm   r   parsedTrader   s                 r<   r   zcex.parse_ws_trade7  sW   0 v.  0##E62!!%2""5)4&&v.''0e#!!%26>S[4'F''6F""5$/%%eW51  
 ul3?!"K
 {F33r=   c                 N   | j                  |di       }| j                  |d      dk(  }| j                  |dd      }| j                  |d      }| j                  |d      }| j                  |d      }| j                  |d	      }	|	$| j                  |	d
      }| j                  |	d      }| j                  |      }
| j                  |      }|
dz   |z   }| j	                  |      }| j                  |
|      }| j                  -| j                  | j                  dd      }t        |      | _        | j                  }| j                  |j                  |i       }| j                  ||      }|| j                  ||      }||d<   | j                  |dd      }|rd|d<   |rd|d<   | j                  |d      }|	||d d|d<   | j                  |d      }||d<   | j                  |      |d<   | j                  |      }|j!                  |       d|z   }|j#                  ||       y )NrI   rH   txr   r   remainsrm   r   r   r   r   ordersLimitr   	remainingcancelFcanceledstatusclosedr   r   r   r{   r   r   )rZ   r`   r   ra   safe_marketcurrency_from_precisionr   rB   r4   r   hashmapparse_ws_order_update	safe_boolsafe_numberr   
safe_orderr   rc   )r:   rS   rd   rI   isTransactionorderIdr   r   r   r   rv   rx   rm   r}   ro   storedOrdersordersBySymbolr   r   r   r{   rN   s                         r<   r   zcex.handle_order_updates  s<   J w3((#6$>$$T49""43!!$1""43tV,%%dI6F&&tY7G&&v.''0e#!!&)..tW=;;%%dllM4HE07DK{{)=)=vrJ8=..tV<E$k>>$%8(E(O&E(OtU+?!E%L
 %%dF3	&k LL3j&E"&(|[1r=   c           	      ,   | j                  |d      d u}| j                  |d      }d }|| j                  |d   |      }| j                  |d      }|s| j                  |d   |       | j                  |d      }| j                  |d      }| j                  |d      }	|	$| j                  |d      }| j                  |d      }| j                  |      }
| j                  |      }d }|

||
d	z   |z   }| j	                  ||      }| j                  |d
| j                               }|}|r| j                  |      }| j                  |dd      }d}|rd}n|rd}i d| j                  |dd      dd d|d|d| j                  |      dd d|d|dd dd dd d| j                  |d      d| j                  |d      dd dd dd d d |d || j                  |d!d"      |d d#d d$}|r| j                  ||      |d%<   | j                  ||      S )&Ndr   rv   r   rm   r   r   r   r   r   r   Fr   r   r   r   r   clientOrderIdrW   r{   r   lastTradeTimestampr   r   timeInForcepostOnlyr   r   	stopPricetriggerPricer   r   r   r   r   )r   filledr   r   rq   rq   )rZ   r`   r   ra   r   rB   millisecondsr   r  r   r   r  safe_number_2r   r  )r:   r   r}   r  remainsPrecisionr   r   r   r   r   rv   rx   rm   r   r{   r   r   parsedOrders                     r<   r  zcex.parse_ws_order_update  s   J s34?++E9=	'44VF^EUVI!!%2((@!!%2""5)4uf-%%eY7F&&ui8G&&v.''0 1CZ%'F!!&&1  0A0A0CD	t,I>>%59FF
$$$UD':
T
 E
 	

 Y/
 !$
 f
 f
 D
 4
 
 D$$UF3
 T%%eW5
 
 D
  t!
" D#
$ "**5%F!
 5
8 $($7$7v$FK!{F33r=   c                     |y t        |      }| j                  |j                  |      |_        |j                          t	        |      S N)r   rA   decimalsreducerC   )r:   r   scaleprecises       r<   from_precisionzcex.from_precisionG  s@    >&/88G$4$4e<7|r=   c                 f    | j                  | j                  |   dd      }| j                  ||      S )N	precisionr   )rB   
currenciesr  )r:   r   r   r  s       r<   r   zcex.currency_from_precisionO  s2    !!$//(";[!L""6511r=   c                    | j                  |d      }| j                  |dg       }| j                  }| j                  (| j                  | j                  dd      }t        |      }t        dt        |            D ]@  }||   }| j                  |      }	| j                  ||	      }
d|
d<   |j                  |
       B || _        d|z   }t        |      }|dkD  r|j                  ||       y y )	NrJ   rI   r   r   r   r   r   r   )r`   rZ   r   rB   r4   r   r]   r^   r   parse_orderr   rc   )r:   rS   rd   rm   	rawOrdersmyOrdersro   ri   rawOrderr}   r   rN   ordersLengths                r<   handle_orders_snapshotzcex.handle_orders_snapshotS  s      !!'51OOGVR8	;;;;%%dllM4HE-e4Hq#i.) 	#A |H%%f-F$$Xv6E$E(OOOE"	# &(8}!NN8[1 r=   c                   K   | j                          d{    | j                          d{    | j                  |      }|d   }| j                  d   d   }d|z   }|dn|}d|d   |d	   gd
|d| j	                         d}| j                  ||      }	| j                  |||	|       d{   }
|
j                         S 7 7 7 w)a.  
        watches information on open orders with bid(buy) and ask(sell) prices, volumes and other data

        https://cex.io/websocket-api#orderbook-subscribe

        :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
        Nrm   r0   r   
orderbook:r   order-book-subscriber   r   T)r   rP   depthrG   )r|   rK   r}   r3   rD   r7   rL   ro   )r:   rm   ro   rM   r}   rO   rN   r+  rP   rQ   r1   s              r<   watch_order_bookzcex.watch_order_booku  s      !!!!!!V$!iit$"V+m%' 8$9% " ??$
	 ""9f5**S+wLL	  + 	"!& Ms2   C
CC
CB C
/C0C
C
C
c                    | j                  |di       }| j                  |d      }| j                  |      }d|z   }| j                  |dd      }| j	                  |d      }| j                  i       }	| j                  |||dd      }
||
d	<   |	j                  |
       d
|i| j                  d   |<   |	| j                  |<   |j                  |	|       y )NrI   r   r)  timestamp_msr{   r   bidsasksnonceincrementalIdr1   )rZ   r`   pair_to_symbolsafe_integer_2rB   
order_bookparse_order_bookresetr4   
orderbooksrc   )r:   rS   rd   rI   r   rm   rN   r{   r2  r1   snapshots              r<   handle_order_book_snapshotzcex.handle_order_book_snapshot  s    . w3f-$$T*"V+''nkJ	))$5OOB'	((vy&&Q)!]-
[!&) #,y+.r=   c                     |j                  d      }| j                  |d      }| j                  |d      }| j                  |      }| j                  |      }|dz   |z   }|S )Nr   r   r@   r   )r   r`   ra   )r:   r   partsr   r   rv   rx   rm   s           r<   r3  zcex.pair_to_symbol  sf    

3!!%+""5!,&&v.''0e#r=   c                 j   | j                  |di       }| j                  |d      }| j                  |dd      }| j                  |      }| j                  | j                  |      }d|z   }||d   dz   k7  r,|j
                  |= |j                  | j                  dz   |       | j                  |d	      }	| j                  |d
g       }
| j                  |dg       }| j                  |d
   |
       | j                  |d   |       |	|d<   | j                  |	      |d<   ||d<   |j                  ||       y )NrI   r   r    r)  r1  r@   z# watchOrderBook() skipped a messager   r0  r/  r{   r   )rZ   rB   r`   r3  r8  r   rejectr   handle_deltasr   rc   )r:   rS   rd   rI   r2  r   rm   storedOrderBookrN   r{   r0  r/  s               r<   handle_order_book_updatezcex.handle_order_book_update  s7    w3))$5fb1$$T*//$//6B"V+OG4q88$$[1MM$''$II;W%%dF3	tVR0tVR0?62D9?62D9'0$&*ll9&=
##0 4r=   c                 L    | j                  |dd      }|j                  |       y )Nr   r@   )parse_bid_ask
storeArray)r:   booksidedeltabidAsks       r<   handle_deltazcex.handle_delta  s$    ##E1a0F#r=   c                 b    t        dt        |            D ]  }| j                  |||           y )Nr   )r]   r^   rI  )r:   rF  deltasri   s       r<   r@  zcex.handle_deltas  s0    q#f+& 	3Ahq	2	3r=   1mc                   K   | j                          d{    | j                  |      }|d   }d|z   }| j                  d   d   }d|d|d   z   d	z   |d
   z   gd}	| j                  ||| j	                  |	|      |       d{   }
| j
                  r|
j                  ||      }| j                  |
||dd      S 7 7 9w)a  

        https://cex.io/websocket-api#minute-data

        watches historical candlestick data containing the open, high, low, and close price, and the volume of a market. It will return the last 120 minutes with the selected timeframe and then 1m candle updates after that.
        :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
        Nrm   ohlcv:r0   r   z
init-ohlcvru   r   rw   r   )rH   ri   rz   r   T)r|   r}   r3   rL   r   r   r   r   )r:   rm   	timeframern   ro   rM   r}   rN   rO   rQ   ohlcvs              r<   watch_ohlcvzcex.watch_ohlcv  s      !!!V$!'iit$&**S06)3DD
 jjk4;;w3OQ\]]??NN651E))%q$GG 	" ^s"   CB=A.CB?8C?Cc                    | j                  |d      }|j                  d      }| j                  |d      }| j                  |d      }| j                  |      }| j                  |      }|dz   |z   }	| j                  |	      }
d|	z   }| j	                  |dg       }| j                  | j                  dd	      }t        |      }| j                  |d      }t        dt        |            D ]&  }|j                  | j                  ||   |
             ( |	| j                  vri | j                  |	<   || j                  |	   d
<   |j                  ||       y )Nr   r   r   r@   r   rN  rI   
OHLCVLimitr   unknown)r`   r   ra   r   rZ   rB   r4   r   sort_byr]   r^   r   parse_ohlcvohlcvsrc   )r:   rS   rd   r   r<  r   r   rv   rx   rm   r}   rN   rI   ro   r   sortedri   s                    r<   handle_init_ohlcvzcex.handle_init_ohlcv  sE   $ 0

3!!%+""5!,&&v.''0e#!!&)'w3!!$,,dC&u-dA&q#f+& 	?AMM$**6!9f=>	?$++%"$DKK)/FI&v{+r=   c                     |S r   r:   rS   rd   s      r<   handle_ohlcv24zcex.handle_ohlcv245  s	     r=   c           	         | j                  |di       }| j                  |d      }| j                  |      }d|z   }| j                  |d      | j	                  |d      | j	                  |d      | j	                  |d      | j	                  |d      | j	                  |d	      g}| j                  | j
                  |      }|j                  |       |j                  ||       y )
NrI   r   rN  r   ohlcv)rZ   r`   r3  safe_timestampr  rW  r   rc   )	r:   rS   rd   rI   r   rm   rN   rP  r   s	            r<   handle_ohlcv1mzcex.handle_ohlcv1m?  s      w3f-$$T*'f-T3'T3'T3'T3'T3'
 f5ev{+r=   c           
      .   | j                  |dg       }| j                  |d      }| j                  |      }d|z   }| j                  |   d   }t	        dt        |            D ]  }| j                  ||   d      | j                  ||   d      | j                  ||   d      | j                  ||   d      | j                  ||   d	      | j                  ||   d
      g}	|j                  |	        t        |      }
|
dkD  r|j                  ||       y y )NrI   r   rN  rT  r   r@   r   rs   r      )
rZ   r`   r3  rW  r]   r^   rd  r  r   rc   )r:   rS   rd   rI   r   rm   rN   r   ri   rP  r   s              r<   handle_ohlcvzcex.handle_ohlcv_  s!    w30$$T*'V$Y/q#d)$ 		!A##DGQ/  a!,  a!,  a!,  a!,  a!,E MM% 		! Y
>NN6;/ r=   r   c                   K   | j                          d{    | j                          d{    d}|| j                  |      }| j                  dt	        |      i|      }| j
                  d   d   }| j                         }d||d}| j                  ||||       d{   }	| j                  |	|      S 7 7 7 w)av  
        fetches information on an order made by the user

        https://docs.cex.io/#ws-api-get-order

        :param str id: the order id
        :param str symbol: not used by cex fetchOrder
        :param dict [params]: extra parameters specific to the cex api endpoint
        :returns dict: An `order structure <https://docs.ccxt.com/#/?id=order-structure>`
        Norder_idr0   r   	get-orderr   )	r|   rK   r}   r   rC   r3   rD   rL   r"  )
r:   r   rm   rM   r}   rI   rO   rN   rQ   responses
             r<   fetch_order_wszcex.fetch_order_ws}  s      !!!!!![[(F{{B
 iit$oo'

 Cg{KK&11! 	"! Ls2   CB;CB=A5C$B?%C=C?Cc                   K   |t        | j                  dz         | j                          d{    | j                          d{    | j	                  |      }| j
                  d   d   }| j                         }| j                  d|d   |d   gi|      }d||d	}	| j                  |||	|       d{   }
| j                  |
||||      S 7 7 7 w)
a  

        https://docs.cex.io/#ws-api-open-orders

        fetch all unfilled currently open orders
        :param str symbol: unified market symbol
        :param int [since]: the earliest time in ms to fetch open orders for
        :param int [limit]: the maximum number of  open orders structures to retrieve
        :param dict [params]: extra parameters specific to the cex api endpoint
        :returns Order[]: a list of `order structures <https://docs.ccxt.com/#/?id=order-structure>`
        Nz% fetchOpenOrdersWs requires a symbol.r0   r   r   r   r   r   r   )
r   r   r|   rK   r}   r3   rD   r   rL   parse_orders)r:   rm   rn   ro   rM   r}   rO   rN   rI   rQ   rl  s              r<   fetch_open_orders_wszcex.fetch_open_orders_ws  s      >#DGG.U$UVV!!!!!!V$iit$oo'{{VH%vi'89
 

 Cg{KK  65%HH 	"! Ls4   .CCCC	A0C9C:CCCr   r   r   r   c                   K   |t        | j                  dz         | j                          d{    | j                          d{    | j	                  |      }| j
                  d   d   }| j                         }	| j                  |d   |d   g|||d|      }
d|	|
d	}| j                  ||	||	       d{   }| j                  ||      S 7 7 7 w)
a  

        https://docs.cex.io/#ws-api-order-placement

        create a trade order
        :param str symbol: unified symbol of the market to create an order in
        :param str type: 'market' or 'limit'
        :param str side: 'buy' or 'sell'
        :param float amount: how much of currency you want to trade in units of base currency
        :param float price: the price at which the order is to be fulfilled, in units of the quote currency, ignored in market orders
        :param dict [params]: extra parameters specific to the kraken api endpoint
        :param boolean [params.maker_only]: Optional, maker only places an order only if offers best sell(<= max) or buy(>= max) price for self pair, if not order placement will be rejected with an error - "Order is not maker"
        :returns dict: an `order structure <https://docs.ccxt.com/en/latest/manual.html#order-structure>`
        Nz( createOrderWs requires a price argumentr0   r   r   r   )r   r   r   r   place-orderr   )
r   r   r|   rK   r}   r3   rD   r   rL   r"  )r:   rm   r   r   r   r   rM   r}   rO   rN   rI   rQ   r%  s                r<   create_order_wszcex.create_order_ws  s      =TWW'QQRR!!!!!!V$iit$oo'{{H%vi'89	

  

 Cg{KK&11# 	"! Ls4   .CCCC	A3C<C=CCCc                   K   |t        | j                  dz         |t        | j                  dz         | j                          d{    | j                          d{    | j	                  |      }| j                  |d   |d   g||||d|      }	| j                         }
| j                  d   d   }d	|
|	d
}| j                  ||
||
|
       d{   }| j                  ||      S 7 7 7 w)a  
        edit a trade order

        https://docs.cex.io/#ws-api-cancel-replace

        :param str id: order id
        :param str symbol: unified symbol of the market to create an order in
        :param str type: 'market' or 'limit'
        :param str side: 'buy' or 'sell'
        :param float amount: how much of the currency you want to trade in units of the base currency
        :param float|None [price]: the price at which the order is to be fulfilled, in units of the quote currency, ignored in market orders
        :param dict [params]: extra parameters specific to the cex api endpoint
        :returns dict: an `order structure <https://docs.ccxt.com/en/latest/manual.html#order-structure>`
        Nz' editOrder() requires a amount argumentz& editOrder() requires a price argumentr   r   )r   r   r   r   rj  r0   r   cancel-replace-orderr   )
r   r   r|   rK   r}   r   rD   r3   rL   r"  )r:   r   rm   r   r   r   r   rM   r}   rI   rN   rO   rQ   rl  s                 r<   edit_order_wszcex.edit_order_ws  s     >#DGG.W$WXX=#DGG.V$VWW!!!!!!V${{H%vi'89
  oo'iit$'

 Cg{KXX&11% 	"!  Ys7   AC5
C/C5"C1#A5C5C3C51C53C5c                 v  K   | j                          d{    | j                          d{    d}|| j                  |      }| j                  d|i|      }| j	                         }| j
                  d   d   }d||d}| j                  |||||       d{   }	| j                  |	|      S 7 7 7 w)a]  

        https://docs.cex.io/#ws-api-order-cancel

        cancels an open order
        :param str id: order id
        :param str symbol: not used by cex cancelOrder()
        :param dict [params]: extra parameters specific to the cex api endpoint
        :returns dict: An `order structure <https://docs.ccxt.com/#/?id=order-structure>`
        Nrj  r0   r   cancel-orderr   )r|   rK   r}   r   rD   r3   rL   r"  )
r:   r   rm   rM   r}   rI   rN   rO   rQ   rl  s
             r<   cancel_order_wszcex.cancel_order_ws  s      !!!!!![[(F{{
 oo'iit$

 Cg{KXX&11! 	"! Ys2   B9B3B9B5A-B9B7B95B97B9idsc                   K   |t        | j                  dz         | j                          d{    | j                          d{    | j	                         }| j                  d|i|      }| j                  d   d   }d||d}| j                  |||||       d{   }| j                  |d      }	| j                  |	ddd|      S 7 7 7 /w)ap  
        cancel multiple orders

        https://docs.cex.io/#ws-api-mass-cancel-place

        :param str[] ids: order ids
        :param str symbol: not used by cex cancelOrders()
        :param dict [params]: extra parameters specific to the cex api endpoint
        :returns dict: a list of `order structures <https://docs.ccxt.com/#/?id=order-structure>`
        Nz1 cancelOrderWs does not allow filtering by symbolzcancel-ordersr0   r   mass-cancel-place-ordersr   )
r   r   r|   rK   rD   r   r3   rL   rZ   ro  )
r:   rz  rm   rM   rN   rI   rO   rQ   rl  canceledOrderss
             r<   cancel_orders_wszcex.cancel_orders_ws   s      TWW'ZZ[[!!!!!!oo'{{S
 iit$+

 Cg{KXX ?C  tT6JJ1 	"! Ys4   .CCCC	AC!C",CCCc                 p    | j                  |d      }| j                  |d      }|j                  ||       y )NrI   rJ   )rZ   r`   rc   )r:   rS   rd   rI   rN   s        r<   resolve_datazcex.resolve_dataG  s4    $ w/&&w6t[)r=   c                     |S r  r[  r\  s      r<   handle_connectedzcex.handle_connected]  s	     r=   c                    	 | j                  |di       }| j                  |d      }| j                  |dd      }| j                  dz   |z   dz   |z   }| j                  | j                  d   ||       | j                  | j                  d   ||       t        |      # t        $ rH}| j                  |d      }| j                  |d	   |      }||j                  ||       n|Y d }~y d }~ww xY w)
NrI   errorrH   r>   exactbroadrJ   futures)	rZ   r`   r   throw_exactly_matched_exceptionr6   throw_broadly_matched_exceptionr   	Exceptionr?  )	r:   rS   rd   rI   r  eventfeedbackrN   futures	            r<   handle_error_messagezcex.handle_error_messagee  s    	??7FB7D$$T73E$$Wc26Eww}u,s2U:H001I5RZ[001I5RZ[)) 	**7E:K__VI%6DF!e[1 2		s   BB 	C.&>C))C.c                    | j                  |d      }|dk(  r| j                  ||       y | j                  |d      }i d| j                  d| j                  d| j                  d| j                  d| j
                  d	| j                  d
| j                  d| j                  d| j                  d| j                  d| j                  d| j                  d| j                  d| j                  d| j                  d| j                   d| j                  | j                  | j                  | j                  | j                  d}| j#                  ||      }|
 |||       y y )Nokr  rH   auth	connectedtickr   zinit-ohlcv-dataohlcv24ohlcv1mrP  rF   r*  	md_updater   r   zhistory-updatehistoryr   rr  )ru  rx  r|  rk  )r`   r  handle_authentication_messager  r   rY  r]  re  rh  rl   r:  rB  r  r   r   r   r   rZ   )r:   rS   rd   r  r  handlershandlers          r<   handle_messagezcex.handle_message~  s   gt,=%%fg6  #.
D66
..
 D&&
 d((	

 t55
 t**
 t**
 T&&
 4..
 #D$C$C
 66
 4,,
 T--
 d//
 t22
  $))!
" 4,,#
$ %)$5$5 --(,(9(9**+
. //(E2FG$ r=   c                 d    | j                  |j                  d      }||j                  d       y y )NauthenticatedT)rZ   r  rc   )r:   rS   rd   r  s       r<   r  z!cex.handle_authentication_message  s/     ANN4  r=   c                 j  K   | j                   d   d   }| j                  |      }d}|j                  d      }| j                  |j                  |      }|| j                          t        | j                               }|| j                  z   }| j                  | j                  |      | j                  | j                        t        j                        }	d| j                  |	j                         |dd}
| j                  ||| j!                  |
|      |       d {    | d {   S 7 7 w)Nr0   r   r  r  )key	signaturer{   )rH   r  )r3   rS   r  rZ   r   check_required_credentialsrC   secondsapiKeyhmacencodesecrethashlibsha256upperrL   r   )r:   rM   rO   rS   rN   r  r  r1  r  r  rQ   s              r<   rK   zcex.authenticate  s    iit$S!%/(<(<kJ ++-'E4;;&D		$++d"3T[[5Mw~~^I;;!*!2!&G **S+t{{7F/K[YYY| Zs$   DD3 D/!	D3*D1+D31D3r  )E__name__
__module____qualname__r   r9   rD   r   rR   r   rl   rC   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r
   r   r   r   r   r   r   r  r  r   r'  r   r,  r:  r3  rB  rI  r@  r[   rQ  rY  r]  re  rh  rm  rp  r   r   floatr	   rs  rv  ry  r~  r  r  r  r  r  rK   __classcell__)r;   s   @r<   r   r      s   ## #J
 *, Q Q(%2V %2N <@d[] &S &SS &S &Sbfglbm &SP1V 1:<16 1* 68 #M #MF #MJ 6:" E7 E E> 9; HC Hv H*0F 04:x -/ H8 H& 04$UYbd "U "UC "Us "Uimnsit "UH 37TX\eg  YC  Ys  YRU  Ylpqvlw  YD/ /4,v 4,l:4xo2& o2ba4F2 2V  2D @DB  !S  !  !S\  !D%/ %/N 5v  5D$3 8<$]ajl H HC HWZ Hquvzq{ H<$, $,LV ,V ,@06 0< ;?r 2s 2C 2: 8<$]ajl I IC IWZ I< pt|~ "2C "2y "2	 "2[` "2il "2  DI "2H im{  IK %2c %23 %2i %2y %2be %2ux %2  PU %2N <@ 2 2S 2: DHPR %K$s) %KS %KN*6 *,v 6 2%V %B!F ! )+ r=   r   )!ccxt.async_supportccxt ccxt.async_support.base.ws.cacher   r   r   r  ccxt.base.typesr   r   r   r	   r
   r   r   r   r   r   r   r   r   !ccxt.async_support.base.ws.clientr   typingr   ccxt.base.errorsr   r   r   ccxt.base.preciser   async_supportr   r[  r=   r<   <module>r     sW     f f  B  B  B  B 4  * . ' %p$


 
  pr=   