
    ei                         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dS )    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d9dZdi fdedefdZdefdZd9d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!d9dZ"dddi fd	ed edededee#         f
d!Z$defd"Z%d9d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*d9d'Z+d9d(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d0Z4di fd	e	dee'         fd1Z5dddi fd	e	dededee'         fd2Z6defd3Z7defd4Z8defd5Z9defd6Z:defd7Z;defd8Z< xZ=S ):hitbtcreturnc                     |                      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    M/var/www/html/volatility/venv/lib/python3.11/site-packages/ccxt/pro/hitbtc.pyrN   zhitbtc.describe   s,   fd 3 3 < < > ># $!%#)."& $#"!&!%!%%)%) & "H#J  "M#O    $.  .! 5" .#     Tw>A
 >A
 > > >	    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

        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reusableFuture
safe_valuesubscriptionsmillisecondshmacencodenumber_to_stringsecrethashlibsha256apiKeywatch)	rO   urlmessageHashr^   futurerT   rZ   r[   requests	            rQ   authenticatezhitbtc.authenticateT   s.      	'')))it$Y/%S!!&&{33(<kJJ ))++I		$++d.C.CI.N.N"O"OQUQ\Q\]a]hQiQikrky  |A  B  BI!##{!*!*	  G JJsK+>>>$ ||||||rR   NnamemessageHashPrefixsymbolsc                   K   |                                   d{V  |                     |          }|                    d          dk    }| j        d         d         d         }g }|D|sBt	          dt          |                    D ]#}|                    |dz   ||         z              $n|                    |           d|                                 |d	}	|                     |	|          }
| 	                    |||
|           d{V S )
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   rp   rq   rr   r\   isBatchrk   messageHashesirv   rn   s              rQ   subscribe_publiczhitbtc.subscribe_public   s=      !!!!!!!!!%%g..))G$$)it$X.w1c'll++ L L$$%6%=
%JKKKKL   !2333!**,,
 
	
 ++i00((mWmTTTTTTTTTrR   symbolc                 v  K   |                                   d{V  |                                  d{V  | j        d         d         d         }|                    d          }|                     |d          }||dz   |z   }|||                                 d}|                     ||||           d{V S )	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   ru   r0   r\   rw   )ry   ro   rH   splitsafe_stringr   rj   )rO   rp   r   r\   rk   	splitNamerl   rv   s           rQ   subscribe_privatezhitbtc.subscribe_private   s       !!!!!!!!!!!!!!!!!!it$Y/JJ|,,	&&y!44%,v5K**,,
 
	
 ZZ[)[IIIIIIIIIrR   c                 *  K   |                                   d{V  |                                  d{V  | j        d         d         d         }t          |                                           }|||d}|                     ||||           d{V S )z
 @ignore
        :param str name: websocket endpoint name
        :param dict [params]: extra parameters specific to the hitbtc api
        Nr-   r   r,   r   )ry   ro   rH   strr   rj   )rO   rp   r\   rk   rl   rv   s         rQ   trade_requestzhitbtc.trade_request   s       !!!!!!!!!!!!!!!!!!it$Y/$**,,''
 
	
 ZZ[)[IIIIIIIIIrR   limitc           	        K   |                      | j        d          }|                     |dd          }|                     |dd|          }|                     |dd          }|                     |dd          }|dk    rd	|z   d
z   |z   dz   }n|dk    rd	|z   d
z   |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

        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\   rr   rw   
orderbooksN)r`   rI   r   safe_string_2marketr   rL   r   )rO   r   r   r\   rI   r   rp   r   speedr   rn   	orderbooks               rQ   watch_order_bookzhitbtc.watch_order_book   sE     $ //$,0@AA(((<LMM!!&(O]SS  $77  %88... 5(3.6=DD666 5(3.6CDV$$F4L>

 //lVHdN^N^_fhnNoNopppppppp	   rR   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   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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 	3rR   c                     |                      |d          }|                      |d          }|                    ||           d S )Nr      )safe_numberstore)rO   booksidedeltapriceamounts        rQ   handle_deltazhitbtc.handle_delta   sF      **!!%++uf%%%%%rR   c                 ~    t          dt          |                    D ]}|                     |||                    d S )Nr   )r|   r}   r   )rO   r   deltasr   s       rQ   r   zhitbtc.handle_deltas%  sH    q#f++&& 	3 	3Ahq	2222	3 	3rR   c                 l   K   |                      |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

        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)  sE       ))6(F;;;;;;;;vv...rR   c           	      N  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$t          |t                     si }|||d         <   |S |                     |d|          S )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\   rr   tickersr   )ry   rz   r`   rI   r   r   implode_paramsomitr~   r|   r}   	market_idr   rL   
newUpdates
isinstancer   filter_by_array)rO   rr   r\   rI   r   r0   r   rp   r   r   r   rn   
newTickersr   s                 rQ   r   zhitbtc.watch_tickers;  s      !!!!!!!!!%%g..//$,>>(((<RSS##FHo}UU  $77""6GU+;<<6Hg#677	?S!!!!1c'll++ + +>>'!*55  ****9

  00y'4K[K[\cekKlKlmmmmmmmm
? 	j$//  "0:
8,-##J'BBBrR   c                    |                      |di           }t          |                                          }g }d}t          dt	          |                    D ]}||         }|                     |          }	|	d         }
|                     ||         |	          }|| j        |
<   |                    |           |dz   |
z   }|	                    ||           |	                    ||           d S )Nr   r   r   r   ru   )
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3%%	q#i..)) 	0 	0A |H%%h//FH%F))$x.&AAF#)DL MM&!!!$,/KNN6;////vu%%%%%rR   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   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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)!
 !
 !
* +  	rR   c           	        K   |                                   d{V  |                     |dd          }|                     | j        d          }|                     |dd          }|                     |dd|          }|                     |dd          }|                     |d|i          }|                     |ddg          }|                     |          }d	d
|ii}	| 	                    |d|| 
                    |	|                     d{V }
| j        r$t          |
t                    si }|
||
d         <   |S |                     |
d|          S )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\   rr   bidaskr   )ry   rz   r`   rI   r   r   r   r   
market_idsr   rL   r   r   r   r   )rO   rr   r\   rI   r   r0   r   rp   r   rn   r   r   s               rQ   watch_bids_askszhitbtc.watch_bids_asks  s      !!!!!!!!!%%gtU;;//$,@@(((<YZZ##FHo}UU  '::""6GU+;<<6Hg#677OOG,,	9

  00x$JZJZ[bdjJkJkllllllll
? 	j$//  "0:
8,-##J'BBBrR   c                    |                      |di           }t          |                                          }g }d}t          dt	          |                    D ]}||         }|                     |          }	|	d         }
|                     ||         |	          }|| j        |
<   |                    |           |dz   |
z   }|	                    ||           |	                    ||           d S )Nr   r   r   r   ru   )
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2%%	q#i..)) 	0 	0A |H%%h//FH%F**4>6BBF$*DM&!MM&!!!$,/KNN6;////vu%%%%%rR   c                 >   |                      |d          }|                     |d         ||                     |          |                     |d          |                     |d          |                     |d          |                     |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2	X&"Y//##FC00))&#66##FC00))&#66	!
 	!
 	 	 		rR   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

        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\   rr   rw   r   tradesrZ   )ry   r   r   rL   r   getLimitfilter_by_since_limit)	rO   r   r  r   r\   r   rn   rp   r  s	            rQ   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LrR   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   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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;////rR   r   c                    |                      |          }g }t          dt          |                    D ]G}|                     |                     ||         |          |          }|                    |           H|                     |dd          }|                     |d          }	|                     ||	||          S )Nr   rZ   rw   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 FOOEMM%    T::!!&(3300NNNrR   c                 d   |                      |d          }|                     ||                     |d          d ||                     |          |                     |d          d |                     |d          d |                     |d          |                     |d          d d d|          S )Nr   r   r   r   pr   r   rw   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1	""5#.."Y//&&vx88$$UC00 %%eS11&&uc22 
  
   	rR   r=   	timeframec           	      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

        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\   rr   rw   Nr   candlesr   )r   rJ   r   r   rL   r   r  r  )rO   r   r  r  r   r\   periodrp   r   rn   ohlcvs              rQ   watch_ohlcvzhitbtc.watch_ohlcv  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ArR   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   rx   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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;////rR   c           	         |                      |d          |                     |d          |                     |d          |                     |d          |                     |d          |                     |d          gS )Nr   r   r   r   r   r   )r   r   )rO   r"  r   s      rQ   parse_ws_ohlcvzhitbtc.parse_ws_ohlcv  s     eS))UC((UC((UC((UC((UC((
 	
rR   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

        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swaprm   rZ   )ry   r   handle_market_type_and_paramsget_supported_mappingr   r   r  r  )	rO   r   r  r   r\   
marketTyper   rp   orderss	            rQ   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LrR   c                 x   | j         /|                     | j        d          }t          |          | _         |                     |dg           }t          |t                    r@t          dt          |                    D ]!}||         }| 	                    |||           "n| 	                    |||           |S )NordersLimitr\   r   )
r8  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    | ;%%dlMBBE077DKw"55dD!! 	<1c$ii(( A AQ((%@@@@A $$VWd;;;rR   c                    | j         }|                     |dd          }|                     |d          }|                    d          }|                     |d          }|                     |          }	|                     |          }
|                    |
           |                    ||           |                    ||dz   |	z              d S )N
instrumentr   r0   _orderr   ru   )r8  safe_string_lower_2r   r   r   parse_orderr~   r   )rO   r^   r   r  r8  r   r0   splitMethodrl   r   parseds              rQ   r<  zhitbtc.handle_order_helperg  s    ++E<JJ!!'844ll8,,&&{A66!!(++!!%((fv{+++v{T1F:;;;;;rR   c                    |                      |d          }|                     |d          }|                     ||                     |d          |                     |d          ||                     |          |                     ||          d |                     |d          |                     |d          |                     |d          |                     |d          d |                     |d	          d d d
d|          S )N
created_atr   trade_idrw   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4""5*55%%eT22"Y//&&x88$$UF33 ,,UMBB%%e];;&&u.>??((<<   
  
$ %  	rR   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 )NrF  r   rG  statusreport_typecanceledr   rw   clientOrderIdclient_order_idrZ   r   lastTradeTimestampr   r   quantityrX   r  timeInForcetime_in_forcepostOnly	post_only
reduceOnlyreduce_onlyfilled	remainingr  )rP  r   r  r  )r   r   rN  parse_order_status
safe_orderr   safe_string_upperr`   )rO   r  r   rZ   r   tradeIdr  r  	rawStatusrQ  parsedStatuss              rQ   parse_ws_orderzhitbtc.parse_ws_order  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# 
$ #+ 
  
  
, -  	rR   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

        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)r2  r4  rm   modebatches)ry   r5  r6  r   r   r   r   )rO   r\   rX   rp   ri  rn   s         rQ   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UrR   rX   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

        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createOrderr4  futures_new_orderr3  margin_new_orderspot_new_order)ry   r   r5  handle_margin_mode_and_paramscreate_order_requestr   r   )rO   r   rX   r  r   r   r\   r   rn   r7  
marginModes              rQ   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FrR   rw   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  

        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>`
        NrT  r(   r4  futures_cancel_orderr3  margin_cancel_orderspot_cancel_order)ry   r   r5  rq  r   r   )	rO   rw   r   r\   r   rn   r7  rs  querys	            rQ   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IrR   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 )am  

        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*   r4  futures_cancel_ordersr3  z5 cancelAllOrdersWs is not supported for margin ordersspot_cancel_orders)ry   r   r5  rq  r   r   rw   )rO   r   r\   r   r7  rs  s         rQ   cancel_all_orders_wszhitbtc.cancel_all_orders_wsI  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IrR   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  

        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>`
        Nrw   r   r)   r4  futures_get_ordersr3  margin_get_ordersspot_get_orders)ry   r   r5  rq  r   )	rO   r   r  r   r\   r   rn   r7  rs  s	            rQ   fetch_open_orders_wszhitbtc.fetch_open_orders_wse  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GrR   c                     |                      |d          }|                     |d          }|                     |          }|                     | j        |          | _        |                    | j        |           d S )Nr0   r\   )r   r`   parse_balancerL   balancer   )rO   r^   r   rl   r\   r  s         rQ   handle_balancezhitbtc.handle_balance  st      &&w99(33$$V,,''g>>t|[11111rR   c                     |S N )rO   r^   r   s      rQ   handle_notificationzhitbtc.handle_notification  s	     rR   c                    |                      |d          }|                     |di           }t          |t                    rig }t	          dt          |                    D ]2}|                     ||                   }|                    |           3|                    ||           n+|                     |          }|                    ||           |S )Nrw   r   r   )	r   r`   r   r   r|   r}   re  r~   r   )rO   r^   r   rl   r   parsedOrdersr   parsedOrders           rQ   handle_order_requestzhitbtc.handle_order_request  s    8 &&w55(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rR   c                 ~   |                      ||          rd S |                     |dd          }||                    d          }|                     |d          }|dk    r |                     |d          }||dk    rd}| j        | 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 )Nrx   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   rT  Trw   )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   rS  firstarrayLengths               rQ   handle_messagezhitbtc.handle_message  s   VW-- 	F$$WdH=="==--L&&|Q77G+%%++L!<<'H,=,=-G,,,!3!%!4"/#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*DrR   c                 D   |                      |d          }d}|r1|                      |j        |          }|                    d           nTt          | j        dz   |                     |          z             }|                    ||           ||j        v r|j        |= |S )Nr   rT   T )r`   futuresr   r   rw   jsonrejectra   )rO   r^   r   successrl   rm   errors          rQ   r  zhitbtc.handle_authenticate  s     //'844% 	6__V^[AAFNN4    '#		'8J8J(JKKEMM%---f222(5rR   c                    |                      |d          }|&	 |                      |d          }|                     |d          }|                     |d          }| j        dz   |z   }|                     | j        d         ||           |                     | j        d         ||           t          |          # t          $ rv}t          |t                    r*d}	|
                    ||	           |	|j        v r|j        |	= n,|                     |d	          }
|
                    ||
           Y d }~d
S d }~ww xY wd S )Nr  coder   descriptionr  exactbroadrT   rw   T)r`   r   rw   throw_exactly_matched_exception
exceptionsthrow_broadly_matched_exceptionr   	Exceptionr   r   r  ra   )rO   r^   r   r  r  errorMessager  feedbackerl   rw   s              rQ   r  zhitbtc.handle_error	  s_    11uf55#//yAA"..umDD7S=;644T_W5MtU]^^^44T_W5M|]efff#H--- 	 	 	a!455 )"1KMM![111"f&:::"0=))'488BMM!R(((ttttt	 ts   B"B= =
D=A+D88D=r  )>__name__
__module____qualname__r   rN   ro   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
   r9  r=  r<  rN  re  r   rk  r   r   floatr	   rt  rz  r~  r  r  r  r  r  r  r  __classcell__)rP   s   @rQ   r   r      s       ?# ? ? ? ? ? ?B- - -^ \`hj U U3 U3 UQX U U U U4 @DB J JC J J J J J* 57 J J J J J J" @DB "! "!S "! "!S\ "! "! "! "!H73 73 73 73 73r& & &
3 3 3 68 / / /F / / / /$ 6:" #C #C7 #C #C #C #C #CJ4&F 4& 4& 4& 4&l5 5 5 5n 8<B  C  CW  C7  C  C  C  CD&V & & & &<    <@d[] M M MS M Mbfglbm M M M M68F 8 8 8 8t 8<$]ajl O Of OC OWZ O O O O   6 ?CQUdhqs B B B B3 B^a Bx|  ~B  yC B B B B87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: pt|~ #G #GC #Gy #G	 #G[` #Gil #G  DI #G #G #G #GJ <@ J J JS Ju J J J JB 8<B J J J4PU; J J J J8 8<$]ajl H H HC HWZ Hquv{q| H H H HB2V 2 2 2 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     s       f f f f f f f f f f  B  B  B  B  B  B  B  B  B  B  B  B  B  B  B  B  B  B  B  B  B  B  B  B  B  B  B  B  B  B 4 4 4 4 4 4       * * * * * * 0 0 0 0 0 0 ) ) ) ) ) )X X X X XT& X X X X XrR   