
    E#i                        S SK rS SKJrJrJrJr  S SK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\RB                  RD                  5      r"g)    N)
ArrayCacheArrayCacheBySymbolByIdArrayCacheBySymbolBySideArrayCacheByTimestamp)AsksBids)AnyBalancesBoolIntMarketOrder	OrderBookPositionStrStringsTickerTickersTrade)Client)List)ExchangeError)AuthenticationError)NotSupportedc                   T  ^  \ rS rSrS\4U 4S jjr0 4S jrS0 4S\S\S\4S	 jjr	0 4S\
4S
 jjrS\4S jrS rS\4S jrSS0 4S\S\S\S\\   4S jjrSS0 4S\\   S\S\S\\   4S jjrS0 4S\S\\   S\4S jjr0 4S\S\4S jjrS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SS0 4S\S\S\S\\   4S jjrS\4S jr S;S\!S\"4S jjr#S  r$S! r%SSS0 4S\S\S\S\\&   4S" jjr'S\4S# jr(S;S\"4S$ jjr)S\4S% jr*S& r+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\S\\/   4
S- jjr0S\4S. jr1S0 4S\S\S\24S/ jjr3S0 r4S1 r5S\4S2 jr6S\4S3 jr7S0 4S\\   S\S\24S4 jjr80 4S5 jr9S\4S6 jr:S\4S7 jr;S\S\<4S8 jr=S\4S9 jr>S:r?U =r@$ )<bitmart   returnc                 $  > U R                  [        [        U ]  5       0 SS_SS_SS_SS_SS_SS_SS_S	S
_SS
_SS
_SS
_SS
_SS
_SS
_SS
_SS
_SS
_S
SS
S.ESS	SSS.SSS.S.00SS
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/S0.S1.S2S30S4.5      $ )5NcreateOrderWsFeditOrderWsfetchOpenOrdersWsfetchOrderWscancelOrderWscancelOrdersWscancelAllOrdersWswsTwatchBalancewatchTickerwatchTickerswatchBidsAskswatchOrderBookwatchOrderBookForSymbolswatchOrderswatchTradeswatchTradesForSymbolsemulated)
watchOHLCVwatchPositionwatchPositionsapiz5wss://ws-manager-compress.{hostname}/api?protocol=1.1z6wss://ws-manager-compress.{hostname}/user?protocol=1.1)publicprivatez/wss://openapi-ws-v2.{hostname}/api?protocol=1.1z0wss://openapi-ws-v2.{hostname}/user?protocol=1.1)spotswapr8   )fetchBalanceSnapshotawaitBalanceSnapshotdepthdepth/increase100ignoreDuplicatesinflate1m3m5m15m30m45m1H2H3H4H1D1W1M)r@   rA   rB   rC   rD   rE   1h2h3h4h1d1wrL   )defaultTyper(   r,   r-   r/   r'   
timeframes	keepAlivei:  )hasurlsoptions	streaming)deep_extendsuperr   describe)self	__class__s    J/home/james-whalen/.local/lib/python3.13/site-packages/ccxt/pro/bitmart.pyr\   bitmart.describe   s   gt = ?u $U 	
   !% $U d  t    !$ +D t  t!" (#$ #!+"&). &]'_!
 'X'Y!	  &,0,1! 0# 0- '  t    -%N UWNB
 N N	    c                   #    U R                  U5      nU R                  U R                  S   S   U   S   5      n0 nS nUS:X  a  SU-   S-   US   -   nSU/S	.nOCS
U-   S-   US   -   nU R                  US5      n	U	b  U R	                  US5      nUSU	-   -  nSU/S.nU R                  XhU R                  Xt5      U5      I S h  vN $  N7f)Nr5   r'   r6   r8   spot/:id	subscribeopargsfutures/speedactionri   )marketimplode_hostnamerW   safe_stringomitwatchrZ   )
r]   channelsymboltypeparamsrn   urlrequestmessageHashrk   s
             r_   rf   bitmart.subscribee   s     V$##DIIe$4T$:4$@$JK6>!G+c1F4L@K!$G
 %w.4vd|CK$$VW5E 673sU{*%$G ZZ$2B2B72SU`aaaas   C C	CC	Nrs   ru   symbolsc                   #    U R                  X2SS5      nU R                  U R                  S   S   U   S   5      nUS:X  a  SOSnUS:X  a  SOS	n/ n/ n	[        S
[	        U5      5       HR  n
U R                  X:   5      nUS-   U-   S-   US   -   nUR                  U5        U	R                  US-   US   -   5        MT     SU0nSX'   U R                  XYU R                  X5      U5      I S h  vN $  N7f)NFTr5   r'   r6   r8   futuresrh   rm   r   /rd   re   rt   ri   rf   )	market_symbolsro   rW   rangelenrn   appendwatch_multiplerZ   )r]   rs   ru   r{   rv   rw   channelType
actionTyperawSubscriptionsmessageHashesirn   messagerx   s                 r_   subscribe_multiplebitmart.subscribe_multiple|   s    %%gUDA##DIIe$4T$:4$@$JK!%fi"fnT8
q#g,'A[[,F!C''1C7&,FG##G,  31A!AB	 ( $
 *((T=M=Mg=^`pqqqqs   C+C4-C2.C4c                   #    U R                  5       I Sh  vN   SnU R                  SSU5      u  p!U R                  X!5      I Sh  vN   0 nUS:X  a  SS/S.nOS/ SQS.nS	U-   nU R                  U R                  S
   S   U   S   5      nU R                  U5      nU R                  XbU5        SnSnU R                  U R                  SSS5      u  pqU R                  U R                  SSS5      u  pU(       a#  U(       a  UR                  US-   5      I Sh  vN   U R                  XTU R                  X15      U5      I Sh  vN $  GN2 GN N4 N7f)a  

https://developer-pro.bitmart.com/en/spot/#private-balance-change
https://developer-pro.bitmart.com/en/futuresv2/#private-assets-channel

watch balance and get the amount of funds available for trading or funds locked in orders
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict: a `balance structure <https://docs.ccxt.com/#/?id=balance-structure>`
Nr8   r(   rf   z spot/user/balance:BALANCE_UPDATErg   )zfutures/asset:USDTzfutures/asset:BTCzfutures/asset:ETHrl   balance:r5   r'   r7   r:   Tr;   Fz:fetchBalanceSnapshot)load_marketshandle_market_type_and_paramsauthenticatero   rW   clientset_balance_cachehandle_option_and_paramsrX   futurerr   rZ   )	r]   rv   ru   rx   ry   rw   r   r:   r;   s	            r_   watch_balancebitmart.watch_balance   sq     !!!99.$PVW---6>!;<G &XG !4'##DIIe$4T$:4$@$KLS!v[9##'+'D'DT\\Sacy{  (A$'+'D'DT\\Sacy  |A  (B$$8--'> >???ZZ$2B2B72SU`aaa3 	" 	.* @asF   EE/EECEE)EEEEEEr   c                 8   X1R                   ;   a  g U R                  U R                  S5      nU R                  USS5      nU(       aE  US-   S-   nXaR                  ;  a.  UR                  U5        U R                  U R                  XU5        0 U R                  U'   g )Nr(   r:   Trd   )	subscriptions
safe_valuerX   	safe_boolr}   r   spawnload_balance_snapshotbalance)r]   r   ru   subscribeHashrX   snapshotry   s          r_   r   bitmart.set_balance_cache   s    000//$,,?>>'+A4H*'==K>>1k*

455vDQTra   c                 :  #    U R                  SU05      I S h  vN nU R                  X@R                  U R                  U0 5      5      U R                  U'   UR                  U   nUR                  5         UR                  U R                  U   SU-   5        g  N7f)Nru   r   )fetch_balanceextendr   r   r}   resolve)r]   r   ry   ru   responser   s         r_   r   bitmart.load_balance_snapshot   s     ++VTN;;![[??4<<QUWY3Z[T,t||D):+<= <s   BBB Bc                 t   U R                  USS5      nU R                  US5      nUc  g UR                  S5      S:  nU(       a  SOSnX R                  U   S'   U(       Ga  [	        U[
        5      (       d  g [        S[        U5      5       H  nU R                  US5      nXR                  U   S	'   U R                  U5      U R                  U   S
'   U R                  XG   S/ 5      n	[        S[        U	5      5       Hu  n
X   nU R                  5       nU R                  US5      nU R                  U5      nU R                  US5      US'   U R                  US5      US'   XR                  U   U'   Mw     M     OnU R                  US5      nU R                  U5      nU R                  5       nU R                  US5      US'   U R                  US5      US'   XR                  U   U'   U R                  U R                  U   5      U R                  U'   SU-   nUR                  U R                  U   U5        g )Ntablegroupdatar8   r   r9   info
event_time	timestampdatetimebalance_detailsccyav_balfreefz_balusedcurrencyavailable_balancefrozen_balancer   )safe_string_2r   findr   
isinstancelistr   r   safe_integeriso8601accountrp   safe_currency_codesafe_balancer   )r]   r   r   rs   r   isSpotru   r   r   balanceDetailsii
rawBalancer   
currencyIdcodery   s                   r_   handle_balancebitmart.handle_balance   s   < $$Wgw?w/<,,v&!+vV%,T6"dD))1c$i( --g|D	2;T";/15i1HT":.!%:KR!P3~#67B!/!2J"llnG!%!1!1*e!DJ22:>D&*&6&6z8&LGFO&*&6&6z8&LGFO/6LL&t, 8 ) ))$
;J**:6DllnG"..t5HIGFO"..t5EFGFO'.LLt$!..t||D/ABT 4't||D);7ra   rt   sincelimitc                 F   #    U R                  U/X#U5      I Sh  vN $  N7f)a+  

https://developer-pro.bitmart.com/en/spot/#public-trade-channel
https://developer-pro.bitmart.com/en/futuresv2/#public-trade-channel

get the list of most recent trades for a particular symbol
:param str symbol: unified symbol of the market to fetch trades for
:param int [since]: timestamp in ms of the earliest trade to fetch
:param int [limit]: the maximum amount of trades to fetch
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict[]: a list of `trade structures <https://docs.ccxt.com/#/?id=public-trades>`
N)watch_trades_for_symbols)r]   rt   r   r   rv   s        r_   watch_tradesbitmart.watch_trades  s$      22F8U6RRRRs   !!c                   #    U R                  5       I Sh  vN   SnU R                  SXU5      u  pnSnU R                  XeX5      I Sh  vN nU R                  (       a5  U R	                  US5      nU R                  US5      n	UR                  X5      nU R                  XrUSS5      n
U R                  SS	S5      (       a%  U R                  U
5      nU R                  US5      nU$ U
$  N N7f)
a  

https://developer-pro.bitmart.com/en/spot/#public-trade-channel

get the list of most recent trades for a list of symbols
:param str[] symbols: 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>`
Nr0   trader   rt   r   Tr/   r>   )r   get_params_for_multiple_subr   
newUpdates	safe_dictrp   getLimitfilter_by_since_limithandle_option!remove_repeated_trades_from_arraysort_by)r]   r{   r   r   rv   
marketTypechannelNametradesfirsttradeSymbolresultfiltereds               r_   r    bitmart.watch_trades_for_symbols  s      !!!
&*&F&FG^`gpv&w#V..{XX??NN61-E**5(;KOOK7E++F5+tTm-?FF==fEH||Hk:HO 	" Ys"   C,C(3C,
C*BC,*C,
methodNamec                     U R                  US SS5      n[        U5      nUS:  a  [        U R                  S-   U-   S-   5      eU R	                  US   5      nS nU R                  XU5      u  ptX'U/$ )NFT    z1() accepts a maximum of 20 symbols in one requestr   )r   r   r   re   rn   r   )r]   r   r{   r   rv   lengthrn   r   s           r_   r   #bitmart.get_params_for_multiple_sub8  s~    %%gtUDAWB;tww}z9<ooppWQZ(
!??
TZ[
V,,ra   c                    #    U R                  5       I Sh  vN   U R                  U5      nU R                  U/U5      I Sh  vN nX1   $  N4 N
7f)a  

https://developer-pro.bitmart.com/en/spot/#public-ticker-channel
https://developer-pro.bitmart.com/en/futuresv2/#public-ticker-channel

watches a price ticker, a statistical calculation with the information calculated over the past 24 hours for a specific market
:param str symbol: unified symbol of the market to fetch the ticker for
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict: a `ticker structure <https://docs.ccxt.com/#/?id=ticker-structure>`
N)r   rt   watch_tickers)r]   rt   rv   tickerss       r_   watch_tickerbitmart.watch_tickerB  sN      !!!V$**F8V<< 	"<s!   AA+AA	AAc                 8  #    U R                  5       I Sh  vN   U R                  U5      nSnU R                  SX25      u  pBU R                  SXAU5      I Sh  vN nU R                  (       a  0 nXVUS   '   U$ U R                  U R                  SU5      $  N N?7f)a  

https://developer-pro.bitmart.com/en/spot/#public-ticker-channel
https://developer-pro.bitmart.com/en/futuresv2/#public-ticker-channel

watches a price ticker, a statistical calculation with the information calculated over the past 24 hours for all markets of a specific list
:param str[] symbols: unified symbol of the market to fetch the ticker for
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict: a `ticker structure <https://docs.ccxt.com/#/?id=ticker-structure>`
Nr*   tickerrt   )r   get_market_from_symbolsr   r   r   filter_by_arrayr   )r]   r{   rv   rn   r   r   r   s          r_   r   bitmart.watch_tickersR  s      !!!--g6
!??PV_
..xfUU??G(.F8$%N##DLL(GDD 	" Vs"   BBABB>BBc                   #    U R                  5       I Sh  vN   U R                  USS5      nU R                  U5      nSnU R                  SX25      u  pBU R	                  U R
                  S   S   U   S   5      nUS:X  a  SOSnUS:X  a  S	OS
n/ n/ n	[        S[        U5      5       HF  n
U R                  X   5      nUR                  US-   US   -   5        U	R                  SX   -   5        MH     US:w  a  US-   /nSU0nSX'   U R                  XYX5      I Sh  vN nU R                  (       a  0 nXUS   '   U$ U R                  U R                  SU5      $  GND N@7f)a  

https://developer-pro.bitmart.com/en/spot/#public-ticker-channel
https://developer-pro.bitmart.com/en/futuresv2/#public-ticker-channel

watches best bid & ask for symbols
:param str[] symbols: unified symbol of the market to fetch the ticker for
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict: a `ticker structure <https://docs.ccxt.com/#/?id=ticker-structure>`
NFr+   r5   r'   r6   r8   r}   rh   rm   r   z/ticker:re   bidask:z/tickerri   rf   rt   )r   r   r   r   ro   rW   r   r   rn   r   r   r   r   bidsasks)r]   r{   rv   firstMarketr   rw   r   r   r   r   r   rn   rx   
newTickersr   s                  r_   watch_bids_asksbitmart.watch_bids_asksh  s     !!!%%gtU;227;
!??Q\e
##DIIe$4T$::$Fx$PQ!+v!5fI(F2T
q#g,'A[[,F##K*$<vd|$KL  WZ!78 (  +i 78$
 *..s7]]
??G,6Jx()N##DMM8WEE3 	"( ^s"   EEDEE>EEc                 d   U R                  US5      nUS Ln/ nU(       a  U R                  US/ 5      nOU R                  US0 5      /n[        U5      (       d  g [	        S[        U5      5       H?  nU R                  XV   5      nUS   nXpR                  U'   SU-   n	UR                  Xy5        MA     g )Nr   r   r   rt   r   )rp   	safe_listr   r   r   parse_ws_bid_askr   r   
r]   r   r   r   r   
rawTickersr   r   rt   ry   s
             r_   handle_bid_askbitmart.handle_bid_ask  s      '2t#
<J//'62>?J:q#j/*A**:=9FH%F$*MM&!#f,KNN6/ +ra   c                 l   U R                  US5      nU R                  X25      nU R                  US5      nU R                  US5      nU R                  UUU R	                  U5      U R                  USS5      U R                  USS5      U R                  USS5      U R                  US	S
5      US.U5      $ )Nrt   ms_task_px	ask_priceask_szask_volbid_px	bid_pricebid_szbid_vol)rt   r   r   ask	askVolumebid	bidVolumer   )rp   safe_marketr   safe_tickerr   r   )r]   r   rn   marketIdrt   r   s         r_   r   bitmart.parse_ws_bid_ask  s    ##FH5!!(3!!&(3%%ff5	"Y/%%fhD++FHiH%%fhD++FHiH	!
 	 		ra   c                 N  #    U R                  5       I Sh  vN   SnSnUb'  U R                  U5      nU R                  U5      nSU-   nSnU R                  SXT5      u  ptU R	                  Xt5      I Sh  vN   SnUS:X  a  Sn	Ub  XS   -  n	OSn	S	U	/S
.nOS	S/S.nU R                  U R                  S   S   U   S   5      n
U R                  XU R                  X5      U5      I Sh  vN nU R                  (       a  U$ U R                  U R                  XUS5      $  GN N N:7f)a>  
watches information on multiple orders made by the user

https://developer-pro.bitmart.com/en/spot/#private-order-progress
https://developer-pro.bitmart.com/en/futuresv2/#private-order-channel

:param str symbol: unified market symbol of the market orders were made in
:param int [since]: the earliest time in ms to fetch orders for
:param int [limit]: the maximum number of order structures to retrieve
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict[]: a list of `order structures <https://docs.ccxt.com/#/?id=order-structure>`
Norderszorders::r8   r.   zspot/user/order:re   zspot/user/orders:ALL_SYMBOLSrf   rg   zfutures/orderrl   r5   r'   r7   T)r   rt   rn   r   r   ro   rW   rr   rZ   r   filter_by_symbol_since_limitr  )r]   rt   r   r   rv   rn   ry   ru   rx   argsRequestrw   	newOrderss               r_   watch_ordersbitmart.watch_orders  sI     !!![[(F[[(F$v-K99-X---6>,K!d|+<!$G &()G ##DIIe$4T$:4$@$KL**St7G7G7XZeff	??00fUTXYY= 	" 	.$ gs5   D%DAD%3D!4A4D%(D#)6D%!D%#D%c                 d   U R                  US5      nUc  g [        U5      n/ n0 nUS:  a  U R                  U R                  SS5      nU R                  c  [        U5      U l        U R                  n[        S[        U5      5       HA  n	U R                  X9   5      n
UR                  U
5        UR                  U
5        U
S   nSXk'   MC     Sn[        UR                  5       5      n[        S[        U5      5       H   n	X   nUS-   U-   nUR                  X^5        M"     UR                  X\5        g )	Nr   r   ordersLimit  rt   Tr  ::)r   r   r   rX   r  r   r   parse_ws_orderr   r   keysr   )r]   r   r   r  ordersLengthr  r{   r   storedr   orderrt   ry   
symbolKeyssymbolSpecificMessageHashs                  r_   handle_ordersbitmart.handle_orders  s   l &1>6{	!%%dllM4HE{{"4U;[[F1c&k*++FI6e$  'x"& + ',,.)
q#j/*A]F(3d(:V(C%NN9@ + 	y.ra   r   rn   c                 b   U R                  US5      nUS L nU(       Ga.  U R                  US5      nU R                  XRSS5      nU R                  US5      nU R                  US5      nU R                  US5      nU R                  US5      n	U R                  US	5      n
U R                  US
5      nU R                  US5      nU R                  US
   U5      nU R	                  US5      nUS   nU R                  US5      nU R                  0 SU_SU_SU_SU_SS _SS _SU_S
U_SS _SS _SU_SU_SS _SS _SU
_SS _SS _U	S US S S.EU5      $ U R                  US5      nU R                  US5      nU R                  XRSS5      nU R                  US5      nU R	                  US5      nU R	                  US 5      nU R                  US!5      nU R                  nU R                  UR                  U0 5      nU R                  UU5      nS nUb  U R                  US"5      nUb  Uc  / nUR                  U5        U R                  0 SU_SU_SU_SU R                  US#5      _SU_SU R                  U5      _SU_S
U R                  US
5      _SS _SS _SU R                  U R                  US5      5      _SU R                  US5      _SS _SS _SU R                  US	5      _SS _SU R                  US$5      _U R                  US%5      S U R                  U R                  US5      5      S US.EU5      $ )&Nrm   rt   _r8   order_id	clientOidpricefilled_sizesizeru   stater   sider   re   clientOrderIdr   r   lastTradeTimestamptimeInForcepostOnly	stopPricetriggerPriceamountcostaverage)filled	remainingstatusfeer   r    r9   create_timeupdate_time
last_trader   client_order_iddeal_avg_price	deal_size)safe_numberrp   r  parse_order_status_by_typer   safe_string_lower
safe_orderr   safe_symbolr  hashmapr   r   parse_ws_order_sideparse_ws_order_status)r]   r   rn   rm   r   r  re   r.  r)  r7  r4  ru   rawStater9  r   rt   r-  	orderInfoorderIdupdatedTimestamp	lastTradecachedOrdersr  cachedOrderr   s                            r_   r  bitmart.parse_ws_order+  sn   X !!%2D.''x8H%%hVDF!!%4B ,,UK@M$$UG4E%%e];F%%eV4F##E62D''w7H44VF^XNF))%8IH%F))%8D?? $$&$ b$  	$
 T$ D$ %i$ $ t$ D$ $ $ T$ $ &$  !$" 4#$$ !! -$. / 2 w7I''	8<H%%hFCF&&y*=G)))]CI#00MJ	<@I;;L__\%9%962FF//&':KF&9$>Fi(?? $$&$ g$  !1!1)=N!O	$
 Y$ DLL3$ %&6$ ((F;$ t$ D$ 001A1A)V1TU$ )))W=$ T$ $ $**9f=$  !$" 4++I7GH#$$ **9kB!44T5E5EeX5VW -$. / ra   c           
      >    SSSSSSSSSS.	nU R                  X!U5      $ )Nclosedopencanceled)	123456789rp   )r]   statusIdstatusess      r_   rI  bitmart.parse_ws_order_status  s9    

 H==ra   c                 4    SSSSS.nU R                  X!U5      $ )Nbuysell)rV  rW  rX  rY  r_  )r]   sideIdsidess      r_   rH  bitmart.parse_ws_order_side  s*    	
 v66ra   c                   #    U R                  5       I Sh  vN   SnU R                  XT5      I Sh  vN   U R                  USSSS5      nSnUb  USSR                  U5      -   -  nSnS	S/S
.nU R	                  U R
                  S   S   U   S   5      n	U R                  XU R                  X5      U5      I Sh  vN n
U R                  (       a  U
$ U R                  U R                  XU5      $  N N N87f)a  

https://developer-pro.bitmart.com/en/futures/#private-position-channel

watch all open positions
:param str[]|None symbols: list of unified market symbols
:param int [since]: the earliest time in ms to fetch positions
:param int [limit]: the maximum number of positions to retrieve
:param dict params: extra parameters specific to the exchange API endpoint
:returns dict[]: a list of `position structure <https://docs.ccxt.com/en/latest/manual.html#position-structure>`
Nr9   TF	positionsr  ,zfutures/positionrf   rl   r5   r'   r7   )r   r   r   joinro   rW   rr   rZ   r   filter_by_symbols_since_limitrj  )r]   r{   r   r   rv   ru   ry   subscriptionHashrx   rw   newPositionss              r_   watch_positionsbitmart.watch_positions  s     !!!---%%gvtT5I!4#((7"333K-!'(
 ##DIIe$4T$:4$@$KL!ZZ$:J:J7:[]mnn??11$..'RWXX! 	"- os2   C6C0C6C2B	C6;C4<5C62C64C6c                 t   U R                  US/ 5      nU R                  c  [        5       U l        U R                  n/ n[        S[	        U5      5       H:  nX6   nU R                  U5      nUR                  U5        UR                  U5        M<     U R                  US5      n	[        S[	        U	5      5       Hk  nX   n
U
R                  S5      nUS   nUR                  S5      nU R                  USUS5      nU R                  U5      (       a  MZ  UR                  X5        Mm     UR                  US	5        g )
Nr   r   zpositions::r     rk  rt   Frj  )r   rj  r   r   r   parse_ws_positionr   find_message_hashessplitr   is_emptyr   )r]   r   r   r   cachero  r   rawPositionpositionr   ry   partssymbolsStringr{   rj  s                  r_   handle_positionsbitmart.handle_positions  s   H w3>>!57DNq#d)$A'K--k:H)LL"	 %
 00Gq#m,-A'*K%%d+E!!HM#))#.G,,\8WeTI==++y6 . 	|[1ra   c                 h   U R                  US5      nU R                  X2S S5      nUS   nU R                  US5      nU R                  US5      nU R                  US5      nU R                  US5      nU R                  0 SU_SS _SU_S	U_S
U R	                  U5      _SU_SS _SUS:X  a  SO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S _SS _SS _SS _SS _S S S S S U R                  US5      US:X  a  SOSS S S S S .E5      $ )!Nrt   r9   r<  r=  position_type	open_typer   re   r   r   lastUpdateTimestamphedgedr-  rs  longshort	contractshold_volumecontractSize
entryPriceopen_avg_price	markPricehold_avg_price	lastPricenotionalleverage
collateralinitialMarginliquidate_priceisolatedcross)initialMarginPercentagemaintenanceMarginmaintenanceMarginPercentageunrealizedPnlrealizedPnlliquidationPrice
marginMode
percentagemarginRatiostopLossPricetakeProfitPrice)rp   r  r   safe_positionr   rB  )	r]   rz  rn   r  rt   openTimestampr   r-  marginModeIds	            r_   rt  bitmart.parse_ws_position  s   " ##Hh7!!(D&A!))(MB%%h>	  ?;((;?!! #
H#
$#
 f#
 	#

 ]3#
 "9#
 d#
 tqyFw#
 ))(MB#
 D,,V^D#
 $**85EF#
 ))(4DE#
 #
 #
 #
  $!#
" T##
$ (,!%+/! $ 0 0;L M)5):*!#9#
  	ra   c                 N   U R                  US5      nUc  g S n[        U5      nSU;   nU(       a.  [        SU5       H  nXW-
  S-
  nU R                  X8   5      nM     O&[        SU5       H  nU R                  X7   5      nM     UR	                  U R
                  U   SU-   5        g )Nr   r   r   rs  ztrade:)r   r   r   handle_trade_loopr   r   )	r]   r   r   r   rt   r   isSwapr   indexs	            r_   handle_tradebitmart.handle_tradeO  s    @ w/<TW$1f%
Q//< &
 1f%//8 &t{{6*Hv,=>ra   c                    U R                  U5      nUS   nU R                  U R                  SS5      nU R                  U R                  U5      c  [        U5      U R                  U'   U R                  U   nUR                  U5        U$ )Nrt   tradesLimitr  )parse_ws_trader   rX   r   r   r   r   )r]   entryr   rt   r  r  s         r_   r  bitmart.handle_trade_loop  sz    ##E*x''mTJ??4;;/7",["9DKKV$era   r   c                    U R                  US5      nU R                  X25      nU R                  US5      nS nUc$  U R                  US5      nU R                  U5      nOU R	                  U5      nS nU R                  US5      nU R                  US5      nUb  Uc  U(       a  SnOSnSnU R                  UU R                  US	5      S UUUS   S UU R                  US
S5      U R                  USS5      S US S.U5      $ )Nrt   r   
created_atr-  mre  rd  takertrade_idr)  
deal_pricer+  deal_vol)r   re   r   r   r   rt   ru   r-  r)  r4  r5  takerOrMakerr:  )rp   r  r   	parse8601r   r   
safe_trader   )	r]   r   rn   r  r   r   r  r-  
buyerMakers	            r_   r  bitmart.parse_ws_trade  s'   . ##E84!!(3%%eV4	''|<Hx0I||I.Hv.^^E3/
!|!D D"L""5*5" X&''wE((
C( 
  	ra   c                    U R                  X5        U R                  US5      nUS Ln/ nU(       a  U R                  US/ 5      nOU R                  US0 5      /n[	        U5      (       d  g [        S[	        U5      5       HY  nU(       a  U R                  XV   5      OU R                  XV   5      nUS   nXpR                  U'   SU-   n	UR                  Xy5        M[     g )Nr   r   r   rt   zticker:)
r   rp   r   r   r   r   parse_tickerparse_ws_swap_tickerr   r   r   s
             r_   handle_tickerbitmart.handle_ticker  s    B 	F,  '2t#
<J//'62>?J:q#j/*A9?T&&z}5TE^E^_i_lEmFH%F#)LL #f,KNN6/ +ra   c                    U R                  US5      nU R                  0 SU R                  X2SS5      _SS _SS _SS _S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S _SS _SS _SU R                  US5      _SS _SS _SS _SS _S U R                  US5      UU R                  US5      U R                  US5      S.EU5      $ )Nrt   r;  r9   r   r   highlowr  r  r  r  r	  r  r
  r  vwaprT  closelast
last_pricepreviousClosechanger  r6  	volume_24
mark_priceindex_price)
baseVolumequoteVolumer   r  
indexPrice)rp   r  rF  )r]   r   rn   r  s       r_   r  bitmart.parse_ws_swap_ticker  s    ##FH5 !
d&&xVD!
!
 !
 D	!

 4!
 4##FK8!
 ))&)<!
 4##FK8!
 ))&)<!
 D!
 D!
 T!
 D$$V\:!
 T!
 d!
  $!!
" t#!
$ ++FK@))&,?**6=A-!
. / 	ra   r@   	timeframec                   #    U R                  5       I Sh  vN   U R                  U5      nU R                  U5      nSnU R                  SXe5      u  puU R	                  U R
                  S0 5      nU R                  X5      n	Sn
US:X  a  SU	-   n
OSU	-   n
U R                  XXu5      I Sh  vN nU R                  (       a  UR                  X5      nU R                  XUSS5      $  N N<7f)	a  

https://developer-pro.bitmart.com/en/spot/#public-kline-channel
https://developer-pro.bitmart.com/en/futuresv2/#public-klinebin-channel

watches historical candlestick data containing the open, high, low, and close price, and the volume of a market
:param str symbol: unified symbol of the market to fetch OHLCV data for
:param str timeframe: the length of time each candle represents
:param int [since]: timestamp in ms of the earliest candle to fetch
:param int [limit]: the maximum amount of candles to fetch
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns int[][]: A list of candles ordered, open, high, low, close, volume
Nr8   r,   rT   klineklineBinr   T)r   rt   rn   r   r   rX   rp   rf   r   r   r   )r]   rt   r  r   r   rv   rn   ru   rT   intervalnameohlcvs               r_   watch_ohlcvbitmart.watch_ohlcv  s      !!!V$V$99:JF[__T\\<D
##J:6>X%D(DnnT4@@??NN61E))%q$GG 	" As"   C(C$BC()C&*;C(&C(c                    U R                  USS5      nUR                  S5      S:  nU R                  US5      nUc  g UR                  S5      nU R	                  USS5      nUR                  S	S5      nUR                  S
S5      nUR                  S5      n	U R	                  U	S5      nU R                  U R                  S0 5      n
U R                  X5      nU R                  U5      nUS-  nU(       Ga=  [        S[        U5      5       GH"  nU R	                  X^   S5      nU R                  U5      nUS   nU R                  X^   S5      nU R                  UU5      nU R                  US   U-  5      U-  US'   U R                  U R                  U0 5      U R                  U'   U R                  U R                  U   U5      nUc:  U R                  U R                  SS5      n[!        U5      nUU R                  U   U'   UR#                  U5        US-   U-   nUR%                  UU5        GM%     g U R	                  US5      nU R                  US S S5      nUS   nU R                  US/ 5      nU R                  U R                  U0 5      U R                  U'   U R                  U R                  U   U5      nUc:  U R                  U R                  SS5      n[!        U5      nUU R                  U   U'   [        S[        U5      5       H+  nUU   nU R                  UU5      nUR#                  U5        M-     UR%                  UU5        g )Nr   r   r8   r   r   r~   rs  r;  r  Binrd   rT   r  rt   candle
OHLCVLimitr9   items)r   r   r   rv  rp   replacerX   find_timeframeparse_timeframer   r   r  parse_ohlcvparse_to_intohlcvsr   r   r   r   )r]   r   r   rs   r   r   r{  part1r  intervalPartsrT   r  durationdurationInMsr   r  rn   rt   rawOHLCVparsedr  r   ry   r  r  s                            r_   handle_ohlcvbitmart.handle_ohlcv>  s   H $$Wgw?,,v&!+w/<c"  2.=="-##E2. s+##M15__T\\<D
''=	''	2$1c$i(++DGX>))(3)??47H=))(F; --fQi,.FG,Vq	&*oodkk62&NF#V)<iH> --dllL$OE259F5;DKK'	2f%%mh6v{3 )" ''h7H%%hdFCFH%FOOD'26E"&//$++vr"JDKK__T[[%8)DF~))$,,dK.u517F#I.1c%j)q))&&9f% * NN67+ra   c                   #    U R                  5       I Sh  vN   U R                  U R                  S0 5      nU R                  USS5      nU R	                  U5      nU R                  U5      nSnU R                  SXc5      u  psUS:X  a  US:X  a  SnU R                  XQXs5      I Sh  vN nUR                  5       $  N N7f)a  

https://developer-pro.bitmart.com/en/spot/#public-depth-all-channel
https://developer-pro.bitmart.com/en/spot/#public-depth-increase-channel
https://developer-pro.bitmart.com/en/futuresv2/#public-depth-channel

watches information on open orders with bid(buy) and ask(sell) prices, volumes and other data
:param str symbol: unified symbol of the market to fetch the order book for
:param int [limit]: the maximum amount of order book entries to return
:param dict [params]: extra parameters specific to the exchange API endpoint
:param str [params.speed]: *futures only* '100ms' or '200ms'
:returns dict: A dictionary of `order book structures <https://docs.ccxt.com/#/?id=order-book-structure>` indexed by market symbols
Nr,   r<   r=   r8   r9   depth50)	r   r   rX   rp   rt   rn   r   rf   r   )	r]   rt   r   rv   rX   r<   rn   ru   	orderbooks	            r_   watch_order_bookbitmart.watch_order_book  s      !!!//$,,0@"E  '3FGV$V$99:JF[6>e'::E..EE	   	" Fs"   B?B;BB?&B='B?=B?c                 n    U R                  US5      nU R                  US5      nUR                  X45        g )Nr   rs  )
safe_floatstore)r]   booksidedeltar)  r4  s        r_   handle_deltabitmart.handle_delta  s.    q)*u%ra   c                 d    [        S[        U5      5       H  nU R                  XU   5        M     g )Nr   )r   r   r  )r]   r  deltasr   s       r_   handle_deltasbitmart.handle_deltas  s)    q#f+&Ahq	2 'ra   c                 H   U R                  US/ 5      nU R                  US/ 5      nU R                  US   U5        U R                  US   U5        U R                  US5      nU R                  US5      nU R	                  U5      nXS'   XcS'   U R                  U5      US'   U$ )Nasksbidsr   rt   r   r   )r   r  r   rp   rF  r   )	r]   r   r   r  r  r  r   r  rt   s	            r_   handle_order_book_message!bitmart.handle_order_book_message  s    * ~~gvr2~~gvr29V,d39V,d3%%gv6	##GX6!!(+$(!*+ $Y 7	*ra   c                    SU;   n/ nU(       a  U R                  USU5      nO&U R                  US5      nUb  UR                  U5        [        U5      nUS::  a  g U R	                  USS5      n/ SQnSn	[        S[        U5      5       H/  n
X   nUR                  U5      S:  d  M  U R                  U5      n	  O   U(       GaQ  UR                  SS5      n[        S[        U5      5       GH$  n
XJ   nU R                  US5      nU R                  U5      nXR                  ;  a&  U R                  0 U	5      nUUS'   UU R                  U'   U R                  U   nU R                  US	5      nUS
:X  d  UR                  S5      S:  d  UR                  0 5        U R                  XU5        U R                  US5      nUS   c  UUS'   U R!                  U5      US'   US-   U-   nUR#                  UU5        US-   U-   nUR#                  UU5        GM'     g UR%                  S5      nUS   R                  SS5      nUS   nUS   nU R                  US5      nU R                  U5      nXR                  ;  a&  U R                  0 U	5      nUUS'   UU R                  U'   U R                  U   nU R                  US5      nUS:X  a  SOSnUS:X  a  ['        / U	5      UU'   O[)        / U	5      UU'   [        S[        U5      5       HP  n
UU
   nU R+                  US5      nU R+                  US5      nU R-                  UU5      nUR/                  UU5        MR     [        US   5      n[        US   5      n US:X  d  U S:X  a  g U R                  US5      nUUS'   U R!                  U5      US'   UnUR#                  UU5        US-   U-   nUR#                  UU5        g )Nr   r   r   r   )100502010rZ  rc   r;  rt   ru   r   increaser   r   r   rd   rj   depthswayrs  r  r  r)  vol)r   r   r   r   r   r   r   r  r  rp   rF  
orderbooks
order_bookresetr  r   r   r   rv  r   r   rB  r   r  )!r]   r   r   r   datasorderBookEntryr   r   limitsToCheckr   r   limitStringrs   updater  rt   obr  ru   r   ry   messageHashForMulti
tablePartsr   r  r  r-  r<   r)  r4  orderbookSide
bidsLength
asksLengths!                                    r_   handle_order_bookbitmart.handle_order_book  s   H W$NN7FE:E!^^GV<N)^,UQ;(('7C6q#m,-A'*K,1))+6	 .
 !))'26G1c%j)++FH=))(3//1U3B#)BxL.0DOOF+ OOF3	''7J&0@0@0LPQ0QOOB'..vyI --ff=	[)1-6Ik*,0LL,CIj))C/(:y+6&-mf&<#y*=>+ *. %**3/J m++J;G8D(^F''h7H%%h/Foo-__R/%8*,'/I##D%0C!Qh6VDax"&r5/	$"&r5/	$1c&k*q	((8))%7 $	4 @##E62 + Yv./JYv./JaZ1_))$7I%.Ik"$(LL$;Ij!%KNN9k2")C-&"8NN9&9:ra   c                   #    U R                  5       I Sh  vN   SnU R                  SXU5      u  pnSnU R                  USSS5      u  pSUS:X  a  US:X  a  SnU R                  XTX5      I Sh  vN nUR	                  5       $  Nl N7f)a  
watches information on open orders with bid(buy) and ask(sell) prices, volumes and other data

https://developer-pro.bitmart.com/en/spot/#public-depth-increase-channel

:param str[] symbols: unified array of symbols
: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.depth]: the type of order book to subscribe to, default is 'depth/increase100', also accepts 'depth5' or 'depth20' or depth50
:returns dict: A dictionary of `order book structures <https://docs.ccxt.com/#/?id=order-book-structure>` indexed by market symbols
Nr-   r<   r=   r9   r  )r   r   r   r   r   )r]   r{   r   rv   ru   rs   r  s          r_   watch_order_book_for_symbols$bitmart.watch_order_book_for_symbolsm  s      !!! $ @ @A[]dms tv77@Z\cexy6>g)<<G11'QQ	   	" Rs"   BBAB.B/BBc                   #    U R                  5         U R                  U R                  S   S   U   S   5      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       5      nU R                  n	Sn
US-   U	-   S-   U
-   nU R                  U R                  U5      U R                  U R                  5      [        R                  5      nS nUS:X  a  SU R                  UU/S	.nOS
U R                  UUS/S.nU R!                  X5      nU R#                  X4X5        UI S h  vN $  N7f)Nr5   r'   r7   authenticatedzbitmart.WebSocket#r8   loginrg   accesswebrl   )check_required_credentialsro   rW   r   reusableFuturer   r   strmillisecondsuidhmacencodesecrethashlibsha256apiKeyr   rr   )r]   ru   rv   rw   ry   r   r   r  r   memopathauth	signaturerx   r   s                  r_   r   bitmart.authenticate  sN    '')##DIIe$4T$:4$@$KL%S!&&{3(<(<kJ D--/0I88D&Ds?T)C/$6D		$++d"3T[[5Mw~~^IGv~!!! '!!	 kk'2GJJs>||s   EEEEc                     U$ N )r]   r   r   s      r_   handle_subscription_status"bitmart.handle_subscription_status  s	     ra   c                 b    SnU R                  UR                  U5      nUR                  S5        g )Nr  T)r   r}   r   )r]   r   r   ry   r   s        r_   handle_authenticatebitmart.handle_authenticate  s)     &=tra   c                 r   U R                  US5      nU R                  US5      n Uc  Ub  U R                  S-   U R                  U5      -   nU R                  U R                  S   X55        U R                  USU5      nU R                  U R                  S   Xe5        U R                  US5      nUS:X  a  [        U5      e[        U5      eg	! [         a_  n[        U[        5      (       a/  S
n	UR                  X5        XR                  ;   a  UR                  U		 UR                  U5         S nAgS nAff = f)N	errorCodeerrorr   exactr   broadrm   r!  Fr  T)rp   re   jsonthrow_exactly_matched_exception
exceptionsr   throw_broadly_matched_exceptionr   r   	Exceptionr   rejectr   )
r]   r   r   r<  r=  feedbackmessageStringrm   ery   s
             r_   handle_error_messagebitmart.handle_error_message  s"    $$Wk:	  '2	$(977S=499W+==44T__W5Myc $E J44T__W5M}g))'8<X%-h77#H-- 	1122-a-"6"66,,[9MM!	s   B&C 
D6AD11D6c           	         U R                  X5      (       a  g SU;   nU(       dY  U R                  USS5      nUbB  U R                  U R                  U R                  S.nU R	                  XT5      nUb	  U" X5        g g g U R                  USS5      nU R
                  U R                  U R                  U R                  U R                  U R                  U R                  U R                  S.n[        UR                  5       5      n[        S[        U5      5       H7  n	X   n
UR!                  U
5      S:  d  M  U R	                  XZ5      nU" X5        M9     g )	Nr   eventrm   )r   r!  rf   r   r   )r<   r   r   r  r   rz  r   assetr   )rI  r   r9  r6  r   r  r  r  r  r#  r}  r   r   r  r   r   r   )r]   r   r   isDataUpdaterL  methodsmethodrs   r  r   keys              r_   handle_messagebitmart.handle_message  sF   $$V55^ ')&&wBE  "55"66!%!@!@	! 8%6+ & ! (('7CG//,,****++ 11..,,	G 'D1c$i(g<<$)!__W:F6+	 )ra   )r  rj  r4  )A__name__
__module____qualname____firstlineno__r	   r\   rf   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#  dictr   r  rI  rH  r   rp  r}  rt  r  r  r  r  r  r   r  r  r   r  r  r  r  r  r  r   r6  r9  r   rI  rR  __static_attributes____classcell__)r^   s   @r_   r   r      s   O# Ob =? b. TX`b r r3 r r. *, #b #bJ
  
 >>8V >8@ <@d[] S SS S Sbfglbm S OSaenp d3i  [^ uyz  vA 8 ]aik -c -DI -VY - 68  F   6:" E7 E E, 8<B $FW $F7 $FL0V 0"  04$UYbd +Z +ZC +Zs +Zimnsit +ZZM/F M/^}D }& }~>7 8<$]ajl YW YC YWZ Yquv~q Y<72v 72r5& 5n/?6 /?b8D 8& 8t00F 00d'6 'R ?CQUdhqs H H H3 H^a Hx|  ~B  yC H>T,6 T,l @DB !S ! !S\ !4&
3 BS; S;j SW_a !$s) !C !fo !, /1 #J 	& 	 6  t  DO,V O, O,ra   r   )#ccxt.async_supportccxt ccxt.async_support.base.ws.cacher   r   r   r   *ccxt.async_support.base.ws.order_book_sider   r   r+  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   r5  ra   r_   <module>rd     sQ     A  A A  ~ ~ ~ ~ 4  * 0 )X,d  (( X,ra   