
    iZ                         d dl Zd dlmZmZ d dl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dS )	    N)
ArrayCacheArrayCacheByTimestamp)Int	OrderBookTickerTrade)Client)List)ExchangeErrorc            
       `    e Zd Z 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 )huobijpc                     |                      t          t          |                                           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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    V/var/www/html/crypto-bot/backend/venv/lib/python3.11/site-packages/ccxt/pro/huobijp.pyr"   zhuobijp.describe   s    gt 4 4 = = ? ?"& %## %"  &;'?   	  $"d	 )B
 B
   	    c                     |                      |                     | j        dd          d          }|| j        d<   t          |          S )N	requestIdr      )sumsafe_integerr   str)r#   r(   s     r%   
request_idzhuobijp.request_id/   sA    HHT..t|[!LLaPP	$-[!9~~r&   symbolreturnc                   K   |                                   d{V  |                     |          }|d         }d|d         z   dz   }|                     | j        dd          }d| j        i}|                     | j        d         d         |         d	         |          }|                                 }||d
}	||||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>`
        Nr.   market.idz.detailr   hostnamer   r   subr2   r2   messageHashr.   params)
load_marketsmarketsafe_stringr   r3   implode_paramsr   r-   watchextend)r#   r.   r8   r:   r7   r   r3   urlr(   requestsubscriptions              r%   watch_tickerzhuobijp.watch_ticker4   s#      !!!!!!!!!V$$!&,.:t|UE::.!!$)E"24"8"=h"GRROO%%	
 

 &	
 
 ZZ[$++gv2N2NP[]ijjjjjjjjjr&   clientc                    |                      |di           }|                     |d          }|                    d          }|                     |d          }|                     |          }|                     ||          }|                      |d          }	|	|d<   |                     |	          |d<   |d         }
|| j        |
<   |                    ||           |S )	Ntickch.r)   ts	timestampdatetimer.   )
safe_valuer;   splitsafe_marketparse_tickeriso8601tickersresolve)r#   rC   messagerE   rF   partsmarketIdr:   tickerrI   r.   s              r%   handle_tickerzhuobijp.handle_tickerP   s    $ w33gt,,##E1--!!(++""400OOGT22	'{!\\)44z!%Vvr"""r&   Nsincelimitc                 R  K   |                                   d{V  |                     |          }|d         }d|d         z   dz   }|                     | j        dd          }d| j        i}|                     | j        d         d         |         d	         |          }	|                                 }
||
d
}|
|||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>`
        Nr.   r1   r2   z.trade.detailr   r3   r   r   r4   r6   rI   T)r9   r:   r;   r   r3   r<   r   r-   r=   r>   
newUpdatesgetLimitfilter_by_since_limit)r#   r.   rW   rX   r8   r:   r7   r   r3   r?   r(   r@   rA   tradess                 r%   watch_tradeszhuobijp.watch_tradesp   s[      !!!!!!!!!V$$!&,.@t|UE::.!!$)E"24"8"=h"GRROO%%	
 

 &	
 
 zz#{DKK4P4PR]_kllllllll? 	3OOFE22E))&%TRRRr&   c                    |                      |di           }|                      |di           }|                     |d          }|                    d          }|                     |d          }|                     |          }|d         }	|                      | j        |	          }
|
5|                     | j        dd          }t          |          }
|
| j        |	<   t          d	t          |                    D ]3}| 
                    ||         |          }|
                    |           4|                    |
|           |S )
NrE   datarF   rG   r)   r.   r   r   r   )rK   r;   rL   rM   r]   r+   r   r   rangelenparse_tradeappendrQ   )r#   rC   rR   rE   r`   rF   rS   rT   r:   r.   tradesCacherX   itrades                 r%   handle_tradeszhuobijp.handle_trades   s9   * w33tVR00gt,,##E1--!!(++!oodk6::%%dlM4HHE$U++K"-DKq#d))$$ 	& 	&A$$T!Wf55Eu%%%%{B'''r&   1mc                   K   |                                   d{V  |                     |          }|d         }|                     | j        ||          }d|d         z   dz   |z   }|                     | j        dd          }	d| j        i}
|                     | j        d         d         |	         d	         |
          }|                                 }||d
}|||||d}| 	                    ||| 
                    ||          ||           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
        Nr.   r1   r2   z.kline.r   r3   r   r   r4   )r2   r7   r.   	timeframer8   r   T)r9   r:   r;   
timeframesr   r3   r<   r   r-   r=   r>   rZ   r[   r\   )r#   r.   rk   rW   rX   r8   r:   intervalr7   r   r3   r?   r(   r@   rA   ohlcvs                   r%   watch_ohlcvzhuobijp.watch_ohlcv   s|      !!!!!!!!!V$$!##DOY	JJ&,.:XEt|UE::.!!$)E"24"8"=h"GRROO%%	
 

 &"
 
 jjk4;;w3O3OQ\^jkkkkkkkk? 	2NN6511E))%q$GGGr&   c                    |                      |d          }|                    d          }|                      |d          }|                     |          }|d         }|                      |d          }|                     |          }	|                     | j        |i           | j        |<   |                     | j        |         |	          }
|
;|                     | j        dd          }t          |          }
|
| j        |         |	<   |                     |d          }| 	                    ||          }|

                    |           |                    |
|           d S )	NrF   rG   r)   r.      r   r   rE   )r;   rL   rM   find_timeframerK   ohlcvsr+   r   r   parse_ohlcvrd   rQ   )r#   rC   rR   rF   rS   rT   r:   r.   rm   rk   storedrX   rE   parseds                 r%   handle_ohlcvzhuobijp.handle_ohlcv   sJ   " gt,,##E1--!!(++!##E1--''11	"oodk62FFFV!4i@@>%%dlL$GGE*511F-3DK	*w//!!$//fvr"""""r&   c                   K   ||dk    rt          | j        dz             |                                  d{V  |                     |          }|d         }|dn|}d|d         z   dz   t	          |          z   }|                     | j        dd          }d	| j        i}|                     | j	        d         d
         |         d         |          }| 
                                }	||	d}
|	||||| j        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
        N   z( watchOrderBook accepts limit = 150 onlyr.   r1   r2   z.mbp.r   r3   r   r   r4   r2   r7   r.   rX   r8   method)r   r2   r9   r:   r,   r;   r   r3   r<   r   r-   handle_order_book_subscriptionr=   r>   rX   )r#   r.   rX   r8   r:   r7   r   r3   r?   r(   r@   rA   	orderbooks                r%   watch_order_bookzhuobijp.watch_order_book   sw      ESLL*T TUUU!!!!!!!!!V$$!E&,.83u::Et|UE::.!!$)E"24"8"=h"GRROO%%	
 

 &9
 
 **S+t{{7F7S7SU`bnoooooooo	   r&   c                    |                      |d          }|                      |d          }| j        |         }|                     |d          }|                     ||          }|                     |d          |d<   |                    |           |j        }	t          dt          |	                    D ]}
| 	                    ||	|
         |            || j        |<   |
                    ||           d S )Nr.   r7   r`   seqNumnoncer   )r;   
orderbooksrK   parse_order_bookr+   resetcachera   rb   handle_order_book_messagerQ   )r#   rC   rR   rA   r.   r7   r}   r`   snapshotmessagesrf   s              r%   handle_order_book_snapshotz"huobijp.handle_order_book_snapshot!  s    * !!,99&&|]CCOF+	w//((v66 --dH==!!!?q#h--(( 	K 	KA**68A;	JJJJ"+y+.....r&   c                   K   |                      |d          }	 |                      |d          }|                     |d          }|                     |d          }|                      | j        dd          }d| j        i}	|                     | j        d         d         |         d         |	          }
|                                 }||d	}|||||| j        d
}| 	                    |
||||           d {V }|
                                S # t          $ r(}|j        |= |                    ||           Y d }~nd }~ww xY wd S )Nr7   r.   rX   r8   r   r3   r   r   )reqr2   rz   )r;   r+   rK   r   r3   r<   r   r-   r   r=   rX   	Exceptionsubscriptionsreject)r#   rC   rR   rA   r7   r.   rX   r8   r   r3   r?   r(   r@   snapshotSubscriptionr}   es                   r%   watch_order_book_snapshotz!huobijp.watch_order_book_snapshotD  s     &&|]CC	*%%lH==F%%lG<<E__\8<<F""4<>>C"DM2H%%di&6t&<S&A(&KXVVC))I" G  *  9$ $  #jji)MabbbbbbbbI??$$$ 	* 	* 	*$[1MM![))))))))	* ts   C3D 
E D;;E c                     |                      |d          }|                      |d          }|                    ||           d S )Nr   r)   )
safe_floatstore)r#   booksidedeltapriceamounts        r%   handle_deltazhuobijp.handle_deltac  sB    q))**uf%%%%%r&   c                 ~    t          dt          |                    D ]}|                     |||                    d S )Nr   )ra   rb   r   )r#   r   deltasrf   s       r%   handle_deltaszhuobijp.handle_deltash  sH    q#f++&& 	3 	3Ahq	2222	3 	3r&   c                    |                      |di           }|                     |d          }|                     |d          }||d         k    r||d         k    r|                      |dg           }|                      |dg           }|                     |d         |           |                     |d         |           ||d<   |                     |d          }	|	|d<   |                     |	          |d	<   |S )
NrE   r   
prevSeqNumr   asksbidsrH   rI   rJ   )rK   r+   r   rO   )
r#   rC   rR   r}   rE   r   r   r   r   rI   s
             r%   r   z!huobijp.handle_order_book_messagel  s   * w33""422&&t\::
)G,,,6Ig<N3N3N??444D??444Dy0$777y0$777!'Ig))'488I%.Ik"$(LL$;$;Ij!r&   c                    |                      |d          }|                     |d          }|                    d          }|                      |d          }|                     |          }| j        |         }|d         |j                            |           d S |                     |||           |                    ||           d S )NrF   rG   r)   r   )	r;   rK   rL   safe_symbolr   r   rd   r   rQ   )	r#   rC   rR   r7   rF   rS   rT   r.   r}   s	            r%   handle_order_bookzhuobijp.handle_order_book  s    . &&w55__Wd++##E1--!!(++OF+	W%O""7+++++**67IFFFNN9k22222r&   c                     |                      |d          }|                     |d          }|| j        v r| j        |= |                     i |          | j        |<   |                     | j        |||           d S )Nr.   rX   )r;   r+   r   
order_bookspawnr   )r#   rC   rR   rA   r.   rX   s         r%   r|   z&huobijp.handle_order_book_subscription  s~    !!,99!!,88T_$$'"&//"e"<"<

4167LQQQQQr&   c                    |                      |d          }|                     |j        d          }|                     ||          }|6|                     |d          }| ||||          S ||j        v r|j        |= |S )Nr2   r{   )r;   index_byr   rK   )r#   rC   rR   r2   subscriptionsByIdrA   r{   s          r%   handle_subscription_statusz"huobijp.handle_subscription_status  s     gt,, MM&*>EE'8"==#__\8<<F!vfg|<<<V)))(,r&   c                     |S N r#   rC   rR   s      r%   handle_system_statuszhuobijp.handle_system_status  s	     r&   c                 F   |                      |d          }|                    d          }|                     |d          }|dk    rW|                     |d          }| j        | j        | j        | j        d}|                      ||          }| |||           d S d S d S )NrF   rG   r   r:      )mbpdetailrg   kline)rK   rL   r;   r   rV   rh   rw   )	r#   rC   rR   rF   rS   type
methodNamemethodsr{   s	            r%   handle_subjectzhuobijp.handle_subject  s    * __Wd++q))8))%33J-,+*	 G __Wj99F!vw'''''  "!r&   c                 l   K   |                     d|                     |d          i           d {V  d S )Npongping)sendr+   r   s      r%   r   zhuobijp.pong  sG       kk64#4#4Wf#E#EFGGGGGGGGGGGr&   c                 >    |                      | j        ||           d S r   )r   r   r   s      r%   handle_pingzhuobijp.handle_ping  s     

49fg.....r&   c                 D   |                      |d          }|dk    r|                      |d          }|                     |j        d          }|                     ||          }||                      |d          }	 |                     | j        d         ||                     |                     nj# t          $ r]}|                      |d          }	|                    ||	           |                    ||           ||j        v r|j        |= Y d }~nd }~ww xY wdS |S )Nstatuserrorr2   zerr-codeexactr7   F)	r;   r   r   rK   throw_exactly_matched_exception
exceptionsjsonr   r   )
r#   rC   rR   r   r2   r   rA   	errorCoder   r7   s
             r%   handle_error_messagezhuobijp.handle_error_message  s=    !!'844W!!'400B $f.BD I I??+<bAAL' ,,WjAA	5889QS\^b^g^gho^p^pqqqq  5 5 5"&"2"2<"O"OKMM![111MM!R(((V111"045 5s   >5B4 4
D>ADDc                 J   |                      ||          r|                     |d          |                     ||           d S |                     |d          |                     ||           d S |                     |d          |                     ||           d S d S d S )Nr2   rF   r   )r   r;   r   r   r   r   s      r%   handle_messagezhuobijp.handle_message'  s    $$VW55 	2 ..://@@@@@!!'400<##FG44444!!'622>  11111%	2 	2" ?>r&   )#__name__
__module____qualname__r"   r-   r,   r   rB   r	   rV   r   r
   r   r^   rh   listro   rw   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 kF k k k k8F    @ <@d[] S S SS S Sbfglbm S S S SB%F % % % %N 8<$]ajl !H !H !HC !HWZ !Hquvzq{ !H !H !H !HF!#6 !# !# !# !#F @DB !! !!S !! !!S\ !! !! !! !!F!/ !/ !/ !/ !/F  >& & &
3 3 3! ! ! ! !F!3 !3 !3 !3 !3FRV R R R R    *6    #(V #( #( #( #(JH H H/& / / / /6    82V 2 2 2 2 2 2 2 2r&   r   )ccxt.async_supportccxt ccxt.async_support.base.ws.cacher   r   ccxt.base.typesr   r   r   r   !ccxt.async_support.base.ws.clientr	   typingr
   ccxt.base.errorsr   async_supportr   r   r&   r%   <module>r      s        N N N N N N N N 9 9 9 9 9 9 9 9 9 9 9 9 4 4 4 4 4 4       * * * * * *l2 l2 l2 l2 l2d ( l2 l2 l2 l2 l2r&   