
    iv                         d dl Zd dlmZmZmZ d dlZd dlmZm	Z	m
Z
mZmZmZmZ d dlmZ d dlmZ d dlmZ d dlmZ  G d d	ej        j                  ZdS )
    N)
ArrayCacheArrayCacheBySymbolByIdArrayCacheByTimestamp)BalancesIntOrder	OrderBookStrTickerTrade)Client)List)AuthenticationError)ArgumentsRequiredc            
           e Zd Z 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 Z#d
efdZ$d
efdZ%d
efd Z& xZ'S )"okcoinc                     |                      t          t          |                                           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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    U/var/www/html/crypto-bot/backend/venv/lib/python3.11/site-packages/ccxt/pro/okcoin.pyr3   zokcoin.describe   s    fd 3 3 < < > ># %"&## $"	 	 < ~/8:g	 	 "(& "+# #
 !'t   	"	 E(A
 (A
 ( ( (	    c                 .  K   |                                   d {V  |                     |          }| j        d         d         }|d         dz   |z   dz   |d         z   }d|gd}|                     |||                     ||          |           d {V S )	Nr   r   r'   /:id	subscribeopargs)load_marketsmarketr.   watchr1   )r4   channelsymbolparamsrA   urlmessageHashrequests           r6   r<   zokcoin.subscribe=   s      !!!!!!!!!V$$it$Vns*W4s:VD\I M
 
 ZZ[$2B2B7F2S2SU`aaaaaaaaar7   NrD   sincer&   returnc                 
  K   |                                   d{V  |                     |          }|                     d||           d{V }| j        r|                    ||          }|                     |||dd          S )a  
        get the list of most recent trades for a particular symbol
        :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)r@   rD   r<   
newUpdatesgetLimitfilter_by_since_limit)r4   rD   rI   r&   rE   tradess         r6   watch_tradeszokcoin.watch_tradesH   s       !!!!!!!!!V$$~~gvv>>>>>>>>? 	3OOFE22E))&%TRRRr7   c                 z  K   |                                   d{V  |                                  d{V  ||                     |          }|                     | j        dd          }|                     |||           d{V }| j        r|                    ||          }|                     |||dd          S )a  
        watches information on multiple orders made by the user
        :param str symbol: unified market symbol of the market orders were made in
        :param int [since]: the earliest time in ms to fetch orders for
        :param int [limit]: the maximum number of order structures to retrieve
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict[]: a list of `order structures <https://docs.ccxt.com/#/?id=order-structure>`
        Nr   r#   rM   T)	r@   authenticaterD   safe_stringr/   r<   rN   rO   rP   )r4   rD   rI   r&   rE   	orderTyperQ   s          r6   watch_orderszokcoin.watch_ordersX   s       !!!!!!!!!!!!!!!!!![[((F$$T\='JJ	~~i@@@@@@@@? 	3OOFE22E))&%TRRRr7   clientc                    |                      |d          }|                     |dg           }t          |          }|dk    r$|                     | j        dd          }| j        t          |          | _        | j        }i }	|                     |          }
t          dt          |
                    D ]G}|
|         }|	                    |           |d         }| 
                    |          }d|	|d         <   Ht          |	                                          }t          dt          |                    D ]-}|d	z   ||         z   }|                    | j        |           ,d S d S )
Ntabledatar   ordersLimit  rD   Tr;   r:   )rU   
safe_valuelensafe_integerr/   ordersr   parse_ordersrangeappendrA   listkeysresolve)r4   rX   messagesubscriptionrZ   ra   ordersLengthr&   stored	marketIdsparsedir#   rD   rA   rf   rG   s                    r6   handle_orderszokcoin.handle_ordersk   sl   L   '22&"556{{!%%dlM4HHE{"4U;;[FI&&v..F1c&kk** / /q	e$$$xV,,*.	&,''	(())D1c$ii(( 9 9#ckDG3t{K8888! 9 9r7   c                 @   K   |                      d||           d{V S )a  
        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
        :returns dict: a `ticker structure <https://docs.ccxt.com/#/?id=ticker-structure>`
        tickerN)r<   )r4   rD   rE   s      r6   watch_tickerzokcoin.watch_ticker   s0       ^^Hff=========r7   c                 (   |                      |d          }|                     |dg           }|                     | j        dd          }t	          dt          |                    D ]}|                     ||                   }|d         }|                      |d         d          }	|d	z   |	z   }
|                     | j        |          }|t          |          }|| j        |<   |	                    |           |
                    ||
           |S )
NrZ   r[   tradesLimitr]   r   rD   infoinstrument_idr:   )rU   r^   r`   r/   rc   r_   parse_traderQ   r   rd   rg   )r4   rX   rh   rZ   r[   rt   rn   rL   rD   marketIdrG   rk   s               r6   handle_tradezokcoin.handle_trade   s       '22w33''mTJJq#d))$$ 
	0 
	0A$$T!W--E8_F''fGGH#+0K__T[&99F~#K00&,F#MM%   NN6;////r7   c                 n   |                      |d          }|                     |dg           }t          dt          |                    D ]i}|                     ||                   }|d         }|                      |d         d          }|dz   |z   }	|| j        |<   |                    ||	           j|S )NrZ   r[   r   rD   ru   rv   r:   )rU   r^   rc   r_   parse_tickertickersrg   )
r4   rX   rh   rZ   r[   rn   rq   rD   rx   rG   s
             r6   handle_tickerzokcoin.handle_ticker   s    .   '22w33q#d))$$ 	0 	0A&&tAw//FH%F''vHHH#+0K#)DL NN6;////r7   1mc                 R  K   |                                   d{V  |                     |          }|                     | j        ||          }d|z   dz   }|                     |||           d{V }| j        r|                    ||          }|                     |||dd          S )aQ  
        watches historical candlestick data containing the open, high, low, and close price, and the volume of a market
        :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)r@   rD   rU   
timeframesr<   rN   rO   rP   )	r4   rD   	timeframerI   r&   rE   intervalnameohlcvs	            r6   watch_ohlcvzokcoin.watch_ohlcv   s       !!!!!!!!!V$$##DOY	JJ("S(nnT66::::::::? 	2NN6511E))%q$GGGr7   c                    |                      |d          }|                     |dg           }|                    d          }|                      |d          }|                    dd          }|                    dd          }|                     |          }t          dt          |                    D ]#}	|                      ||	         d	          }
|                     ||	         d          }|                     |
          }|d
         }|                     ||          }|                     | j	        |i           | j	        |<   |                     | j	        |         |          }|;| 
                    | j        dd          }t          |          }|| j	        |         |<   |                    |           |dz   |
z   }|                    ||           %d S )NrZ   r[   r9      r    r   r   rv   rD   
OHLCVLimitr]   r:   )rU   r^   splitreplacefind_timeframerc   r_   safe_marketparse_ohlcvohlcvsr`   r/   r   rd   rg   )r4   rX   rh   rZ   r[   partspart1r   r   rn   rx   r   rA   rD   rm   rk   r&   rG   s                     r6   handle_ohlcvzokcoin.handle_ohlcv  s   &   '22w33C    **==2..##C,,''11	q#d))$$ 	0 	0A''QAAH__T!Wh77F%%h//FH%F%%ff55F"&//$+vr"J"JDK__T[%8)DDF~))$,dKK.u5517F#I.MM&!!!#+0KNN6;////	0 	0r7   c                    K   |                      | j        di           }|                     |dd          }|                     |||           d{V }|                                S )a  
        watches information on open orders with bid(buy) and ask(sell) prices, volumes and other data
        :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^   r/   rU   r<   r&   )r4   rD   r&   rE   r/   r(   	orderbooks          r6   watch_order_bookzokcoin.watch_order_book/  sn       //$,0@"EE  '>BB..????????	   r7   c                     |                      |d          }|                      |d          }|                    ||           d S )Nr   r   )
safe_floatstore)r4   booksidedeltapriceamounts        r6   handle_deltazokcoin.handle_delta<  sB    q))**uf%%%%%r7   c                 ~    t          dt          |                    D ]}|                     |||                    d S )Nr   )rc   r_   r   )r4   r   deltasrn   s       r6   handle_deltaszokcoin.handle_deltasA  sH    q#f++&& 	3 	3Ahq	2222	3 	3r7   c                 ^   |                      |dg           }|                      |dg           }|                     |d         |           |                     |d         |           |                     |                     |d                    }||d<   |                     |          |d<   |S )NasksbidsrM   datetime)r^   r   	parse8601rU   iso8601)r4   rX   rh   r   r   r   rM   s          r6   handle_order_book_messagez okcoin.handle_order_book_messageE  s    $ w33w339V,d3339V,d333NN4#3#3G[#I#IJJ	!*	+ $Y 7 7	*r7   c                    |                      |d          }|                     |dg           }|                      |d          }|dk    rt          dt          |                    D ]}||         }|                      |d          }|                     |          }	|	d         }
|                     | j        di           }|                     |d	d
          }|                     i |          }|| j        |
<   | 	                    |||           |dz   |z   }|
                    ||           nt          dt          |                    D ]}||         }|                      |d          }|                     |          }	|	d         }
|
| j        v rB| j        |
         }| 	                    |||           |dz   |z   }|
                    ||           |S )Nactionr[   rZ   partialr   rv   rD   r   r&   r$   r:   )rU   r^   rc   r_   r   r/   r`   
order_book
orderbooksr   rg   )r4   rX   rh   r   r[   rZ   rn   updaterx   rA   rD   r/   r&   r   rG   s                  r6   handle_order_bookzokcoin.handle_order_book`  s   d !!'844w33  '22Y1c$ii(( 7 7a++FODD))(33)//$,8H"MM))'7C@@ OOB66	*3'..vvyIII#ckH4y+66667 1c$ii(( 	; 	;a++FODD))(33)T_,, $ 7I22669MMM"'#+"8KNN9k:::r7   c                 N  K   |                                   | j        d         d         }d}|                     |          }|                     |j        |          }||                    d          }t          |                                           }d}d}||z   |z   }	|                     | 	                    |	          | 	                    | j
                  t          j        d          }
|| j        | j        ||
gd}|                     | j        |||||           | d {V S )	Nr   r   loginauthenticatedGETz/users/self/verifybase64r=   )check_required_credentialsr.   rX   r^   subscriptionsfuturestrsecondshmacencodesecrethashlibsha256apiKeypasswordspawnrB   )r4   rE   rF   rG   rX   r   rM   methodpathauth	signaturerH   s               r6   rT   zokcoin.authenticate  s#     '')))it$S!!!5{CC>]]?33FDLLNN++IF'Dv%,D		$++d"3"3T[[5M5Mw~_ghhI!KM	 G JJtz3Wk6RRR||||||r7   c                 
  K   |                      | j        dd          }|                     |d|          }|t          | j        dz             |                                  d{V }|                     ||           d{V S )a  
        watch balance and get the amount of funds available for trading or funds locked in orders
        :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_2r/   rU   r   r;   rT   subscribe_to_user_account)r4   rE   r   r'   negotiations        r6   watch_balancezokcoin.watch_balance  s       ((~}UU<<<#DG  /D  %D  E  E  E --////////33KHHHHHHHHHr7   c                 $  K   |                      | j        dd          }|                     |d|          }|t          | j        dz             |                                  d {V  |                     |d          }|                     |d|                     |                    }d }||                     |          }|                     |d          }|                     |          }	|	d u }
|d u }|dk    r |rt          | j        d	z   |z   d
z             nY|dk    s|dk    s|dk    r |
rt          | j        dz   |z   d
z             n'|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}| 
                    |g d          }|                     |||                     ||          |           d {V S )Nr   r   r'   r   currencycoderD   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accountr9   r:   r   r   r<   r=   )r   r   rv   rD   r'   )r   r/   rU   r   r;   r@   safe_currency_coder   rA   r.   omitrB   r1   )r4   r   rE   r   r'   
currencyIdr   r   rD   rA   marketUndefinedcurrencyUndefinedsuffixaccountTyper   rG   subscriptionHashrF   rH   querys                       r6   r   z okcoin.subscribe_to_user_account  s     ((~}UU<<<#DG  /D  %D  E  E  E!!!!!!!!!%%fj99
0G0G
0S0STT}}T**H!!&(33V$$!T>%-6>>  U'2~(~  BF  )F  IT  )T  U  U  UUhDFNN8H8H \'  3F  )F  IM  )M  P[  )[  \  \  \\Y  A_ A'  3k  )k  nr  )r  u@  )@  A  A  A  	"d^FF  	"D\F!%!1!1ff'+x'7'7""i!C''1&,v5it$%&
 
 		&"Y"Y"YZZZZ[$2B2B7E2R2RTdeeeeeeeeer7   c                 ^   |                      |d          }|                    d          }|                     |dg           }|| j        d<   |                      |d          }|dk    r|                      |d          }|dk    rd	}t	          dt          |                    D ]}|                     ||          }	|                     | j        |i           }
|                     |
|	          }|                     |          | j        |<   |	                    | j        |         |           d S )
NrZ   r9   r[   ru   r   r"   r   r   r   )
rU   r   r^   balancerc   r_   parseBalanceByTyper1   safe_balancerg   )r4   rX   rh   rZ   r   r[   r'   r   rn   r   
oldBalance
newBalances               r6   handle_balancezokcoin.handle_balance   s2   B   '22C  w33#Vq))6>>$$UA..E(((q#d))$$ 	6 	6A--dD99GtR@@J))*g>>J!%!2!2:!>!>DLNN4<-u5555	6 	6r7   c                     |S N r4   rX   rh   s      r6   handle_subscription_statusz!okcoin.handle_subscription_status1  s	     r7   c                 2    |                     |d           |S )Nr   )rg   r   s      r6   handle_authenticatezokcoin.handle_authenticate9  s     	w000r7   c                     dS )Nr+   r   )r4   rX   s     r6   r+   zokcoin.ping@  s	     vr7   c                 8    |                                  |_        |S r   )millisecondslastPongr   s      r6   handle_pongzokcoin.handle_pongE  s    ++--r7   c                    |                      |d          }	 ||| j        dz   |                     |          z   }|                     | j        d         ||           |                     |d          }|"|                     | j        d         ||           n[# t          $ rN}t          |t                    r/|
                    |d           d}||j        v r|j        |= Y d }~dS Y d }~nd }~ww xY w|S )	N	errorCode exactrh   broadr   r   F)rU   r;   jsonthrow_exactly_matched_exception
exceptionsr^   throw_broadly_matched_exception	Exception
isinstancer   rejectr   )r4   rX   rh   r   feedbackmessageStringer   s           r6   handle_error_messagezokcoin.handle_error_messageI  s,    $$Wk::		$7S=499W+=+==44T_W5MyZbccc $ C C ,889QS`bjkkk 	 	 	!011 a111 V111,V4uuuuu    	 s   A>B 
C/!>C**C/c           	         |                      ||          sd S |dk    r|                     ||           d S |                     |d          }|Q|                     |d          }|5| j        | j        d}|                     ||          }| |||           d S d S d S |                    d          }|                     |d          }| j        | j        | j        | j        | j	        | j
        | j
        | j        d}|                     ||          }|                    d          d	k    r| j        }| |||           d S d S )
NpongrZ   event)r   r<   r9   r   )r(   depth5r%   rq   rL   r   r   r#   r   r   )r  r   rU   r   r   r^   r   r   r}   ry   r   ro   findr   )	r4   rX   rh   rZ   r  methodsr   r   r   s	            r6   handle_messagezokcoin.handle_message`  s   ((99 	Fl fVW---F  '22=$$Wg66E  "5!%!@	  %88%F67+++++ !  &% KK$$E##E1--D/0 $ 6,*."&"5+
 
G __Wd33Fyy""a''*!vw''''' "!r7   r   )(__name__
__module____qualname__r3   r<   r   r   r   r   rR   r
   r   rW   r   ro   r   rr   ry   r}   re   r   r   r	   r   r   r   r   r   rT   r   r   r   r   r   r   r+   r   r  r	  __classcell__)r5   s   @r6   r   r      s       ) ) ) ) )V 79 	b 	b 	b 	b <@d[] S S SS S Sbfglbm S S S S  04$UYbd S S SC Ss Simnsit S S S S&99 99F 99 99 99 99v 68 > > >F > > > >6    @ F        D 8<$]ajl H H HC HWZ Hquvzq{ H H H H&)06 )0 )0 )0 )0V @DB ! !S ! !S\ ! ! ! !& & &
3 3 3    6N N N N N` )+    2 *, I I I I I I CE 'f 'f 'f 'fR/6V /6 /6 /6 /6b    &      
&    6    .Z(V Z( Z( Z( Z( Z( Z( Z( Z(r7   r   )ccxt.async_supportccxt ccxt.async_support.base.ws.cacher   r   r   r   ccxt.base.typesr   r   r   r	   r
   r   r   !ccxt.async_support.base.ws.clientr   typingr   ccxt.base.errorsr   r   async_supportr   r   r7   r6   <module>r     s        f f f f f f f f f f  O O O O O O O O O O O O O O O O O O 4 4 4 4 4 4       0 0 0 0 0 0 . . . . . .j
( j
( j
( j
( j
(T& j
( j
( j
( j
( j
(r7   