
    eiO                        d dl Zd dlmZmZmZmZ d dl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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" d d
lm#Z#  G d dej$        j%                  Z%dS )    N)
ArrayCacheArrayCacheBySymbolByIdArrayCacheBySymbolBySideArrayCacheByTimestamp)AsksBids)AnyBalancesBoolIntMarketOrder	OrderBookPositionStrStringsTickerTickersFundingRateFundingRatesTrade)Client)List)ExchangeError)AuthenticationError)ArgumentsRequired)NotSupportedc                   D    e Zd Zdef fdZi fdZdi fdededefdZi fde	fd	Z
d
efdZd Zd
efdZddi fdedededee         fdZddi fdee         dededee         fdZi fdedefdZi fdee         defdZdi fdedee         defdZi fdedefdZdi fdedefdZi fdedefdZdi fdedefdZdi fdedefdZd
efdZdJdZdddi fde dededee!         fdZ"di fde defdZ#d
efd Z$dJd!e%d"e&fd#Z'd$ Z(d% Z)dddi fdedededee*         fd&Z+di fdedefd'Z,d
efd(Z-dJd"e&fd)Z.d
efd*Z/d+ Z0dJd,e%d"e&fd-Z1d
efd.Z2dJd"e&fd/Z3d0ddi fded1edededee4         f
d2Z5d0i fded1edefd3Z6d
efd4Z7di fdedede8fd5Z9i fdedefd6Z:d7 Z;d8 Z<d
efd9Z=d
efd:Z>di fdee         dede8fd;Z?i fdee         defd<Z@i fdedeAfd=ZBdi fdedeCfd>ZDd
efd?ZEi fd@ZFd
efdAZGd
efdBZHd
edeIfdCZJd
efdDZKdE ZLdF ZMdGefdHZNd
efdIZO xZPS )Kbitmartreturnc                 n   |                      t          t          |                                           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
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iidd
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/d0d1d2d3id4          S )5NcreateOrderWsFeditOrderWsfetchOpenOrdersWsfetchOrderWscancelOrderWscancelOrdersWscancelAllOrdersWswsTwatchBalancewatchFundingRatewatchFundingRateswatchTickerwatchTickerswatchBidsAskswatchOrderBookwatchOrderBookForSymbolswatchOrdersemulated)watchTradeswatchTradesForSymbols
watchOHLCVwatchPositionwatchPositionsunWatchBidsAsksunWatchOHLCVunWatchOrderBookunWatchOrderBookForSymbolsunWatchOrdersunWatchPositionsunWatchTickerunWatchTickersunWatchTradesunWatchTradesForSymbols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swaprF   )fetchBalanceSnapshotawaitBalanceSnapshotdepthdepth/increase100ignoreDuplicatesinflate1m3m5m15m30m45m1H2H3H4H1D1W1M)rN   rO   rP   rQ   rR   rS   1h2h3h4h1d1wrZ   )defaultTyper*   r0   r1   r4   r)   
timeframes	keepAlivei:  )hasurlsoptions	streaming)deep_extendsuperr   describe)self	__class__s    N/var/www/html/volatility/venv/lib/python3.11/site-packages/ccxt/pro/bitmart.pyrj   zbitmart.describe   s"   gt 4 4 = = ? ?!!u! $U! 	!
  ! !%! $U! d! ! #D! $T! t! !  ! !$!  +D!!" t#!$  $)-"!+"&#( $$(.2!%$(!%"&!%+/A! ! !F &]'_! !
 'X'Y! !	 	  &,0,1! ! 0# 0- '  t     -% %N UoZB
 ZB
 Z Z Z	    c                 N  K   |                      |          }|                     | j        d         d         |         d                   }i }d }|                     |dd          }	d}
d}|	r|                     |d          }d}
d}|d	k    rd
|z   dz   |d         z   }||gd}nMd|z   dz   |d         z   }|                     |d          }||                     |d          }|d|z   z  }||gd}|
|z   }|                     |||                     ||          |           d {V S )NrC   r)   rD   unsubscribeF 	subscribeunsubscribe::rF   spot/:idopargsfutures/speedactionry   )marketimplode_hostnamere   	safe_boolomitsafe_stringwatchrh   )rk   channelsymboltypeparamsr~   urlrequestmessageHashrp   prefix	requestOpr{   s                rm   rr   zbitmart.subscriber   sv     V$$##DIe$4T$:4$@$JKKnnV]EBB	 	&YYv}55F$F%I6>>!G+c1F4L@K$ GG
 %w.4vd|CK$$VW55E 6733sU{*#$ G {*ZZ[$2B2B7F2S2SU`aaaaaaaaarn   Nr   r   symbolsc                   K   |                      ||dd          }|                     | j        d         d         |         d                   }|dk    rdnd}|dk    rdnd	}g }g }	g }
|                     |d
d          }d}d}|r|                     |d
          }d}d
}t          dt          |                    D ]}|                     ||                   }|dz   |z   dz   |d         z   }||z   }||z   dz   |d         z   }|                    |           |
                    |           |	                    |           d|i}|||<   | 	                    ||	| 
                    ||          |
           d {V S )NFTrC   r)   rD   rF   futuresrx   r}   rp   rq   rr   rs   r   /ru   rv   r   ry   )market_symbolsr   re   r   r   rangelenr~   appendwatch_multiplerh   )rk   r   r   r   r   r   channelType
actionTyperawSubscriptionsmessageHashes	subHashesrp   r   r   ir~   messagesubHashr   r   s                       rm   subscribe_multiplezbitmart.subscribe_multiple   s     %%gtUDAA##DIe$4T$:4$@$JKK!%ffi"fnnTT8
	nnV]EBB	 	&YYv}55F$F%Iq#g,,'' 	. 	.A[[,,F!C''1C7&,FGw&G 7*S06(3CCK##G,,,W%%%  ---- $
 (
((mT=M=MgW]=^=^`ijjjjjjjjjrn   c                   K   |                                   d{V  d}|                     dd|          \  }}|                     ||           d{V  i }|dk    rddgd}ndg dd}d	|z   }|                     | j        d
         d         |         d                   }|                     |          }|                     |||           d}d}|                     |ddd          \  }}|                     |ddd          \  }}|r |r|                    |dz              d{V  | 	                    ||| 
                    ||          |           d{V S )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>`
        NrF   r*   rr   z spot/user/balance:BALANCE_UPDATErw   )zfutures/asset:USDTzfutures/asset:BTCzfutures/asset:ETHr|   balance:rC   r)   rE   rH   TrI   Fz:fetchBalanceSnapshot)load_marketshandle_market_type_and_paramsauthenticater   re   clientset_balance_cachehandle_option_and_paramsfuturer   rh   )	rk   r   r   r   r   r   r   rH   rI   s	            rm   watch_balancezbitmart.watch_balance   s      !!!!!!!!!99.$PVWWff---------6>>!;< GG &XXX G !4'##DIe$4T$:4$@$KLLS!!vt[999##'+'D'DV^]suy'z'z$f'+'D'DV^]suz'{'{$f 	@$8 	@--'> >?????????ZZ[$2B2B7F2S2SU`aaaaaaaaarn   r   c                    ||j         v rd S |                     | j        d          }|                     |dd          }|rC|dz   dz   }||j        vr2|                    |           |                     | j        |||           i | j        |<   d S )Nr*   rH   Tru   )	subscriptions	safe_dictrf   r   r   r   spawnload_balance_snapshotbalance)rk   r   r   subscribeHashrf   snapshotr   s          rm   r   zbitmart.set_balance_cache   s    F000F..~>>>>'+A4HH 	R*'==K6>11k***

45v{DQQQTrn   c                 T  K   |                      d|i           d {V }|                     ||                     | j        |i                     | j        |<   ||j        v rG|j        |         }|                                 |                    | j        |         d|z              d S d S )Nr   r   )fetch_balanceextend
safe_valuer   r   resolve)rk   r   r   r   responser   s         rm   r   zbitmart.load_balance_snapshot   s      ++VTN;;;;;;;;![[4??4<QUWY3Z3Z[[T&.((^K0FNNNN4<-zD/@AAAAA )(rn   c                    |                      |dd          }|                     |d          }|d S |                    d          dk    }|rdnd}|| j        |         d<   |rIt	          |t
                    sd S t          dt          |                    D ]}|                     |d          }|| j        |         d	<   | 	                    |          | j        |         d
<   | 
                    ||         dg           }	t          dt          |	                    D ]}
|	|         }|                                 }|                     |d          }|                     |          }|                     |d          |d<   |                     |d          |d<   || j        |         |<   n|                     |d          }|                     |          }|                                 }|                     |d          |d<   |                     |d          |d<   || j        |         |<   |                     | j        |                   | j        |<   d|z   }|                    | j        |         |           d S )NtablegroupdatarF   r   rG   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	safe_listaccountr   safe_currency_codesafe_balancer   )rk   r   r   r   r   isSpotr   r   r   balanceDetailsii
rawBalancer   
currencyIdcoder   s                   rm   handle_balancezbitmart.handle_balance   s   < $$Wgw??w//<F,,v&&!++vvV%,T6" 	/dD)) 1c$ii(( 7 7 --g|DD	2;T";/15i1H1HT":.!%Q9JB!O!O3~#6#677 7 7B!/!2J"llnnG!%!1!1*e!D!DJ22:>>D&*&6&6z8&L&LGFO&*&6&6z8&L&LGFO/6DL&t,,77 ))$
;;J**:66DllnnG"..t5HIIGFO"..t5EFFGFO'.DLt$!..t|D/ABBT 4't|D);77777rn   r   sincelimitc                 D   K   |                      |g|||           d{V S )aq  

        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)rk   r   r   r   r   s        rm   watch_tradeszbitmart.watch_trades.  s6       22F8UE6RRRRRRRRRrn   c                   K   |                                   d{V  d}|                     d|||          \  }}}d}|                     ||||           d{V }| j        rB|                     |d          }|                     |d          }	|                    |	|          }|                     |||dd          }
|                     dd	d          r-| 	                    |
          }| 
                    |d          }|S |
S )
ar  

        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 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>`
        Nr5   trader   r   r   Tr4   rL   )r   get_params_for_multiple_subr   
newUpdatesr   r   getLimitfilter_by_since_limithandle_option!remove_repeated_trades_from_arraysort_by)rk   r   r   r   r   
marketTypechannelNametradesfirsttradeSymbolresultfiltereds               rm   r   z bitmart.watch_trades_for_symbols=  s1      !!!!!!!!!
&*&F&FG^`ginpv&w&w#V..{JQWXXXXXXXX? 	8NN61--E**5(;;KOOK77E++FE5+tTTm-?FF 	==fEEH||Hk::HOrn   c                 @   K   |                      |g|           d{V S )a  
        unWatches from the stream channel

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

        :param str symbol: unified symbol of the market to fetch trades for
        :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)un_watch_trades_for_symbolsrk   r   r   s      rm   un_watch_tradeszbitmart.un_watch_tradesZ  s2       55vhGGGGGGGGGrn   c                    K   |                                   d{V  d}|                     d|d|          \  }}}d}|                     |ddi          }|                     ||||           d{V S )a  
        unsubscribes from the trades channel

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

        :param str[] symbols: unified symbol of the market to fetch trades for
        :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>`
        NrB   r   rp   T)r   r   r   r   )rk   r   r   r   r   s        rm   r   z#bitmart.un_watch_trades_for_symbolsg  s       !!!!!!!!!
&*&F&FG`bikoqw&x&x#VVmT%:;;,,[*gvVVVVVVVVVrn   
methodNamec                    |                      |d dd          }t          |          }|dk    rt          | j        dz   |z   dz             |                     |d                   }d }|                     |||          \  }}|||gS )NFT    z1() accepts a maximum of 20 symbols in one requestr   )r   r   r   rv   r~   r   )rk   r   r   r   r   lengthr~   r   s           rm   r   z#bitmart.get_params_for_multiple_suby  s    %%gtUDAAWB;;tw}z9<oopppWQZ((
!??
FTZ[[
FV,,rn   c                    K   |                                   d{V  |                     |          }|                     |g|           d{V }||         S )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   r   watch_tickers)rk   r   r   tickerss       rm   watch_tickerzbitmart.watch_ticker  sp       !!!!!!!!!V$$**F8V<<<<<<<<vrn   c                 <  K   |                                   d{V  |                     |          }d}|                     d||          \  }}|                     d|||           d{V }| j        ri }|||d         <   |S |                     | j        d|          S )a7  

        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.   tickerr   )r   get_market_from_symbolsr   r   r   filter_by_arrayr   )rk   r   r   r~   r   r   r   s          rm   r   zbitmart.watch_tickers  s       !!!!!!!!!--g66
!??PVX^__
F..xWfUUUUUUUU? 	G(.GF8$%N##DL(GDDDrn   c                 @   K   |                      |g|           d{V S )a+  
        unWatches a price ticker, a statistical calculation with the information calculated over the past 24 hours for a specific market

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

        :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)un_watch_tickersr   s      rm   un_watch_tickerzbitmart.un_watch_ticker  s2       **F8V<<<<<<<<<rn   c                   K   |                                   d{V  |                     |          }d}|                     d||          \  }}|                     |ddi          }|                     d|||           d{V S )a;  
        unWatches a price ticker, a statistical calculation with the information calculated over the past 24 hours for all markets of a specific list

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

        :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.   rp   Tr   )r   r   r   r   r   )rk   r   r   r~   r   s        rm   r  zbitmart.un_watch_tickers  s       !!!!!!!!!--g66
!??PVX^__
FVmT%:;;,,Xz7FSSSSSSSSSrn   c                   K   |                                   d{V  |                     |dd          }|                     |          }d}|                     d||          \  }}|                     | j        d         d         |         d                   }|dk    rdnd}|dk    rd	nd
}g }g }	t          dt          |                    D ]\}
|                     ||
                   }|	                    |dz   |d         z              |		                    d||
         z              ]|dk    r|dz   g}d|i}d||<   | 
                    ||	||           d{V }| j        ri }|||d         <   |S |                     | j        d|          S )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/   rC   r)   rD   rF   r   rx   r}   r   z/ticker:rv   bidask:z/tickerry   rr   r   )r   r   r   r   r   re   r   r   r~   r   r   r   r   bidsasks)rk   r   r   firstMarketr   r   r   r   r   r   r   r~   r   
newTickersr   s                  rm   watch_bids_askszbitmart.watch_bids_asks  s      !!!!!!!!!%%gtU;;227;;
!??Q\^dee
F##DIe$4T$::$Fx$PQQ!+v!5!5ffI(F22TT
q#g,,'' 	9 	9A[[,,F##K*$<vd|$KLLL  WQZ!78888 +i 78$
 *
..sM7L\]]]]]]]]
? 	G,6GJx()N##DM8WEEErn   c                    |                      |d          }|d u}g }|r|                     |dg           }n|                     |di           g}t          |          sd S t	          dt          |                    D ]J}|                     ||                   }|d         }|| j        |<   d|z   }	|                    ||	           Kd S )Nr   r   r   r   r  )r   r   r   r   r   parse_ws_bid_askr  r   
rk   r   r   r   r   
rawTickersr   r   r   r   s
             rm   handle_bid_askzbitmart.handle_bid_ask  s      '22t#
 	?<<JJ..&"==>J: 	Fq#j//** 	0 	0A**:a=99FH%F$*DM&!#f,KNN6;////	0 	0rn   c                    |                      |d          }|                     ||          }|                      |d          }|                     |d          }|                     |||                     |          |                     |dd          |                     |dd          |                     |dd          |                     |d	d
          |d|          S )Nr   ms_task_px	ask_priceask_szask_volbid_px	bid_pricebid_szbid_vol)r   r   r   ask	askVolumebid	bidVolumer   )r   safe_marketr   safe_tickerr   r   )rk   r   r~   marketIdr   r   s         rm   r  zbitmart.parse_ws_bid_ask  s    ##FH55!!(F33!!&(33%%ff55	"Y//%%fhDD++FHiHH%%fhDD++FHiHH	!
 	!
 	 	 		rn   c                 x  K   |                                   d{V  d}d}|/|                     |          }|                     |          }d|z   }d}|                     d||          \  }}|                     ||           d{V  d}|dk    rd}	||	|d         z  }	nd}	d	|	gd
}nd	dgd}|                     | j        d         d         |         d                   }
|                     |
||                     ||          |           d{V }| j	        r|S | 
                    | j        |||d          S )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::rF   r2   spot/user/order:rv   spot/user/orders:ALL_SYMBOLSrr   rw   futures/orderr|   rC   r)   rE   T)r   r   r~   r   r   r   re   r   rh   r   filter_by_symbol_since_limitr!  )rk   r   r   r   r   r~   r   r   r   argsRequestr   	newOrderss               rm   watch_orderszbitmart.watch_orders  s      !!!!!!!!![[((F[[((F$v-K99-QWXXff---------6>>,K!vd|+<!$ GG &() G ##DIe$4T$:4$@$KLL**S+t7G7GQW7X7XZeffffffff	? 	00feUTXYYYrn   c                 n  K   |                                   d{V  d}d}|Q|                     |          }|                     |          }|d         rt          | j        dz             |d|z   z  }d}|                     d||          \  }}|                     ||           d{V  d}|dk    rd}|||d	         z  }nd
}d|gd}nddgd}|                     | j        d         d         |         d                   }| 	                    ||| 
                    ||          |           d{V S )a  
        unWatches 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 dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict[]: a list of `order structures <https://docs.ccxt.com/#/?id=order-structure>`
        Nzunsubscribe::ordersrG   zZ unWatchOrders() does not support a symbol for swap markets, unWatch from all markets only::rF   r2   r"  rv   r#  rp   rw   r$  r|   rC   r)   rE   )r   r   r~   r   rv   r   r   r   re   r   rh   )	rk   r   r   r~   r   r   r   r&  r   s	            rm   un_watch_orderszbitmart.un_watch_orders<  s      !!!!!!!!!+[[((F[[((Ff~ K"47  .J  $J  K  K  K4&=(K99-QWXXff---------6>>,K!vd|+<#$ GG (() G ##DIe$4T$:4$@$KLLZZ[$2B2B7F2S2SU`aaaaaaaaarn   c                    |                      |d          }|d S t          |          }g }i }|dk    r|                     | j        dd          }| j        t          |          | _        | j        }t          dt          |                    D ]T}	|                     ||	                   }
|                    |
           |                    |
           |
d         }d||<   Ud}t          |
                                          }t          dt          |                    D ](}	||	         }|dz   |z   }|                    ||           )|                    ||           d S )	Nr   r   ordersLimit  r   Tr!  r*  )r   r   r   rf   r!  r   r   parse_ws_orderr   r   keysr   )rk   r   r   r!  ordersLengthr'  r   r   storedr   orderr   r   
symbolKeyssymbolSpecificMessageHashs                  rm   handle_orderszbitmart.handle_ordersf  so   l 00>F6{{	!%%dlM4HHE{"4U;;[F1c&kk** ' '++F1I66e$$$  '''x"&',,..))
q#j//** 	A 	AA]F(3d(:V(C%NN9&?@@@@y+.....rn   r3  r~   c                 v   |                      |d          }|d u }|ri|                     |d          }|                     ||dd          }|                     |d          }|                     |d          }|                     |d          }|                     |d          }	|                     |d	          }
|                     |d
          }|                     |d          }|                     |d
         |          }|                     |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|          S |                     |d          }|                     |d          }|                     ||dd          }|                     |d          }|                     |d          }|                     |d           }|                     |d!          }| j	        }| 
                    |j        |i           }| 
                    ||          }d }|| 
                    |d"          }||g }|                    |           |                     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|          S )&Nr}   r   _rF   order_id	clientOidpricefilled_sizesizer   stater  sider   rv   clientOrderIdr   r   lastTradeTimestamptimeInForcepostOnly	stopPricetriggerPriceamountcostaverage)filled	remainingstatusfeer   r3  rq   rG   create_timeupdate_time
last_trader   client_order_iddeal_avg_price	deal_size)safe_numberr   r  parse_order_status_by_typer   safe_string_lower
safe_orderr   safe_symbolr!  r   hashmapr   r   parse_ws_order_sideparse_ws_order_status)rk   r3  r~   r}   r   r  rv   r@  r;  rI  rF  r   rawStaterK  r   r   r?  	orderInfoorderIdupdatedTimestamp	lastTradecachedOrdersr!  cachedOrderr   s                            rm   r/  zbitmart.parse_ws_order  s   X !!%22D. O	''x88H%%hVDDF!!%44B ,,UK@@M$$UG44E%%e];;F%%eV44F##E622D''w77H44VF^XNNF))%88IH%F))%88D?? $$&$ b$  	$
 T$ D$ %i$ $ t$ D$ $ $ T$ $ &$  !$" 4#$$ !! -$ $ $. /  2 ug66I''	8<<H%%hFCCF&&y*==G)))]CCI#00MJJy,??I;L__\%962FFF//&'::KF&99$>Fi(((?? $$&$ g$  !1!1)=N!O!O	$
 Y$ DLL33$ %&6$ ((F;;$ t$ D$ 001A1A)V1T1TUU$ )))W==$ T$ $ $**9f==$  !$" 4++I7GHH#$$ **9kBB!44T5E5EeX5V5VWW -$ $ $. /  rn   c           
      H    dddddddddd	}|                      |||          S )Nclosedopencanceled)	123456789r   )rk   statusIdstatusess      rm   rZ  zbitmart.parse_ws_order_status4  sC    

 

 (H===rn   c                 >    ddddd}|                      |||          S )Nbuysell)rf  rg  rh  ri  ro  )rk   sideIdsidess      rm   rY  zbitmart.parse_ws_order_sideB  s4    	
 
 vv666rn   c                 
  K   |                                   d{V  d}|                     ||           d{V  |                     |dddd          }d}||dd                    |          z   z  }d}d	dgd
}|                     | j        d         d         |         d                   }	|                     |	||                     ||          |           d{V }
| j        r|
S | 	                    | j
        |||          S )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>`
        NrG   TF	positionsr*  ,futures/positionrr   r|   rC   r)   rE   )r   r   r   joinr   re   r   rh   r   filter_by_symbols_since_limitrx  )rk   r   r   r   r   r   r   subscriptionHashr   r   newPositionss              rm   watch_positionszbitmart.watch_positionsK  sH      !!!!!!!!!f---------%%gvtT5II!4#((7"3"333K-!'(
 
 ##DIe$4T$:4$@$KLL!ZZ[$:J:J7TZ:[:[]mnnnnnnnn? 	 11$.'5RWXXXrn   c                 n  K   |,t          |          }|dk    rt          | j        dz             |                                  d{V  ddgd}d}|                     | j        d         d	         d
         d                   }|                     |||                     ||          |           d{V S )aH  
        unWatches all open positions

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

        :param str[] [symbols]: list of unified market symbols
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: status of the unwatch request
        Nr   zU unWatchPositions() does not support a list of symbols, unWatch from all markets onlyrp   rz  r|   zunsubscribe::positionsrC   r)   rG   rE   )r   r   rv   r   r   re   r   rh   )rk   r   r   r   r   r   r   s          rm   un_watch_positionszbitmart.un_watch_positionsi  s       \\Fzz"47  .E  $E  F  F  F!!!!!!!!!#'(
 
 /##DIe$4T$:6$B9$MNNZZ[$2B2B7F2S2SU`aaaaaaaaarn   c                    |                      |dg           }| j        t                      | _        | j        }g }t          dt	          |                    D ]I}||         }|                     |          }|                    |           |                    |           J|                     |d          }	t          dt	          |	                    D ]}|	|         }
|
                    d          }|d         }|                    d          }| 	                    |d|d          }| 
                    |          s|                    ||
           |                    |d	           d S )
Nr   r   zpositions::r*     ry  r   Frx  )r   rx  r   r   r   parse_ws_positionr   find_message_hashessplitr   is_emptyr   )rk   r   r   r   cacher~  r   rawPositionpositionr   r   partssymbolsStringr   rx  s                  rm   handle_positionszbitmart.handle_positions  sj   H ~~gvr22>!577DNq#d))$$ 	# 	#Aq'K--k::H)))LL""""00GGq#m,,-- 	7 	7A'*K%%d++E!!HM#))#..G,,\8WeTTI==++ 7y+666|[11111rn   c                    |                      |d          }|                     ||d d          }|d         }|                     |d          }|                     |d          }|                     |d          }|                     |d          }|                     i d|dd d|d	|d
|                     |          d|dd d|dk    rdn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k    rdndd d d d d           S )!Nr   rG   rM  rN  position_type	open_typer   rv   r   r   lastUpdateTimestamphedgedr?  r  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)r   r  r   safe_positionr   rS  )	rk   r  r~   r  r   openTimestampr   r?  marginModeIds	            rm   r  zbitmart.parse_ws_position  s    " ##Hh77!!(FD&AA!))(MBB%%h>>	  ?;;((;??!! #
H#
$#
 f#
 	#

 ]33#
 "9#
 d#
 tqyyFFw#
 ))(MBB#
 D,,V^DD#
 $**85EFF#
 ))(4DEE#
 #
 #
 #
  $!#
" T##
$ (,!%+/! $ 0 0;L M M)5):):**!#9#
 #
 #
   	rn   c                 z   |                      |d          }|d S d }t          |          }d|v }|r7t          d|          D ]%}||z
  dz
  }|                     ||                   }&n.t          d|          D ]}|                     ||                   }|                    | j        |         d|z              d S )Nr   r   r   r  ztrade:)r   r   r   handle_trade_loopr   r   )	rk   r   r   r   r   r   isSwapr   indexs	            rm   handle_tradezbitmart.handle_trade  s    @ ~~gv..<FTW$ 	91f%% = =
Q//U<<=
 1f%% 9 9//Q88t{6*Hv,=>>>>>rn   c                     |                      |          }|d         }|                     | j        dd          }|                     | j        |          t          |          | j        |<   | j        |         }|                    |           |S )Nr   tradesLimitr.  )parse_ws_trader   rf   r   r   r   r   )rk   entryr   r   r  r2  s         rm   r  zbitmart.handle_trade_loop!  s    ##E**x''mTJJ??4;//7",["9"9DKV$ern   r   c                 V   |                      |d          }|                     ||          }|                     |d          }d }|,|                      |d          }|                     |          }n|                     |          }d }|                      |d          }|                     |d          }|||rd}nd}d}|                     ||                      |d	          d |||d         d ||                     |d
d          |                     |dd          d |d d|          S )Nr   r  
created_atr?  mrt  rs  takertrade_idr;  
deal_pricer=  deal_vol)r   rv   r3  r   r   r   r   r?  r;  rF  rG  takerOrMakerrL  )r   r  r   	parse8601r   r   
safe_trader   )	rk   r   r~   r  r   r   r  r?  
buyerMakers	            rm   r  zbitmart.parse_ws_trade+  s]   . ##E844!!(F33%%eV44	''|<<Hx00II||I..Hv..^^E3//
!| !!DD D"L""5*55" X&''wEE((
CC( 
  
   	rn   c                    |                      ||           |                     |d          }|d u}g }|r|                     |dg           }n|                     |di           g}t	          |          sd S t          dt	          |                    D ]g}|r|                     ||                   n|                     ||                   }|d         }|| j        |<   d|z   }	|	                    ||	           hd S )Nr   r   r   r   zticker:)
r  r   r   r   r   r   parse_tickerparse_ws_swap_tickerr   r   r  s
             rm   handle_tickerzbitmart.handle_tickere  s   B 	FG,,,  '22t#
 	?<<JJ..&"==>J: 	Fq#j//** 	0 	0A9?mT&&z!}555TE^E^_ijk_lEmEmFH%F#)DL #f,KNN6;////	0 	0rn   c                 <   |                      |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d dd d |                      |d          ||                      |d          |                      |d          d|          S )Nr   rq   rG   r   r   highlowr  r  r  r  r  r  r  r  vwaprd  closelast
last_pricepreviousClosechanger  rH  	volume_24
mark_priceindex_price)
baseVolumequoteVolumer   r  
indexPrice)r   r  rW  )rk   r   r~   r  s       rm   r  zbitmart.parse_ws_swap_ticker  s    ##FH55 !
d&&xVDD!
!
 !
 D	!

 4!
 4##FK88!
 ))&)<<!
 4##FK88!
 ))&)<<!
 D!
 D!
 T!
 D$$V\::!
 T!
 d!
  $!!
" t#!
$ ++FK@@))&,??**6=AA-!
 !
 !
. /  	rn   rN   	timeframec                   K   |                                   d{V  |                     |          }|                     |          }d}|                     d||          \  }}|                     | j        di           }|                     ||          }	d}
|dk    rd|	z   }
nd|	z   }
|                     |
|||           d{V }| j        r|	                    ||          }| 
                    |||dd          S )	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
        NrF   r6   rb   klineklineBinr   T)r   r   r~   r   r   rf   r   rr   r   r   r   )rk   r   r  r   r   r   r~   r   rb   intervalnameohlcvs               rm   watch_ohlcvzbitmart.watch_ohlcv  s!      !!!!!!!!!V$$V$$99,PVWWf^^DL,CC
##J	::6>>X%DD(DnnT64@@@@@@@@? 	2NN6511E))%q$GGGrn   c                   K   |                                   d{V  |                     |          }|                     |          }d}|                     d||          \  }}|                     | j        di           }|                     ||          }d}|dk    rd|z   }nd|z   }|                     |ddi          }|                     ||||           d{V S )	a^  
        unWatches historical candlestick data containing the open, high, low, and close price, and the volume of a market

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

        :param str symbol: unified symbol of the market to fetch OHLCV data for
        :param str timeframe: the length of time each candle represents
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns int[][]: A list of candles ordered, open, high, low, close, volume
        NrF   r:   rb   r  r  rp   T)	r   r   r~   r   r   rf   r   r   rr   )	rk   r   r  r   r~   r   rb   r  r  s	            rm   un_watch_ohlcvzbitmart.un_watch_ohlcv  s      !!!!!!!!!V$$V$$99.&RXYYf^^DL,CC
##J	::6>>X%DD(DVmT%:;;^^D&$?????????rn   c                 ~   |                      |dd          }|                    d          dk    }|                     |d          }|d S |                    d          }|                     |dd          }|                    d	d          }|                    d
d          }|                    d          }	|                     |	d          }|                     | j        di           }
|                     ||
          }| 	                    |          }|dz  }|rht          dt          |                    D ]G}|                     ||         d          }|                     |          }|d         }|                     ||         d          }|                     ||          }|                     |d         |z            |z  |d<   |                     | j        |i           | j        |<   |                     | j        |         |          }|;|                     | j        dd          }t%          |          }|| j        |         |<   |                    |           |dz   |z   }|                    ||           Id S |                     |d          }|                     |d d d          }|d         }|                     |dg           }|                     | j        |i           | j        |<   |                     | j        |         |          }|;|                     | j        dd          }t%          |          }|| j        |         |<   t          dt          |                    D ]5}||         }|                     ||          }|                    |           6|                    ||           d S )Nr   r   rF   r   r   r   r  rq   r  Binru   rb   r.  r   candle
OHLCVLimitrG   items)r   r   r   r  r   replacer   rf   find_timeframeparse_timeframer   r   r  r   parse_ohlcvparse_to_intohlcvsr   r   r   r   )rk   r   r   r   r   r   r  part1r  intervalPartsrb   r  durationdurationInMsr   r  r~   r   rawOHLCVparsedr2  r   r   r  r  s                            rm   handle_ohlcvzbitmart.handle_ohlcv  s   H $$Wgw??,,v&&!+w//<Fc""  2..=="--##E2.. s++##M155^^DL,CC
''*==	''	22$  	,1c$ii(( 4 4++DGX>>))(33)>>$q'8<<))(F;; --fQi,.FGG,Vq	&*oodk62&N&NF#V)<iHH> --dlL$OOE2599F5;DK'	2f%%%%mh6v{33334 4" ''h77H%%hdFCCFH%FNN4"55E"&//$+vr"J"JDK__T[%8)DDF~))$,dKK.u5517F#I.1c%jj)) & &q))&&99f%%%%NN67+++++rn   c                   K   |                                   d{V  |                     | j        di           }|                     |dd          }|                     |          }|                     |          }d}|                     d||          \  }}|dk    r|dk    rd}|                     ||||           d{V }|                                S )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
        Nr0   rJ   rK   rF   rG   depth50)	r   r   rf   r   r   r~   r   rr   r   )	rk   r   r   r   rf   rJ   r~   r   	orderbooks	            rm   watch_order_bookzbitmart.watch_order_bookP  s       !!!!!!!!!../?DD  '3FGGV$$V$$99:JFTZ[[f6>>e':::E..fEEEEEEEE	   rn   c                   K   |                                   d{V  |                     | j        di           }|                     |dd          }|                     |          }|                     |          }d}|                     d||          \  }}|dk    r|dk    rd}|                     |d	d
i          }|                     ||||           d{V S )ar  
        unWatches information on open orders with bid(buy) and ask(sell) prices, volumes and other data

        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

        :param str symbol: unified array of symbols
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: A dictionary of `order book structures <https://docs.ccxt.com/#/?id=order-book-structure>` indexed by market symbols
        Nr0   rJ   rK   rF   r;   rG   r  rp   T)	r   r   rf   r   r   r~   r   r   rr   )rk   r   r   rf   rJ   r~   r   s          rm   un_watch_order_bookzbitmart.un_watch_order_bookj  s       !!!!!!!!!../?DD  '3FGGV$$V$$99:LfV\]]f6>>e':::EVmT%:;;^^E64@@@@@@@@@rn   c                     |                      |d          }|                      |d          }|                    ||           d S )Nr   r  )
safe_floatstore)rk   booksidedeltar;  rF  s        rm   handle_deltazbitmart.handle_delta  sB    q))**uf%%%%%rn   c                 ~    t          dt          |                    D ]}|                     |||                    d S )Nr   )r   r   r   )rk   r  deltasr   s       rm   handle_deltaszbitmart.handle_deltas  sH    q#f++&& 	3 	3Ahq	2222	3 	3rn   c                    |                      |dg           }|                      |dg           }|                     |d         |           |                     |d         |           |                     |d          }|                     |d          }|                     |          }||d<   ||d<   |                     |          |d<   |S )Nasksbidsr  r   r   r   )r   r  r   r   rW  r   )	rk   r   r   r  r  r  r   r  r   s	            rm   handle_order_book_messagez!bitmart.handle_order_book_message  s    * ~~gvr22~~gvr229V,d3339V,d333%%gv66	##GX66!!(++$	(!*	+ $Y 7 7	*rn   c                    d|v }g }|r|                      |d|          }n-|                     |d          }||                    |           t          |          }|dk    rd S |                     |dd          }g d}d}	t          dt          |                    D ]:}
||
         }|                    |          dk    r|                     |          }	 n;|r|                    dd          }t          dt          |                    D ]I}
||
         }| 	                    |d          }| 
                    |          }|| j        vr%|                     i |	          }||d<   || j        |<   | j        |         }| 	                    |d	          }|d
k    s|                    d          dk    s|                    i            |                     |||           |                     |d          }|d         ||d<   |                     |          |d<   |dz   |z   }|                    ||           |dz   |z   }|                    ||           Kd S |                    d          }|d                             dd          }|d         }|d         }| 	                    |d          }| 
                    |          }|| j        vr%|                     i |	          }||d<   || j        |<   | j        |         }|                     |d          }|dk    rdnd}|dk    rt'          g |	          ||<   nt)          g |	          ||<   t          dt          |                    D ]b}
||
         }|                     |d          }|                     |d          }|                     ||          }|                    ||           ct          |d                   }t          |d                   } |dk    s| dk    rd S |                     |d          }||d<   |                     |          |d<   |}|                    ||           |dz   |z   }|                    ||           d S )Nr   r   r   r   )100502010rj  rt   rq   r   r   r   increaser  r   r   ru   rz   depthswayr  r  r  r;  vol)r   r   r   r   r   r   r   r  r  r   rW  
orderbooks
order_bookresetr  r   r   r   r  r   r   rS  r   r  )!rk   r   r   r   datasorderBookEntryr   r   limitsToCheckr   r   limitStringr   updater  r   obr  r   r   r   messageHashForMulti
tablePartsr   r  r  r?  rJ   r;  rF  orderbookSide
bidsLength
asksLengths!                                    rm   handle_order_bookzbitmart.handle_order_book  s   H W$ 	-NN7FE::EE!^^GV<<N)^,,,UQ;;F(('7CC666q#m,,-- 	 	A'*K,,11))+66 2  ;	;!))'266G1c%jj)) ? ?q++FH==))(33$/11U33B#)BxL.0DOF+ OF3	''77J&&0@0@0L0LPQ0Q0QOOB'''..vvyIII --ff==	[)1-6Ik*,0LL,C,CIj))C/(:y+666&-mf&<#y*=>>>>+? ?. %**3//J m++J;;G8D(^F''h77H%%h//Fdo--__R//%8*,'/I##D%00C!Qhh66VDaxx"&r5//	$"&r5//	$1c&kk** 3 3q	((88))%77 $	4 @ @##E62222Yv.//JYv.//JaZ1__))$77I%.Ik"$(LL$;$;Ij!%KNN9k222")C-&"8NN9&9:::::rn   c                 0  K   |                                   d{V  d}|                     d|||          \  }}}d}|                     |ddd          \  }}|dk    r|dk    rd}|                     ||||           d{V }|                                S )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
        Nr1   rJ   rK   rG   r  )r   r   r   r   r   )rk   r   r   r   r   r   r  s          rm   watch_order_book_for_symbolsz$bitmart.watch_order_book_for_symbolsA  s       !!!!!!!!! $ @ @A[]dfkms t tv77@Z\cexyy6>>g)<<<G11'4&QQQQQQQQ	   rn   c                 8  K   |                                   d{V  d}|                     d|d|          \  }}}d}|                     |ddd          \  }}|dk    r|dk    rd}|                     |ddi          }|                     ||||           d{V S )	au  
        unWatches 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 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<   rJ   rK   rG   r  rp   T)r   r   r   r   r   )rk   r   r   r   r   s        rm   un_watch_order_book_for_symbolsz'bitmart.un_watch_order_book_for_symbolsW  s       !!!!!!!!! $ @ @A]_fhlnt u uv77@\^egz{{6>>g)<<<GVmT%:;;,,WdGVLLLLLLLLLrn   c                    K   |                                   d{V  |                     |          }|                     |g|           d{V }||         S )aq  
        watch the current funding rate

        https://developer-pro.bitmart.com/en/futuresv2/#public-funding-rate-channel

        :param str symbol: unified market symbol
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: a `funding rate structure <https://docs.ccxt.com/?id=funding-rate-structure>`
        N)r   r   watch_funding_rates)rk   r   r   fundingRates       rm   watch_funding_ratezbitmart.watch_funding_ratel  sq       !!!!!!!!!V$$ 44fXvFFFFFFFF6""rn   c                 n  K   |t          | j        dz             |                                  d{V  |                     |          }d}|                     d||          \  }}|                     d|||           d{V }| j        ri }|||d         <   |S |                     | j        d|          S )a  
        watch the funding rate for multiple markets

        https://developer-pro.bitmart.com/en/futuresv2/#public-funding-rate-channel

        :param str[] symbols: a list of unified market symbols
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: a dictionary of `funding rate structures <https://docs.ccxt.com/?id=funding-rate-structure>`, indexed by market symbols
        Nz1 watchFundingRates() requires an array of symbolsr,   r&  r   )	r   rv   r   r   r   r   r   r   fundingRates)rk   r   r   r~   r   r&  r)  s          rm   r%  zbitmart.watch_funding_rates{  s       ?#DG.a$abbb!!!!!!!!!--g66
!??@SU[]cdd
F 33M:wX^________? 	 !#L2=LX./##D$5xIIIrn   c                     |                      |di           }|                     |          }|d         }|| j        |<   d|z   }|                    ||           d S )Nr   r   zfundingRate:)r   parse_funding_rater)  r   )rk   r   r   r   r&  r   r   s          rm   handle_funding_ratezbitmart.handle_funding_rate  sg      ~~gvr22--d33X&$/&!$v-{K00000rn   c                   K   |                                   |                     | j        d         d         |         d                   }d}|                     |          }|                    |          }|                     |j        |          }|t          |                                           }| j	        }	d}
|dz   |	z   dz   |
z   }| 
                    |                     |          |                     | j                  t          j                  }d }|dk    rd| j        ||gd	}nd
| j        ||dgd}|                     ||          }|                     ||||           | d {V S )NrC   r)   rE   authenticatedzbitmart.WebSocket#rF   loginrw   accesswebr|   )check_required_credentialsr   re   r   reusableFuturer   r   strmillisecondsuidhmacencodesecrethashlibsha256apiKeyr   r   )rk   r   r   r   r   r   r   r.  r   memopathauth	signaturer   r   s                  rm   r   zbitmart.authenticate  s     '')))##DIe$4T$:4$@$KLL%S!!&&{33(<kJJ D--//00I8D&Ds?T)C/$6D		$++d"3"3T[[5M5Mw~^^IGv~~!!!  '!!	  kk'622GJJsK+>>>||||||rn   c                     |S N )rk   r   r   s      rm   handle_subscription_statusz"bitmart.handle_subscription_status  s	     rn   c                 j    d}|                      |j        |          }|                    d           d S )Nr.  T)r   r   r   )rk   r   r   r   r   s        rm   handle_authenticatezbitmart.handle_authenticate  s6     &==trn   c                    |                      |d          }|                      |d          }	 ||| j        dz   |                     |          z   }|                     | j        d         ||           |                     |d|          }|                     | j        d         ||           |                      |d          }|dk    rt          |          t          |          d	S # t          $ r^}t          |t                    r)d
}	|                    ||	           |	|j        v r|j        |	= |                    |           Y d }~dS d }~ww xY w)N	errorCodeerrorr   exactr   broadr}   r1  Fr.  T)r   rv   jsonthrow_exactly_matched_exception
exceptionsr   throw_broadly_matched_exceptionr   r   	Exceptionr   rejectr   )
rk   r   r   rI  rJ  feedbackmessageStringr}   er   s
             rm   handle_error_messagezbitmart.handle_error_message  ss    $$Wk::	  '22	$(97S=499W+=+==44T_W5MyZbccc $E J J44T_W5M}^fggg))'8<<X%%-h777#H---5 	 	 	1122 :-a---&"666,[9MM!44444	s   B9C) )
E3AEEc                 V   |                      |dd          }d|z   }|                     |          }|                     |d          }d|z   }|                     |dd          }|                     ||||           |                     ||||           |                     |           d S )Ntopicr   rs   r   subHashIsPrefixF)r   get_un_sub_paramsr   r   clean_unsubscriptionclean_cache)	rk   r   r   messageTopicunSubMessageTopicsubscriptionr   	unsubHashrY  s	            rm   handle_un_subscriptionzbitmart.handle_un_subscription  s    * ))'7GDD+l: --l;;""<;;#g-	..7H%PP!!&'9oNNN!!&,8I?[[[&&&&&rn   c                    |                     d          }|                     |d          }|                     d          }|                     |d          }|                     |          }|                     |d          }|                     |d          }||d|z   z  }|                     |d          }	g }
d }|}d}d}|                     |          }|dk    s|dk    r|}d	}|	N|	d
k    rH|dk    rdnd}|                     |	d ||          }|d         }|||z   z  }|
                    |           nd}g }|                    d          r|                    dd          }|                    d          r|                    dd          }| 	                    | j
        di           }|                     ||          }||g}|                    |           ||
|||d}|S )Nru   r   r   r     Fr!  rx  r*  ALL_SYMBOLSrF   r8  rq   r   Tr  r  rb   )rX  r   r   symbolsAndTimeframesrY  )r  r   rU  parse_market_typeparse_topicr  r   
startswithr  r   rf   r  )rk   r]  r  r   marketTypeAndTopicrawMarketTyper   rX  	thirdPartr  r   r   r   hashDelimiterrY  parsedTopic	delimiterr~   re  r  rb   r  symbolAndTimeframer   s                           rm   rZ  zbitmart.get_un_sub_params$  s<   ""3''""5!,,$]]3//../A1EE++M::
  !3Q77$$%7;;	 S9_$E##E1--&&u--8##)C)C!G M x='@'@ *f 4 42I%%hiLLFH%F}v--GNN6"""""O!G$$ 	<}}Wb11H""5)) 7#++E266lBGGJ++HjAAI"()!4 ''(:;;; $8.
 
 rn   c                     |                     d          rdS |                     d          rdS ddddddd	}|                     |||          S )
NrJ   r  r  r  r   r   r!  rx  )r   r   z
user/orderzuser/ordersr3  r  )rh  r   )rk   rX  topicss      rm   rg  zbitmart.parse_topicT  sp    G$$ 	;G$$ 	7"##
 
 u555rn   r   c                 :    ddd}|                      |||          S )NrF   rG   )rF   r   ro  )rk   r   typess      rm   rf  zbitmart.parse_market_typec  s.    
 
 z:>>>rn   c           	         |                      ||          rd S d|v }|s^|                     |dd          }|A| j        | j        | j        | j        d}|                     ||          }| |||           d S d S d S |                     |dd          }| j        | j        | j        | j	        | j
        | j        | j        | j        d}|                    d          d	k    r|                     ||           t          |                                          }t#          d	t%          |                    D ]E}	||	         }
|                    |
          d	k    r"|                     ||
          } |||           Fd S )
Nr   eventr}   )r0  r1  rr   rp   r   r   )rJ   r   r   r  r3  r  r   assetr&  r   )rV  r   rG  rE  ra  r   r  r  r  r  r6  r  r   r   r,  r   r0  r   r   )rk   r   r   isDataUpdateru  methodsmethodr   r0  r   keys              rm   handle_messagezbitmart.handle_messagej  s   $$VW55 	F^ ')  	,&&wBBE  "5"6!%!@#'#>! ! %88%F67+++++ !  &% (('7CCG/,**+ 1.,	 	G ||M**a//((999''D1c$ii(( , ,1g<<$$))!__Wc::FF67+++	, ,rn   rC  )Q__name__
__module____qualname__r	   rj   rr   r5  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   r   r(  r+  r6  dictr   r/  rZ  rY  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   r%  r,  r   rE  rG  r   rV  ra  rZ  rg  rf  r{  __classcell__)rl   s   @rm   r   r      sK	       [# [ [ [ [ [ [z =? b b b b> TX`b  k  k  k3  k  k  k  k  kD *, #b #b #b #b #b #bJ
  
  
  
  
 B B B>8V >8 >8 >8 >8@ <@d[] S S SS S Sbfglbm S S S S OSaenp  d3i  [^ uyz  vA    : 9; H HC Hs H H H H LN W Wc WRU W W W W$ ]aik - -c -DI -VY - - - - 68   F      6:" E E7 E E E E E, 9; = =C =s = = = = 9=R T Tg TC T T T T$ 8<B $F $FW $F7 $F $F $F $FL0V 0 0 0 0"     04$UYbd +Z +Z +ZC +Zs +Zimnsit +Z +Z +Z +ZZ 37r (b (bC (bc (b (b (b (bTM/F M/ M/ M/ M/^} }D }& } } } }~> > >7 7 7 8<$]ajl Y YW YC YWZ Yquv~q Y Y Y Y< ;?r b b bc b b b b.72v 72 72 72 72r5 5& 5 5 5 5n/?6 /? /? /? /?b  8 8D 8& 8 8 8 8t00F 00 00 00 00d' '6 ' ' ' 'R ?CQUdhqs H H H H3 H^a Hx|  ~B  yC H H H H> BFb @ @3 @3 @UX @ @ @ @6T,6 T, T, T, T,l @DB ! !S ! !S\ ! ! ! !4 =? A A A3 A A A A0& & &
3 3 3    BS; S; S; S; S;j SW_a ! !$s) !C !fo ! ! ! !, PR M MT#Y MVY M M M M* <> # #s #+ # # # # <@ J J J| J J J J.1& 1 1 1 1. /1 # # # #J    	& 	 	 	 	 6  t        D!'V !' !' !' !'F. . .`6 6 6?C ? ? ? ?R,V R, R, R, R, R, R, R, R,rn   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   r   r   !ccxt.async_support.base.ws.clientr   typingr   ccxt.base.errorsr   r   r   r   async_supportr   rD  rn   rm   <module>r     s       A  A  A  A  A  A  A  A  A  A  A  A A A A A A A A A  Z  Z  Z  Z  Z  Z  Z  Z  Z  Z  Z  Z  Z  Z  Z  Z  Z  Z  Z  Z  Z  Z  Z  Z  Z  Z  Z  Z  Z  Z  Z  Z  Z  Z 4 4 4 4 4 4       * * * * * * 0 0 0 0 0 0 . . . . . . ) ) ) ) ) )i, i, i, i, i,d ( i, i, i, i, i,rn   