
    E#i                         S SK rS SKJrJrJr  S SKrS SKJrJ	r	J
r
JrJrJrJrJrJrJrJrJrJr  S SKJr  S SKJr  S SKJr  S SKJr  S SKJr   " S	 S
\R:                  R<                  5      rg)    N)
ArrayCacheArrayCacheBySymbolByIdArrayCacheByTimestamp)AnyBalancesIntNumOrder	OrderBook	OrderSide	OrderTypeStrStringsTickerTickersTrade)Client)List)ExchangeError)AuthenticationError)NotSupportedc                   B  ^  \ rS rSrS\4U 4S jjrS rS0 4S\S\S\4S	 jjr	S0 4S\S
\
4S jjr0 4S\4S jjrS0 4S
\S\S\4S jjrS\4S jrS rS r0 4S
\S\4S jjrS0 4S\S\4S jjrS\4S jrS;S jrS0 4S\S\4S jjrS\4S jrS;S jrSS0 4S
\S\S\S\\   4S jjrS\4S jr SSS0 4S\!S\S\4S jjr"S;S jr#S SS0 4S
\S!\S\S\S\\$   4
S" jjr%S\4S# jr&S;S\$4S$ jjr'SSS0 4S
\
S\S\S\\(   4S% jjr)S\4S& jr*S\4S' jr+S;S( jr,S;S) jr-0 4S\.4S* jjr/S0 4S
\S+\0S,\1S-\2S.\3S\(4S/ jjr4S0 4S0\S
\
S\(4S1 jjr5S0 4S
\
S\\(   4S2 jjr6SSS0 4S
\
S\S\S\\(   4S3 jjr7S\4S4 jr8S\4S5 jr9S\4S6 jr:S\4S7 jr;S\4S8 jr<S\4S9 jr=S:r>U =r?$ )<hitbtc   returnc                    > U R                  [        [        U ]  5       SSSSSSSSSSSSSSSS.SSSS.0SSS	S.0S
.SSS0SS0SS0SS0S.SSSSSSSSSSS.
SS0S.5      $ )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/home/james-whalen/.local/lib/python3.13/site-packages/ccxt/pro/hitbtc.pyrO   hitbtc.describe   s    fd < ># $!%#)."& $#"!&!%!%%)%)& "H#J "M#O  $.  .! 5" .#   Tw>A
 > >	    c                   #    U R                  5         U R                  S   S   S   nSnU R                  U5      nUR                  U5      nU R	                  UR
                  U5      nUc  U R                  5       nU R                  U R                  U R                  U5      5      U R                  U R                  5      [        R                  S5      nSSU R                  UUS	.S
.nU R                  XX5        UI Sh  vN $  N7f)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)r1   params)check_required_credentialsrI   clientreusableFuture
safe_valuesubscriptionsmillisecondshmacencodenumber_to_stringsecrethashlibsha256apiKeywatch)	rP   urlmessageHashr`   futurerV   r\   r]   requests	            rR   authenticatehitbtc.authenticateT   s      	'')iit$Y/%S!&&{3(<(<kJ ))+I		$++d.C.CI.N"OQUQ\Q\]a]h]hQikrkyky  |A  BI!##{{!*!*	G JJs>$ ||s   C=D?D DNnamemessageHashPrefixsymbolsc                   #    U R                  5       I Sh  vN   U R                  U5      nUR                  S5      S:  nU R                  S   S   S   n/ nUb=  U(       d6  [	        S[        U5      5       H  nUR                  US-   X8   -   5        M     OUR                  U5        SU R                  5       US	.n	U R                  X5      n
U R                  XgX5      I Sh  vN $  N N7f)
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)r1   idch)
load_marketsmarket_symbolsfindrI   rangelenappendnonceextendwatch_multiple)rP   rs   rt   ru   r^   isBatchrm   messageHashesiry   rp   s              rR   subscribe_publichitbtc.subscribe_public   s      !!!%%g.))G$)iit$X.w1c'l+$$%6%=
%JK ,   !23!**,
	
 ++i0((WTTT! 	"  Us"   C+C'CC+"C)#C+)C+symbolc                 X  #    U R                  5       I Sh  vN   U R                  5       I Sh  vN   U R                  S   S   S   nUR                  S5      nU R	                  US5      nUb  US-   U-   nUUU R                  5       S.nU R                  XFXv5      I Sh  vN $  N Nw N7f)	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   rx   r1   r^   rz   )r|   rq   rI   splitsafe_stringr   rl   )rP   rs   r   r^   rm   	splitNamern   ry   s           rR   subscribe_privatehitbtc.subscribe_private   s      !!!!!!iit$Y/JJ|,	&&y!4%,v5K**,
	
 ZZ)III 	"! Js2   B*B$B*B&A0B*B( B*&B*(B*c                   #    U R                  5       I Sh  vN   U R                  5       I Sh  vN   U R                  S   S   S   n[        U R	                  5       5      nUUUS.nU R                  X4XT5      I Sh  vN $  Nj NT N7f)z
@ignore
       :param str name: websocket endpoint name
       :param dict [params]: extra parameters specific to the hitbtc api
       Nr.   r   r-   r   )r|   rq   rI   strr   rl   )rP   rs   r^   rm   rn   ry   s         rR   trade_requesthitbtc.trade_request   s      !!!!!!iit$Y/$**,'
	
 ZZ)III 	"! Js2   BBBBAB<B=BBBlimitc           	        #    U R                  U R                  S5      nU R                  USS5      nU R                  USSU5      nU R                  USS5      nU R                  USS5      nUS:X  a  S	U-   S
-   U-   S-   nOUS:X  a  S	U-   S
-   U-   S-   nU R	                  U5      n	SSU	S   /00n
U R                  USU/U R                  X5      5      I Sh  vN nUR                  5       $  N7f)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#   r1   r2   defaultMethoddepth20100zorderbook/{depth}/{speed}zorderbook/D/mszorderbook/{depth}/{speed}/batchzms/batchr^   ru   rz   
orderbooksN)rb   rJ   r   safe_string_2marketr   rM   r   )rP   r   r   r^   rJ   r   rs   r   speedr   rp   	orderbooks               rR   watch_order_book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^_fNopp	   qs   CC.C,C.r`   c                    U R                  US5      nU R                  US5      nU(       a  UOUnU(       a  SOSn[        UR                  5       5      n[        S[	        U5      5       GHw  nXx   n	U R                  U	5      n
U
S   nXY   nSU-   nXR                  ;  aN  U R                  UR                  U0 5      nU R                  US5      nU R                  0 U5      U R                  U'   U R                  U   nU R                  US5      nU R                  US5      nUS:X  a&  U R                  XUS	S
5      nUR                  U5        OPU R                  US
/ 5      nU R                  US	/ 5      nU R                  US   U5        U R                  US   U5        UUS'   U R                  U5      US'   UUS'   UUS'   UU R                  U'   UR                  UU5        GMz     g )Nsnapshotupdater   r   zorderbooks::r   tsbaasksbidsr\   datetimer   )	safe_dictlistkeysr   r   safe_marketr   rc   safe_integer
order_bookparse_order_bookreset	safe_listhandle_deltasiso8601resolve)rP   r`   messager   r   datarZ   	marketIdsr   marketIdr   r   itemrn   subscriptionr   r   r\   r   parsedSnapshotr   r   s                         rR   handle_order_bookhitbtc.handle_order_book   s   0 >>':62#x%z8%	q#i.)A |H%%h/FH%F>D(61Koo-#~~f.B.BKQST)),@*.//"e*D'/I))$4I%%dC0Ez!!%!6!6tYPSUX!Y/~~dC4~~dC4""9V#4d;""9V#4d;%.Ik"$(LL$;Ij!!&Ig"(Ih&/DOOF#NN9k25 *rT   c                 n    U R                  US5      nU R                  US5      nUR                  X45        g )Nr      )safe_numberstore)rP   booksidedeltapriceamounts        rR   handle_deltahitbtc.handle_delta   s2      *!!%+u%rT   c                 d    [        S[        U5      5       H  nU R                  XU   5        M     g )Nr   )r   r   r   )rP   r   deltasr   s       rR   r   hitbtc.handle_deltas%  s)    q#f+&Ahq	2 'rT   c                 f   #    U R                  U/U5      I Sh  vN nU R                  X15      $  N7f)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_tickersrb   )rP   r   r^   tickers       rR   watch_tickerhitbtc.watch_ticker)  s2      ))6(F;;v.. <s   1/1c           	        #    U R                  5       I Sh  vN   U R                  U5      nU R                  U R                  S5      nU R	                  USS5      nU R                  USSU5      nU R	                  USS5      nU R                  USU05      nU R                  USS/5      n/ nUc  UR                  S5        O@[        S	[        U5      5       H'  n	U R                  X   5      n
UR                  U
5        M)     S
SU00nU R                  USXR                  X5      5      I Sh  vN nU R                  (       a   [        U[         5      (       d  0 nXUS   '   U$ U R#                  USU5      $  GN_ NK7f)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   r1   zticker/{speed}/batchr   r   1s*r   r^   ru   tickersr   )r|   r}   rb   rJ   r   r   implode_paramsomitr   r   r   	market_idr   rM   
newUpdates
isinstancer   filter_by_array)rP   ru   r^   rJ   r   r1   r   rs   r   r   r   rp   
newTickersr   s                 rR   r   hitbtc.watch_tickers;  sy     !!!%%g.//$,,>(((<RS##FHo}U  $7""6GU+;<6Hg#67	?S!1c'l+>>'*5  * , 9

  00y'K[K[\cKlmm
??j$// "0:
8,-##J'BB5 	"( ns#   E:E5DE:,E8-A	E:8E:c                    U R                  US0 5      n[        UR                  5       5      n/ nSn[        S[	        U5      5       Hi  nXG   nU R                  U5      n	U	S   n
U R                  X8   U	5      nXR                  U
'   UR                  U5        US-   U
-   nUR                  X5        Mk     UR                  XV5        g )Nr   r   r   r   rx   )
rb   r   r   r   r   r   parse_ws_tickerr   r   r   rP   r`   r   r   r   resulttopicr   r   r   r   r   rn   s                rR   handle_tickerhitbtc.handle_ticker`  s    N w3%	q#i.)A |H%%h/FH%F))$.&AF#)LL MM&!$,/KNN6/ * 	v%rT   c                 B   U R                  US5      nU R                  S U5      nU R                  US5      nU R                  0 SU_SU_SU R	                  U5      _SU R                  US5      _SU R                  US	5      _S
U R                  US5      _SU R                  US5      _SU R                  US5      _SU R                  US5      _SS _SU R                  US5      _SU_SU_SS _SS _SS _SS _U R                  US5      U R                  US5      US.EU5      $ )Nr   cr   r\   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   )rP   r   r   r\   r   r   s         rR   r   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)!
* + 	rT   c           	      p  #    U R                  5       I Sh  vN   U R                  USS5      nU R                  U R                  S5      nU R	                  USS5      nU R                  USSU5      nU R	                  USS5      nU R                  USU05      nU R                  USS/5      nU R                  U5      nS	S
U00n	U R                  USXR                  X5      5      I Sh  vN n
U R                  (       a   [        U
[        5      (       d  0 nXU
S   '   U$ U R                  U
SU5      $  GN NK7f)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    r1   zorderbook/top/{speed}/batchr   r   100msr^   ru   bidaskr   )r|   r}   rb   rJ   r   r   r   r   
market_idsr   rM   r   r   r   r   )rP   ru   r^   rJ   r   r1   r   rs   r   rp   r   r   s               rR   watch_bids_askshitbtc.watch_bids_asks  s>     !!!%%gtU;//$,,@(((<YZ##FHo}U  ':""6GU+;<6Hg#67OOG,	9

  00xJZJZ[bJkll
??j$// "0:
8,-##J'BB) 	" ms#   D6D1CD6(D4)A	D64D6c                    U R                  US0 5      n[        UR                  5       5      n/ nSn[        S[	        U5      5       Hi  nXG   nU R                  U5      n	U	S   n
U R                  X8   U	5      nXR                  U
'   UR                  U5        US-   U
-   nUR                  X5        Mk     UR                  XV5        g )Nr   r	  r   r   rx   )
r   r   r   r   r   r   parse_ws_bid_askbidsasksr   r   r   s                rR   handle_bid_askhitbtc.handle_bid_ask  s     ~~gvr2%	q#i.)A |H%%h/FH%F**4>6BF$*MM&!MM&!$,/KNN6/ * 	v%rT   c                     U R                  US5      nU R                  US   UU R                  U5      U R                  US5      U R                  US5      U R                  US5      U R                  US5      US.U5      $ )Nr   r   r   r   r   r   )r   r\   r   r   r   r   r   r  )r   r  r   r   )rP   r   r   r\   s       rR   r  hitbtc.parse_ws_bid_ask  s    %%fc2	X&"Y/##FC0))&#6##FC0))&#6	!
 	 		rT   sincec           	      L  #    U R                  5       I Sh  vN   U R                  U5      nSSUS   /00nUb  X6S'   SnU R                  USU/U R                  Xd5      5      I Sh  vN nU R                  (       a  UR                  X5      nU R                  XUS5      $  N N;7f)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^   ru   rz   r   tradesr\   )r|   r   r   rM   r   getLimitfilter_by_since_limit)	rP   r   r  r   r^   r   rp   rs   r  s	            rR   watch_tradeshitbtc.watch_trades  s      !!!V$F4L>

 $G,,T8fXtGWGWX_Ghii??OOF2E))&LL 	" js"   B$B AB$&B"':B$"B$c                 (   U R                  USS0 5      n[        UR                  5       5      n[        S[	        U5      5       H  nXE   nU R                  U5      nU R                  U R                  SS5      nUS   n	U R                  U R                  U	5      n
U
c  [        U5      n
XR                  U	'   U R                  X6   U5      n[        S[	        U5      5       H  nU
R                  X   5        M     SU	-   nUR                  X5        M     U$ )Nr   r   r   r3   r0   r   ztrades::)safe_value_2r   r   r   r   r   r   rJ   rb   r  r   parse_ws_tradesr   r   )rP   r`   r   r   r   r   r   r   r3   r   storedr  jrn   s                 rR   handle_tradeshitbtc.handle_trades5  s    P   *hC%	q#i.)A |H%%h/F++DLL-NKH%F__T[[&9F~#K0&,F#))$.&AF1c&k*fi( +$v-KNN6/ * rT   r   c                 8   U R                  U5      n/ n[        S[        U5      5       H8  nU R                  U R	                  X   U5      U5      nUR                  U5        M:     U R                  USS5      nU R                  US5      n	U R                  XiX45      $ )Nr   r\   rz   r   )	to_arrayr   r   r   parse_ws_trader   	sort_by_2r   filter_by_symbol_since_limit)
rP   r  r   r  r   r^   r   r   trader   s
             rR   r  hitbtc.parse_ws_tradeso  s    v&q#f+&AKK 3 3FIv FOEMM%  ' T:!!&(300NNrT   c                 $   U R                  US5      nU R                  UU R                  US5      S UU R                  U5      U R                  US5      S U R                  US5      S U R                  US5      U R                  US5      S S S.U5      $ )Nr   r   r   r   pr   r  rz   orderr\   r   r   rZ   sidetakerOrMakerr   r   costfee)r   
safe_trader   r   )rP   r'  r   r\   s       rR   r$  hitbtc.parse_ws_tradey  s     %%eS1	""5#."Y/&&vx8$$UC0 %%eS1&&uc2 
  	rT   r>   	timeframec           	      \  #    U R                  U R                  X"5      nSU-   nU R                  U5      nSSUS   /00n	Ub  XIS   S'   U R                  USU/U R	                  X5      5      I Sh  vN n
U R
                  (       a  U
R                  X5      nU R                  XUS5      $  N97f)	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^   ru   rz   Nr   candlesr   )r   rK   r   r   rM   r   r  r  )rP   r   r3  r  r   r^   periodrs   r   rp   ohlcvs              rR   watch_ohlcvhitbtc.watch_ohlcv  s      !!$//9HF"V$F4L>

 ).Hg&++D)fXtGWGWX_Ghii??NN61E))%qAA js   A.B,0B*1:B,c                    U R                  USS0 5      n[        UR                  5       5      nU R                  US5      nUR	                  S5      nU R                  US5      nU R                  U5      n[        S[        U5      5       H  n	XI   n
U R                  U
5      nUS   nU R                  U R                  U0 5      U R                  U'   U R                  U R                  U   U5      nUc9  U R                  U R                  SS	5      n[        U5      nXR                  U   U'   U R                  X:   U5      n[        S[        U5      5       H  nUR                  UU   5        M     S
U-   nUR!                  UU5        M     U$ )Nr   r   r{   r   r   r   r   
OHLCVLimitr0   z	candles::)r  r   r   r   r   find_timeframer   r   r   rb   ohlcvsr   rJ   r   parse_ws_ohlcvsr   r   )rP   r`   r   r   r   channelsplitChannelr6  r3  r   r   r   r   r  r   r=  r  rn   s                     rR   handle_ohlcvhitbtc.handle_ohlcv  sk   D   *hC%	""7D1}}S)!!,2''/	q#i.)A |H%%h/FH%F"&//$++vr"JDKK__T[[%8)DF~))$,,dK.u517F#I.))$.&AF1c&k*fQi( +%.KNN6;/ * rT   c           	          U R                  US5      U R                  US5      U R                  US5      U R                  US5      U R                  US5      U R                  US5      /$ )Nr   r   r   r   r   r   )r   r   )rP   r7  r   s      rR   parse_ws_ohlcvhitbtc.parse_ws_ohlcv  sn     eS)UC(UC(UC(UC(UC(
 	
rT   c                 f  #    U R                  5       I Sh  vN   SnSnUb  U R                  U5      nU R                  SXd5      u  pTU R                  USSSSS.5      nU R	                  XqU5      I Sh  vN nU R
                  (       a  UR                  X5      nU R                  XUS5      $  N N;7f)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swapro   r\   )r|   r   handle_market_type_and_paramsget_supported_mappingr   r   r  r  )	rP   r   r  r   r^   
marketTyper   rs   orderss	            rR   watch_ordershitbtc.watch_orders  s      !!!
[[(F!??v^
))*$(')	7
  --dFCC??OOF2E))&LL 	" Ds"   B1B-AB13B/4:B1/B1c                 T   U R                   c,  U R                  U R                  S5      n[        U5      U l         U R	                  US/ 5      n[        U[        5      (       a4  [        S[        U5      5       H  nXE   nU R                  XU5        M     U$ U R                  XU5        U$ )NordersLimitr^   r   )
rP  r   rJ   r   rb   r   r   r   r   handle_order_helper)rP   r`   r   r   r   r   r,  s          rR   handle_orderhitbtc.handle_order  s    | ;;%%dllMBE07DKw"5dD!!1c$i(((%@ )
  $$Vd;rT   c                 b   U R                   nU R                  USS5      nU R                  US5      nUR                  S5      nU R                  US5      nU R	                  U5      n	U R                  U5      n
UR                  U
5        UR                  XH5        UR                  XHS-   U	-   5        g )N
instrumentr   r1   _orderr   rx   )rP  safe_string_lower_2r   r   r  parse_orderr   r   )rP   r`   r   r,  rP  r   r1   splitMethodrn   r   parseds              rR   rU  hitbtc.handle_order_helperg  s    ++E<J!!'84ll8,&&{A6!!(+!!%(fv+vT1F:;rT   c                    U R                  US5      nU R                  US5      nU R                  UU R                  US5      U R                  US5      UU R                  U5      U R	                  XB5      S U R                  US5      U R                  US5      U R                  US5      U R                  US5      S U R                  US	5      S S S
.S.U5      $ )N
created_atr   trade_idrz   r-  trade_takertrade_pricetrade_quantity	trade_fee)r/  currencyrater+  )r   r   r1  r   r   )rP   r'  r   r\   r   s        rR   parse_ws_order_tradehitbtc.parse_ws_order_trades  s    6 %%e\:	##E84""5*5%%eT2"Y/&&x8$$UF3 ,,UMB%%e];&&u.>?((<  
$ % 	rT   c                 <   U R                  US5      nU R                  US5      nU R                  XB5      nU R                  US5      nS nUb  U R                  X5      nU/nU R                  US5      nU R                  US5      n	S n
U	S:X  a  U R                  U	5      n
OU R                  U5      n
U R	                  0 SU_SU R                  US5      _S	U R                  US
5      _SU_SU R                  U5      _SS _SUS   _SU R                  US5      _SU R                  US5      _SU R                  US5      _SU R                  US5      _SU R                  US5      _SU R                  US5      _SU R                  US5      _SS _SS _SS _U
S US S.EU5      $ )Nra  r   rb  statusreport_typecanceledr  rz   clientOrderIdclient_order_idr\   r   lastTradeTimestampr   r   quantityrZ   r-  timeInForcetime_in_forcepostOnly	post_only
reduceOnlyreduce_onlyfilled	remainingr/  )rl  r   r  r0  )r   r   ri  parse_order_status
safe_orderr   safe_string_upperrb   )rP   r,  r   r\   r   tradeIdr  r'  	rawStatusrm  parsedStatuss              rR   parse_ws_orderhitbtc.parse_ws_order  s5   6 $$UL9	##E84!!(3""5*5--e<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# 
$ #+ 
, - 	rT   c                 B  #    U R                  5       I Sh  vN   SnU R                  SSU5      u  p!U R                  USSSS.5      nU R                  USS5      nU R	                  US5      nSU0nU R                  USU R                  XQ5      5      I Sh  vN $  N N7f)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)rJ  rL  ro   modebatches)r|   rM  rN  r   r   r   r   )rP   r^   rZ   rs   r  rp   s         rR   watch_balancehitbtc.watch_balance  s      !!!99.$PVW))$,/11
 
 	:66*D
 ++D$G8TUUU 	" Vs"   BBA?BBBBrZ   r-  r   r   c           
        #    U R                  5       I Sh  vN   U R                  U5      nSnSn	U R                  SXv5      u  pSn
U R                  SU5      u  pU R	                  XyX#XEX5      u  pU R                  X5      nU	S:X  a  U R                  SU5      I Sh  vN $ U	S:X  d  U
b  U R                  SU5      I Sh  vN $ U R                  SU5      I Sh  vN $  N NC N" N
7f)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createOrderrL  futures_new_orderrK  margin_new_orderspot_new_order)r|   r   rM  handle_margin_mode_and_paramscreate_order_requestr   r   )rP   r   rZ   r-  r   r   r^   r   rp   rO  
marginModes              rR   create_order_wshitbtc.create_order_ws  s     * !!!V$
!??v^

!??vV
33FTZcmv++g.++,?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&rz   c                   #    U R                  5       I Sh  vN   SnSU0nUb  U R                  U5      nSnU R                  SXC5      u  pcU R                  SU5      u  pxU R	                  XX5      nUS:X  a  U R                  SU5      I Sh  vN $ US:X  d  Ub  U R                  SU5      I Sh  vN $ U R                  SU5      I Sh  vN $  N NC N" N
7f)	a1  

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>`
Nrp  r)   rL  futures_cancel_orderrK  margin_cancel_orderspot_cancel_order)r|   r   rM  r  r   r   )	rP   rz   r   r^   r   rp   rO  r  querys	            rR   cancel_order_wshitbtc.cancel_order_ws(  s      !!!r
 [[(F
!??QW`
 >>PVW
++g-++,BGLLLH$**@++,A7KKK++,?III! 	" MKIsF   CCA4CC"C.C/CC	CCCCc                   #    U R                  5       I Sh  vN   SnUb  U R                  U5      nSnU R                  SX25      u  pBSnU R                  SU5      u  pRUS:X  a  U R	                  SU5      I Sh  vN $ US:X  d  Ub  [        U R                  S-   5      eU R	                  SU5      I Sh  vN $  N NA N7f)a'  

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+   rL  futures_cancel_ordersrK  z5 cancelAllOrdersWs is not supported for margin ordersspot_cancel_orders)r|   r   rM  r  r   r   rz   )rP   r   r^   r   rO  r  s         rR   cancel_all_orders_wshitbtc.cancel_all_orders_wsI  s      !!![[(F
!??@SU[d

!??@SU[\
++,CVLLLH$**@tww)``aa++,@&III 	" M Js4   B>B8A!B>8B:9:B>3B<4B>:B><B>c                   #    U R                  5       I Sh  vN   Sn0 nUb  U R                  U5      nUS   US'   SnU R                  SXT5      u  ptSnU R                  SU5      u  pUS:X  a  U R	                  SU5      I Sh  vN $ US:X  d  Ub  U R	                  SU5      I Sh  vN $ U R	                  S	U5      I Sh  vN $  N NC N" N
7f)
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>`
Nrz   r   r*   rL  futures_get_ordersrK  margin_get_ordersspot_get_orders)r|   r   rM  r  r   )	rP   r   r  r   r^   r   rp   rO  r  s	            rR   fetch_open_orders_wshitbtc.fetch_open_orders_wse  s       !!![[(F &tGH
!??@SU[d

!??@SU[\
++,@'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                     U R                  US5      nU R                  US5      nU R                  U5      nU R                  U R                  U5      U l        UR                  U R                  U5        g )Nr1   r^   )r   rb   parse_balancerM   balancer   )rP   r`   r   rn   r^   r  s         rR   handle_balancehitbtc.handle_balance  s`      &&w9(3$$V,''g>t||[1rT   c                     U$ N )rP   r`   r   s      rR   handle_notificationhitbtc.handle_notification  s	     rT   c                 h   U R                  US5      nU R                  US0 5      n[        U[        5      (       aU  / n[	        S[        U5      5       H'  nU R                  XF   5      nUR                  U5        M)     UR                  XS5        U$ U R                  U5      nUR                  Xs5        U$ )Nrz   r   r   )	r   rb   r   r   r   r   r  r   r   )rP   r`   r   rn   r   parsedOrdersr   parsedOrders           rR   handle_order_requesthitbtc.handle_order_request  s    8 &&w5(B7fd##L1c&k*"11&)<##K0 + NN<5  --f5KNN;4rT   c                    U R                  X5      (       a  g U R                  USS5      nUb  UR                  S5      nU R                  US5      nUS:X  a  U R                  US5      nUb  US:X  a  SnU R                  U R
                  U R                  U R                  U R                  U R                  U R                  U R                  U R                  U R                  U R                  U R                  U R                  S	.nU R                  Xc5      nUb	  U" X5        g g U R                  US
5      nU R                  US5      n	U	b  U R                  X5        USL a  SU;  a  U R                  X5        [        U[        5      (       a=  U R                  US0 5      n
[!        U5      nUS:X  d  SU
;   a  U R                  X5        g g g )Nr{   r1   r   r   r   r   toporderbook/top)r5  r   r  r   r  
spot_orderspot_ordersmargin_ordermargin_ordersfutures_orderfutures_ordersspot_balancefutures_balancer   rp  Trz   )handle_errorr   r   r   rA  r   r   r   r  rV  r  rb   r  handle_authenticater   r   r   )rP   r`   r   r?  r@  channel2methodsr1   r   ro  firstarrayLengths               rR   handle_messagehitbtc.handle_message  s   V--$$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 __W6F!v' " __Wh7F ,,V5FGM())&:$((9&$''26!&k1$*;u*D--f> +E	 (rT   c                 H   U R                  US5      nSnU(       a/  U R                  UR                  U5      nUR                  S5        U$ [        U R                  S-   U R                  U5      -   5      nUR                  Xd5        XAR                  ;   a  UR                  U	 U$ )Nr   rV   T )rb   futuresr   r   rz   jsonrejectrc   )rP   r`   r   successrn   ro   errors          rR   r  hitbtc.handle_authenticate  s     //'84%__V^^[AFNN4  	 (#		'8J(JKEMM%-222((5rT   c                 P   U R                  US5      nUb   U R                  US5      nU R                  US5      nU R                  US5      nU R                  S-   U-   nU R                  U R                  S   XG5        U R                  U R                  S   XW5        [        U5      eg ! [         ar  n[        U[        5      (       a0  Sn	UR                  X5        XR                  ;   a  UR                  U		 O#U R                  US	5      n
UR                  X5         S nAg
S nAff = f)Nr  coder   descriptionr  exactbroadrV   rz   T)rb   r   rz   throw_exactly_matched_exception
exceptionsthrow_broadly_matched_exceptionr   	Exceptionr   r   r  rc   )rP   r`   r   r  r  errorMessager  feedbackern   rz   s              rR   r  hitbtc.handle_error	  s    1uf5#//yA"..umD77S=;644T__W5Mt^44T__W5M|f#H--   	a!455"1KMM!1"&:&::"00=))'48BMM!(	s   BB) )
D%3A(D  D%)r  rP  r  )@__name__
__module____qualname____firstlineno__r   rO   rq   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   r8  rA  rD  r
   rQ  rV  rU  ri  r  r   r  r   r   floatr	   r  r  r  r  r  r  r  r  r  r  __static_attributes____classcell__)rQ   s   @rR   r   r      s   ?# ?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 ?CQUdhqs B B B3 B^a Bx|  ~B  yC 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4PU; J8 8<$]ajl H HC HWZ Hquv{q| HB2V 2,& '6 'R)?V )?V& & 6    rT   r   )ccxt.async_supportccxt ccxt.async_support.base.ws.cacher   r   r   ri   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  rT   rR   <module>r     sT     f f  B  B  B  B 4  * 0 )XT&& XrT   