
    ei                         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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ArrayCacheByTimestamp)AnyBalancesBoolIntNumOrder	OrderBook	OrderSide	OrderTypeStrStringsTickerTickersTrade)Client)List)ExchangeError)ArgumentsRequired)
BadRequest)Precisec                   V    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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dee/         f
d)Z0defd*Z1defd+Z2defd,Z3defd-Z4di fd.e
defd/Z5dddi fded	ed
efd0Z6di fde
d1e7d2e8d3e9d4e:defd5Z;ddi fd.e
de
d1e7d2e8d3e:d4e:defd6Z<di fd.e
defd7Z=di fd8ee
         defd9Z>defd:Z?defd;Z@dedeAfd<ZBdefd=ZCdefd>ZDi fd?ZE xZFS )Acexreturnc           	         |                      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i dd id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symbol)	orderbookr"   )hasurlsoptions	streaming
exceptions)deep_extendsuperr   describe)self	__class__s    J/var/www/html/volatility/venv/lib/python3.11/site-packages/ccxt/pro/cex.pyr;   zcex.describe   sZ   c4 0 0 9 9 ; ;d t 	
 t (   t !$ d     t   !$  !" $T#$ "&"&'  , .  d  G%>
 %>
 % % %	    c                     |                                   |                     |                     | j        dd          d          }|| j        d<   |                                  t          |          S )N	requestIdr      )lock_idsumsafe_integerr6   	unlock_idstr)r<   rA   s     r>   
request_idzcex.request_id<   s^    HHT..t|[!LLaPP	$-[!9~~r?   c                 $  K   |                      |           d{V  |                                 }| j        d         d         }di |                                 d}|                     ||          }|                     |||||           d{V S )aN  
        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>`
        Nr1   r   get-balanceedataoid)authenticaterH   r5   r9   watch)r<   paramsmessageHashurl	subscriberequests         r>   watch_balancezcex.watch_balanceC   s       '''''''''oo''it$??$$
 
	
 ""9f55ZZ[';PPPPPPPPPr?   clientc                    |                      |di           }|                      |di           }|                      |di           }d|i}t          |                                          }t          dt	          |                    D ]j}||         }	|                                 }
|                     ||	          |
d<   |                     ||	          |
d<   |                     |	          }|
||<   k|                     |          | _	        |                     |d          }|
                    | j	        |           d S )	NrM   balanceobalanceinfor   freeusedrN   )
safe_valuelistkeysrangelenaccountsafe_stringsafe_currency_codesafe_balancerY   resolve)r<   rW   messagerM   freeBalanceusedBalanceresultcurrencyIdsi
currencyIdrc   coderR   s                r>   handle_balancezcex.handle_balanceW   s7   * w33oodIr::oodJ;;D
 ;++--..q#k**++ 	# 	#A$QJllnnG"..{JGGGFO"..{JGGGFO**:66D"F4LL((00&&w66t|[11111r?   Nr2   sincelimitc                 J  K   |                      | j        d         d          }|||k    rt          | j        dz             || j        d         d<   |                                  d{V  |                     |          }|d         }| j        d         d         }d}d|z   }	|                     | j        |          }
|
|t          |
j
                                                  }t          d	t          |                    D ]8}||         }||	k    r|d	d
         }|dk    rt          | j        dz             9dd|d         z   dz   |d         z   gd}|                     ||          }|                     ||||	           d{V }|                     |||dd          S )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>`
        r"   r2   Nz : self exchange only supports watching trades for one symbol per instance. You should either set .options["watchTrades"]["symbol"] to new symbol, or create a new instancer1   r   tradeszold:r      oldz; watchTrades() only supports watching one symbol at a time.rT   pair-base-quoterL   rooms	timestampT)rd   r6   r   idload_marketsmarketr5   r^   clientsr_   subscriptionsr`   ra   rb   r   r9   rP   filter_by_since_limit)r<   r2   rq   rr   rQ   currentSymbolr   rS   rR   subscriptionHashrW   subscriptionKeysrm   subscriptionKeyrh   rU   rt   s                    r>   watch_tradeszcex.watch_trades~   s      ((m)DhOO$&)@)@#DG  /\  %\  ]  ]  ]06]#H-!!!!!!!!!V$$!it$!F?s33#F$8$=$=$?$?@@1c"23344 q q"21"5"&666"1!A#"6"e++'2o(oppp , v.4vgFG
 
 ""7F33zz#{G=MNNNNNNNN))&%TRRRr?   c                 2    |                      ||           d S Nhandle_trades_innerr<   rW   rh   s      r>   handle_trades_snapshotzcex.handle_trades_snapshot   s      	  11111r?   c                    t          |t                    s|                    d          }|                     |d          }|                     |d          }|                     |d          }|                     |d          }|                     |d          }|                     ||||                     |          |                     |d          d |d d ||d d d|          S )	N:r   rB      ru      r2   )r[   r~   r}   datetimer2   typesideordertakerOrMakerpriceamountcostfee)
isinstancer_   splitrd   rE   
safe_tradeiso8601)r<   trader   r   r}   r   r   r~   s           r>   parse_ws_old_tradezcex.parse_ws_old_trade   s     %&& 	%KK$$Eq))%%eQ//	!!%++  **eQ''"Y//&&vx88  
  
   	r?   c                 2    |                      ||           d S r   r   r   s      r>   handle_tradezcex.handle_trade   s      	  11111r?   c                 F   |                      |dg           }|                     | j        d         d          }|| j        vr3|                     | j        dd          }t          |          | j        |<   | j        |         }|                     |          }t          |          }t          d|          D ]=}	|dz
  |	z
  }
||
         }| 	                    ||          }|
                    |           >d}|| j        |<   |                    | j        |         |           d S )	NrM   r"   r2   tradesLimit  r   rB   rt   )	safe_listrd   r6   rt   rE   r   r   rb   ra   r   appendrg   )r<   rW   rh   rM   r2   rr   storedr   
dataLengthrm   indexrawTradeparsedrR   s                 r>   r   zcex.handle_trades_inner   s!   ~~gvr22!!$,}"=xHH$+%%%%dlM4HHE",U"3"3DKV$V$$YY
q*%% 	" 	"ANQ&EE{H,,Xv>>FMM&!!!!$Ft{6*K88888r?   c                   K   |                                   d{V  |                     |          }|d         }| j        d         d         }d|z   }|                     |dd          }dd	gd
}d	}|dk    rE|                                  d{V  d|d         |d         g|                                 d}d|z   }|                     ||          }	|                     |||	|           d{V S )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>`
        Nr2   r1   r   ticker:methodprivaterT   tickersr{   tickerbaseIdquoteIdrK   )r   r   r5   rd   rO   rH   r9   rP   )
r<   r2   rQ   r   rS   rR   r   rh   r   rU   s
             r>   watch_tickerzcex.watch_ticker   sF      !!!!!!!!!V$$!it$&(!!&(I>>
 
 %Y##%%%%%%%%%8$fY&7 (( G  )61""7F33ZZ[';KLLLLLLLLLr?   symbolsc                   K   |                                   d{V  |                     |          }| j        d         d         }d}ddgd}|                     ||          }|                     ||||           d{V }|d         }|2|                     ||          s|                     ||           d{V S | j        r	i }	||	|<   |	S |                     | j	        d|          S )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>`
        Nr1   r   r   rT   r{   r2   )
r   market_symbolsr5   r9   rP   in_arraywatch_tickers
newUpdatesfilter_by_arrayr   )
r<   r   rQ   rS   rR   rh   rU   r   tickerSymbolrk   s
             r>   r   zcex.watch_tickers  s/      !!!!!!!!!%%g..it$
 
 ""7F33zz#{G[IIIIIIIIh't}}\7'K'K++GV<<<<<<<<<? 	F#)F< M##DL(GDDDr?   c                 >  K   |                                   d{V  |                     |          }| j        d         d         }|                                 }|                     d||d         |d         gd|          }|                     ||||           d{V S )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>`
        Nr1   r   r   rx   rz   rL   rN   rM   )r   r   r5   rH   extendrP   )r<   r2   rQ   r   rS   rR   rU   s          r>   fetch_ticker_wszcex.fetch_ticker_ws4  s       !!!!!!!!!V$$it$oo''++F^VG_5
 
 	 
 ZZ[';GGGGGGGGGr?   c                 L   |                      |di           }|                     |          }|d         }|d S || j        |<   d|z   }|                    ||           |                    |d           |                     |d          }||                    ||           d S d S )NrM   r2   r   r   rN   )r^   parse_ws_tickerr   rg   rd   )r<   rW   rh   rM   r   r2   rR   s          r>   handle_tickerzcex.handle_tickerI  s     w33%%d++!>F%V&(v{+++vy)))&&w66"NN6;///// #"r?   c           	         |                      |dg           }|                     |d          }||                     |d          }|                     |d          }||                     |d          }|                     |          }|                     |          }|dz   |z   }|                     |d          }	|	|	dz  }	|                     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|          S )Npairsymbol1r   symbol2rB   /r}   r   r2   r   highlowbid	bidVolumeask	askVolumevwapopenopen24closelastr   previousClosechangepriceChange
percentagepriceChangePercentageaveragevolume)
baseVolumequoteVolumer[   )r^   rd   re   rE   safe_tickerr   safe_string_2)
r<   r   r   r   r   r   rx   rz   r2   r}   s
             r>   r   zcex.parse_ws_tickerc  ss   0 vvr22!!&)44>%%dA..F""6955?&&tQ//G&&v..''00e#%%fk::	 !D(I !
f!
!
 Y//!
 D$$VV44	!

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

        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>`
        Nr1   r   rJ   )rL   rN   )r   rO   r5   rH   r   rP   )r<   rQ   rS   rR   rU   s        r>   fetch_balance_wszcex.fetch_balance_ws  s       !!!!!!!!!!!!!!!!!!it$oo''++
 
   ZZ[';GGGGGGGGGr?   c                   K   |t          | j        dz             |                                  d{V  |                     |           d{V  | j        d         d         }|                     |          }|d         }d|z   }dd|d	         |d
         gi|d}|                     ||          }	|                     |||	||	           d{V }
| j        r|
	                    ||          }| 
                    |
|||d          S )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 argumentr1   r   r2   orders:open-ordersr   r   r   rK   T)r   r~   r   rO   r5   r   r9   rP   r   getLimitfilter_by_symbol_since_limit)r<   r2   rq   rr   rQ   rS   r   rR   rh   rU   orderss              r>   watch_orderszcex.watch_orders  sO      >#DG.Y$YZZZ!!!!!!!!!'''''''''it$V$$!&(8$9% 	
 	
 ""7F33zz#{G['RRRRRRRR? 	3OOFE22E00tTTTr?   c                   K   |t          | j        dz             |                                  d{V  |                     |           d{V  | j        d         d         }|                     |          }d|d         z   }d|d         z   }dd	|d
         |d         gi|d         d}	|                     |	|          }
|                     |||
||
           d{V }|                     ||d         ||          S )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 argumentr1   r   	myTrades:r2   r   r   r   r   r   rK   )	r   r~   r   rO   r5   r   r9   rP   r   )r<   r2   rq   rr   rQ   rS   r   rR   r   rh   rU   r   s               r>   watch_my_tradeszcex.watch_my_trades  sG      >#DG.[$[\\\!!!!!!!!!'''''''''it$V$$!F8$44$vh'778$9% (#	
 	
 ""7F33zz#{G=MwWWWWWWWW009I5RWXXXr?   c                     |                      |d          }|                     |d          }|d S |                     ||           |                     ||           d S )NrM   r   )r^   rd   handle_order_updatehandle_my_trades)r<   rW   rh   rM   r   s        r>   handle_transactionzcex.handle_transaction  sf    w//""433?F  111fg.....r?   c                 @   |                      |di           }| j        }|2|                     | j        dd          }t	          |          }|| _        |                     |          }|                    |           d|d         z   }|                    ||           d S )NrM   r   r   r   r2   )r^   myTradesrE   r6   r   parse_ws_trader   rg   )r<   rW   rh   rM   r   rr   r   rR   s           r>   r   zcex.handle_my_trades   s    V w33>%%dlM4HHE+E22F"DM##D))e!E(O3v{+++++r?   c                    |                      |d          }|                      |d          }|                      |d          }|                      |d          }|                      |d          }|                     |          }|                     |          }	|dz   |	z   }
|                      |d          }|dk    r|	dz   |z   }
t          j        ||          }|                      |d	          |                      |d
          ||                     |          ||
d |d |d |d d}|                      |d          }|	||	d d|d<   |                     ||          S )Nr   r   timer2   r   r   r   sellr~   r   )r~   r   r[   r}   r   r2   r   r   r   r   r   r   r   
fee_amountr   currencyrater   )rd   re   r   
string_div	parse8601r   )r<   r   r   r   r   r   r   r   rx   rz   r2   r   parsedTrader   s                 r>   r   zcex.parse_ws_trade6  s   0 v..  00##E622!!%22""5)44&&v..''00e#!!%226>>S[4'F'66F""5$//%%eW5511  
 
 ul33?!" "K
 {F333r?   c                    |                      |di           }|                     |d          dk    }|                     |dd          }|                     |d          }|                     |d          }|                     |d          }|                      |d	          }	|	,|                     |	d
          }|                     |	d          }|                     |          }
|                     |          }|
dz   |z   }|                     |          }|                     |
|          }| j        0|                     | j        dd          }t          |          | _        | j        }|                      |j
        |i           }|                      ||          }||                     ||          }||d<   |                     |dd          }|rd|d<   |rd|d<   |                     |d          }|	||d d|d<   |                     |d          }||d<   |                     |          |d<   |                     |          }|                    |           d|z   }|                    ||           d S )NrM   rL   txr~   r   remainsr2   r   r   r   r   ordersLimitr   	remainingcancelFcanceledstatusclosedr   r   r   r}   r   r   )r^   rd   r   re   safe_marketcurrency_from_precisionr   rE   r6   r   hashmapparse_ws_order_update	safe_boolsafe_numberr   
safe_orderr   rg   )r<   rW   rh   rM   isTransactionorderIdr   r   r   r   rx   rz   r2   r   rr   storedOrdersordersBySymbolr   r  r   r}   rR   s                         r>   r   zcex.handle_order_updater  s   J w33((#66$>$$T499""433!!$11""433tV,,%%dI66F&&tY77G&&v..''00e#!!&))..tW==;%%dlM4HHE077DK{)=vrJJ88=..tV<<E$k>>$%88 	)(E(O 	'&E(OtU++?! E%L
 %%dF33	&k LL33j&&E"""&(|[11111r?   c           	         |                      |d          d u}|                     |d          }d }||                     |d         |          }|                     |d          }|s|                     |d         |           |                     |d          }|                     |d          }|                      |d          }	|	,|                     |d          }|                     |d          }|                     |          }
|                     |          }d }|

||
d	z   |z   }|                     ||          }|                     |d
|                                           }|}|r|                     |          }|                     |dd          }d}|rd}n|rd}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$}|r|                     ||          |d%<   |                     ||          S )&Ndr   rx   r   r2   r   r   r   r   r   r  Fr   r  r  r~   r   clientOrderIdr[   r}   r   lastTradeTimestampr  r   timeInForcepostOnlyr   r   	stopPricetriggerPricer   r   r   r   r   )r   filledr  r   rt   rt   )r^   rd   r  re   r  rE   millisecondsr   r  r   r   r  safe_number_2r   r  )r<   r   r   r  remainsPrecisionr  r   r   r   r   rx   rz   r2   r   r}   r  r  parsedOrders                     r>   r
  zcex.parse_ws_order_update  sW   J s334?++E9==	'44VF^EUVVI!!%22 	A((@@@!!%22""5)44uf--%%eY77F&&ui88G&&v..''00 1CZ%'F!!&&11  0A0A0C0CDD	 	-t,,I>>%599 	FF 	F
$$$UD'::
T
 E
 	

 Y//
 !$
 f
 f
 D
 4
 
 D$$UF33
 T%%eW55
 
 D
  t!
" D#
$ "**5%FF! 
 5
 
 
8  	G$($7$7v$F$FK!{F333r?   c                     |d S t          |          }|                     |j        |          |_        |                                 t	          |          S r   )r   rD   decimalsreducerG   )r<   r   scaleprecises       r>   from_precisionzcex.from_precisionF  sL    >4&//88G$4e<<7||r?   c                 r    |                      | j        |         dd          }|                     ||          S )N	precisionr   )rE   
currenciesr$  )r<   r   r   r"  s       r>   r  zcex.currency_from_precisionN  s6    !!$/(";[!LL""65111r?   c                    |                      |d          }|                     |dg           }| j        }| j        +|                     | j        dd          }t          |          }t          dt          |                    D ]O}||         }|                     |          }	| 	                    ||	          }
d|
d<   |
                    |
           P|| _        d|z   }t          |          }|dk    r|                    ||           d S d S )	NrN   rM   r  r   r   r   r  r   )rd   r^   r   rE   r6   r   ra   rb   r  parse_orderr   rg   )r<   rW   rh   r2   	rawOrdersmyOrdersrr   rm   rawOrderr   r   rR   ordersLengths                r>   handle_orders_snapshotzcex.handle_orders_snapshotR  s     !!'511OOGVR88	;;%%dlM4HHE-e44Hq#i..)) 	# 	#A |H%%f--F$$Xv66E$E(OOOE""""&(8}}!NN8[11111 r?   c                   K   |                                   d{V  |                                  d{V  |                     |          }|d         }| j        d         d         }d|z   }|dn|}d|d         |d	         gd
|d|                                 d}|                     ||          }	|                     |||	|           d{V }
|
                                S )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
        Nr2   r1   r   
orderbook:r   order-book-subscriber   r   T)r   rT   depthrK   )r   rO   r   r5   rH   r9   rP   rr   )r<   r2   rr   rQ   r   rS   rR   r2  rT   rU   r3   s              r>   watch_order_bookzcex.watch_order_bookt  s*      !!!!!!!!!!!!!!!!!!V$$!it$"V+m%' 8$9% "  ??$$
 
	 ""9f55**S+wLLLLLLLL	   r?   c                    |                      |di           }|                     |d          }|                     |          }d|z   }|                     |dd          }|                     |d          }|                     i           }	|                     |||dd          }
||
d	<   |	                    |
           d
|i| j        d         |<   |	| j	        |<   |
                    |	|           d S )NrM   r   r0  timestamp_msr}   r~   bidsasksnonceincrementalIdr3   )r^   rd   pair_to_symbolsafe_integer_2rE   
order_bookparse_order_bookresetr6   
orderbooksrg   )r<   rW   rh   rM   r   r2   rR   r}   r9  r3   snapshots              r>   handle_order_book_snapshotzcex.handle_order_book_snapshot  s    . w33f--$$T**"V+''nkJJ	))$55OOB''	((vy&&QQ)!!!]-
[!&) #,y+.....r?   c                     |                     d          }|                     |d          }|                     |d          }|                     |          }|                     |          }|dz   |z   }|S )Nr   r   rB   r   )r   rd   re   )r<   r   partsr   r   rx   rz   r2   s           r>   r:  zcex.pair_to_symbol  st    

3!!%++""5!,,&&v..''00e#r?   c                    |                      |di           }|                     |d          }|                     |dd          }|                     |          }|                      | j        |          }d|z   }||d         dz   k    r(|j        |= |                    | j        dz   |           d S |                     |d	          }	|                      |d
g           }
|                      |dg           }|                     |d
         |
           |                     |d         |           |	|d<   | 	                    |	          |d<   ||d<   |
                    ||           d S )NrM   r~   r    r0  r8  rB   z# watchOrderBook() skipped a messager   r7  r6  r}   r   )r^   rE   rd   r:  r?  r   rejectr~   handle_deltasr   rg   )r<   rW   rh   rM   r9  r   r2   storedOrderBookrR   r}   r7  r6  s               r>   handle_order_book_updatezcex.handle_order_book_update  sq    w33))$55fb11$$T**//$/6BB"V+OG4q888$[1MM$'$II;WWWF%%dF33	tVR00tVR00?62D999?62D999'0$&*ll9&=&=
##0 44444r?   c                 ^    |                      |dd          }|                    |           d S )Nr   rB   )parse_bid_ask
storeArray)r<   booksidedeltabidAsks       r>   handle_deltazcex.handle_delta  s3    ##E1a00F#####r?   c                 ~    t          dt          |                    D ]}|                     |||                    d S )Nr   )ra   rb   rP  )r<   rM  deltasrm   s       r>   rG  zcex.handle_deltas  sH    q#f++&& 	3 	3Ahq	2222	3 	3r?   1m	timeframec                   K   |                                   d{V  |                     |          }|d         }d|z   }| j        d         d         }d|d|d         z   d	z   |d
         z   gd}	|                     |||                     |	|          |           d{V }
| j        r|
                    ||          }|                     |
||dd          S )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
        Nr2   ohlcv:r1   r   z
init-ohlcvrw   r   ry   r   )rL   rm   r|   r   T)r   r   r5   rP   r   r   r   r   )r<   r2   rT  rq   rr   rQ   r   rR   rS   rU   ohlcvs              r>   watch_ohlcvzcex.watch_ohlcv  s      !!!!!!!!!V$$!'it$&**S06)3DD
 
 jjk4;;w3O3OQ\]]]]]]]]? 	2NN6511E))%q$GGGr?   c                    |                      |d          }|                    d          }|                      |d          }|                      |d          }|                     |          }|                     |          }|dz   |z   }	|                     |	          }
d|	z   }|                     |dg           }|                     | j        dd	          }t          |          }|                     |d          }t          dt          |                    D ]1}|                    |                     ||         |
                     2|	| j        vr
i | j        |	<   || j        |	         d
<   |                    ||           d S )Nr   r   r   rB   r   rV  rM   
OHLCVLimitr   unknown)rd   r   re   r  r^   rE   r6   r   sort_byra   rb   r   parse_ohlcvohlcvsrg   )r<   rW   rh   r   rC  r   r   rx   rz   r2   r   rR   rM   rr   r   sortedrm   s                    r>   handle_init_ohlcvzcex.handle_init_ohlcv  s}   $ 00

3!!%++""5!,,&&v..''00e#!!&))'w33!!$,dCC&u--dA&&q#f++&& 	? 	?AMM$**6!9f==>>>>$+%%"$DK)/FI&v{+++++r?   c                     |S r    r   s      r>   handle_ohlcv24zcex.handle_ohlcv245  s	     r?   c           	          |                      |di           }|                     |d          }|                     |          }d|z   }|                     |d          |                     |d          |                     |d          |                     |d          |                     |d          |                     |d	          g}|                      | j        |          }|                    |           |                    ||           d S )
NrM   r   rV  r   ohlcv)r^   rd   r:  safe_timestampr  r^  r   rg   )	r<   rW   rh   rM   r   r2   rR   rW  r   s	            r>   handle_ohlcv1mzcex.handle_ohlcv1m?  s     w33f--$$T**'f--T3''T3''T3''T3''T3''
 f55ev{+++++r?   c           
         |                      |dg           }|                     |d          }|                     |          }d|z   }| j        |         d         }t	          dt          |                    D ]}|                     ||         d          |                     ||         d          |                     ||         d          |                     ||         d          |                     ||         d	          |                     ||         d
          g}	|                    |	           t          |          }
|
dk    r|	                    ||           d S d S )NrM   r   rV  r[  r   rB   r   ru   r      )
r^   rd   r:  r^  ra   rb   rj  r  r   rg   )r<   rW   rh   rM   r   r2   rR   r   rm   rW  r   s              r>   handle_ohlcvzcex.handle_ohlcv_  sX    w3300$$T**'V$Y/q#d))$$ 		! 		!A##DGQ//  a!,,  a!,,  a!,,  a!,,  a!,,E MM%    YY
>>NN6;///// >r?   r~   c                   K   |                                   d{V  |                                  d{V  d}||                     |          }|                     dt	          |          i|          }| j        d         d         }|                                 }d||d}|                     ||||           d{V }	|                     |	|          S )at  
        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_idr1   r   	get-orderr   )	r   rO   r   r   rG   r5   rH   rP   r)  )
r<   r~   r2   rQ   r   rM   rS   rR   rU   responses
             r>   fetch_order_wszcex.fetch_order_ws}  s
      !!!!!!!!!!!!!!!!!![[((F{{B
  it$oo''
 

 Cg{KKKKKKKK&111r?   c                   K   |t          | j        dz             |                                  d{V  |                                  d{V  |                     |          }| j        d         d         }|                                 }|                     d|d         |d         gi|          }d||d	}	|                     |||	|           d{V }
| 	                    |
||||          S )
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.r1   r   r   r   r   r   r   )
r   r~   r   rO   r   r5   rH   r   rP   parse_orders)r<   r2   rq   rr   rQ   r   rS   rR   rM   rU   rr  s              r>   fetch_open_orders_wszcex.fetch_open_orders_ws  s)      >#DG.U$UVVV!!!!!!!!!!!!!!!!!!V$$it$oo''{{VH%vi'89
  
 

 Cg{KKKKKKKK  65%HHHr?   r   r   r   r   c                   K   |t          | j        dz             |                                  d{V  |                                  d{V  |                     |          }| j        d         d         }|                                 }	|                     |d         |d         g|||d|          }
d|	|
d	}|                     ||	||	           d{V }| 	                    ||          S )
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 argumentr1   r   r   r   )r   r   r   r   place-orderr   )
r   r~   r   rO   r   r5   rH   r   rP   r)  )r<   r2   r   r   r   r   rQ   r   rS   rR   rM   rU   r,  s                r>   create_order_wszcex.create_order_ws  s1      =TW'QQRRR!!!!!!!!!!!!!!!!!!V$$it$oo''{{H%vi'89	
 

   
 

 Cg{KKKKKKKK&111r?   c                   K   |t          | j        dz             |t          | j        dz             |                                  d{V  |                                  d{V  |                     |          }|                     |d         |d         g||||d|          }	|                                 }
| j        d         d         }d	|
|	d
}|                     ||
||
|
           d{V }| 	                    ||          S )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   rp  r1   r   cancel-replace-orderr   )
r   r~   r   rO   r   r   rH   r5   rP   r)  )r<   r~   r2   r   r   r   r   rQ   r   rM   rR   rS   rU   rr  s                 r>   edit_order_wszcex.edit_order_ws  sQ      >#DG.W$WXXX=#DG.V$VWWW!!!!!!!!!!!!!!!!!!V$${{H%vi'89
 
   oo''it$'
 

 Cg{KXXXXXXXX&111r?   c                   K   |                                   d{V  |                                  d{V  d}||                     |          }|                     d|i|          }|                                 }| j        d         d         }d||d}|                     |||||           d{V }	|                     |	|          S )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>`
        Nrp  r1   r   cancel-orderr   )r   rO   r   r   rH   r5   rP   r)  )
r<   r~   r2   rQ   r   rM   rR   rS   rU   rr  s
             r>   cancel_order_wszcex.cancel_order_ws  s      !!!!!!!!!!!!!!!!!![[((F{{
  oo''it$
 

 Cg{KXXXXXXXX&111r?   idsc                   K   |t          | j        dz             |                                  d{V  |                                  d{V  |                                 }|                     d|i|          }| j        d         d         }d||d}|                     |||||           d{V }|                     |d          }	| 	                    |	ddd|          S )an  
        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-ordersr1   r   mass-cancel-place-ordersr   )
r   r~   r   rO   rH   r   r5   rP   r^   ru  )
r<   r  r2   rQ   rR   rM   rS   rU   rr  canceledOrderss
             r>   cancel_orders_wszcex.cancel_orders_ws   s!      TW'ZZ[[[!!!!!!!!!!!!!!!!!!oo''{{S
  it$+
 

 Cg{KXXXXXXXX ?CC  tT6JJJr?   c                     |                      |d          }|                     |d          }|                    ||           d S )NrM   rN   )r^   rd   rg   )r<   rW   rh   rM   rR   s        r>   resolve_datazcex.resolve_dataG  sF    $ w//&&w66t[)))))r?   c                     |S r   rb  r   s      r>   handle_connectedzcex.handle_connected]  s	     r?   c                     	 |                      |di           }|                     |d          }|                     |dd          }| j        dz   |z   dz   |z   }|                     | j        d         ||           |                     | j        d         ||           t          |          # t          $ rW}|                     |d          }|                      |d	         |          }||                    ||           Y d }~d
S |d }~ww xY w)NrM   errorrL   rE   exactbroadrN   futuresT)	r^   rd   r~   throw_exactly_matched_exceptionr8   throw_broadly_matched_exceptionr   	ExceptionrF  )	r<   rW   rh   rM   r  eventfeedbackrR   futures	            r>   handle_error_messagezcex.handle_error_messagee  s(   	??7FB77D$$T733E$$Wc266Ew}u,s2U:H001I5RZ[[[001I5RZ[[[))) 	 	 	**7E::K__VI%6DDF!e[111ttttt	s   B*B, ,
D6A
DDDc                 0   |                      |d          }|dk    r|                     ||           d S |                      |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}|                     ||          }| |||           d S d S )Nokr  rL   auth	connectedtickr   zinit-ohlcv-dataohlcv24ohlcv1mrW  rJ   r1  	md_updater   r   zhistory-updatehistoryr   rx  )r{  r~  r  rq  )rd   r  handle_authentication_messager  r   r`  rc  rk  rn  rp   rA  rI  r  r   r   r   r   r^   )r<   rW   rh   r  r  handlershandlers          r>   handle_messagezcex.handle_message  s   gt,,==%%fg666F  #..
D6
.
 D&
 d(	

 t5
 t*
 t*
 T&
 4.
 #D$C
 6
 4,
 T-
 d/
 t2
  $)!
" 4,#
$ %)$5 -(,(9*+
 
 
. //(E22GFG$$$$$ r?   c                 n    |                      |j        d          }||                    d           d S d S )NauthenticatedT)r^   r  rg   )r<   rW   rh   r  s       r>   r  z!cex.handle_authentication_message  s?     AANN4      r?   c                 |  K   | j         d         d         }|                     |          }d}|                    d          }|                     |j        |          }||                                  t          |                                           }|| j        z   }| 	                    | 
                    |          | 
                    | j                  t          j                  }	d| j        |	                                |dd}
|                     |||                     |
|          |           | d {V S )Nr1   r   r  r  )key	signaturer}   )rL   r  )r5   rW   reusableFuturer^   r   check_required_credentialsrG   secondsapiKeyhmacencodesecrethashlibsha256upperrP   r   )r<   rQ   rS   rW   rR   r  r  r8  r  r  rU   s              r>   rO   zcex.authenticate  s(     it$S!!%&&77(<kJJ ++---''E4;&D		$++d"3"3T[[5M5Mw~^^I;!*!2!2!&  G JJsKWf)E)E{SSS||||||r?   r   )G__name__
__module____qualname__r   r;   rH   r   rV   r   rp   rG   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  rA  r:  rI  rP  rG  r_   rX  r`  rc  rk  rn  rs  rv  r   r   floatr
   ry  r|  r  r  r  r  r   r  r  r  rO   __classcell__)r=   s   @r>   r   r      s^       &# & & & & & &P   *, Q Q Q Q Q Q(%2V %2 %2 %2 %2N <@d[] &S &S &SS &S &Sbfglbm &S &S &S &SP2V 2 2 2 2   >	26 	2 	2 	2 	29& 9 9 9 9$ 68 #M #M #MF #M #M #M #MJ 6:" E E7 E E E E E> 9; H HC Hv H H H H*0F 0 0 0 04: : : :x -/ H H8 H H H H& 04$UYbd "U "U "UC "Us "Uimnsit "U "U "U "UH 37TX\eg  Y  YC  Ys  YRU  Ylpqvlw  Y  Y  Y  YD/ / / / /4,v 4, 4, 4, 4,l:4 :4 :4 :4xo2& o2 o2 o2 o2ba4 a4 a4 a4F  2 2 2 2V  2  2  2  2D @DB  !  !S  !  !S\  !  !  !  !D%/ %/ %/ %/ %/N  !5v !5 !5 !5 !5F$ $ $3 3 3 ?CQUdhqs H H H H3 H^a Hx|  ~B  yC H H H H<$, $, $, $, $,LV    ,V , , , ,@06 0 0 0 0< ;?r 2 2s 2C 2 2 2 2: 8<$]ajl I I IC IWZ I I I I< pt|~ "2 "2C "2y "2	 "2[` "2il "2  DI "2 "2 "2 "2H im{  IK %2 %2c %23 %2i %2y %2be %2ux %2  PU %2 %2 %2 %2N <@ 2 2 2S 2 2 2 2: DHPR %K %K$s) %KS %K %K %K %KN*6 * * * *,v    6 t    4%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   r   !ccxt.async_support.base.ws.clientr   typingr   ccxt.base.errorsr   r   r   ccxt.base.preciser   async_supportr   rb  r?   r>   <module>r     s       f f f f f f f f f f  H  H  H  H  H  H  H  H  H  H  H  H  H  H  H  H  H  H  H  H  H  H  H  H  H  H  H  H  H  H  H  H 4 4 4 4 4 4       * * * * * * . . . . . . ' ' ' ' ' ' % % % % % %q q q q q$

  q q q q qr?   