
    i                         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 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)BalancesIntNumOrder	OrderBook	OrderSide	OrderTypeStrStringsTickerTickersTrade)Client)List)ExchangeError)ArgumentsRequired)
BadRequest)Precisec                   8    e Zd Z 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	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(Z/defd)Z0defd*Z1defd+Z2di fd,e	defd-Z3dddi fded	e
d
e
fd.Z4di fde	d/e5d0e6d1e7d2e8defd3Z9ddi fd,e	de	d/e5d0e6d1e8d2e8d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;Z@defd<ZAi fd=ZB xZCS )?cexc           	          |                      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idddiidi ii i d          S )NwsTwatchBalancewatchTickerwatchTickerswatchTrades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    R/var/www/html/crypto-bot/backend/venv/lib/python3.11/site-packages/ccxt/pro/cex.pyr6   zcex.describe   sF   c4 0 0 9 9 ; ;d t 	
 t   t !$ d     t   !$   $T!"  #$ !$% * . R?!>
 !>
 ! ! !	    c                     |                      |                     | j        dd          d          }|| j        d<   t          |          S )N	requestIdr      )sumsafe_integerr1   str)r7   r<   s     r9   
request_idzcex.request_id8   sA    HHT..t|[!LLaPP	$-[!9~~r:   returnc                 $  K   |                      |           d{V  |                                 }| j        d         d         }di |                                 d}|                     ||          }|                     |||||           d{V S )aT  
        watch balance and get the amount of funds available for trading or funds locked in orders
        :see: 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>`
        Nr-   r   get-balanceedataoid)authenticaterA   r0   r4   watch)r7   paramsmessageHashurl	subscriberequests         r9   watch_balancezcex.watch_balance=   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 )	NrG   balanceobalanceinfor   freeusedrH   )
safe_valuelistkeysrangelenaccountsafe_stringsafe_currency_codesafe_balancerS   resolve)r7   rQ   messagerG   freeBalanceusedBalanceresultcurrencyIdsi
currencyIdr]   coderL   s                r9   handle_balancezcex.handle_balanceO   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:   Nsymbolsincelimitc                   K   |                                   d{V  |                     |          }|d         }| j        d         d         }d}d|z   }|| j        d<   |                     | 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 }t          dt          |                    D ]}|||         d<   |                     |||dd          S )aA  
        get the list of most recent trades for a particular symbol. Note: can only watch one symbol at a time.
        :see: 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>`
        Nrk   r-   r   tradeszold:currentWatchTradeSymbolr      oldz; watchTrades() only supports watching one symbol at a time.rN   pair-base-quoterF   rooms	timestampT)load_marketsmarketr0   r1   rX   clientsrY   subscriptionsrZ   r[   r\   r   idr4   rJ   filter_by_since_limit)r7   rk   rl   rm   rK   r{   rM   rL   subscriptionHashrQ   subscriptionKeysrg   subscriptionKeyrb   rO   ro   s                   r9   watch_tradeszcex.watch_tradesv   s      !!!!!!!!!V$$!it$!F?28./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q#f++&& 	) 	)A"(F1Ih))&%TRRRr:   c                    |                      |dg           }|                     | j        dd          }t          |          }|                     | j        d          }|d S |                     |          }t          |          }t          d|          D ]=}	|dz
  |	z
  }
||
         }|                     ||          }|	                    |           >d}|| _
        |                    | j
        |           d S )NrG   tradesLimit  rp   r   r=   ro   )	safe_listr?   r1   r   r^   r{   r\   r[   parse_ws_old_tradeappendro   ra   )r7   rQ   rb   rG   rm   storedrk   r{   
dataLengthrg   indexrawTradeparsedrL   s                 r9   handle_trades_snapshotzcex.handle_trades_snapshot   s     ~~gvr22!!$,tDDE""!!$,0IJJ>FV$$YY
q*%% 	" 	"ANQ&EE{H,,Xv>>FMM&!!!!t{K00000r:   c                    t          |t                    s|                    d          }|                     |d          }|                     |d          }|                     |d          }|                     |d          }|                     |d          }|                     ||||                     |          |                     |d          d |d d ||d d d|          S )	N:r   r=      rq      rk   )rU   r~   ry   datetimerk   typesideordertakerOrMakerpriceamountcostfee)
isinstancerY   splitr^   r?   
safe_tradeiso8601)r7   trader{   r   ry   r   r   r~   s           r9   r   zcex.parse_ws_old_trade   s     %&& 	%KK$$Eq))%%eQ//	!!%++  **eQ''"Y//&&vx88  
  
   	r:   c                 B   |                      |dg           }| j        }t          |          }t          d|          D ]<}|dz
  |z
  }||         }|                     |          }	|                    |	           =d}
|| _        |                    | j        |
           d S )NrG   r   r=   ro   )rX   ro   r\   r[   r   r   ra   )r7   rQ   rb   rG   r   r   rg   r   r   r   rL   s              r9   handle_tradezcex.handle_trade   s     w33YY
q*%% 	" 	"ANQ&EE{H,,X66FMM&!!!!t{K00000r:   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  
        :see: 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>`
        Nrk   r-   r   ticker:methodprivaterN   tickersrw   tickerbaseIdquoteIdrE   )rz   r{   r0   r^   rI   rA   r4   rJ   )
r7   rk   rK   r{   rM   rL   r   rb   r   rO   s
             r9   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  
        :see: 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>`
        Nr-   r   r   rN   rw   rk   )
rz   market_symbolsr0   r4   rJ   in_arraywatch_tickers
newUpdatesfilter_by_arrayr   )
r7   r   rK   rM   rL   rb   rO   r   tickerSymbolre   s
             r9   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  
        :see: 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>`
        Nr-   r   r   rt   rv   rF   rH   rG   )rz   r{   r0   rA   extendrJ   )r7   rk   rK   r{   rM   rL   rO   s          r9   fetch_ticker_wszcex.fetch_ticker_ws,  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 )NrG   rk   r   r   rH   )rX   parse_ws_tickerr   ra   r^   )r7   rQ   rb   rG   r   rk   rL   s          r9   handle_tickerzcex.handle_ticker?  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   symbol2r=   /ry   r   rk   r   highlowbid	bidVolumeask	askVolumevwapopenopen24closelastr   previousClosechangepriceChange
percentagepriceChangePercentageaveragevolume)
baseVolumequoteVolumerU   )rX   r^   r_   r?   safe_tickerr   safe_string_2)
r7   r   r{   r   r   r   rt   rv   rk   ry   s
             r9   r   zcex.parse_ws_tickerY  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 )aR  
        :see: 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>`
        Nr-   r   rD   )rF   rH   )rz   rI   r0   rA   r   rJ   )r7   rK   rM   rL   rO   s        r9   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.
        :see: 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 argumentr-   r   rk   orders:open-ordersr   r   r   rE   T)r   r~   rz   rI   r0   r{   r4   rJ   r   getLimitfilter_by_symbol_since_limit)r7   rk   rl   rm   rK   rM   r{   rL   rb   rO   orderss              r9   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.
        :see: 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 argumentr-   r   	myTrades:rk   r   r   r   r   r   rE   )	r   r~   rz   rI   r0   r{   r4   rJ   r   )r7   rk   rl   rm   rK   rM   r{   rL   r   rb   rO   r   s               r9   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 )NrG   r   )rX   r^   handle_order_updatehandle_my_trades)r7   rQ   rb   rG   r   s        r9   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 )NrG   r   r   r   rk   )rX   myTradesr?   r1   r   parse_ws_trader   ra   )r7   rQ   rb   rG   r   rm   r   rL   s           r9   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   timerk   r   r   r   sellr~   r   )r~   r   rU   ry   r   rk   r   r   r   r   r   r   r   
fee_amountr   currencyrater   )r^   r_   r   
string_div	parse8601r   )r7   r   r{   r   r   r   r   r   rt   rv   rk   r   parsedTrader   s                 r9   r   zcex.parse_ws_trade&  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 )NrG   rF   txr~   r   remainsrk   r   r   r   r   ordersLimitr   	remainingcancelFcanceledstatusclosedr   r   r   ry   r   r   )rX   r^   r   r_   safe_marketcurrency_from_precisionr   r?   r1   r   hashmapparse_ws_order_update	safe_boolsafe_numberr   
safe_orderr   ra   )r7   rQ   rb   rG   isTransactionorderIdr   r   r   r   rt   rv   rk   r{   rm   storedOrdersordersBySymbolr   r   r   ry   rL   s                         r9   r   zcex.handle_order_updateb  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   rt   r   rk   r   r   r   r   r   r   Fr   r   r   r~   r   clientOrderIdrU   ry   r   lastTradeTimestampr   r   timeInForcepostOnlyr   r   	stopPricetriggerPricer   r   r   r   r   )r   filledr   r   ro   ro   )rX   r^   r   r_   r   r?   millisecondsr   r  r   r   r  safe_number_2r   r  )r7   r   r{   r  remainsPrecisionr   r   r   r   r   rt   rv   rk   r   ry   r   r   parsedOrders                     r9   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 N)r   r>   decimalsreducer@   )r7   r   scaleprecises       r9   from_precisionzcex.from_precision6  sL    >4&//88G$4e<<7||r:   c                 r    |                      | j        |         dd          }|                     ||          S )N	precisionr   )r?   
currenciesr  )r7   r   r   r  s       r9   r   zcex.currency_from_precision>  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 )	NrH   rG   r   r   r   r   r   r   )r^   rX   r   r?   r1   r   r[   r\   r   parse_orderr   ra   )r7   rQ   rb   rk   	rawOrdersmyOrdersrm   rg   rawOrderr{   r   rL   ordersLengths                r9   handle_orders_snapshotzcex.handle_orders_snapshotB  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 )a2  
        watches information on open orders with bid(buy) and ask(sell) prices, volumes and other data
        :see: 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
        Nrk   r-   r   
orderbook:r   order-book-subscriber   r   T)r   rN   depthrE   )rz   rI   r{   r0   rA   r4   rJ   rm   )r7   rk   rm   rK   r{   rM   rL   r)  rN   rO   r.   s              r9   watch_order_bookzcex.watch_order_bookd  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 )NrG   r   r'  timestamp_msry   r~   bidsasksnonceincrementalIdr.   )rX   r^   pair_to_symbolsafe_integer_2r  
order_bookparse_order_bookresetr1   
orderbooksra   )r7   rQ   rb   rG   r   rk   rL   ry   r0  r.   snapshots              r9   handle_order_book_snapshotzcex.handle_order_book_snapshot  s    . w33f--$$T**"V+''nkJJ	((t44OOB''	((vy&&QQ)!!!]-
[!&) #,y+.....r:   c                     |                     d          }|                     |d          }|                     |d          }|                     |          }|                     |          }|dz   |z   }|S )Nr   r   r=   r   )r   r^   r_   )r7   r   partsr   r   rt   rv   rk   s           r9   r1  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	          }	|                      |d
g           }
|                      |dg           }| 	                    |d
         |
           | 	                    |d         |           |	|d<   | 
                    |	          |d<   ||d<   |                    ||           d S )NrG   r~   r    r'  r/  r=   z# watchOrderBook() skipped a messager   r.  r-  ry   r   )rX   r  r^   r1  r6  r}   rejectr~   r?   handle_deltasr   ra   )r7   rQ   rb   rG   r0  r   rk   storedOrderBookrL   ry   r.  r-  s               r9   handle_order_book_updatezcex.handle_order_book_update  sl    w33((t44fb11$$T**//$/6BB"V+OG4q888$[1MM$'$II;WWW%%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   r=   )parse_bid_ask
storeArray)r7   booksidedeltabidAsks       r9   handle_deltazcex.handle_delta  s3    ##E1a00F#####r:   c                 ~    t          dt          |                    D ]}|                     |||                    d S )Nr   )r[   r\   rG  )r7   rD  deltasrg   s       r9   r>  zcex.handle_deltas  sH    q#f++&& 	3 	3Ahq	2222	3 	3r:   1mc                   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  
        :see: 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
        Nrk   ohlcv:r-   r   z
init-ohlcvrs   r   ru   r   )rF   rg   rx   r   T)rz   r{   r0   rJ   r   r   r   r   )r7   rk   	timeframerl   rm   rK   r{   rL   rM   rO   ohlcvs              r9   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   r=   r   rL  rG   
OHLCVLimitr   unknown)r^   r   r_   r   rX   r?   r1   r   sort_byr[   r\   r   parse_ohlcvohlcvsra   )r7   rQ   rb   r   r:  r   r   rt   rv   rk   r{   rL   rG   rm   r   sortedrg   s                    r9   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   r7   rQ   rb   s      r9   handle_ohlcv24zcex.handle_ohlcv24   s	     r:   c           	          |                      |di           }|                     |d          }|                     |          }d|z   }|                     |d          |                     |d          |                     |d          |                     |d          |                     |d          |                     |d	          g}|                      | j        |          }|                    |           |                    ||           d S )
NrG   r   rL  r   ohlcv)rX   r^   r1  safe_timestampr  rU  r   ra   )	r7   rQ   rb   rG   r   rk   rL   rN  r   s	            r9   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 )NrG   r   rL  rR  r   r=   r   rq   r      )
rX   r^   r1  rU  r[   r\   rb  r  r   ra   )r7   rQ   rb   rG   r   rk   rL   r   rg   rN  r   s              r9   handle_ohlcvzcex.handle_ohlcvJ  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 )aV  
        fetches information on an order made by the user
        :see: https://docs.cex.io/#ws-api-get-order
        :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_idr-   r   	get-orderr   )	rz   rI   r{   r   r@   r0   rA   rJ   r   )
r7   r~   rk   rK   r{   rG   rM   rL   rO   responses
             r9   fetch_order_wszcex.fetch_order_wsh  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  
        :see: 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.r-   r   r   r   r   r   r   )
r   r~   rz   rI   r{   r0   rA   r   rJ   parse_orders)r7   rk   rl   rm   rK   r{   rM   rL   rG   rO   rj  s              r9   fetch_open_orders_wszcex.fetch_open_orders_ws  s)      >#DG.T$TUUU!!!!!!!!!!!!!!!!!!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  
        :see: 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 fullfilled, 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 argumentr-   r   r   r   )r   r   r   r   place-orderr   )
r   r~   rz   rI   r{   r0   rA   r   rJ   r   )r7   rk   r   r   r   r   rK   r{   rM   rL   rG   rO   r#  s                r9   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
        :see: 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 fullfilled, 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   rh  r-   r   cancel-replace-orderr   )
r   r~   rz   rI   r{   r   rA   r0   rJ   r   )r7   r~   rk   r   r   r   r   rK   r{   rG   rL   rM   rO   rj  s                 r9   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 )aa  
        :see: 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>`
        Nrh  r-   r   cancel-orderr   )rz   rI   r{   r   rA   r0   rJ   r   )
r7   r~   rk   rK   r{   rG   rL   rM   rO   rj  s
             r9   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 )at  
        cancel multiple orders
        :see: 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-ordersr-   r   mass-cancel-place-ordersr   )
r   r~   rz   rI   rA   r   r0   rJ   rX   rm  )
r7   rx  rk   rK   rL   rG   rM   rO   rj  canceledOrderss
             r9   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 )NrG   rH   )rX   r^   ra   )r7   rQ   rb   rG   rL   s        r9   resolve_datazcex.resolve_data%  sF    $ w//&&w66t[)))))r:   c                     |S r  rY  rZ  s      r9   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          $ rX}|                     |d          }|                      |d	         |          }||                    ||           n|Y d }~d S d }~ww xY w)
NrG   errorrF   r<   exactbroadrH   futures)	rX   r^   r~   throw_exactly_matched_exceptionr3   throw_broadly_matched_exceptionr   	Exceptionr=  )	r7   rQ   rb   rG   r  eventfeedbackrL   futures	            r9   handle_error_messagezcex.handle_error_messageC  s2   	??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1 211111		s   B*B, ,
D6A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  rF   auth	connectedtickr   zinit-ohlcv-dataohlcv24ohlcv1mrN  rD   r(  	md_updater   r   zhistory-updatehistoryr   rp  )rs  rv  rz  ri  )r^   r  handle_authentication_messager  r   rW  r[  rc  rf  rj   r8  r@  r~  r   r   r   r   rX   )r7   rQ   rb   r  r  handlershandlers          r9   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)rX   r  ra   )r7   rQ   rb   r  s       r9   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  | d {V S )Nr-   r   r  r  )key	signaturery   )rF   r  )r0   rQ   r  rX   r}   check_required_credentialsr@   secondsapiKeyhmacencodesecrethashlibsha256upperrJ   r   )r7   rK   rM   rQ   rL   r  r  r/  r  r  rO   s              r9   rI   zcex.authenticate  s8     it$S!!%//(<kJJ ++---''E4;&D		$++d"3"3T[[5M5Mw~^^I;!*!2!2!&  G **S+t{{7F/K/K[YYYYYYYYY||||||r:   r  )D__name__
__module____qualname__r6   rA   r   rP   r   rj   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*  r8  r1  r@  rG  r>  rY   rO  rW  r[  rc  rf  rk  rn  r   r   floatr   rq  rt  rw  r|  r~  r  r  r  r  rI   __classcell__)r8   s   @r9   r   r      s       " " " " "H  
 *, Q Q Q Q Q Q$%2V %2 %2 %2 %2N <@d[] $S $S $SS $S $Sbfglbm $S $S $S $SL1V 1 1 1 1:   <16 1 1 1 1* 68 !M !M !MF !M !M !M !MF 6:" E E7 E E E E E: 9; H HC 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D 37TX\eg Y YC Ys YRU Ylpqvlw Y Y Y Y@/ / / / /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\ ! ! ! !@%/ %/ %/ %/ %/N   5v  5  5  5  5D$ $ $3 3 3 8<$]ajl H H HC HWZ Hquvzq{ H H H H8$, $, $, $, $,LV    ,V , , , ,@06 0 0 0 0< ;?r 2 2s 2C 2 2 2 24 8<$]ajl I I IC IWZ I I I I8 pt|~  2  2C  2y  2	  2[`  2il  2  DI  2  2  2  2D im{  IK #2 #2c #23 #2i #2y #2be #2ux #2  PU #2 #2 #2 #2J <@ 2 2 2S 2 2 2 26 DHPR #K #K$s) #KS #K #K #K #KJ*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   !ccxt.async_support.base.ws.clientr   typingr   ccxt.base.errorsr   r   r   ccxt.base.preciser   async_supportr   rY  r:   r9   <module>r     s>       f f f f f f f f f f  | | | | | | | | | | | | | | | | | | | | | | | | | | | | 4 4 4 4 4 4       * * * * * * . . . . . . ' ' ' ' ' ' % % % % % %N N N N N$

  N N N N Nr:   