
    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  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)AuthenticationError)NotSupportedc                       e Zd Z fdZd Zdi fde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i fdededefdZdefdZd Zd Zi fdedefdZdi fdedefdZdefdZd5d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fdZd5dZdddi fdedededee         fdZ defdZ!d5d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&d5d#Z'd5d$Z(i fde)fd%Z*di fded&e+d'e,d(e-d)e.de#fd*Z/di fd+edede#fd,Z0di fdefd-Z1dddi fdedededee#         fd.Z2defd/Z3defd0Z4defd1Z5defd2Z6defd3Z7defd4Z8 xZ9S )6hitbtcc                     |                      t          t          |                                           ddddddddddddddddddiddd	did
dddiddiddidddddddddddd
ddid          S )NTF)wswatchTickerwatchTickerswatchTradeswatchOrderBookwatchBalancewatchOrders
watchOHLCVwatchMyTradescreateOrderWscancelOrderWsfetchOpenOrdersWscancelAllOrdersWsr   z$wss://api.hitbtc.com/api/3/ws/publicz%wss://api.hitbtc.com/api/3/ws/trading)publicprivatez)wss://api.demo.hitbtc.com/api/3/ws/publicz*wss://api.demo.hitbtc.com/api/3/ws/trading)apitest  methodticker/{speed}orderbook/full)tradesLimitr   r   r   M1M3M5M15M30H1H4D1D71M)
1m3m5m15m30m1h4h1d1wr9   	keepAlivei  )hasurlsoptions
timeframes	streaming)deep_extendsuperr   describe)self	__class__s    U/var/www/html/crypto-bot/backend/venv/lib/python3.11/site-packages/ccxt/pro/hitbtc.pyrK   zhitbtc.describe   s   fd 3 3 < < > ># $#"& $#"!&!%!%%)%) " "H#J  "M#O    $.  .! .#    Tm9A
 9A
 9 9 9	    c                 8  K   |                                   | j        d         d         d         }d}|                     |          }|                    |          }|                     |j        |          }||                                 }|                     |                     | 	                    |                    |                     | j
                  t          j        d          }dd| j        ||d	d
}|                     ||||           | d{V S )z
         * @ignore
        authenticates the user to access private web socket channels
        :see: https://api.hitbtc.com/#socket-authentication
        :returns dict: response from exchange
        r)   r   r(   authenticatedNhexloginHS256)typeapi_key	timestamp	signature)r,   params)check_required_credentialsrE   clientfuture
safe_valuesubscriptionsmillisecondshmacencodenumber_to_stringsecrethashlibsha256apiKeywatch)	rL   urlmessageHashr[   r\   rQ   rW   rX   requests	            rN   authenticatezhitbtc.authenticateO   s,      	'')))it$Y/%S!!{++(<kJJ ))++I		$++d.C.CI.N.N"O"OQUQ\Q\]a]hQiQikrky  |A  B  BI!##{!*!*	  G JJsK+>>>$ ||||||rO   NnamemessageHashPrefixsymbolsc                 B  K   |                                   d{V  | j        d         d         d         }|}||dz   d                    |          z   }d|                                 |d}|                     ||          }|                     ||||           d{V S )	z
         * @ignore
        :param str name: websocket endpoint name
        :param str[] [symbols]: unified CCXT symbol(s)
        :param dict [params]: extra parameters specific to the hitbtc api
        Nr)   r   r'   ::,	subscribe)r,   idch)load_marketsrE   joinnonceextendrg   )	rL   rl   rm   rn   rY   rh   ri   rr   rj   s	            rN   subscribe_publiczhitbtc.subscribe_public|   s       !!!!!!!!!it$X.'%,sxx/@/@@K!**,,
 
	
 ++i00ZZ[';GGGGGGGGGrO   symbolc                 v  K   |                                   d{V  |                                  d{V  | j        d         d         d         }|                    d          }|                     |d          }||dz   |z   }|||                                 d}|                     ||||           d{V S )	
         * @ignore
        :param str name: websocket endpoint name
        :param str [symbol]: unified CCXT symbol
        :param dict [params]: extra parameters specific to the hitbtc api
        Nr)   r   r(   
_subscriber   rp   r,   rY   rs   )ru   rk   rE   splitsafe_stringrw   rg   )rL   rl   rz   rY   rh   	splitNameri   rr   s           rN   subscribe_privatezhitbtc.subscribe_private   s       !!!!!!!!!!!!!!!!!!it$Y/JJ|,,	&&y!44%,v5K**,,
 
	
 ZZ[)[IIIIIIIIIrO   c                 *  K   |                                   d{V  |                                  d{V  | j        d         d         d         }t          |                                           }|||d}|                     ||||           d{V S )r|   Nr)   r   r(   r~   )ru   rk   rE   strrw   rg   )rL   rl   rY   rh   ri   rr   s         rN   trade_requestzhitbtc.trade_request   s       !!!!!!!!!!!!!!!!!!it$Y/$**,,''
 
	
 ZZ[)[IIIIIIIIIrO   limitreturnc           	      V  K   |                      | j        d          }|                     |dd          }|                     |dd|          }|                     |dd          }|                     |dd          }|dk    rd	|z   d
z   |z   dz   }n2|dk    rd	|z   d
z   |z   dz   }n|dk    r	d|z   dz   }n|dk    rd|z   dz   }|                     |          }	dd|	d         gii}
|                     |d|g|                     |
|                     d{V }|                                S )a~  
        watches information on open orders with bid(buy) and ask(sell) prices, volumes and other data
        :see: https://api.hitbtc.com/#subscribe-to-full-order-book
        :see: https://api.hitbtc.com/#subscribe-to-partial-order-book
        :see: https://api.hitbtc.com/#subscribe-to-partial-order-book-in-batches
        :see: https://api.hitbtc.com/#subscribe-to-top-of-book
        :see: https://api.hitbtc.com/#subscribe-to-top-of-book-in-batches
        :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
        :param str [params.method]: 'orderbook/full', 'orderbook/{depth}/{speed}', 'orderbook/{depth}/{speed}/batch', 'orderbook/top/{speed}', or 'orderbook/top/{speed}/batch'
        :param int [params.depth]: 5 , 10, or 20(default)
        :param int [params.speed]: 100(default), 500, or 1000
        :returns dict: A dictionary of `order book structures <https://docs.ccxt.com/#/?id=order-book-structure>` indexed by market symbols
        r   r,   r.   defaultMethoddepth20100zorderbook/{depth}/{speed}zorderbook/D/mszorderbook/{depth}/{speed}/batchzms/batchzorderbook/top/{speed}zorderbook/top/zorderbook/top/{speed}/batchrY   rn   rs   
orderbooksN)r]   rF   r   safe_string_2marketry   rI   r   )rL   rz   r   rY   rF   r   rl   r   speedr   rj   	orderbooks               rN   watch_order_bookzhitbtc.watch_order_book   s       //$,0@AA(((<LMM!!&(O]SS  $77  %88... 5(3.6=DD666 5(3.6CDD,,,#e+d2DD222#e+j8DV$$F4L>

 //lVHdN^N^_fhnNoNopppppppp	   rO   r[   c                 H   |                      |d          }|                      |d          }|r|n|}|rdnd}t          |                                          }t          dt	          |                    D ]}||         }	|                     |	          }
|
d         }||	         }d|z   }|| j        vrP|                      |j        |i           }|                     |d          }| 	                    i |          | j        |<   | j        |         }|                     |d          }|                     |d          }|dk    r/| 
                    |||d	d
          }|                    |           nf|                     |d
g           }|                     |d	g           }|                     |d         |           |                     |d         |           ||d<   |                     |          |d<   ||d<   ||d<   || j        |<   |                    ||           d S )Nsnapshotupdater   rz   zorderbooks::r   tsbaasksbidsrW   datetimerw   )	safe_dictlistkeysrangelensafe_marketr   r^   safe_integer
order_bookparse_order_bookreset	safe_listhandle_deltasiso8601resolve)rL   r[   messager   r   datarU   	marketIdsimarketIdr   rz   itemri   subscriptionr   r   rW   rw   parsedSnapshotr   r   s                         rN   handle_order_bookzhitbtc.handle_order_book   s<   0 >>':6622#/xx%3zz8%%	q#i..)) 	3 	3A |H%%h//FH%F>D(61Kdo--#~~f.BKQSTT)),@@*.//"e*D*D'/I))$44I%%dC00Ez!!!%!6!6tVYPSUX!Y!Y////~~dC44~~dC44""9V#4d;;;""9V#4d;;;%.Ik"$(LL$;$;Ij!!&Ig"(Ih&/DOF#NN9k22225	3 	3rO   c                     |                      |d          }|                      |d          }|                    ||           d S )Nr      )safe_numberstore)rL   booksidedeltapriceamounts        rN   handle_deltazhitbtc.handle_delta  sF      **!!%++uf%%%%%rO   c                 ~    t          dt          |                    D ]}|                     |||                    d S )Nr   )r   r   r   )rL   r   deltasr   s       rN   r   zhitbtc.handle_deltas  sH    q#f++&& 	3 	3Ahq	2222	3 	3rO   c           	        K   |                      | j        d          }|                     |dd          }|                     |dd|          }|                     |dd          }|                     |d|i          }|                     |ddg          }|                     |          }dd|d	         gii}	|                     |d
|g|                     |	|                     d{V }
|                      |
|          S )a+  
        watches a price ticker, a statistical calculation with the information calculated over the past 24 hours for a specific market
        :see: https://api.hitbtc.com/#subscribe-to-ticker
        :see: https://api.hitbtc.com/#subscribe-to-ticker-in-batches
        :see: https://api.hitbtc.com/#subscribe-to-mini-ticker
        :see: https://api.hitbtc.com/#subscribe-to-mini-ticker-in-batches
        :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]: 'ticker/{speed}'(default), or 'ticker/price/{speed}'
        :param str [params.speed]: '1s'(default), or '3s'
        :returns dict: a `ticker structure <https://docs.ccxt.com/#/?id=ticker-structure>`
        r   r,   r-   r   r   1srY   rn   rs   tickersN)	r]   rF   r   r   implode_paramsomitr   ry   rI   )rL   rz   rY   rF   r   r,   r   rl   r   rj   results              rN   watch_tickerzhitbtc.watch_ticker  s      //$,>>(((<LMM##FHo}UU  $77""6GU+;<<6Hg#677V$$F4L>

 ,,T9vhHXHXY`bhHiHijjjjjjjjvv...rO   c           	        K   |                                   d{V  |                     | j        d          }|                     |dd          }|                     |dd|          }|                     |dd          }|                     |d|i          }|                     |ddg          }g }||                    d           nPt          d	t          |                    D ]2}	| 
                    ||	                   }
|                    |
           3d
d|ii}|                     |d||                     ||                     d{V }| j        r|S |                     | j        d|          S )ai  
        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']: 'ticker/{speed}'(default),'ticker/price/{speed}', 'ticker/{speed}/batch', or 'ticker/{speed}/price/batch''
        :param str params['speed']: '1s'(default), or '3s'
        :returns dict: a `ticker structure <https://docs.ccxt.com/en/latest/manual.html#ticker-structure>`
        Nr   r,   r-   r   r   r   *r   rY   rn   r   rz   )ru   r]   rF   r   r   r   r   appendr   r   	market_idry   rI   
newUpdatesfilter_by_arrayr   )rL   rn   rY   rF   r   r,   r   rl   r   r   r   rj   r   s                rN   watch_tickerszhitbtc.watch_tickers;  s      !!!!!!!!!//$,>>(((<LMM##FHo}UU  $77""6GU+;<<6Hg#677	?S!!!!1c'll++ + +>>'!*55  ****9

 --dIwHXHXY`bhHiHijjjjjjjj? 	N##DL(GDDDrO   c                 ,   |                      |di           }t          |                                          }i }t          dt	          |                    D ]R}||         }|                     |          }|d         }	|                     ||         |          }
|
| j        |	<   |
||	<   S|                    |d           | 	                    |d          }t          dt	          |                    D ]}||         }|
                    d          }|d         }|
                    d          }|                     |d|          }t          |                                          }t	          |          }|dk    r|                    ||           |S )	Nr   r   rz   r   z	tickers::rp   r   rq   )r]   r   r   r   r   r   parse_ws_tickerr   r   find_message_hashesr   r   )rL   r[   r   r   r   
newTickersr   r   r   rz   tickermessageHashesri   partssymbolsStringrn   r   tickersSymbols
numTickerss                      rN   handle_tickerzhitbtc.handle_ticker\  s   N w33%%	
q#i..)) 	( 	(A |H%%h//FH%F))$x.&AAF#)DL !'Jvz9---00EEq#m,,-- 		5 		5A'*K%%d++E!!HM#))#..G**:xIIG!',,..11N^,,JA~~w444rO   c                    |                      |d          }|                     d |          }|                     |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|          S )Nr   crz   rW   r   highhlowlbidr   	bidVolumeBaskr   	askVolumeAvwapopenocloselastpreviousClosechange
percentageaveragevq)
baseVolumequoteVolumeinfo)r   safe_symbolr   safe_tickerr   )rL   r   r   rW   rz   r   s         rN   r   zhitbtc.parse_ws_ticker  s   : %%fc22	!!$//,, !
f!
!
 Y//!
 D$$VS11	!

 4##FC00!
 4##FC00!
 ))&#66!
 4##FC00!
 ))&#66!
 D!
 D$$VS11!
 T!
 D!
 T!
 d!
  $!!
" t#!
$ **6377++FC88)!
 !
 !
* +  	rO   sincec           	      `  K   |                                   d{V  |                     |          }dd|d         gii}|||d<   d}|                     |d|g|                     ||                     d{V }| j        r|                    ||          }|                     |||d          S )a  
        get the list of most recent trades for a particular symbol
        :see: https://api.hitbtc.com/#subscribe-to-trades
        :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>`
        NrY   rn   rs   r   tradesrW   )ru   r   ry   rI   r   getLimitfilter_by_since_limit)	rL   rz   r   r   rY   r   rj   rl   r   s	            rN   watch_tradeszhitbtc.watch_trades  s       !!!!!!!!!V$$F4L>

 $GG,,T8fXtGWGWX_agGhGhiiiiiiii? 	3OOFE22E))&%LLLrO   c                    |                      |ddi           }t          |                                          }t          dt	          |                    D ]}||         }|                     |          }|                     | j        dd          }|d         }	|                     | j	        |	          }
|
t          |          }
|
| j	        |	<   |                     ||         |          }t          dt	          |                    D ]}|
                    ||                    d|	z   }|                    |
|           |S )Nr   r   r   r/   r+   rz   ztrades::)safe_value_2r   r   r   r   r   r   rF   r]   r   r   parse_ws_tradesr   r   )rL   r[   r   r   r   r   r   r   r/   rz   storedr   jri   s                 rN   handle_tradeszhitbtc.handle_trades  s;   P   *hCC%%	q#i..)) 	0 	0A |H%%h//F++DL-NNKH%F__T[&99F~#K00&,F#))$x.&AAF1c&kk** ) )fQi(((($v-KNN6;////rO   r   c                    |                      |          }g }t          dt          |                    D ]G}|                     |                     ||         |          |          }|                    |           H|                     |dd          }|                     |d          }	|                     ||	||          S )Nr   rW   rs   rz   )	to_arrayr   r   rx   parse_ws_trader   	sort_by_2r   filter_by_symbol_since_limit)
rL   r   r   r   r   rY   r   r   traderz   s
             rN   r   zhitbtc.parse_ws_trades%  s    v&&q#f++&& 	! 	!AKK 3 3F1Iv F FOOEMM%    T::!!&(3300NNNrO   c                 d   |                      |d          }|                     ||                     |d          d ||                     |          |                     |d          d |                     |d          d |                     |d          |                     |d          d d d|          S )Nr   r   rz   r   pr   r   rs   orderrW   r   rz   rU   sidetakerOrMakerr   r   costfee)r   
safe_trader   r   )rL   r  r   rW   s       rN   r  zhitbtc.parse_ws_trade/  s     %%eS11	""5#.."Y//&&vx88$$UC00 %%eS11&&uc22 
  
   	rO   r:   c           	      v  K   |                      | j        ||          }d|z   }|                     |          }dd|d         gii}	|||	d         d<   |                     |d|g|                     |	|                     d{V }
| j        r|
                    ||          }|                     |
||d          S )	u  
        watches historical candlestick data containing the open, high, low, and close price, and the volume of a market
        :see: https://api.hitbtc.com/#subscribe-to-candles
        :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]: not used by hitbtc watchOHLCV
        :param int [limit]: 0 – 1000, default value = 0(no history returned)
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns int[][]: A list of candles ordered, open, high, low, close, volume
        zcandles/rY   rn   rs   Nr   candlesr   )r   rG   r   ry   rI   r   r   r   )rL   rz   	timeframer   r   rY   periodrl   r   rj   ohlcvs              rN   watch_ohlcvzhitbtc.watch_ohlcvJ  s       !!$/9iHHF"V$$F4L>

 ).GHg&++D)fXtGWGWX_agGhGhiiiiiiii? 	2NN6511E))%qAAArO   c                    |                      |ddi           }t          |                                          }|                     |d          }|                    d          }|                     |d          }|                     |          }t          dt          |                    D ]}	||	         }
|                     |
          }|d         }| 	                    | j
        |i           | j
        |<   | 	                    | j
        |         |          }|;|                     | j        dd	          }t          |          }|| j
        |         |<   |                     ||
         |          }t          dt          |                    D ]}|                    ||                    d
|z   }|                    ||           |S )Nr   r   rt   r   r   r   rz   
OHLCVLimitr+   z	candles::)r   r   r   r   r   find_timeframer   r   r   r]   ohlcvsr   rF   r   parse_ws_ohlcvsr   r   )rL   r[   r   r   r   channelsplitChannelr  r  r   r   r   rz   r   r   r  r   ri   s                     rN   handle_ohlcvzhitbtc.handle_ohlcvd  s   D   *hCC%%	""7D11}}S))!!,22''//	q#i..)) 	0 	0A |H%%h//FH%F"&//$+vr"J"JDK__T[%8)DDF~))$,dKK.u5517F#I.))$x.&AAF1c&kk** ) )fQi((((%.KNN6;////rO   c           	         |                      |d          |                     |d          |                     |d          |                     |d          |                     |d          |                     |d          gS )Nr   r   r   r   r   r   )r   r   )rL   r  r   s      rN   parse_ws_ohlcvzhitbtc.parse_ws_ohlcv  s     eS))UC((UC((UC((UC((UC((
 	
rO   c                 ~  K   |                                   d{V  d}d}||                     |          }|                     d||          \  }}|                     |ddddd          }|                     |||           d{V }| j        r|                    ||          }|                     |||d          S )a  
        watches information on multiple orders made by the user
        :see: https://api.hitbtc.com/#subscribe-to-reports
        :see: https://api.hitbtc.com/#subscribe-to-reports-2
        :see: https://api.hitbtc.com/#subscribe-to-reports-3
        :param str [symbol]: unified CCXT market symbol
        :param int [since]: timestamp in ms of the earliest order to fetch
        :param int [limit]: the maximum amount of orders to fetch
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict[]: a list of `order structures <https://docs.ccxt.com/en/latest/manual.html#order-structure>`
        Nr    spot_subscribemargin_subscribefutures_subscribe)spotmarginswapr\   rW   )ru   r   handle_market_type_and_paramsget_supported_mappingr   r   r   r   )	rL   rz   r   r   rY   
marketTyper   rl   orderss	            rN   watch_orderszhitbtc.watch_orders  s       !!!!!!!!!
[[((F!??vW]^^
F))*$(')	7
 7
   --dFFCCCCCCCC? 	3OOFE22E))&%LLLrO   c                 x   | j         /|                     | j        d          }t          |          | _         |                     |dg           }t          |t                    r@t          dt          |                    D ]!}||         }| 	                    |||           "n| 	                    |||           |S )NordersLimitrY   r   )
r+  r   rF   r   r]   
isinstancer   r   r   handle_order_helper)rL   r[   r   r   r   r   r  s          rN   handle_orderzhitbtc.handle_order  s    | ;%%dlMBBE077DKw"55dD!! 	<1c$ii(( A AQ((%@@@@A $$VWd;;;rO   c                    | j         }|                     |dd          }|                     |d          }|                    d          }|                     |d          }|                     |          }	|                     |          }
|                    |
           |                    ||           |                    ||dz   |	z              d S )N
instrumentrz   r,   _orderr   rp   )r+  safe_string_lower_2r   r   r   parse_orderr   r   )rL   r[   r   r  r+  r   r,   splitMethodri   rz   parseds              rN   r0  zhitbtc.handle_order_helper  s    ++E<JJ!!'844ll8,,&&{A66!!(++!!%((fv{+++v{T1F:;;;;;rO   c                    |                      |d          }|                     |d          }|                     ||                     |d          |                     |d          ||                     |          |                     ||          d |                     |d          |                     |d          |                     |d          |                     |d          d |                     |d	          d d d
d|          S )N
created_atrz   trade_idrs   r  trade_takertrade_pricetrade_quantity	trade_fee)r  currencyrater
  )r   r   r  r   r   )rL   r  r   rW   r   s        rN   parse_ws_order_tradezhitbtc.parse_ws_order_trade%  s   6 %%e\::	##E844""5*55%%eT22"Y//&&x88$$UF33 ,,UMBB%%e];;&&u.>??((<<   
  
$ %  	rO   c                    |                      |d          }|                      |d          }|                     ||          }|                      |d          }d }||                     ||          }|g}|                      |d          }|                      |d          }	d }
|	dk    r|                     |	          }
n|                     |          }
|                     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|          S )Nr:  rz   r;  statusreport_typecanceledr   rs   clientOrderIdclient_order_idrW   r   lastTradeTimestampr   r   quantityrU   r  timeInForcetime_in_forcepostOnly	post_only
reduceOnlyreduce_onlyfilled	remainingr  )rD  r   r   r  )r   r   rB  parse_order_status
safe_orderr   safe_string_upperr]   )rL   r  r   rW   r   tradeIdr   r  	rawStatusrE  parsedStatuss              rN   parse_ws_orderzhitbtc.parse_ws_orderV  s   6 $$UL99	##E844!!(F33""5*55--eV<<EWF$$UH55	&&um<<*$$22;??LL229==L  
E 
$""5$// 
 T--e5FGG 
 	 

 Y// 
 !$ 
 fX& 
 T%%eW55 
 d&&uj99 
 D$$UF33 
 D**5&99 
 4++E?CC 
 ((<< 
 $//%?? 
 d 
  ! 
" D# 
$ #+ 
  
  
, -  	rO   c                 j  K   |                                   d{V  d}|                     dd|          \  }}|                     |dddd          }|                     |dd          }|                     |d          }d|i}|                     |d|                     ||                     d{V S )a  
        watches balance updates, cannot subscribe to margin account balances
        :see: https://api.hitbtc.com/#subscribe-to-spot-balances
        :see: https://api.hitbtc.com/#subscribe-to-futures-balances
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :param str [params.type]: 'spot', 'swap', or 'future'
         *
         * EXCHANGE SPECIFIC PARAMETERS
        :param str [params.mode]: 'updates' or 'batches'(default), 'updates' = messages arrive after balance updates, 'batches' = messages arrive at equal intervals if there were any updates
        :returns dict[]: a list of `balance structures <https://docs.ccxt.com/#/?id=balance-structure>`
        Nr   spot_balance_subscribefutures_balance_subscribe)r%  r'  r\   modebatches)ru   r(  r)  r   r   r   rx   )rL   rY   rU   rl   r]  rj   s         rN   watch_balancezhitbtc.watch_balance  s       !!!!!!!!!99.$PVWWf))$,/11
 1
  
 	::66**D
 ++D$GV8T8TUUUUUUUUUrO   rU   r  r   r   c           
        K   |                                   d{V  |                     |          }d}d}	|                     d||          \  }	}d}
|                     d|          \  }
}|                     ||	|||||
|          \  }}|                     ||          }|	dk    r|                     d|           d{V S |	dk    s|
|                     d|           d{V S |                     d|           d{V S )a  
        create a trade order
        :see: https://api.hitbtc.com/#create-new-spot-order
        :see: https://api.hitbtc.com/#create-margin-order
        :see: https://api.hitbtc.com/#create-futures-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 exchange API endpoint
        :param str [params.marginMode]: 'cross' or 'isolated' only 'isolated' is supported for spot-margin, swap supports both, default is 'cross'
        :param bool [params.margin]: True for creating a margin order
        :param float [params.triggerPrice]: The price at which a trigger order is triggered at
        :param bool [params.postOnly]: if True, the order will only be posted to the order book and not executed immediately
        :param str [params.timeInForce]: "GTC", "IOC", "FOK", "Day", "GTD"
        :returns dict: an `order structure <https://github.com/ccxt/ccxt/wiki/Manual#order-structure>`
        NcreateOrderr'  futures_new_orderr&  margin_new_orderspot_new_order)ru   r   r(  handle_margin_mode_and_paramscreate_order_requestrx   r   )rL   rz   rU   r  r   r   rY   r   rj   r*  
marginModes              rN   create_order_wszhitbtc.create_order_ws  sU     & !!!!!!!!!V$$
!??vW]^^
F
!??vVV
F33FJdTZ\acmouvv++gv..++,?IIIIIIIIIH$$**@++,>HHHHHHHHH++,<gFFFFFFFFFrO   rs   c                   K   |                                   d{V  d}d|i}||                     |          }d}|                     d||          \  }}|                     d|          \  }}|                     ||          }|dk    r|                     d|           d{V S |dk    s||                     d|           d{V S |                     d|           d{V S )	a  
        :see: https://api.hitbtc.com/#cancel-spot-order-2
        :see: https://api.hitbtc.com/#cancel-futures-order-2
        :see: https://api.hitbtc.com/#cancel-margin-order-2
        cancels an open order
        :param str id: order id
        :param str symbol: unified symbol of the market the order was made in
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :param str [params.marginMode]: 'cross' or 'isolated' only 'isolated' is supported
        :param bool [params.margin]: True for canceling a margin order
        :returns dict: An `order structure <https://docs.ccxt.com/#/?id=order-structure>`
        NrH  r$   r'  futures_cancel_orderr&  margin_cancel_orderspot_cancel_order)ru   r   r(  re  rx   r   )	rL   rs   rz   rY   r   rj   r*  rg  querys	            rN   cancel_order_wszhitbtc.cancel_order_ws  s=      !!!!!!!!!r
 [[((F
!??QWY_``
F >>PVWW
E++gu--++,BGLLLLLLLLLH$$**@++,A7KKKKKKKKK++,?IIIIIIIIIrO   c                   K   |                                   d{V  d}||                     |          }d}|                     d||          \  }}d}|                     d|          \  }}|dk    r|                     d|           d{V S |dk    s|t          | j        dz             |                     d|           d{V S )ay  
        :see: https://api.hitbtc.com/#cancel-spot-orders
        :see: https://api.hitbtc.com/#cancel-futures-order-3
        cancel all open orders
        :param str symbol: unified market symbol, only orders in the market of self symbol are cancelled when symbol is not None
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :param str [params.marginMode]: 'cross' or 'isolated' only 'isolated' is supported
        :param bool [params.margin]: True for canceling margin orders
        :returns dict[]: a list of `order structures <https://docs.ccxt.com/#/?id=order-structure>`
        Nr&   r'  futures_cancel_ordersr&  z5 cancelAllOrdersWs is not supported for margin ordersspot_cancel_orders)ru   r   r(  re  r   r   rs   )rL   rz   rY   r   r*  rg  s         rN   cancel_all_orders_wszhitbtc.cancel_all_orders_ws  s      !!!!!!!!![[((F
!??@SU[]cdd
F
!??@SU[\\
F++,CVLLLLLLLLLH$$**@tw)``aaa++,@&IIIIIIIIIrO   c                   K   |                                   d{V  d}i }| |                     |          }|d         |d<   d}|                     d||          \  }}d}|                     d|          \  }}|dk    r|                     d|           d{V S |dk    s||                     d|           d{V S |                     d	|           d{V S )
a$  
        :see: https://api.hitbtc.com/#get-active-futures-orders-2
        :see: https://api.hitbtc.com/#get-margin-orders
        :see: https://api.hitbtc.com/#get-active-spot-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 exchange API endpoint
        :param str [params.marginMode]: 'cross' or 'isolated' only 'isolated' is supported
        :param bool [params.margin]: True for fetching open margin orders
        :returns Order[]: a list of `order structures <https://docs.ccxt.com/#/?id=order-structure>`
        Nrs   rz   r%   r'  futures_get_ordersr&  margin_get_ordersspot_get_orders)ru   r   r(  re  r   )	rL   rz   r   r   rY   r   rj   r*  rg  s	            rN   fetch_open_orders_wszhitbtc.fetch_open_orders_ws  s6      !!!!!!!!![[((F &tGH
!??@SU[]cdd
F
!??@SU[\\
F++,@'JJJJJJJJJH$$**@++,?IIIIIIIII++,=wGGGGGGGGGrO   c                     |                      |d          }|                     |d          }|                     |          }|                     | j        |          | _        |                    | j        |           d S )Nr,   rY   )r   r]   parse_balancerI   balancer   )rL   r[   r   ri   rY   rz  s         rN   handle_balancezhitbtc.handle_balance.  st      &&w99(33$$V,,''g>>t|[11111rO   c                     |S N )rL   r[   r   s      rN   handle_notificationzhitbtc.handle_notificationD  s	     rO   c                    |                      |d          }|                     |di           }t          |t                    rig }t	          dt          |                    D ]2}|                     ||                   }|                    |           3|                    ||           n+|                     |          }|                    ||           |S )Nrs   r   r   )	r   r]   r/  r   r   r   rY  r   r   )rL   r[   r   ri   r   parsedOrdersr   parsedOrders           rN   handle_order_requestzhitbtc.handle_order_requestJ  s    8 ''66(B77fd## 	5L1c&kk** 1 1"11&)<<##K0000NN<5555--f55KNN;444rO   c                 "   |                      ||           |                     |dd          }||                    d          }|                     |d          }| j        | j        | j        | j        | j        | j        | j        | j        | j        | j        | j	        | j	        d}| 
                    ||          }| |||           d S d S | 
                    |d          }|                     |d          }||                     ||           |du rd	|vr|                     ||           t          |t                    rH| 
                    |di           }	t          |          }
|
dk    sd|	v r|                     ||           d S d S d S )
Nrt   r,   r   r   )r  r   r   r   
spot_orderspot_ordersmargin_ordermargin_ordersfutures_orderfutures_ordersspot_balancefutures_balancer   rH  Trs   )handle_errorr   r   r   r  r   r  r   r1  r{  r]   r  handle_authenticater/  r   r   )rL   r[   r   r  r  methodsr,   r   rG  firstarrayLengths              rN   handle_messagezhitbtc.handle_messages  s   &'***$$WdH=="==--L&&|Q77G,,,!3"/#0 $ 1!%!2!%!2"&"3 $ 3#'#6 G __Wg66F!vw''''' "! __Wh77F ,,V5FGGM())&':::$((999&$'' ?266!&kk1$$*;u*D*D--fg>>>>>? ? +E*DrO   c                 D   |                      |d          }d}|r1|                      |j        |          }|                    d           nTt          | j        dz   |                     |          z             }|                    ||           ||j        v r|j        |= |S )Nr   rQ   T )r]   futuresr   r   rs   jsonrejectr^   )rL   r[   r   successri   r\   errors          rN   r  zhitbtc.handle_authenticate  s     //'844% 	6__V^[AAFNN4    '#		'8J8J(JKKEMM%---f222(5rO   c                 z   |                      |d          }||                      |d          }|                     |d          }|                     |d          }| j        dz   |z   }|                     | j        d         ||           |                     | j        d         ||           t          |          d S )Nr  coder   descriptionr  exactbroad)r]   r   rs   throw_exactly_matched_exception
exceptionsthrow_broadly_matched_exceptionr   )rL   r[   r   r  r  errorMessager  feedbacks           rN   r  zhitbtc.handle_error  s     11??5&11D++E9==L**5-@@Kw}{2H001I4QYZZZ001I<Yabbb)))trO   r}  ):__name__
__module____qualname__rK   rk   r   r   ry   r   r   r   r   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  objectr   r  r   r  r  r   r	   r,  r1  r0  rB  rY  r   r_  r   r   floatr   rh  rn  rr  rw  r{  r  r  r  r  r  __classcell__)rM   s   @rN   r   r      s       : : : : :x+ + +Z \`hj H H3 H3 HQX H H H H( @DB J JC J J J J J* 57 J J J J J J$ @DB $! $!S $! $!S\ $! $! $! $!L73 73 73 73 73r& & &
3 3 3 68 / / /F / / / /8 6:" E E7 E E E E EB=F = = = =~5 5 5 5n <@d[] M M MS M Mbfglbm M M M M28F 8 8 8 8t 8<$]ajl O Of OC OWZ O O O O   6 8<$]ajl B B BC BWZ Bquvzq{ B B B B476 7 7 7 7r
 
D 
 
 
 
* 04$UYbd M M MC Ms Mimnsit M M M M:H6 H H H HT
<& 
< 
< 
< 
</ / / /b@ @ @ @D *, V V V V V V6 pt|~ !G !GC !Gy !G	 !G[` !Gil !G  DI !G !G !G !GF <@ J J JS Ju J J J J> 8<B J J J J J J4 8<$]ajl H H HC HWZ Hquv{q| H H H H>2V 2 2 2 2,&    '6 ' ' ' 'R#?V #? #? #? #?J&    &6        rO   r   )ccxt.async_supportccxt ccxt.async_support.base.ws.cacher   r   r   rd   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   async_supportr   r~  rO   rN   <module>r     s,       f f f f f f f f f f  | | | | | | | | | | | | | | | | | | | | | | | | | | | | 4 4 4 4 4 4       * * * * * * 0 0 0 0 0 0 ) ) ) ) ) )o o o o oT& o o o o orO   