
    ":h<y                         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 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.                  j0                        Zy)
    N)
ArrayCacheArrayCacheBySymbolByIdArrayCacheByTimestamp)AnyBalancesIntOrder	OrderBookStrTickerTrade)Client)List)AuthenticationError)ArgumentsRequiredc            
           e Zd Zdef fdZi fdZddi fdedededee	   fdZ
dddi fdedededee   fd	Zd!d
efdZi fdedefdZd
efdZd
efdZdddi fdedededee   fdZd
efdZdi fdededefdZd Zd Zd
efdZd
efdZi fdZi fdefdZi fdZ d
efdZ!d
efdZ"d
efdZ#d
efdZ$d
efdZ%d
efdZ&d
efd Z' xZ(S )"okcoinreturnc                     | j                  t        t        |          dddddddddd	ddidddd	d
ddgddddddddid| j                  ddd      S )NTF)	wswatchTickerwatchTickerswatchOrderBookwatchOrderswatchTradeswatchTradesForSymbolswatchBalance
watchOHLCVr   z wss://real.okcoin.com:8443/ws/v3zchttps://user-images.githubusercontent.com/1294454/27766791-89ffb502-5ee5-11e7-8a5b-c5950b68ac65.jpgzhttps://www.okcoin.comzhttps://www.okcoin.com/docs/en/z https://www.okcoin.com/coin-feeszIhttps://www.okcoin.com/account/register?flag=activity&channelId=600001513)apilogowwwdocfeesreferralspotorder  depth_l2_tbt)limittypedepthinflate)fetchMarketsr   r   r   r   i N  )ping	keepAlive)hasurlsoptions	streaming)deep_extendsuperr   describer.   )self	__class__s    I/var/www/html/turnos/venv/lib/python3.12/site-packages/ccxt/pro/okcoin.pyr6   zokcoin.describe   s    fd < ># %"&##). $"
 < ~/8:g	 "(& "+#
 !'t  		"	G)A
 ) )	    c                   K   | j                          d {    | j                  |      }| j                  d   d   }|d   dz   |z   dz   |d   z   }d|gd}| j                  ||| j	                  ||      |       d {   S 7 m7 w)	Nr   r   r*   /:id	subscribeopargs)load_marketsmarketr1   watchr4   )r7   channelsymbolparamsrD   urlmessageHashrequests           r9   r?   zokcoin.subscribe>   s     !!!V$iit$Vns*W4s:VD\I M
 ZZ[$2B2B7F2SU`aaa 	" bs"   BBA(B?B BBNrG   sincer)   c                    K   | j                          d{    | j                  |      }| j                  d||       d{   }| j                  r|j	                  ||      }| j                  |||dd      S 7 c7 9w)a5  
        get the list of most recent trades for a particular symbol

        https://www.okcoin.com/docs-v5/en/#websocket-api-public-channel-trades-channel

        :param str symbol: unified symbol of the market to fetch trades for
        :param int [since]: timestamp in ms of the earliest trade to fetch
        :param int [limit]: the maximum amount of trades to fetch
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict[]: a list of `trade structures <https://docs.ccxt.com/#/?id=public-trades>`
        Ntrade	timestampT)rC   rG   r?   
newUpdatesgetLimitfilter_by_since_limit)r7   rG   rL   r)   rH   tradess         r9   watch_tradeszokcoin.watch_tradesI   sv      !!!V$~~gvv>>??OOFE2E))&%TRR 	">s!   A>A:+A>A<8A><A>c                 r  K   | j                          d{    | j                          d{    || j                  |      }| j                  | j                  dd      }| j                  |||       d{   }| j                  r|j                  ||      }| j                  |||dd      S 7 7 7 ;w)aE  
        watches information on multiple orders made by the user

        https://www.okcoin.com/docs-v5/en/#websocket-api-private-channel-order-channel

        :param str symbol: unified market symbol of the market orders were made in
        :param int [since]: the earliest time in ms to fetch orders for
        :param int [limit]: the maximum number of order structures to retrieve
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict[]: a list of `order structures <https://docs.ccxt.com/#/?id=order-structure>`
        Nr   r&   rO   T)	rC   authenticaterG   safe_stringr2   r?   rP   rQ   rR   )r7   rG   rL   r)   rH   	orderTyperS   s          r9   watch_orderszokcoin.watch_orders\   s      !!!!!![[(F$$T\\='J	~~i@@??OOFE2E))&%TRR 	"! As2   B7B1B7B3A
B79B5:8B73B75B7clientc                 r   | j                  |d      }| j                  |dg       }t        |      }|dkD  r| j                  | j                  dd      }| j
                  t        |      | _        | j
                  }i }	| j                  |      }
t        dt        |
            D ]6  }|
|   }|j                  |       |d   }| j                  |      }d|	|d   <   8 t        |	j                               }t        dt        |            D ])  }|d	z   ||   z   }|j                  | j
                  |       + y y )
Ntabledatar   ordersLimit  rG   Tr>   r=   )rW   
safe_valuelensafe_integerr2   ordersr   parse_ordersrangeappendrD   listkeysresolve)r7   rZ   messagesubscriptionr\   rc   ordersLengthr)   stored	marketIdsparsedir&   rG   rD   rh   rJ   s                    r9   handle_orderszokcoin.handle_ordersr   s0   L   '2&"56{!%%dllM4HE{{"4U;[[F I&&v.F1c&k* /q	e$xV,*.	&,'/ 	()D1c$i( 9#ckDG3t{{K89 r:   c                 D   K   | j                  d||       d{   S 7 w)a  
        watches a price ticker, a statistical calculation with the information calculated over the past 24 hours for a specific market

        https://www.okcoin.com/docs-v5/en/#websocket-api-public-channel-tickers-channel

        :param str symbol: unified symbol of the market to fetch the ticker for
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: a `ticker structure <https://docs.ccxt.com/#/?id=ticker-structure>`
        tickerN)r?   )r7   rG   rH   s      r9   watch_tickerzokcoin.watch_ticker   s!      ^^Hff====s     c                    | j                  |d      }| j                  |dg       }| j                  | j                  dd      }t	        dt        |            D ]  }| j                  ||         }|d   }| j                  |d   d      }	|d	z   |	z   }
| j                  | j                  |      }|t        |      }|| j                  |<   |j                  |       |j                  ||
        |S )
Nr\   r]   tradesLimitr_   r   rG   infoinstrument_idr=   )rW   r`   rb   r2   re   ra   parse_traderS   r   rf   ri   )r7   rZ   rj   r\   r]   rv   rp   rN   rG   marketIdrJ   rm   s               r9   handle_tradezokcoin.handle_trade   s        '2w3''mTJq#d)$ 
	0A$$T!W-E8_F''fGH#+0K__T[[&9F~#K0&,F#MM% NN6;/
	0 r:   c                 2   | j                  |d      }| j                  |dg       }t        dt        |            D ]Y  }| j	                  ||         }|d   }| j                  |d   d      }|dz   |z   }	|| j
                  |<   |j                  ||	       [ |S )Nr\   r]   r   rG   rw   rx   r=   )rW   r`   re   ra   parse_tickertickersri   )
r7   rZ   rj   r\   r]   rp   rs   rG   rz   rJ   s
             r9   handle_tickerzokcoin.handle_ticker   s    .   '2w3q#d)$ 	0A&&tAw/FH%F''vHH#+0K#)DLL NN6;/	0 r:   1mc                 J  K   | j                          d{    | j                  |      }| j                  | j                  ||      }d|z   dz   }| j	                  |||       d{   }| j
                  r|j                  ||      }| j                  |||dd      S 7 7 9w)a  
        watches historical candlestick data containing the open, high, low, and close price, and the volume of a market

        https://www.okcoin.com/docs-v5/en/#websocket-api-public-channel-candlesticks-channel

        :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
        Ncandlesr   T)rC   rG   rW   
timeframesr?   rP   rQ   rR   )	r7   rG   	timeframerL   r)   rH   intervalnameohlcvs	            r9   watch_ohlcvzokcoin.watch_ohlcv   s      !!!V$##DOOY	J("S(nnT66::??NN651E))%q$GG 	" ;s"   B#BAB#'B!(8B#!B#c                 :   | j                  |d      }| j                  |dg       }|j                  d      }| j                  |d      }|j                  dd      }|j                  dd      }| j	                  |      }t        dt        |            D ]  }	| j                  ||	   d	      }
| j                  ||	   d      }| j                  |
      }|d
   }| j                  ||      }| j                  | j                  |i       | j                  |<   | j                  | j                  |   |      }|:| j                  | j                  dd      }t        |      }|| j                  |   |<   |j                  |       |dz   |
z   }|j                  ||        y )Nr\   r]   r<      r    r   r   rx   rG   
OHLCVLimitr_   r=   )rW   r`   splitreplacefind_timeframere   ra   safe_marketparse_ohlcvohlcvsrb   r2   r   rf   ri   )r7   rZ   rj   r\   r]   partspart1r   r   rp   rz   r   rD   rG   ro   rm   r)   rJ   s                     r9   handle_ohlcvzokcoin.handle_ohlcv  s   &   '2w3C   *==2.##C,''1	q#d)$ 	0A''QAH__T!Wh7F%%h/FH%F%%ff5F"&//$++vr"JDKK__T[[%8)DF~))$,,dK.u517F#I.MM&!#+0KNN6;/	0r:   c                    K   | j                  | j                  di       }| j                  |dd      }| j                  |||       d{   }|j	                         S 7 w)aP  
        watches information on open orders with bid(buy) and ask(sell) prices, volumes and other data

        https://www.okcoin.com/docs-v5/en/#websocket-api-public-channel-order-book-channel

        :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
        r   r+   r(   N)r`   r2   rW   r?   r)   )r7   rG   r)   rH   r2   r+   	orderbooks          r9   watch_order_bookzokcoin.watch_order_book<  s[      //$,,0@"E  '>B..??	   @s   AA 	A
A c                 p    | j                  |d      }| j                  |d      }|j                  ||       y )Nr   r   )
safe_floatstore)r7   booksidedeltapriceamounts        r9   handle_deltazokcoin.handle_deltaL  s0    q)*uf%r:   c                 b    t        dt        |            D ]  }| j                  |||           y )Nr   )re   ra   r   )r7   r   deltasrp   s       r9   handle_deltaszokcoin.handle_deltasQ  s0    q#f+& 	3Ahq	2	3r:   c                    | j                  |dg       }| j                  |dg       }| j                  |d   |       | j                  |d   |       | j                  | j                  |d            }||d<   | j	                  |      |d<   |S )NasksbidsrO   datetime)r`   r   	parse8601rW   iso8601)r7   rZ   rj   r   r   r   rO   s          r9   handle_order_book_messagez okcoin.handle_order_book_messageU  s    $ w3w39V,d39V,d3NN4#3#3G[#IJ	!*	+ $Y 7	*r:   c                 0   | j                  |d      }| j                  |dg       }| j                  |d      }|dk(  rt        dt        |            D ]  }||   }| j                  |d      }| j	                  |      }	|	d   }
| j                  | j
                  di       }| j                  |d	d
      }| j                  i |      }|| j                  |
<   | j                  |||       |dz   |z   }|j                  ||        |S t        dt        |            D ]z  }||   }| j                  |d      }| j	                  |      }	|	d   }
|
| j                  v s?| j                  |
   }| j                  |||       |dz   |z   }|j                  ||       | |S )Nactionr]   r\   partialr   rx   rG   r   r)   r'   r=   )rW   r`   re   ra   r   r2   rb   
order_book
orderbooksr   ri   )r7   rZ   rj   r   r]   r\   rp   updaterz   rD   rG   r2   r)   r   rJ   s                  r9   handle_order_bookzokcoin.handle_order_bookp  s   d !!'84w3  '2Y1c$i( 7a++FOD))(3)//$,,8H"M))'7C@ OOB6	*3'..vvyI#ckH4y+670  1c$i( 	;a++FOD))(3)T__, $ 7I22669M"'#+"8KNN9k:	; r:   c                 B  K   | j                          | j                  d   d   }d}| j                  |      }| j                  |j                  |      }||j                  d      }t        | j                               }d}d}||z   |z   }	| j                  | j                  |	      | j                  | j                        t        j                  d      }
|| j                  | j                  ||
gd}| j                  | j                   |||||       | d {   S 7 w)	Nr   r   loginauthenticatedGETz/users/self/verifybase64r@   )check_required_credentialsr1   rZ   r`   subscriptionsfuturestrsecondshmacencodesecrethashlibsha256apiKeypasswordspawnrE   )r7   rH   rI   rJ   rZ   r   rO   methodpathauth	signaturerK   s               r9   rV   zokcoin.authenticate  s    '')iit$S!!5!5{C>]]?3FDLLN+IF'Dv%,D		$++d"3T[[5Mw~~_ghI!KKMM	G JJtzz3Wk6R||s   DDDDc                 
  K   | j                  | j                  dd      }| j                  |d|      }|t        | j                  dz         | j                          d{   }| j                  ||       d{   S 7 7 w)ax  
        watch balance and get the amount of funds available for trading or funds locked in orders

        https://www.okcoin.com/docs-v5/en/#websocket-api-private-channel-account-channel

        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: a `balance structure <https://docs.ccxt.com/#/?id=balance-structure>`
        r   defaultTyper*   NS watchBalance requires a type parameter(one of 'spot', 'margin', 'futures', 'swap'))safe_string_2r2   rW   r   r>   rV   subscribe_to_user_account)r7   rH   r   r*   negotiations        r9   watch_balancezokcoin.watch_balance  s      ((~}U<<#DGG  /D  %D  E  E --//33KHHH 0Hs$   AB A?!B:B;BBc                   K   | j                  | j                  dd      }| j                  |d|      }|t        | j                  dz         | j                          d {    | j                  |d      }| j                  |d| j                  |            }d }|| j                  |      }| j                  |d      }| j                  |      }	|	d u }
|d u }|dk(  r |rtt        | j                  d	z   |z   d
z         |dk(  s
|dk(  s|dk(  r |
rEt        | j                  dz   |z   d
z         |dk(  r"|r |
rt        | j                  dz   |z   d
z         d }|s|d   }n|
s|	d   }|dk(  rdn|}|dk(  rdnd}|dz   |z   }|dz   |z   }| j                  d   d   }d|gd}| j                  |g d      }| j                  ||| j                  ||      |       d {   S 7 7 w)Nr   r   r*   r   currencycoderG   r%   zJ watchBalance requires a 'currency'(id) or a unified 'code' parameter for z	 accountsmarginswapoptionzQ watchBalance requires a 'instrument_id'(id) or a unified 'symbol' parameter for futureszv watchBalance requires a 'currency'(id), or unified 'code', or 'instrument_id'(id), or unified 'symbol' parameter for r>   margin_accountaccountr<   r=   r   r   r?   r@   )r   r   rx   rG   r*   )r   r2   rW   r   r>   rC   safe_currency_coder   rD   r1   omitrE   r4   )r7   r   rH   r   r*   
currencyIdr   r   rG   rD   marketUndefinedcurrencyUndefinedsuffixaccountTyper   rJ   subscriptionHashrI   rK   querys                       r9   r   z okcoin.subscribe_to_user_account  s    ((~}U<<#DGG  /D  %D  E  E!!!%%fj9
0G0G
0ST}}T*H!!&(3V$!T>%-6> '2~(~  BF  )F  IT  )T  U  UhDFN8H'  3F  )F  IM  )M  P[  )[  \  \Y _'  3k  )k  nr  )r  u@  )@  A  A d^F D\F!%!1f'+x'7"i!C''1&,v5iit$%&
 		&"YZZZ[$2B2B7E2RTdeeeE 	"D fs%   AG$ G!E9G$G"G$"G$c                    | j                  |d      }|j                  d      }| j                  |dg       }|| j                  d<   | j                  |d      }|dk(  r| j                  |d      }|dk(  rd	}t	        dt        |            D ]  }| j                  ||      }	| j                  | j                  |i       }
| j                  |
|	      }| j                  |      | j                  |<   |j                  | j                  |   |        y )
Nr\   r<   r]   rw   r   r%   r   r   r   )
rW   r   r`   balancere   ra   parseBalanceByTyper4   safe_balanceri   )r7   rZ   rj   r\   r   r]   r*   r   rp   r   
oldBalance
newBalances               r9   handle_balancezokcoin.handle_balance  s   B   '2C w3#Vq)6>$$UA.E((q#d)$ 	6A--dD9GtR@J))*g>J!%!2!2:!>DLLNN4<<-u5	6r:   c                     |S N r7   rZ   rj   s      r9   handle_subscription_statusz!okcoin.handle_subscription_statusD  s	     r:   c                 *    |j                  |d       |S )Nr   )ri   r   s      r9   handle_authenticatezokcoin.handle_authenticateL  s     	w0r:   c                      y)Nr.   r   )r7   rZ   s     r9   r.   zokcoin.pingS  s     r:   c                 0    | j                         |_        |S r   )millisecondslastPongr   s      r9   handle_pongzokcoin.handle_pongX  s    ++-r:   c                    | j                  |d      }	 |u| j                  dz   | j                  |      z   }| j                  | j                  d   ||       | j                  |d      }| | j                  | j                  d   ||       |S # t        $ rO}t        |t              r4|j                  |d       d}||j                  v r|j                  |= Y d }~yY d }~|S d }~ww xY w)	N	errorCode exactrj   broadr   r   F)rW   r>   jsonthrow_exactly_matched_exception
exceptionsr`   throw_broadly_matched_exception	Exception
isinstancer   rejectr   )r7   rZ   rj   r   feedbackmessageStringer   s           r9   handle_error_messagezokcoin.handle_error_message\  s     $$Wk:		$77S=499W+==44T__W5MyZbc $ C ,889QS`bjk   	!01a1 V111,,V4 2 	s   A7B 	C%?C  C%c           	         | j                  ||      sy |dk(  r| j                  ||       y | j                  |d      }|M| j                  |d      }|8| j                  | j                  d}| j                  ||      }|
 |||       y y y |j                  d      }| j                  |d      }| j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  d}| j                  ||      }|j                  d      d	k\  r| j                  }|
 |||       y y )
Npongr\   event)r   r?   r<   r   )r+   depth5r(   rs   rN   r   r   r&   r   r   )r  r   rW   r   r   r`   r   r   r   r{   r   rq   findr   )	r7   rZ   rj   r\   r  methodsr   r   r   s	            r9   handle_messagezokcoin.handle_messages  s]   ((9l fVW-  '2=$$Wg6E  "55!%!@!@	! %8%67+ & ! KK$E##E1-D//00 $ 6 6,,**.."&"5"5++
G __Wd3Fyy"a'**!vw' "r:   r   ))__name__
__module____qualname__r   r6   r?   r   r   r   r   rT   r   r	   rY   r   rq   r   rt   r{   r   rg   r   r   r
   r   r   r   r   r   rV   r   r   r   r   r   r   r.   r   r  r  __classcell__)r8   s   @r9   r   r      s   *# *X 79 	b <@d[] S SS S Sbfglbm S& 04$UYbd S SC Ss Simnsit S,99F 99v 68 
> 
>F 
>6 @ F  D 8<$]ajl H HC HWZ Hquvzq{ H,)06 )0V @DB !S ! !S\ ! &
3 6N N` )+ 2 *, I I" CE 'fR/6V /6b & 6 
& 6 .Z(V Z(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   !ccxt.async_support.base.ws.clientr   typingr   ccxt.base.errorsr   r   async_supportr   r   r:   r9   <module>r     sB     f f  T T T 4  0 .}
(T&& }
(r:   