
    ":h&[                         d dl Zd dlmZmZ d dl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  G d dej"                  j$                        Zy)	    N)
ArrayCacheArrayCacheByTimestamp)AnyInt	OrderBookTickerTrade)Client)List)ExchangeErrorc            
       Z    e Zd Zdef fdZd Zi fde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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e	fdZd Zd Z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 Zde	fdZ de	fdZ!de	fdZ" xZ#S )bittradereturnc                     | j                  t        t        |          dddddddddddddddiiid	d	dd
didd      S )NTF)wswatchOrderBookwatchTickerswatchTickerwatchTradeswatchTradesForSymbolswatchBalance
watchOHLCVapir   zwss://{hostname}/wszwss://{hostname}/ws/v2)publicprivate  gunzip)tradesLimit
OHLCVLimitr   r   )hasurlsoptions)deep_extendsuperr   describe)self	__class__s    K/var/www/html/turnos/venv/lib/python3.12/site-packages/ccxt/pro/bittrade.pyr%   zbittrade.describe   s    h > @"& %##). %"	 &;'? 	  $"d	+C
  	    c                     | j                  | j                  | j                  dd      d      }|| j                  d<   t        |      S )N	requestIdr      )sumsafe_integerr"   str)r&   r+   s     r(   
request_idzbittrade.request_id0   s=    HHT..t||[!LaP	$-[!9~r)   symbolc                   K   | j                          d{    | j                  |      }|d   }d|d   z   dz   }| j                  | j                  dd      }d| j                  i}| j                  | j                  d   d   |   d	   |      }| j                         }||d
}	||||d}
| j                  ||| j                  |	|      ||
       d{   S 7 7 w)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>`
        Nr1   market.idz.detailr   hostnamer   r   subr4   r4   messageHashr1   params)
load_marketsmarketsafe_stringr"   r5   implode_paramsr!   r0   watchextend)r&   r1   r:   r<   r9   r   r5   urlr+   requestsubscriptions              r(   watch_tickerzbittrade.watch_ticker5   s      !!!V$!&,.:t||UE:$dmm4!!$))E"24"8"=h"GROO%	

 &	
 ZZ[$++gv2NP[]ijjj' 	"& ks"   CCB<CCCCclientc                 ~   | j                  |di       }| j                  |d      }|j                  d      }| j                  |d      }| j                  |      }| j	                  ||      }| j                  |d      }	|	|d<   | j                  |	      |d<   |d   }
|| j                  |
<   |j                  ||       |S )	Ntickch.r,   ts	timestampdatetimer1   )
safe_valuer=   splitsafe_marketparse_tickeriso8601tickersresolve)r&   rE   messagerG   rH   partsmarketIdr<   tickerrK   r1   s              r(   handle_tickerzbittrade.handle_tickerQ   s    $ w3gt,##E1-!!(+""40OOGT2	'{!\\)4z!%Vvr"r)   Nsincelimitc                 "  K   | j                          d{    | j                  |      }|d   }d|d   z   dz   }| j                  | j                  dd      }d| j                  i}| j                  | j                  d   d   |   d	   |      }	| j                         }
||
d
}|
|||d}| j                  |	|| j                  ||      ||       d{   }| j                  r|j                  ||      }| j                  |||dd      S 7 7 9w)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>`
        Nr1   r3   r4   z.trade.detailr   r5   r   r   r6   r8   rK   T)r;   r<   r=   r"   r5   r>   r!   r0   r?   r@   
newUpdatesgetLimitfilter_by_since_limit)r&   r1   rY   rZ   r:   r<   r9   r   r5   rA   r+   rB   rC   tradess                 r(   watch_tradeszbittrade.watch_tradesq   s'     !!!V$!&,.@t||UE:$dmm4!!$))E"24"8"=h"GROO%	

 &	
 zz#{DKK4PR]_kll??OOFE2E))&%TRR- 	"& ms"   DDB<DD8DDc                 6   | j                  |di       }| j                  |di       }| j                  |d      }|j                  d      }| j                  |d      }| j                  |      }|d   }	| j                  | j                  |	      }
|
7| j                  | j                  dd      }t        |      }
|
| j                  |	<   t        d	t        |            D ](  }| j                  ||   |      }|
j                  |       * |j                  |
|       |S )
NrG   datarH   rI   r,   r1   r   r   r   )rM   r=   rN   rO   r_   r.   r"   r   rangelenparse_tradeappendrS   )r&   rE   rT   rG   rb   rH   rU   rV   r<   r1   tradesCacherZ   itrades                 r(   handle_tradeszbittrade.handle_trades   s   * w3tVR0gt,##E1-!!(+!oodkk6:%%dllM4HE$U+K"-DKKq#d)$ 	&A$$T!Wf5Eu%	& 	{B'r)   1mc                 f  K   | j                          d{    | j                  |      }|d   }| j                  | j                  ||      }d|d   z   dz   |z   }| j                  | j                  dd      }	d| j
                  i}
| j                  | j                  d   d   |	   d	   |
      }| j                         }||d
}|||||d}| j                  ||| j                  ||      ||       d{   }| j                  r|j                  ||      }| j                  |||dd      S 7 7 :w)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
        Nr1   r3   r4   z.kline.r   r5   r   r   r6   )r4   r9   r1   	timeframer:   r   T)r;   r<   r=   
timeframesr"   r5   r>   r!   r0   r?   r@   r\   r]   r^   )r&   r1   rm   rY   rZ   r:   r<   intervalr9   r   r5   rA   r+   rB   rC   ohlcvs                   r(   watch_ohlcvzbittrade.watch_ohlcv   sG     !!!V$!##DOOY	J&,.:XEt||UE:$dmm4!!$))E"24"8"=h"GROO%	

 &"
 jjk4;;w3OQ\^jkk??NN651E))%q$GG/ 	"( ls"   D1D,CD14D/58D1/D1c                 x   | j                  |d      }|j                  d      }| j                  |d      }| j                  |      }|d   }| j                  |d      }| j                  |      }	| j	                  | j
                  |i       | j
                  |<   | j	                  | j
                  |   |	      }
|
:| j                  | j                  dd      }t        |      }
|
| j
                  |   |	<   | j	                  |d      }| j                  ||      }|
j                  |       |j                  |
|       y )	NrH   rI   r,   r1      r   r   rG   )r=   rN   rO   find_timeframerM   ohlcvsr.   r"   r   parse_ohlcvrf   rS   )r&   rE   rT   rH   rU   rV   r<   r1   ro   rm   storedrZ   rG   parseds                 r(   handle_ohlcvzbittrade.handle_ohlcv   s   " gt,##E1-!!(+!##E1-''1	"oodkk62FFV!4i@>%%dllL$GE*51F-3DKK	*w/!!$/fvr"r)   c                 V  K   ||dk7  rt        | j                  dz         | j                          d{    | j                  |      }|d   }|dn|}d|d   z   dz   t	        |      z   }| j                  | j                  dd      }d	| j                  i}| j                  | j                  d   d
   |   d   |      }| j                         }	||	d}
|	||||| j                  d}| j                  ||| j                  |
|      ||       d{   }|j                         S 7 7 w)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
        N   z( watchOrderBook accepts limit = 150 onlyr1   r3   r4   z.mbp.r   r5   r   r   r6   r4   r9   r1   rZ   r:   method)r   r4   r;   r<   r/   r=   r"   r5   r>   r!   r0   handle_order_book_subscriptionr?   r@   rZ   )r&   r1   rZ   r:   r<   r9   r   r5   rA   r+   rB   rC   	orderbooks                r(   watch_order_bookzbittrade.watch_order_book   sE     ESL*T TUU!!!V$!E&,.83u:Et||UE:$dmm4!!$))E"24"8"=h"GROO%	

 &99
 **S+t{{7F7SU`bnoo	  / 	", ps"   3D)D%CD)D'D)'D)c                    | j                  |d      }| j                  |d      }| j                  |   }| j                  |d      }| j                  ||      }| j	                  |d      |d<   |j                  |       |j                  }	t        dt        |	            D ]  }
| j                  ||	|
   |        || j                  |<   |j                  ||       y )Nr1   r9   rb   seqNumnoncer   )r=   
orderbooksrM   parse_order_bookr.   resetcacherc   rd   handle_order_book_messagerS   )r&   rE   rT   rC   r1   r9   r   rb   snapshotmessagesrh   s              r(   handle_order_book_snapshotz#bittrade.handle_order_book_snapshot"  s    * !!,9&&|]COOF+	w/((v6 --dH=!??q#h-( 	KA**68A;	J	K"+y+.r)   c                 Z  K   | j                  |d      }	 | j                  |d      }| j                  |d      }| j                  |d      }| j                  | j                  dd      }d| j                  i}	| j                  | j                  d   d   |   d   |	      }
| j                         }||d	}|||||| j                  d
}| j                  |
||||       d {   }|j                         S 7 # t        $ r)}|j                  |= |j                  ||       Y d }~y d }~ww xY ww)Nr9   r1   rZ   r:   r   r5   r   r   )reqr4   r|   )r=   r.   rM   r"   r5   r>   r!   r0   r   r?   rZ   	Exceptionsubscriptionsreject)r&   rE   rT   rC   r9   r1   rZ   r:   r   r5   rA   r+   rB   snapshotSubscriptionr   es                   r(   watch_order_book_snapshotz"bittrade.watch_order_book_snapshotE  s<    &&|]C	*%%lH=F%%lG<E__\8<F""4<<>C($--8H%%dii&6t&<S&A(&KXVC)I"G  *  99*  #jji)MabbI??$$ c 	*$$[1MM![))	*sA   D+C	C6 C4 C6 3D+4C6 6	D(?D#D+#D((D+c                 p    | j                  |d      }| j                  |d      }|j                  ||       y )Nr   r,   )
safe_floatstore)r&   booksidedeltapriceamounts        r(   handle_deltazbittrade.handle_deltad  s0    q)*uf%r)   c                 b    t        dt        |            D ]  }| j                  |||           y )Nr   )rc   rd   r   )r&   r   deltasrh   s       r(   handle_deltaszbittrade.handle_deltasi  s0    q#f+& 	3Ahq	2	3r)   c                    | j                  |di       }| j                  |d      }| j                  |d      }||d   k  r||d   kD  r| j                  |dg       }| j                  |dg       }| j                  |d   |       | j                  |d   |       ||d<   | j                  |d      }	|	|d<   | j                  |	      |d	<   |S )
NrG   r   
prevSeqNumr   asksbidsrJ   rK   rL   )rM   r.   r   rQ   )
r&   rE   rT   r   rG   r   r   r   r   rK   s
             r(   r   z"bittrade.handle_order_book_messagem  s    * w3""42&&t\:
)G,,6Ig<N3N??44D??44Dy0$7y0$7!'Ig))'48I%.Ik"$(LL$;Ij!r)   c                 ^   | j                  |d      }| j                  |d      }|j                  d      }| j                  |d      }| j                  |      }| j                  |   }|d   |j
                  j                  |       y | j                  |||       |j                  ||       y )NrH   rI   r,   r   )	r=   rM   rN   safe_symbolr   r   rf   r   rS   )	r&   rE   rT   r9   rH   rU   rV   r1   r   s	            r(   handle_order_bookzbittrade.handle_order_book  s    . &&w5__Wd+##E1-!!(+OOF+	W%OO""7+**67IFNN9k2r)   c                     | j                  |d      }| j                  |d      }|| j                  v r| j                  |= | j                  i |      | j                  |<   | j	                  | j
                  |||       y )Nr1   rZ   )r=   r.   r   
order_bookspawnr   )r&   rE   rT   rC   r1   rZ   s         r(   r~   z'bittrade.handle_order_book_subscription  so    !!,9!!,8T__$'"&//"e"<

41167LQr)   c                     | j                  |d      }| j                  |j                  d      }| j                  ||      }|9| j                  |d      }|
 ||||      S ||j                  v r|j                  |= |S )Nr4   r}   )r=   index_byr   rM   )r&   rE   rT   r4   subscriptionsByIdrC   r}   s          r(   handle_subscription_statusz#bittrade.handle_subscription_status  s     gt, MM&*>*>E'8"=#__\8<F!fg|<<V)))((,r)   c                     |S N r&   rE   rT   s      r(   handle_system_statuszbittrade.handle_system_status  s	     r)   c                 8   | j                  |d      }|j                  d      }| j                  |d      }|dk(  r`| j                  |d      }| j                  | j                  | j
                  | j                  d}| j                  ||      }|
 |||       y y y )NrH   rI   r   r<      )mbpdetailri   kline)rM   rN   r=   r   rX   rj   ry   )	r&   rE   rT   rH   rU   type
methodNamemethodsr}   s	            r(   handle_subjectzbittrade.handle_subject  s    * __Wd+q)8))%3J--,,++**	G __Wj9F!vw' " r)   c                 f   K   |j                  d| j                  |d      i       d {    y 7 w)Npongping)sendr.   r   s      r(   r   zbittrade.pong  s,      kk64#4#4Wf#EFGGGs   '1/1c                 >    | j                  | j                  ||       y r   )r   r   r   s      r(   handle_pingzbittrade.handle_ping	  s    

499fg.r)   c                    | j                  |d      }|dk(  r| j                  |d      }| j                  |j                  d      }| j                  ||      }|C| j                  |d      }	 | j	                  | j
                  d   || j                  |             yy|S # t        $ r[}| j                  |d      }	|j                  ||	       |j                  ||       ||j                  v r|j                  |= Y d }~yd }~ww xY w)Nstatuserrorr4   zerr-codeexactr9   F)	r=   r   r   rM   throw_exactly_matched_exception
exceptionsjsonr   r   )
r&   rE   rT   r   r4   r   rC   	errorCoder   r9   s
             r(   handle_error_messagezbittrade.handle_error_message  s    !!'84W!!'40B $f.B.BD I??+<bAL' ,,WjA	5889QS\^b^g^gho^pq  ! 5"&"2"2<"OKMM![1MM!R(V111"0045s   -/B   	D)AC??Dc                    | j                  ||      rp| j                  |d      | j                  ||       y | j                  |d      | j                  ||       y | j                  |d      | j	                  ||       y y y )Nr4   rH   r   )r   r=   r   r   r   r   s      r(   handle_messagezbittrade.handle_message(  s    $$VW5 .://@!!'40<##FG4!!'62>  1 ?# 6r)   )$__name__
__module____qualname__r   r%   r0   r/   r   rD   r
   rX   r   r   r	   r`   rj   listrq   ry   r   r   r   r   r   r   r   r   r~   r   r   r   r   r   r   r   __classcell__)r'   s   @r(   r   r      s   # @
 68 k kF k8F @ <@d[] S SS S Sbfglbm SB%F %N 8<$]ajl !H !HC !HWZ !Hquvzq{ !HF!#6 !#F @DB !!S !! !!S\ !!F!/ !/F>&
3! !F!3 !3FRV R *6 #(V #(JH/& /6 82V 2r)   r   )ccxt.async_supportccxt ccxt.async_support.base.ws.cacher   r   ccxt.base.typesr   r   r   r   r	   !ccxt.async_support.base.ws.clientr
   typingr   ccxt.base.errorsr   async_supportr   r   r)   r(   <module>r      s6     N > > 4  *m2t!!** m2r)   