
    ":h                         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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y)    N)
ArrayCacheArrayCacheBySymbolByIdArrayCacheByTimestamp)AnyBalancesIntNumOrder	OrderBook	OrderSide	OrderTypeStrStringsTickerTickersTrade)Client)List)ExchangeError)AuthenticationError)NotSupportedc                       e Zd Zdef 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d8dZdi fdedefdZdefdZd8d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!d8dZ"dddi fd	edededee#   fd Z$defd!Z%d8d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*d8d&Z+d8d'Z,i fde-fd(Z.di fd	ed)e/d*e0d+e1d,e2de'fd-Z3di fd.ed	e	de'fd/Z4di fd	e	fd0Z5dddi fd	e	dededee'   fd1Z6defd2Z7defd3Z8defd4Z9defd5Z:defd6Z;defd7Z< xZ=S )9hitbtcreturnc                     | j                  t        t        |          dd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idddddddddddd
ddid      S )NTF)wswatchTickerwatchTickerswatchBidsAskswatchTradeswatchTradesForSymbols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  methodzticker/{speed}zorderbook/top/{speed}orderbook/full)tradesLimitr   r   r   r"   M1M3M5M15M30H1H4D1D71M)
1m3m5m15m30m1h4h1d1wr<   	keepAlivei  )hasurlsoptions
timeframes	streaming)deep_extendsuperr   describe)self	__class__s    I/var/www/html/turnos/venv/lib/python3.12/site-packages/ccxt/pro/hitbtc.pyrN   zhitbtc.describe   s    fd < ># $!%#)."& $#"!&!%!%%)%)& "H#J "M#O  $.  .! 5" .#   Tw>A
 > >	    c                   K   | j                          | j                  d   d   d   }d}| j                  |      }|j                  |      }| j	                  |j
                  |      }|| j                         }| j                  | j                  | j                  |            | j                  | j                        t        j                  d      }dd| j                  ||d	d
}| j                  ||||       | d{   S 7 w)z
 @ignore
        authenticates the user to access private web socket channels

        https://api.hitbtc.com/#socket-authentication

        :returns dict: response from exchange
        r-   r   r,   authenticatedNhexloginHS256)typeapi_key	timestamp	signature)r0   params)check_required_credentialsrH   clientfuture
safe_valuesubscriptionsmillisecondshmacencodenumber_to_stringsecrethashlibsha256apiKeywatch)	rO   urlmessageHashr^   r_   rT   rZ   r[   requests	            rQ   authenticatezhitbtc.authenticateT   s      	'')iit$Y/%S!{+(<(<kJ ))+I		$++d.C.CI.N"OQUQ\Q\]a]h]hQikrkyky  |A  BI!##{{!*!*	G JJsK+>$ ||s   C>D DDNnamemessageHashPrefixsymbolsc                   K   | j                          d{    | j                  |      }|j                  d      dk\  }| j                  d   d   d   }g }|7|s5t	        dt        |            D ]  }|j                  |dz   ||   z           n|j                  |       d| j                         |d	}	| j                  |	|      }
| j                  |||
|       d{   S 7 7 w)
a  
 @ignore
        :param str name: websocket endpoint name
        :param str messageHashPrefix: prefix for the message hash
        :param str[] [symbols]: unified CCXT symbol(s)
        :param dict [params]: extra parameters specific to the hitbtc api
        Nbatchr   r-   r   r+   ::	subscribe)r0   idch)
load_marketsmarket_symbolsfindrH   rangelenappendnonceextendwatch_multiple)rO   ro   rp   rq   r\   isBatchrk   messageHashesiru   rm   s              rQ   subscribe_publiczhitbtc.subscribe_public   s      !!!%%g.))G$)iit$X.w1c'l+ L$$%6%=
%JKL   !23!**,
	
 ++i0((mWmTTT! 	"  Us"   C'C#CC'C%C'%C'symbolc                 Z  K   | j                          d{    | j                          d{    | j                  d   d   d   }|j                  d      }| j	                  |d      }||dz   |z   }||| j                         d}| j                  ||||       d{   S 7 7 x7 w)	z
 @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   rt   r0   r\   rv   )rx   rn   rH   splitsafe_stringr~   rj   )rO   ro   r   r\   rk   	splitNamerl   ru   s           rQ   subscribe_privatezhitbtc.subscribe_private   s      !!!!!!iit$Y/JJ|,	&&y!4%,v5K**,
	
 ZZ[)[III 	"! Js2   B+B%B+B'A1B+ B)!B+'B+)B+c                   K   | j                          d{    | j                          d{    | j                  d   d   d   }t        | j	                               }|||d}| j                  ||||       d{   S 7 l7 V7 w)z
 @ignore
        :param str name: websocket endpoint name
        :param dict [params]: extra parameters specific to the hitbtc api
        Nr-   r   r,   r   )rx   rn   rH   strr~   rj   )rO   ro   r\   rk   rl   ru   s         rQ   trade_requestzhitbtc.trade_request   s      !!!!!!iit$Y/$**,'
	
 ZZ[)[III 	"! Js2   B	BB	BAB	>B?B	B	B	limitc           	        K   | j                  | j                  d      }| j                  |dd      }| j                  |dd|      }| j                  |dd      }| j                  |dd      }|dk(  rd	|z   d
z   |z   dz   }n|dk(  rd	|z   d
z   |z   dz   }| j	                  |      }	dd|	d   gii}
| j                  |d|g| j                  |
|             d{   }|j                         S 7 w)a'  
        watches information on open orders with bid(buy) and ask(sell) prices, volumes and other data

        https://api.hitbtc.com/#subscribe-to-full-order-book
        https://api.hitbtc.com/#subscribe-to-partial-order-book
        https://api.hitbtc.com/#subscribe-to-partial-order-book-in-batches
        https://api.hitbtc.com/#subscribe-to-top-of-book
        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'
        :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"   r0   r1   defaultMethoddepth20100zorderbook/{depth}/{speed}zorderbook/D/mszorderbook/{depth}/{speed}/batchzms/batchr\   rq   rv   
orderbooksN)r`   rI   r   safe_string_2marketr   rL   r   )rO   r   r   r\   rI   r   ro   r   speedr   rm   	orderbooks               rQ   watch_order_bookzhitbtc.watch_order_book   s    $ //$,,0@A(((<LM!!&(O]S  $7  %8.. 5(3.6=D66 5(3.6CDV$F4L>

 //lVHdN^N^_fhnNopp	   qs   CC-C+C-r^   c                    | j                  |d      }| j                  |d      }|r|n|}|rdnd}t        |j                               }t        dt	        |            D ]w  }||   }	| j                  |	      }
|
d   }||	   }d|z   }|| j                  vrN| j                  |j                  |i       }| j                  |d      }| j                  i |      | j                  |<   | j                  |   }| j                  |d      }| j                  |d      }|dk(  r'| j                  |||d	d
      }|j                  |       nP| j                  |d
g       }| j                  |d	g       }| j                  |d   |       | j                  |d   |       ||d<   | j                  |      |d<   ||d<   ||d<   || j                  |<   |j                  ||       z y )Nsnapshotupdater   r   zorderbooks::r   tsbaasksbidsrZ   datetimer~   )	safe_dictlistkeysr{   r|   safe_marketr   ra   safe_integer
order_bookparse_order_bookreset	safe_listhandle_deltasiso8601resolve)rO   r^   messager   r   datarX   	marketIdsr   marketIdr   r   itemrl   subscriptionr   r   rZ   r~   parsedSnapshotr   r   s                         rQ   handle_order_bookzhitbtc.handle_order_book   s   0 >>':62#x%z8%	q#i.) 	3A |H%%h/FH%F>D(61Kdoo-#~~f.B.BKQST)),@*.//"e*D'/I))$4I%%dC0Ez!!%!6!6tVYPSUX!Y/~~dC4~~dC4""9V#4d;""9V#4d;%.Ik"$(LL$;Ij!!&Ig"(Ih&/DOOF#NN9k25	3rR   c                 p    | j                  |d      }| j                  |d      }|j                  ||       y )Nr      )safe_numberstore)rO   booksidedeltapriceamounts        rQ   handle_deltazhitbtc.handle_delta   s4      *!!%+uf%rR   c                 b    t        dt        |            D ]  }| j                  |||           y )Nr   )r{   r|   r   )rO   r   deltasr   s       rQ   r   zhitbtc.handle_deltas%  s0    q#f+& 	3Ahq	2	3rR   c                 h   K   | j                  |g|       d{   }| j                  ||      S 7 w)a  
        watches a price ticker, a statistical calculation with the information calculated over the past 24 hours for a specific market

        https://api.hitbtc.com/#subscribe-to-ticker
        https://api.hitbtc.com/#subscribe-to-ticker-in-batches
        https://api.hitbtc.com/#subscribe-to-mini-ticker
        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>`
        N)watch_tickersr`   )rO   r   r\   tickers       rQ   watch_tickerzhitbtc.watch_ticker)  s5      ))6(F;;vv.. <s   202c           	        K   | j                          d{    | j                  |      }| j                  | j                  d      }| j	                  |dd      }| j                  |dd|      }| j	                  |dd      }| j                  |d|i      }| j                  |ddg      }g }||j                  d       n?t        d	t        |            D ]'  }	| j                  ||	         }
|j                  |
       ) d
d|ii}| j                  |d|| j                  ||             d{   }| j                  rt        |t               si }|||d   <   |S | j#                  |d|      S 7 V7 Bw)a:  
        watches a price ticker, a statistical calculation with the information calculated over the past 24 hours for a specific market
        :param str[] [symbols]:
        :param dict params: extra parameters specific to the exchange API endpoint
        :param str params['method']: 'ticker/{speed}' ,'ticker/price/{speed}', 'ticker/{speed}/batch'(default), 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   r0   zticker/{speed}/batchr   r   1s*r   r\   rq   tickersr   )rx   ry   r`   rI   r   r   implode_paramsomitr}   r{   r|   	market_idr   rL   
newUpdates
isinstancer   filter_by_array)rO   rq   r\   rI   r   r0   r   ro   r   r   r   rm   
newTickersr   s                 rQ   r   zhitbtc.watch_tickers;  s     !!!%%g.//$,,>(((<RS##FHo}U  $7""6GU+;<6Hg#67	?S!1c'l+ +>>'!*5  *+ 9

  00y'4K[K[\cekKlmm
??j$/ "0:
8,-##J'BB5 	"( ns#   E1E,DE1,E/-A E1/E1c                    | j                  |di       }t        |j                               }g }d}t        dt	        |            D ]l  }||   }| j                  |      }	|	d   }
| j                  ||   |	      }|| j                  |
<   |j                  |       |dz   |
z   }|j                  ||       n |j                  ||       y )Nr   r   r   r   rt   )
r`   r   r   r{   r|   r   parse_ws_tickerr   r}   r   rO   r^   r   r   r   resulttopicr   r   r   r   r   rl   s                rQ   handle_tickerzhitbtc.handle_ticker`  s    N w3%	q#i.) 	0A |H%%h/FH%F))$x.&AF#)DLL MM&!$,/KNN6;/	0 	vu%rR   c                 B   | j                  |d      }| j                  d |      }| j                  |d      }| j                  i d|d|d| j	                  |      d| j                  |d      d| j                  |d	      d
| j                  |d      d| j                  |d      d| j                  |d      d| j                  |d      dd d| j                  |d      d|d|dd dd dd dd | j                  |d      | j                  |d      |d|      S )Nr   cr   rZ   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   )rO   r   r   rZ   r   r   s         rQ   r   zhitbtc.parse_ws_ticker  s   : %%fc2	!!$/, !
f!
!
 Y/!
 D$$VS1	!

 4##FC0!
 4##FC0!
 ))&#6!
 4##FC0!
 ))&#6!
 D!
 D$$VS1!
 T!
 D!
 T!
 d!
  $!!
" t#!
$ **637++FC8)!
* + 	rR   c           	      b  K   | j                          d{    | j                  |dd      }| j                  | j                  d      }| j	                  |dd      }| j                  |dd|      }| j	                  |dd      }| j                  |d|i      }| j                  |ddg      }| j                  |      }d	d
|ii}	| j                  |d|| j                  |	|             d{   }
| j                  rt        |
t              si }|
||
d   <   |S | j                  |
d|      S 7 7 Bw)a#  
        watches best bid & ask for symbols

        https://api.hitbtc.com/#subscribe-to-top-of-book

        :param str[] symbols: 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]: 'orderbook/top/{speed}' or 'orderbook/top/{speed}/batch(default)'
        :param str [params.speed]: '100ms'(default) or '500ms' or '1000ms'
        :returns dict: a `ticker structure <https://docs.ccxt.com/#/?id=ticker-structure>`
        NFr   r0   zorderbook/top/{speed}/batchr   r   100msr\   rq   bidaskr   )rx   ry   r`   rI   r   r   r   r   
market_idsr   rL   r   r   r   r   )rO   rq   r\   rI   r   r0   r   ro   r   rm   r   r   s               rQ   watch_bids_askszhitbtc.watch_bids_asks  sD     !!!%%gtU;//$,,@(((<YZ##FHo}U  ':""6GU+;<6Hg#67OOG,	9

  00x$JZJZ[bdjJkll
??j$/ "0:
8,-##J'BB) 	" ms#   D/D*CD/*D-+A D/-D/c                    | j                  |di       }t        |j                               }g }d}t        dt	        |            D ]l  }||   }| j                  |      }	|	d   }
| j                  ||   |	      }|| j                  |
<   |j                  |       |dz   |
z   }|j                  ||       n |j                  ||       y )Nr   r   r   r   rt   )
r   r   r   r{   r|   r   parse_ws_bid_askbidsasksr}   r   r   s                rQ   handle_bid_askzhitbtc.handle_bid_ask  s     ~~gvr2%	q#i.) 	0A |H%%h/FH%F**4>6BF$*DMM&!MM&!$,/KNN6;/	0 	vu%rR   c                     | j                  |d      }| j                  |d   || j                  |      | j                  |d      | j                  |d      | j                  |d      | j                  |d      |d|      S )Nr   r   r   r   r   r   )r   rZ   r   r   r   r   r   r   )r   r   r   r   )rO   r   r   rZ   s       rQ   r   zhitbtc.parse_ws_bid_ask  s    %%fc2	X&"Y/##FC0))&#6##FC0))&#6	!
 	 		rR   sincec           	      H  K   | j                          d{    | j                  |      }dd|d   gii}|||d<   d}| j                  |d|g| j                  ||             d{   }| j                  r|j                  ||      }| j                  |||d      S 7 7 8w)a  
        get the list of most recent trades for a particular symbol

        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>`
        Nr\   rq   rv   r   tradesrZ   )rx   r   r   rL   r   getLimitfilter_by_since_limit)	rO   r   r  r   r\   r   rm   ro   r  s	            rQ   watch_tradeszhitbtc.watch_trades  s      !!!V$F4L>

 $GG,,T8fXtGWGWX_agGhii??OOFE2E))&%LL 	" js"   B"BAB"'B (7B" B"c                 (   | j                  |ddi       }t        |j                               }t        dt	        |            D ]  }||   }| j                  |      }| j                  | j                  dd      }|d   }	| j                  | j                  |	      }
|
t        |      }
|
| j                  |	<   | j                  ||   |      }t        dt	        |            D ]  }|
j                  ||           d|	z   }|j                  |
|        |S )Nr   r   r   r2   r/   r   ztrades::)safe_value_2r   r   r{   r|   r   r   rI   r`   r  r   parse_ws_tradesr}   r   )rO   r^   r   r   r   r   r   r   r2   r   storedr  jrl   s                 rQ   handle_tradeszhitbtc.handle_trades5  s   P   *hC%	q#i.) 	0A |H%%h/F++DLL-NKH%F__T[[&9F~#K0&,F#))$x.&AF1c&k* )fQi()$v-KNN6;/	0 rR   r   c                 :   | j                  |      }g }t        dt        |            D ]8  }| j                  | j	                  ||   |      |      }|j                  |       : | j                  |dd      }| j                  |d      }	| j                  ||	||      S )Nr   rZ   rv   r   )	to_arrayr{   r|   r   parse_ws_trader}   	sort_by_2r   filter_by_symbol_since_limit)
rO   r  r   r  r   r\   r   r   trader   s
             rQ   r
  zhitbtc.parse_ws_tradeso  s    v&q#f+& 	!AKK 3 3F1Iv FOEMM% 	! T:!!&(300NNrR   c                 $   | j                  |d      }| j                  || j                  |d      d || j                  |      | j                  |d      d | j                  |d      d | j                  |d      | j                  |d      d d d|      S )Nr   r   r   r   pr   r   rv   orderrZ   r   r   rX   sidetakerOrMakerr   r   costfee)r   
safe_trader   r   )rO   r  r   rZ   s       rQ   r  zhitbtc.parse_ws_tradey  s     %%eS1	""5#."Y/&&vx8$$UC0 %%eS1&&uc2 
  	rR   r=   c           	      Z  K   | j                  | j                  ||      }d|z   }| j                  |      }dd|d   gii}	|||	d   d<   | j                  |d|g| j	                  |	|             d{   }
| j
                  r|
j                  ||      }| j                  |
||d      S 7 6w)	u  
        watches historical candlestick data containing the open, high, low, and close price, and the volume of a market

        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/r\   rq   rv   Nr   candlesr   )r   rJ   r   r   rL   r   r  r  )rO   r   	timeframer  r   r\   periodro   r   rm   ohlcvs              rQ   watch_ohlcvzhitbtc.watch_ohlcv  s      !!$//9iHF"V$F4L>

 ).GHg&++D)fXtGWGWX_agGhii??NN651E))%qAA js   A0B+2B)37B+c                    | j                  |ddi       }t        |j                               }| j                  |d      }|j	                  d      }| j                  |d      }| j                  |      }t        dt        |            D ]  }	||	   }
| j                  |
      }|d   }| j                  | j                  |i       | j                  |<   | j                  | j                  |   |      }|:| j                  | j                  dd	      }t        |      }|| j                  |   |<   | j                  ||
   |      }t        dt        |            D ]  }|j                  ||           d
|z   }|j!                  ||        |S )Nr   r   rw   r   r   r   r   
OHLCVLimitr/   z	candles::)r	  r   r   r   r   find_timeframer{   r|   r   r`   ohlcvsr   rI   r   parse_ws_ohlcvsr}   r   )rO   r^   r   r   r   channelsplitChannelr   r  r   r   r   r   r  r   r&  r  rl   s                     rQ   handle_ohlcvzhitbtc.handle_ohlcv  s{   D   *hC%	""7D1}}S)!!,2''/	q#i.) 	0A |H%%h/FH%F"&//$++vr"JDKK__T[[%8)DF~))$,,dK.u517F#I.))$x.&AF1c&k* )fQi()%.KNN6;/	0 rR   c           	          | j                  |d      | j                  |d      | j                  |d      | j                  |d      | j                  |d      | j                  |d      gS )Nr   r   r   r   r   r   )r   r   )rO   r!  r   s      rQ   parse_ws_ohlcvzhitbtc.parse_ws_ohlcv  sn     eS)UC(UC(UC(UC(UC(
 	
rR   c                 d  K   | j                          d{    d}d}|| j                  |      }| j                  d||      \  }}| j                  |ddddd      }| j	                  |||       d{   }| j
                  r|j                  ||      }| j                  |||d      S 7 7 8w)a|  
        watches information on multiple orders made by the user

        https://api.hitbtc.com/#subscribe-to-reports
        https://api.hitbtc.com/#subscribe-to-reports-2
        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_   rZ   )rx   r   handle_market_type_and_paramsget_supported_mappingr   r   r  r  )	rO   r   r  r   r\   
marketTyper   ro   orderss	            rQ   watch_orderszhitbtc.watch_orders  s      !!!
[[(F!??vW]^
F))*$(')	7
  --dFFCC??OOFE2E))&%LL 	" Ds"   B0B,AB05B.67B0.B0c                 J   | j                   ,| j                  | j                  d      }t        |      | _         | j	                  |dg       }t        |t              r4t        dt        |            D ]  }||   }| j                  |||        |S | j                  |||       |S )NordersLimitr\   r   )
r7  r   rI   r   r`   r   r   r{   r|   handle_order_helper)rO   r^   r   r   r   r   r  s          rQ   handle_orderzhitbtc.handle_order  s    | ;;%%dllMBE07DKw"5dD!1c$i( AQ((%@A
  $$VWd;rR   c                 f   | j                   }| j                  |dd      }| j                  |d      }|j                  d      }| j                  |d      }| j	                  |      }	| j                  |      }
|j                  |
       |j                  ||       |j                  ||dz   |	z          y )N
instrumentr   r0   _orderr   rt   )r7  safe_string_lower_2r   r   r   parse_orderr}   r   )rO   r^   r   r  r7  r   r0   splitMethodrl   r   parseds              rQ   r;  zhitbtc.handle_order_helperg  s    ++E<J!!'84ll8,&&{A6!!(+!!%(fv{+v{T1F:;rR   c                    | j                  |d      }| j                  |d      }| j                  || j                  |d      | j                  |d      || j                  |      | j	                  ||      d | j                  |d      | j                  |d      | j                  |d      | j                  |d      d | j                  |d	      d d d
d|      S )N
created_atr   trade_idrv   r  trade_takertrade_pricetrade_quantity	trade_fee)r  currencyrater  )r   r   r  r   r   )rO   r  r   rZ   r   s        rQ   parse_ws_order_tradezhitbtc.parse_ws_order_trades  s    6 %%e\:	##E84""5*5%%eT2"Y/&&x8$$UF3 ,,UMB%%e];&&u.>?((<  
$ % 	rR   c                 <   | j                  |d      }| j                  |d      }| j                  ||      }| j                  |d      }d }|| j                  ||      }|g}| j                  |d      }| j                  |d      }	d }
|	dk(  r| j                  |	      }
n| j                  |      }
| j	                  i d|d| j                  |d      d	| j                  |d
      d|d| j                  |      dd d|d   d| j                  |d      d| j                  |d      d| j                  |d      d| j                  |d      d| j                  |d      d| j                  |d      d| j                  |d      dd dd dd |
d |d d|      S )NrE  r   rF  statusreport_typecanceledr   rv   clientOrderIdclient_order_idrZ   r   lastTradeTimestampr   r   quantityrX   r  timeInForcetime_in_forcepostOnly	post_only
reduceOnlyreduce_onlyfilled	remainingr  )rO  r   r  r  )r   r   rM  parse_order_status
safe_orderr   safe_string_upperr`   )rO   r  r   rZ   r   tradeIdr  r  	rawStatusrP  parsedStatuss              rQ   parse_ws_orderzhitbtc.parse_ws_order  s9   6 $$UL9	##E84!!(F3""5*5--eV<EWF$$UH5	&&um<*$22;?L229=L  
E 
$""5$/ 
 T--e5FG 
 	 

 Y/ 
 !$ 
 fX& 
 T%%eW5 
 d&&uj9 
 D$$UF3 
 D**5&9 
 4++E?C 
 ((< 
 $//%? 
 d 
  ! 
" D# 
$ #+ 
, - 	rR   c                 F  K   | j                          d{    d}| j                  dd|      \  }}| j                  |dddd      }| j                  |dd      }| j	                  |d      }d|i}| j                  |d| j                  ||             d{   S 7 7 w)a  
        watches balance updates, cannot subscribe to margin account balances

        https://api.hitbtc.com/#subscribe-to-spot-balances
        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)r1  r3  r_   modebatches)rx   r4  r5  r   r   r   r   )rO   r\   rX   ro   rh  rm   s         rQ   watch_balancezhitbtc.watch_balance  s      !!!99.$PVWf))$,/11
 
 	:66*D
 ++D$GV8TUUU 	" Vs"   B!BBB!BB!B!rX   r  r   r   c           
        K   | j                          d{    | j                  |      }d}d}	| j                  d||      \  }	}d}
| j                  d|      \  }
}| j	                  ||	|||||
|      \  }}| j                  ||      }|	dk(  r| j                  d|       d{   S |	dk(  s|
| j                  d|       d{   S | j                  d|       d{   S 7 7 A7 "7 
w)a  
        create a trade order

        https://api.hitbtc.com/#create-new-spot-order
        https://api.hitbtc.com/#create-margin-order
        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 fulfilled, 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createOrderr3  futures_new_orderr2  margin_new_orderspot_new_order)rx   r   r4  handle_margin_mode_and_paramscreate_order_requestr   r   )rO   r   rX   r  r   r   r\   r   rm   r6  
marginModes              rQ   create_order_wszhitbtc.create_order_ws  s    * !!!V$
!??vW]^
F
!??vV
F33FJdTZ\acmouv++gv.++,?IIIH$**@++,>HHH++,<gFFF 	" JHFsF   C,C$BC,$C&% C,C(C,C* C,&C,(C,*C,rv   c                   K   | j                          d{    d}d|i}|| j                  |      }d}| j                  d||      \  }}| j                  d|      \  }}| j	                  ||      }|dk(  r| j                  d|       d{   S |dk(  s|| j                  d|       d{   S | j                  d|       d{   S 7 7 A7 "7 
w)	a  

        https://api.hitbtc.com/#cancel-spot-order-2
        https://api.hitbtc.com/#cancel-futures-order-2
        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>`
        NrS  r(   r3  futures_cancel_orderr2  margin_cancel_orderspot_cancel_order)rx   r   r4  rp  r   r   )	rO   rv   r   r\   r   rm   r6  rr  querys	            rQ   cancel_order_wszhitbtc.cancel_order_ws(  s      !!!r
 [[(F
!??QWY_`
F >>PVW
E++gu-++,BGLLLH$**@++,A7KKK++,?III! 	" MKIsF   CCA6CC C.C/CC	CCCCc                 ~  K   | j                          d{    d}|| j                  |      }d}| j                  d||      \  }}d}| j                  d|      \  }}|dk(  r| j	                  d|       d{   S |dk(  s|t        | j                  dz         | j	                  d|       d{   S 7 7 ?7 w)ao  

        https://api.hitbtc.com/#cancel-spot-orders
        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*   r3  futures_cancel_ordersr2  z5 cancelAllOrdersWs is not supported for margin ordersspot_cancel_orders)rx   r   r4  rp  r   r   rv   )rO   r   r\   r   r6  rr  s         rQ   cancel_all_orders_wszhitbtc.cancel_all_orders_wsI  s      !!![[(F
!??@SU[]cd
F
!??@SU[\
F++,CVLLLH$**@tww)``aa++,@&III 	" M Js4   B=B7A"B=9B9:8B=2B;3B=9B=;B=c                   K   | j                          d{    d}i }|| j                  |      }|d   |d<   d}| j                  d||      \  }}d}| j                  d|      \  }}|dk(  r| j	                  d|       d{   S |dk(  s|| j	                  d|       d{   S | j	                  d	|       d{   S 7 7 A7 "7 
w)
a  

        https://api.hitbtc.com/#get-active-futures-orders-2
        https://api.hitbtc.com/#get-margin-orders
        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>`
        Nrv   r   r)   r3  futures_get_ordersr2  margin_get_ordersspot_get_orders)rx   r   r4  rp  r   )	rO   r   r  r   r\   r   rm   r6  rr  s	            rQ   fetch_open_orders_wszhitbtc.fetch_open_orders_wse  s       !!![[(F &tGH
!??@SU[]cd
F
!??@SU[\
F++,@'JJJH$**@++,?III++,=wGGG 	" KIGsF   CCA,CC C$C%C>C	?CCC	Cc                     | j                  |d      }| j                  |d      }| j                  |      }| j                  | j                  |      | _        |j                  | j                  |       y )Nr0   r\   )r   r`   parse_balancerL   balancer   )rO   r^   r   rl   r\   r  s         rQ   handle_balancezhitbtc.handle_balance  s`      &&w9(3$$V,''g>t||[1rR   c                     |S N )rO   r^   r   s      rQ   handle_notificationzhitbtc.handle_notification  s	     rR   c                 `   | j                  |d      }| j                  |di       }t        |t              rUg }t	        dt        |            D ]'  }| j                  ||         }|j                  |       ) |j                  ||       |S | j                  |      }|j                  ||       |S )Nrv   r   r   )	r   r`   r   r   r{   r|   rd  r}   r   )rO   r^   r   rl   r   parsedOrdersr   parsedOrders           rQ   handle_order_requestzhitbtc.handle_order_request  s    8 &&w5(B7fd#L1c&k* 1"11&)<##K01 NN<5  --f5KNN;4rR   c                 v   | j                  ||      ry | j                  |dd      }||j                  d      }| j                  |d      }|dk(  r| j                  |d      }||dk(  rd}| j                  | j
                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  d	}| j                  ||      }|
 |||       y y | j                  |d
      }| j                  |d      }	|	| j                  ||       |du rd|vr| j                  ||       t        |t              r;| j                  |di       }
t!        |      }|dk(  sd|
v r| j                  ||       y y y )Nrw   r0   r   r   r   r   toporderbook/top)r  r   r  r   r  
spot_orderspot_ordersmargin_ordermargin_ordersfutures_orderfutures_ordersspot_balancefutures_balancer   rS  Trv   )handle_errorr   r   r   r*  r   r  r   r   r<  r  r`   r  handle_authenticater   r   r|   )rO   r^   r   r(  r)  channel2methodsr0   r   rR  firstarrayLengths               rQ   handle_messagezhitbtc.handle_message  s   VW-$$WdH="==-L&&|Q7G+%++L!<'H,=-G,,,,,,!33!%!4!4"//#00 $ 1 1!%!2!2!%!2!2"&"3"3 $ 3 3#'#6#6G __Wg6F!vw' " __Wh7F ,,V5FGM())&':$((9&$'26!&k1$*;u*D--fg> +E	 (rR   c                 >   | j                  |d      }d}|r/| j                  |j                  |      }|j                  d       |S t        | j                  dz   | j                  |      z         }|j                  ||       ||j                  v r|j                  |= |S )Nr   rT   T )r`   futuresr   r   rv   jsonrejectra   )rO   r^   r   successrl   r_   errors          rQ   r  zhitbtc.handle_authenticate  s     //'84%__V^^[AFNN4  	 (#		'8J(JKEMM%-f222((5rR   c                 H   | j                  |d      }|	 | j                  |d      }| j                  |d      }| j                  |d      }| j                  dz   |z   }| j                  | j                  d   ||       | j                  | j                  d   ||       t        |      y # t        $ rn}t        |t              r0d}	|j                  ||	       |	|j                  v r2|j                  |	= n$| j                  |d	      }
|j                  ||
       Y d }~y
d }~ww xY w)Nr  coder   descriptionr  exactbroadrT   rv   T)r`   r   rv   throw_exactly_matched_exception
exceptionsthrow_broadly_matched_exceptionr   	Exceptionr   r   r  ra   )rO   r^   r   r  r  errorMessager  feedbackerl   rv   s              rQ   r  zhitbtc.handle_error	  s    1uf5#//yA"..umD77S=;644T__W5MtU]^44T__W5M|]ef#H--   	a!45"1KMM![1"f&:&::"00=))'48BMM!R(	s   BB* *	D!3A$DD!r  )>__name__
__module____qualname__r   rN   rn   r   r   r   r   r   r   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
   r8  r<  r;  rM  rd  r   rj  r   r   floatr	   rs  ry  r}  r  r  r  r  r  r  r  __classcell__)rP   s   @rQ   r   r      sn   ?# ?B-^ \`hj U3 U3 UQX U4 @DB JC J J* 57 J J" @DB "!S "! "!S\ "!H73 73r&
3 68 / /F /$ 6:" #C7 #C #CJ4&F 4&l5n 8<B  CW  C7  CD&V &< <@d[] M MS M Mbfglbm M68F 8t 8<$]ajl Of OC OWZ O6 8<$]ajl B BC BWZ Bquvzq{ B876 7r
D 
* 04$UYbd M MC Ms Mimnsit M>H6 HT
<& 
</b@D *, V V: pt|~ #GC #Gy #G	 #G[` #Gil #G  DI #GJ <@ J JS Ju JB 8<B J J8 8<$]ajl H HC HWZ Hquv{q| HB2V 2,& '6 'R)?V )?V& & 6  rR   r   )ccxt.async_supportccxt ccxt.async_support.base.ws.cacher   r   r   rg   ccxt.base.typesr   r   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  rR   rQ   <module>r     sT     f f  B  B  B  B 4  * 0 )XT&& XrR   