
    ei                       d dl Zd dlmZmZmZmZ d dlZd dlm	Z	m
Z
mZmZmZmZmZmZmZmZmZmZ 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 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)AnyBalancesBoolIntOrder	OrderBookPositionStrStringsTickerTickersTrade)Client)List)ExchangeError)AuthenticationError)ArgumentsRequired)
BadRequest)RateLimitExceeded)ChecksumError)UnsubscribeError)Precisec                       e Zd Zdef fdZdi fdefdZ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efdZdFdZd	i fd
edefdZdefdZdFdZdd	d	i fdededededee         f
dZdi fdededefdZdefdZdFdefdZd	i fdededefdZi fdedefdZi fdededededef
dZd	i fd
ee         dedefd Zdefd!Z deded"efd#Z!d$ Z"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'defd)Z(dFd*Z)d	d	d	i fd
edededee*         fd+Z+defd,Z,dFd-Z-d	d	d	i fde.dededee/         fd.Z0defd/Z1dFd0Z2d1 Z3d	d	d	i fde.dededee$         fd2Z4defd3Z5i fde6fd4Z7defd5Z8i fd6Z9i fd7Z:i fd8Z;i fd9Z<i fd:Z=defd;Z>dede?fd<Z@defd=ZAdefd>ZBdefd?ZCdefd@ZDdefdAZEdefdBZFdefdCZGdefdDZHdefdEZI xZJS )Gbitgetreturnc                    |                      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id d d!d"d#d$d%d&d'd(d)d*d+d,d-didd.d/| j        idt
          t          t
          t          t          t          t          t          t          t          t
          t          t
          d0i d1id2          S )3NwsTcreateOrderWsFeditOrderWsfetchOpenOrdersWsfetchOrderWscancelOrderWscancelOrdersWscancelAllOrdersWswatchBalancewatchMyTrades
watchOHLCVwatchOHLCVForSymbolswatchOrderBookwatchOrderBookForSymbolswatchOrderswatchTickerwatchTickers)watchBidsAskswatchTradeswatchTradesForSymbolswatchPositionsapiz wss://ws.bitget.com/v2/ws/publicz!wss://ws.bitget.com/v2/ws/privatez wss://ws.bitget.com/v3/ws/publicz!wss://ws.bitget.com/v3/ws/private)publicprivate	utaPublic
utaPrivatez#wss://wspap.bitget.com/v2/ws/publicz$wss://wspap.bitget.com/v2/ws/privatez#wss://wspap.bitget.com/v3/ws/publicz$wss://wspap.bitget.com/v3/ws/private)r!   demo  1m3m5m15m30m1H4H6H12H1D1W)r=   r>   r?   r@   rA   1h4h6h12h1d1wignoreDuplicates)tradesLimit
OHLCVLimit
timeframesr3   utaping)30001300023000330004300053000630007300113001230013300143001530016)exactbroad)hasurlsoptions	streaming
exceptions)deep_extendsuperr   describerS   r   r   r   )self	__class__s    M/var/www/html/volatility/venv/lib/python3.11/site-packages/ccxt/pro/bitget.pyrj   zbitget.describe   s   fd 3 3 < < > >d u $U	
    !% $U    d ' !$ +D t  t!" #$ "&#)-"&+  0 "D#F%G&I	  #H#I%J&L	  "  $"      '  ) . 	 !+!4!+!4!4!2!2!4!4!4!+!4!+   ! CUA
 UA
 U U U	    FrR   c                    d }||                      d |          \  }}n,|d         s|d         r|                      ||          \  }}nd}d }|                     ||d|          \  }}|}|r|                                }||gS )NswapfutureSPOTinstType)handleProductTypeAndParamshandle_option_and_paramslower)rk   
methodNamemarketrR   paramsrs   
instypeAuxs          rm   get_inst_typezbitget.get_inst_typep   s    >#>>tVLLHffVn 	&"2 	#>>vvNNHffH
!::6:z[cdd
F 	(~~''H&!!rn   symbolc                 n  K   |                                   d{V  |                     |          }|d         }d|z   }d}d}|                     |ddd          \  }}|                     d|||          \  }}d|i}|rdnd	}|rdnd
}	d||<   |d         ||	<   |                     ||||           d{V S )a  
        watches a price ticker, a statistical calculation with the information calculated over the past 24 hours for a specific market

        https://www.bitget.com/api-doc/spot/websocket/public/Tickers-Channel
        https://www.bitget.com/api-doc/contract/websocket/public/Tickers-Channel
        https://www.bitget.com/api-doc/uta/websocket/public/Tickers-Channel

        :param str symbol: unified symbol of the market to watch the ticker for
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :param boolean [params.uta]: set to True for the unified trading account(uta), defaults to False
        :returns dict: a `ticker structure <https://docs.ccxt.com/?id=ticker-structure>`
        Nr|   ticker:r0   rR   Frs   topicchannelinstIdtickerid)load_marketsrx   ru   r{   watch_public)
rk   r|   ry   rx   messageHashrs   rR   argstopicOrChannelsymbolOrInstIds
             rm   watch_tickerzbitget.watch_ticker   s      !!!!!!!!!V$$!&(33FM5RWXXV--mVS&QQ&
 %(6Y%(6h'^%d|^&&sKvFFFFFFFFFrn   c                 x   K   |                                   d{V  |                     |ddd|           d{V S )a  
        unsubscribe from the ticker channel

        https://www.bitget.com/api-doc/spot/websocket/public/Tickers-Channel
        https://www.bitget.com/api-doc/contract/websocket/public/Tickers-Channel

        :param str symbol: unified symbol of the market to unwatch the ticker for
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns any: status of the unwatch request
        Nr   r0   )r   un_watch_channel)rk   r|   ry   s      rm   un_watch_tickerzbitget.un_watch_ticker   s[       !!!!!!!!!**68X}V\]]]]]]]]]rn   Nsymbolsc                   K   |                                   d{V  |                     |dd          }|                     |d                   }d}d}|                     |ddd          \  }}|                     d|||          \  }}g }g }t          dt          |                    D ]l}||         }	|                     |	          }
d|i}|rdnd}|rd	nd
}d||<   |
d         ||<   |                    |           |                    d|	z              m|                     ||||           d{V }| j	        ri }|||d	         <   |S | 
                    | j        d	|          S )a  
        watches a price ticker, a statistical calculation with the information calculated over the past 24 hours for all markets of a specific list

        https://www.bitget.com/api-doc/spot/websocket/public/Tickers-Channel
        https://www.bitget.com/api-doc/contract/websocket/public/Tickers-Channel
        https://www.bitget.com/api-doc/uta/websocket/public/Tickers-Channel

        :param str[] symbols: unified symbol of the market to watch the tickers for
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :param boolean [params.uta]: set to True for the unified trading account(uta), defaults to False
        :returns dict: a `ticker structure <https://docs.ccxt.com/?id=ticker-structure>`
        NFr   r1   rR   rs   r   r   r|   r   r   r   r~   )r   market_symbolsrx   ru   r{   rangelenappendwatch_public_multiple
newUpdatesfilter_by_arraytickersrk   r   ry   rx   rs   rR   topicsmessageHashesir|   marketInnerr   r   r   r   results                   rm   watch_tickerszbitget.watch_tickers   s      !!!!!!!!!%%gtU;;WQZ((33FNESXYYV--nfc6RR&q#g,,'' 	5 	5AQZF++f--KHD ),:WWN),:XX(N#+D #.t#4D MM$  V!34444223vvVVVVVVVV? 	F(/F78$%M##DL(GDDDrn   clientc                     |                      ||           |                     |          }|d         }|| j        |<   d|z   }|                    ||           d S )Nr|   r~   )handle_bid_askparse_ws_tickerr   resolverk   r   messager   r|   r   s         rm   handle_tickerzbitget.handle_ticker   sh    p 	FG,,,%%g..!%V&(v{+++++rn   c           	         |                      |di           }|                      |dg           }|                      |di           }|                     |d          }|                     |d|          }|                     |d          }|dk    rdnd}	|                     |d          }
|                     |d	|
          }|                     ||d |	          }|                     |d
d          }|                     |dd          }t          j        |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/d0          |d1|          S )2Nargdatar   tsrs   spotcontractr|   r   lastPr	lastPriceprice24hPcnt	change24h100	timestampdatetimehighhigh24hhighPrice24hlowlow24hlowPrice24hbidbidPr	bid1Price	bidVolumebidSzbid1SizeaskaskPr	ask1Price	askVolumeaskSzask1Sizevwapopenopen24hopenPrice24hcloselastpreviousClosechange
percentageaverage
baseVolume	volume24hquoteVolumeturnover24h)r   r   info)

safe_valuesafe_integersafe_string_lowersafe_stringsafe_marketsafe_string_2r   
string_mulsafe_tickeriso8601)rk   r   rx   r   r   r   utaTimestampr   rs   
marketTypeutaMarketIdmarketIdr   changeCoefficientchangePercentages                  rm   r   zbitget.parse_ws_ticker  s   | oogub11w33q"--(($77%%fdLAA	))#z:: (F 2 2VV
&&sH55##FHkBB!!(FD*EE""68[AA ..v~{SS"-.?GG !
fX&!
!
 Y//!
 D&&vy.II	!

 4%%fhFF!
 4%%fg{CC!
 ++FGZHH!
 4%%fg{CC!
 ++FGZHH!
 D!
 D&&vy.II!
 U!
 E!
 T!
 d!
  *!!
" t#!
$ ,,V\;OO--fm]SS)!
 !
 !
* +  	rn   c                   K   |                                   d{V  |                     |dd          }|                     |d                   }d}d}|                     |ddd          \  }}|                     d|||          \  }}g }g }t          dt          |                    D ]l}||         }	|                     |	          }
d|i}|rdnd	}|rd
nd}d||<   |
d         ||<   |                    |           |                    d|	z              m|                     ||||           d{V }| j	        ri }|||d
         <   |S | 
                    | j        d
|          S )a  
        watches best bid & ask for symbols

        https://www.bitget.com/api-doc/spot/websocket/public/Tickers-Channel
        https://www.bitget.com/api-doc/contract/websocket/public/Tickers-Channel
        https://www.bitget.com/api-doc/uta/websocket/public/Tickers-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
        :param boolean [params.uta]: set to True for the unified trading account(uta), defaults to False
        :returns dict: a `ticker structure <https://docs.ccxt.com/?id=ticker-structure>`
        NFr   r1   rR   r2   rs   r   r   r|   r   r   r   bidask:)r   r   rx   ru   r{   r   r   r   r   r   r   bidsasksr   s                   rm   watch_bids_askszbitget.watch_bids_asks  s      !!!!!!!!!%%gtU;;WQZ((33FNESXYYV--ovsFSS&q#g,,'' 	5 	5AQZF++f--KHD ),:WWN),:XX(N#+D #.t#4D MM$  V!34444223vvVVVVVVVV? 	F(/F78$%M##DM8WEEErn   c                     |                      |          }|d         }|| j        |<   d|z   }|                    ||           d S )Nr|   r   )parse_ws_bid_askr   r   r   s         rm   r   zbitget.handle_bid_ask  sO    &&w//! &f&(v{+++++rn   c                    |                      |di           }|                      |dg           }|                      |di           }|                     |d          }|                     |d|          }|                     |d          }|dk    rdnd}	|                     |d          }
|                     |d	|
          }|                     ||d |	          }|                     |d         ||                     |          |                     |d
d          |                     |dd          |                     |dd          |                     |dd          |d|          S )Nr   r   r   r   rs   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   )rk   r   rx   r   r   r   r   r   rs   r   r   r   s               rm   r   zbitget.parse_ws_bid_ask  sr   oogub11w33q"--(($77%%fdLAA	))#z:: (F 2 2VV
&&sH55##FHkBB!!(FD*EEX&"Y//%%fg{CC++FGZHH%%fg{CC++FGZHH	!
 	!
 	 	 		rn   r=   	timeframesincelimitc                   K   |                                   d{V  |                     |          }|d         }|                     | j        d          }|                     ||          }d}	d}
d}|                     |ddd          \  }}|                     d|||          \  }
}d|
i}|r3d|d	<   |d
         |d<   ||d<   |                     |ddi          }d|z   }	nd|z   |d<   |d
         |d<   d|z   dz   |z   }	|                     ||	||           d{V }| j	        r|
                    ||          }|                     |||dd          S )a  
        watches historical candlestick data containing the open, high, low, close price, and the volume of a market

        https://www.bitget.com/api-doc/spot/websocket/public/Candlesticks-Channel
        https://www.bitget.com/api-doc/contract/websocket/public/Candlesticks-Channel
        https://www.bitget.com/api-doc/uta/websocket/public/Candlesticks-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 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
        :param boolean [params.uta]: set to True for the unified trading account(uta), defaults to False
        :returns int[][]: A list of candles ordered, open, high, low, close, volume
        Nr|   rQ   r+   rR   Frs   kliner   r   intervalTkline:candler   r   candles::r   )r   rx   r   re   r   ru   r{   extendr   r   getLimitfilter_by_since_limit)rk   r|   r   r   r   ry   rx   rQ   r   r   rs   rR   r   ohlcvs                 rm   watch_ohlcvzbitget.watch_ohlcv  s       !!!!!!!!!V$$!__T\<@@
##J	::33FL%QVWWV--lFCPP&
  		@#DM#D\DN'D[[%77F"V+KK&1DO#D\DN$y036?K''[$GGGGGGGG? 	2NN6511E))%q$GGGrn   c                 B  K   |                                   d{V  |                     | j        d          }|                     ||          }d}d}||                     |          }d}d}	|                     |ddd          }
|
d         }|                     d|||          \  }}d|i}|r:d}||d	<   |d
         |d<   ||d<   |                     |ddi          }||d<   ||z   }	nd|z   }||d<   |d
         |d<   d|z   }	|                     |||	d|           d{V S )a  
        unsubscribe from the ohlcv channel

        https://www.bitget.com/api-doc/spot/websocket/public/Candlesticks-Channel
        https://www.bitget.com/api-doc/contract/websocket/public/Candlesticks-Channel
        https://www.bitget.com/api-doc/uta/websocket/public/Candlesticks-Channel

        :param str symbol: unified symbol of the market to unwatch the ohlcv for
        :param str [timeframe]: the period for the ratio, default is 1 minute
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :param boolean [params.uta]: set to True for the unified trading account(uta), defaults to False
        :returns dict: A dictionary of `order book structures <https://docs.ccxt.com/?id=order-book-structure>` indexed by market symbols
        NrQ   r+   rR   Fr   rs   r   r   r   r|   r   Tr   r   r   r   )	r   	safe_dictre   r   rx   ru   r{   r   r   )rk   r|   r   ry   rQ   r   r   rx   rs   r   valuesrR   r   s                rm   un_watch_ohlcvzbitget.un_watch_ohlcv  s      !!!!!!!!!^^DL,??
##J	::[[((F..v|UERR1I--lFCPP&
  	0G#DM#D\DN'D[[%77F!)F:!F*KK)G%DO#D\DN$x/K**67KW]^^^^^^^^^rn   c                    |                      |di           }|                     |d          }|dk    rdnd}|                     |dd          }|                     |d d |          }|d         }|                      | j        |i           | j        |<   |                     |dd          }	|                     |d	          }
d }|
d
}|	                    dd          }
nd}|                      | j        d          }|                     |
|          }|                      | j        |         |          }|;| 	                    | j        dd          }t          |          }|| j        |         |<   |                      |dg           }t          dt          |                    D ]3}|                     ||         |          }|                    |           4d }|rd|z   }nd|z   dz   |z   }|                    ||           d S )Nr   rs   r   r   r   r|   r   r   r   Fr    TrQ   rP   r<   r   r   r   r   r   )r   r   r   r   ohlcvsr   replacere   find_timeframer   r   r   r   parse_ws_ohlcvr   r   )rk   r   r   r   rs   r   r   rx   r|   r   r   isUtarQ   r   storedr   r   r   parsedr   s                       rm   handle_ohlcvzbitget.handle_ohlcv3  s   t oogub11))#z:: (F 2 2VV
%%c8X>>!!(D$
CC!"oodk62FFF$$S)W==##C44Ex44HHE__T\<@@
''*==	V!4i@@>%%dlL$GGE*511F-3DK	*w33q#d))$$ 	" 	"A((a&99FMM&!!!! 	@"V+KK$y036?Kv{+++++rn   c           
      &   |d         rdnd}|                      |dd          |                     |dd          |                     |dd	          |                     |d
d          |                     |dd          |                     |d|          gS )Ninverse      startr   r      r      r      r      volume)safe_integer_2safe_number_2)rk   r   rx   volumeIndexs       rm   r  zbitget.parse_ws_ohlcv  s    2 #9-5aaAw22ufa00ufa00ueQ//ugq11uh<<
 	
rn   c                 B   K   |                      |g||           d{V S )aJ  
        watches information on open orders with bid(buy) and ask(sell) prices, volumes and other data

        https://www.bitget.com/api-doc/spot/websocket/public/Depth-Channel
        https://www.bitget.com/api-doc/contract/websocket/public/Order-Book-Channel
        https://www.bitget.com/api-doc/uta/websocket/public/Order-Book-Channel

        :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 boolean [params.uta]: set to True for the unified trading account(uta), defaults to False
        :returns dict: A dictionary of `order book structures <https://docs.ccxt.com/?id=order-book-structure>` indexed by market symbols
        N)watch_order_book_for_symbols)rk   r|   r   ry   s       rm   watch_order_bookzbitget.watch_order_book  s4       66xOOOOOOOOOrn   c                 (  K   |                                   d{V  d}|                     |d          }|dk    s|dk    s|dk    s|dk    r(|                     |d          }|t          |          z  }|                     ||dd	|           d{V S )
a  
        unsubscribe from the orderbook channel

        https://www.bitget.com/api-doc/spot/websocket/public/Depth-Channel
        https://www.bitget.com/api-doc/contract/websocket/public/Order-Book-Channel
        https://www.bitget.com/api-doc/uta/websocket/public/Order-Book-Channel

        :param str symbol: unified symbol of the market to fetch the order book for
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :param int [params.limit]: orderbook limit, default is None
        :param boolean [params.uta]: set to True for the unified trading account(uta), defaults to False
        :returns dict: A dictionary of `order book structures <https://docs.ccxt.com/?id=order-book-structure>` indexed by market symbols
        Nbooksr   r  r     2   	orderbookr-   )r   r   omitstrr   )rk   r|   ry   r   r   s        rm   un_watch_order_bookzbitget.un_watch_order_book  s       !!!!!!!!!!!&'22QJJEQJJERKKUb[[YYvw//Fs5zz!G**67KIY[abbbbbbbbbrn   r   messageHashTopicrw   c                   K   |                                   d {V  |                     |          }d|z   dz   |d         z   }d }d }	|                     ||dd          \  }	}|                     |||	|          \  }}d|i}
|	rY||
d<   |d         |
d<   |                     |d	d
          |
d	<   |                     |ddi          }|                     |d	          }n||
d<   |d         |
d<   |                     |	||
|           d {V S )Nzunsubscribe:r   r|   rR   Frs   r   r   r   r=   Tr   r   )r   rx   ru   r{   r   r   r  un_watch_public)rk   r|   r   r   rw   ry   rx   r   rs   rR   r   s              rm   r   zbitget.un_watch_channel  sP     !!!!!!!!!V$$$'77#=x@PP33FJuUUV--j&#vNN&
  	*#DM#D\DN#//
DIID[[%77FYYvz22FF%DO#D\DN))#{D&IIIIIIIIIrn   c                   K   |                                   d{V  |                     |          }d}d}|dk    s|dk    s|dk    s|dk    r|t          |          z  }d}g }g }d}|                     |d	d
d          \  }}t	          dt          |                    D ]}	||	         }
|                     |
          }d}|                     d	|||          \  }}d|i}|rdnd}|rdnd}|||<   |d         ||<   |                    |           |                    d|
z              |rd|d
<   | 	                    ||||           d{V }|r|
                                S |S )a-  
        watches information on open orders with bid(buy) and ask(sell) prices, volumes and other data

        https://www.bitget.com/api-doc/spot/websocket/public/Depth-Channel
        https://www.bitget.com/api-doc/contract/websocket/public/Order-Book-Channel
        https://www.bitget.com/api-doc/uta/websocket/public/Order-Book-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 boolean [params.uta]: set to True for the unified trading account(uta), defaults to False
        :returns dict: A dictionary of `order book structures <https://docs.ccxt.com/?id=order-book-structure>` indexed by market symbols
        Nr  Tr  r  r  r  Fr.   rR   r   rs   r   r   r|   r   r   
orderbook:)r   r   r  ru   r   r   rx   r{   r   r   r   )rk   r   r   ry   r   incrementalFeedr   r   rR   r   r|   rx   rs   r   r   r   r  s                    rm   r  z#bitget.watch_order_book_for_symbols  s      !!!!!!!!!%%g..QJJEQJJERKKUb[[s5zz!G#O33F<VX]_deeVq#g,,'' 	8 	8AQZF[[((FH#112LfVY[abbHfHD ),:WWN),:XX(N#*D #)$<D MM$  !67777 	! F5M44S-QWXXXXXXXX	 	??$$$rn   c                 J   |                      |d          }|                     |dd          }|                     |d          }|dk    rdnd}|                     |dd          }|                     |d d |          }|d         }	d	|	z   }
|                      |d
          }|                      |d          }|                     |d          }|dk    }|rH|	| j        vr$|                     i           }|	|d<   || j        |	<   | j        |	         }|                     |ddg           }|                     |ddg           }|                     |d         |           |                     |d         |           ||d<   | 	                    |          |d<   | 
                    ddd          }|                     |d          dk    }|sT|rQ|d         }|d         }t          |          }t          |          }g }t          dd          D ]}||k     rN|                    ||         d         d                    |                    ||         d         d                    ||k     rN|                    ||         d         d                    |                    ||         d         d                    d                    |          }|                     |d          }|                     |d          }||k    r|                     | j        ||	|
           d S ne|                     i           }d}d} d|v rd|vrd} d|v rd|vrd}|                     ||	|||           }!|                    |!           || j        |	<   |                    | j        |	         |
           d S )Nr   r   r   rs   r   r   r   r|   r$  r   r   r   r  asksabidsbr   r   r-   checksumTactionsnapshot   r  r  r   )r   r   r   r   r   
orderbookscounted_order_booksafe_list_2handle_deltasr   handle_optionr   r   r   r   joincrc32spawnhandle_check_sum_error
order_bookparse_order_bookresetr   )"rk   r   r   r   r   rs   r   r   rx   r|   r   r   rawOrderBookr   incrementalBookobstoredOrderBookr'  r)  r+  
isSnapshot
storedAsks
storedBids
asksLength
bidsLengthpayloadArrayr   payloadcalculatedChecksumresponseChecksumr  bidsKeyasksKeyparsedOrderbooks"                                     rm   handle_order_bookzbitget.handle_order_book  s   ^ oogu--$$S)W==))#z:: (F 2 2VV
%%c8X>>!!(D$
CC!"V+w//tQ//%%lD99	!W, 4	0do--,,R00%8*,'"of5O##L&#rBBD##L&#rBBDv6===v6===+4OK(*.,,y*A*AOJ'))*:JMMH))'8<<
JJ ( ,V4
,V4
 __
 __
!q" A AA:~~$++JqM!,<Q,?@@@$++JqM!,<Q,?@@@:~~$++JqM!,<Q,?@@@$++JqM!,<Q,?@@@((<00%)ZZ%>%>"#'#4#4\:#N#N %)999
 JJt:FFKXXXF++IGGl"",..!Gl"",..!G"33L&)U\^effOOOO,,,&/DOF#tv.<<<<<rn   r   c                    K   |                      |           d {V  t          | j        dz   |                     |          z             }|                    ||           d S )N )r  r   r   orderbook_checksum_messagereject)rk   r   r|   r   errors        rm   r7  zbitget.handle_check_sum_error  sl      &&v.........dgmd.M.Mf.U.UUVVe[)))))rn   c                     |                      |dd          }|                    |           |                    |           d S )Nr   r  )parse_bid_askr   
storeArray)rk   booksidedeltabidAsks       rm   handle_deltazbitget.handle_delta  sG    ##E1a00 	eF#####rn   c                 ~    t          dt          |                    D ]}|                     |||                    d S )Nr   )r   r   rW  )rk   rT  deltasr   s       rm   r2  zbitget.handle_deltas  sH    q#f++&& 	3 	3Ahq	2222	3 	3rn   c                 D   K   |                      |g|||           d{V S )a4  
        get the list of most recent trades for a particular symbol

        https://www.bitget.com/api-doc/spot/websocket/public/Trades-Channel
        https://www.bitget.com/api-doc/contract/websocket/public/New-Trades-Channel
        https://www.bitget.com/api-doc/uta/websocket/public/New-Trades-Channel

        :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
        :param boolean [params.uta]: set to True for the unified trading account(uta), defaults to False
        :returns dict[]: a list of `trade structures <https://docs.ccxt.com/?id=public-trades>`
        N)watch_trades_for_symbols)rk   r|   r   r   ry   s        rm   watch_tradeszbitget.watch_trades  s6       22F8UE6RRRRRRRRRrn   c                   K   t          |          }|dk    rt          | j        dz             |                                  d{V  |                     |          }d}|                     |ddd          \  }}g }g }t          dt          |                    D ]}	||	         }
|                     |
          }d}|                     d|||          \  }}d|i}|rdnd	}|rd
nd}|rdnd||<   |d         ||<   |	                    |           |	                    d|
z              |r| 
                    |ddi          }|                     ||||           d{V }| j        rB|                     |d          }|                     |d
          }|                    ||          }|                     |||dd          }|                     ddd          r-|                     |          }|                     |d          }|S |S )a7  
        get the list of most recent trades for a particular symbol

        https://www.bitget.com/api-doc/spot/websocket/public/Trades-Channel
        https://www.bitget.com/api-doc/contract/websocket/public/New-Trades-Channel
        https://www.bitget.com/api-doc/uta/websocket/public/New-Trades-Channel

        :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
        :param boolean [params.uta]: set to True for the unified trading account(uta), defaults to False
        :returns dict[]: a list of `trade structures <https://docs.ccxt.com/?id=public-trades>`
        r   z> watchTradesForSymbols() requires a non-empty array of symbolsNr4   rR   Frs   r   r   r|   r   publicTradetrader   trade:Tr   r3   rN   )r   r   r   r   r   ru   r   rx   r{   r   r   r   r   r   r   r   r   r3  !remove_repeated_trades_from_arraysort_by)rk   r   r   r   ry   symbolsLengthrR   r   r   r   r|   rx   rs   r   r   r   tradesfirsttradeSymbolr   filtereds                        rm   r[  zbitget.watch_trades_for_symbols  sg      GA#DG.n$nooo!!!!!!!!!%%g..33F<SUZ\abbVq#g,,'' 	4 	4AQZF[[((FH#112I6SVX^__HfHD ),:WWN),:XX(N47#D==WD #)$<D MM$  F!23333 	8[[%77F11#}ffUUUUUUUU? 	8OOFA..E**5(;;KOOK77E++FE5+tTTm-?FF 	==fEEH||Hk::HOrn   c                    K   |                      |ddd          }|d         }|rdnd}|                     ||dd|           d{V S )aj  
        unsubscribe from the trades channel

        https://www.bitget.com/api-doc/spot/websocket/public/Trades-Channel
        https://www.bitget.com/api-doc/contract/websocket/public/New-Trades-Channel
        https://www.bitget.com/api-doc/uta/websocket/public/New-Trades-Channel

        :param str symbol: unified symbol of the market to unwatch the trades for
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :param boolean [params.uta]: set to True for the unified trading account(uta), defaults to False
        :returns any: status of the unwatch request
        r3   rR   Fr   r^  r_  N)ru   r   )rk   r|   ry   r   rR   channelTopics         rm   un_watch_tradeszbitget.un_watch_trades  sg       ..v}eUSS1I(+8}}**6<-Y_`````````rn   c                    |                      |di           }|                     |d          }|dk    rdnd}|                     |dd          }|                     |d d |          }|d         }|                      | j        |          }	|	5|                     | j        dd          }
t          |
          }	|	| j        |<   |                     |d	g           }t          |          }t          d
|          D ]=}||z
  dz
  }||         }|                     ||          }|	                    |           >d|z   }|                    |	|           d S )Nr   rs   r   r   r   r|   rO   r<   r   r   r  r`  )r   r   r   r   rd  r   re   r   	safe_listr   r   parse_ws_trader   r   )rk   r   r   r   rs   r   r   rx   r|   r  r   r   lengthr   indexrawTrader  r   s                     rm   handle_tradeszbitget.handle_trades  se   F oogub11))#z:: (F 2 2VV
%%c8X>>!!(D$
CC!f55>%%dlM4HHE&&F"(DK~~gvr22Tq&!! 	" 	"AQJNEE{H((6::FMM&!!!!'v{+++++rn   c                    |                      |dd          }|                     |d          }|                     |d          }d }||dk    rdnd}n|dnd}||                     |d d |          }|                     |g d          }|                     |d	g           }|                     |d
          }	d }
|	W|                     |	d          }|                     |          }t          j        |                      |	dd                    |d}
| 	                    || 
                    |g d          |                      |dd          ||                     |          |d         |                     |d          |                      |dd          |                     |d          | 
                    |g d          | 
                    |g d          | 
                    |g d          |
d|          S )Nr|   r   posModecategoryrr   r   r   )uTimecTimer   TexecTime	feeDetailr   feeCointotalFeefeecostcurrency)tradeIdr   execIdorderIdL	orderTypesideS
tradeScope)priceAvgprice	execPriceP)sizer   execQtyv)amountr   	execValue)r   r   orderr   r   r|   typer  takerOrMakerr  r  r~  r|  )r   r   r   safe_integer_nrl  r   safe_currency_coder   
string_abs
safe_tradesafe_string_nr   )rk   r_  rx   r   rs  rt  defaultTyper   ry  re  r|  feeCurrencyIdfeeCurrencyCodes                rm   rm  zbitget.parse_ws_trade.  s8   ~ ##E8X>>""5)44##E:66)1V););**&KK)0)<**6K>%%fdD+FFF''/X/X/XYY	NN5+r::	y!,, ,,UI>>M"55mDDO*4+=+=eZQV+W+WXX+ C $$U,F,F,FGG''y#>>"Y//X&$$UK88&&ufc:: ,,ULAA''/V/V/VWW((0V0V0VWW&&u.T.T.TUU 
  
   	rn   c                 <  K   |                                   d{V  d}d}d}d}d}	|                     |ddd          \  }	}|                     |          }|                     |          s0|                     |          }|                     d||	|          \  }}|	rd}|d	z   |z   }d
|i}
|	rdnd}|	rdnd}||
|<   |	sd|
d<   n|                     |ddi          }|                     |	|||
|           d{V }| j        r|S | 	                    ||||d          S )a  
        watch all open positions

        https://www.bitget.com/api-doc/contract/websocket/private/Positions-Channel
        https://www.bitget.com/api-doc/uta/websocket/private/Positions-Channel

        :param str[]|None symbols: list of unified market symbols
        :param int [since]: the earliest time in ms to fetch positions for
        :param int [limit]: the maximum number of positions to retrieve
        :param dict params: extra parameters specific to the exchange API endpoint
        :param str [params.instType]: one of 'USDT-FUTURES', 'USDC-FUTURES', 'COIN-FUTURES', 'SUSDT-FUTURES', 'SUSDC-FUTURES' or 'SCOIN-FUTURES', default is 'USDT-FUTURES'
        :param boolean [params.uta]: set to True for the unified trading account(uta), defaults to False
        :returns dict[]: a list of `position structure <https://docs.ccxt.com/en/latest/manual.html#position-structure>`
        Nr   	positionsUSDT-FUTURESr5   rR   FUTA
:positionsrs   r   r   positiondefaultr   T)
r   ru   r   is_emptyget_market_from_symbolsr{   r   watch_privater   filter_by_symbols_since_limit)rk   r   r   r   ry   rx   r   subscriptionHashrs   rR   r   r   r   newPositionss                 rm   watch_positionszbitget.watch_positions  s      !!!!!!!!!&!33F<LeUZ[[V%%g..}}W%% 	Y11'::F#112BFCQWXXHf 	H-;
 %(6Y #4**&^ 	8&DNN[[%77F!//[BRTXZ`aaaaaaaa? 	 11,PUW[\\\rn   c                    |                      |di           }|                     |dd          }| j        i | _        |                     |d          }|| j        vs|dk    rt                      | j        |<   | j        |         }|                     |dg           }g }t          dt          |                    D ]y}	||	         }
|                     |
dd	          }|                     |d d d
          }| 	                    |
|          }|
                    |           |
                    |           z|                     ||dz             }t          dt          |                    D ]}	||	         }|                    d          }|d         }|                    d          }|                     |d	|d          }|                     |          s|                    ||           |                    ||dz              d S )Nr   rs   r   r,  r-  r   r   r   r|   r   z:positions::z::r  ,Fr  )r   r   r  r   rl  r   r   r   r   parse_ws_positionr   find_message_hashessplitr   r  r   )rk   r   r   r   rs   r,  cacherawPositionsr  r   rawPositionr   rx   r  r   r   partssymbolsStringr   r  s                       rm   handle_positionszbitget.handle_positions  s   Z nnWeR00##CR88>!DN!!'844DN***0D0D'?'A'ADN8$x(~~gvr::q#l++,, 	# 	#A&q/K))+xJJH%%hdJGGF--k6BBH)))LL""""00N9RSSq#m,,-- 	7 	7A'*K%%d++E!!HM#))#..G,,\8WeTTI==++ 7y+666|X%<=====rn   c                 v   |                      |dd          }|                     |d          }|                     |ddd          }|                      |dd          }|d	k    rd
nd}|                     |g d          }|                      |dd          }	t	          j        |	d          }
d }||d         }|                     i d|d|                     |d          d|                     ||d d          dd d|d|                     |dd          d|                     |dd          d|                     |dd          d| 	                    |
          d|                     |d d!          d|d"| 
                    |d"          d#|                      |d$d%          d&|d'|d(|                     |          d)d |                     |d*d+          | 
                    |d,          | 
                    |d-          d | 
                    |d.          | 
                    |d/          d0          S )1Nr   r|   
marginModecrossisolated)crossedr  rs  holdMode
hedge_modeTF)updatedTimeru  rv  createdTimeunrealizedPLR
profitRater   contractSizer   r   posIdr   notionalliquidationPriceliqPrice
entryPriceopenPriceAvgavgPriceunrealizedPnlunrealizedPLunrealisedPnlr   	contractstotalr  	markPricer  holdSideposSidehedgedr   r   maintenanceMarginkeepMarginRatemmr	available
marginSizeleverage
marginRate)maintenanceMarginPercentage
collateralinitialMargininitialMarginPercentager  marginRatio)r   r   get_supported_mappingr  r   r   safe_positionsafe_symbolr  parse_numbersafe_numberr   )rk   r  rx   r   marginModeIdr  hedgedIdr  r   percentageDecimalr   r  s               rm   r  zbitget.parse_ws_positionG  s   n %%h(CC'',??//"?
 ?
  
 %%h	:FF"l22''2b2b2bcc	 ..x,WW'(95AA
!.1L!! #
H#
$""8W55#
 d&&xzJJ#
 	#

 *#
  2 28=OQ[ \ \#
 $,,X~zRR#
 T//./ZZ#
 $++J77#
 ++HgvFF#
 L#
 ))(K@@#
 D&&xYGG#
 f#
 #
  Y//!#
"  ##
$ ,0+=+=hHXZ_+`+`**8[AA!--hEE'+((:>>++HlCC/#
 #
 #
   	rn   c                 z  K   |                                   d{V  d}d}d}|                     |          \  }}|rdnd}d}	|-|                     |          }|d         }|d         }|dz   |z   }d}
|                     |dd	d
          \  }
}|                     |d          }d}|                     d||          \  }}d}|                     d||d          \  }}|dk    s|dk    r|d}||dk    r|	|dz   |z   }n#|dk    r|dz   }n|dk    r|dz   }n|dk    r|dz   }d}|	|dk    rd}n|                     d||
|          \  }}|dk    r
||	dz   |z   }	|r|	dz   }	|dk    s|dk    r|nd}|rdnd}d}|                     d|          \  }}|d}|dz   |z   }|dk    rd}nd}|
rd}d}|	dz   |z   }	d|i}|
rd nd!}|||<   |
s||d"<   n| 	                    |d	d#i          }| 
                    |
||	||           d{V }| j        r|                    ||          }|                     ||||d#          S )$a  
        watches information on multiple orders made by the user

        https://www.bitget.com/api-doc/spot/websocket/private/Order-Channel
        https://www.bitget.com/api-doc/spot/websocket/private/Plan-Order-Channel
        https://www.bitget.com/api-doc/contract/websocket/private/Order-Channel
        https://www.bitget.com/api-doc/contract/websocket/private/Plan-Order-Channel
        https://www.bitget.com/api-doc/margin/cross/websocket/private/Cross-Orders
        https://www.bitget.com/api-doc/margin/isolated/websocket/private/Isolate-Orders
        https://www.bitget.com/api-doc/uta/websocket/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
        :param boolean [params.trigger]: *contract only* set to True for watching trigger orders
        :param str [params.marginMode]: 'isolated' or 'cross' for watching spot margin orders]
        :param str [params.type]: 'spot', 'swap'
        :param str [params.subType]: 'linear', 'inverse'
        :param boolean [params.uta]: set to True for the unified trading account(uta), defaults to False
        :returns dict[]: a list of `order structures <https://docs.ccxt.com/?id=order-structure>`
        NtriggerOrderr  zorder:tradesr|   r   r   r/   rR   FproductTypelinearr   marginr  r  z:linearzCOIN-FUTURESz:inversezUSDC-FUTURESz:usdcfuturesrr   z:stoporders-algoordersMARGINr  orders-isolatedorders-crossedr  rs   r   r   r   T)r   is_trigger_orderrx   ru   r   handle_market_type_and_paramshandle_sub_type_and_paramsr{   handle_margin_mode_and_paramsr   r  r   r   filter_by_symbol_since_limit)rk   r|   r   r   ry   rx   r   	isTriggerr   r  rR   r  r  subTypers   r   r   r  r   r   r  s                        rm   watch_orderszbitget.watch_orders  sZ     . !!!!!!!!!	 11&99	6)2@nn)[[((FH%Fd|H%+f4K33FM5RWXXV&&v}==99-QWXXf99-QWYabbFNNdh..V^ Hdfnn6>%+g5KKN**%	1KKN**%
2KKN**%6K>dfnnHH#11-fUUHf6>>v1/#5> 	:/'9"fnn0@0@y#,:--(
!??vVV
F!H%+j8KZ''+* 	HG+c1H<
 %(6Y&^ 	8#DNN[[%77F))#{<LdTZ[[[[[[[[? 	3OOFE22E00tTTTrn   c                 N   |                      |di           }|                     |dd          }|                     |d          }|                     |d          }d }|dk    rd}n|dk    rd}nd}|                     |d	g           }|                      |d
i           }	|                     |	d|          }
|
dk    }|
dk    }|
dk    }| j        D|                     | j        dd          }t          |          | _        t          |          | _	        |dk    p|dk    }|r| j	        n| j        }|rdnd}i }t          d
t          |                    D ]r}||         }|                     |dd|          }|                     |d d |          }|                     ||          }|                    |           |d         }d||<   st          |                                          }t          d
t          |                    D ]?}||         }|dz   |z   }|dk    r|dz   }n|dk    r|dz   }|                    ||           @|                    ||           |r|                    |d           |r|                    |d           |r|                    |d           d S d S )Nr   r   r   rs   r   r   r  r   r   r   rt  zusdt-futureszcoin-futureszusdc-futuresordersLimitr<   r  
ordersAlgor  r  r|   Tr   r  z:crossr  z	:isolatedzorder:linearzorder:inversezorder:usdcfutures)r   r   r   r   rl  r  r   re   r   triggerOrdersr   r   r   parse_ws_orderr   listkeysr   )rk   r   r   r   r   rs   	argInstIdr   r   re  rt  isLinearSwapisInverseSwapisUSDCFuturesr   r  r  r   marketSymbolsr   r  r   rx   r  r|   r  innerMessageHashs                              rm   handle_orderzbitget.handle_order   s   d nnWeR00$$S)W==))#z::$$S(33	
vJJ!!JJ#J~~gvr22tQ++))%XFF N2!^3!^3;%%dlM4HHE077DK!7!>!>D-K7l3J	'0A##dk(1>nnw q#d))$$ 	) 	)AGE))%8YOOH%%hdJGGF((77FMM&!!!H%F$(M&!!M&&(())q#d))$$ 	5 	5A!WF*S069***#3h#>  ---#3k#A NN6#34444v{+++ 	3NN6>222 	4NN6?333 	8NN6#677777	8 	8rn   c           	         d|v}d|v }|                      |d          }|dk    rd}|dk    rd}|                     |dd          }|                     ||          }|                     |d	d
          }|d         }|                     |dd          }	|                     |dg           }
|                     |
d          }|                     |d          }d }|S|                     |d          }|                     t          j        |                    | 	                    |          d}| 
                    |d          }|d u}d }|s| 
                    |d          }n|r|r| 
                    |d          }|                     |                     |g d                    }|                     |d          }|                     |d          }|                     |                     |dd                    }|                     |                     |dd                    }|dk    }|dk    }d }d }d }d }|                     |dd          }|r|rF|                     |dd          }|                     |d d          }|                     |d!d          }n|                     |d           }||}n|}|r
|r|}|}n`|}n]|                     |dd          }nE|                     |d d          }|                     |d"d          }|                     |d#d          }t          j        ||          }|                     i d$|d|d%|                     |d&          d'|                     |d(          d)|d*|                     |          d+|                     |d,d-          d.|d/|                     |d0d/          d1d d|d|d|d2|d3|d4|d5|||                     |	          |d d6|          S )7Nrs  loanTypert  r   Tr  r   r|   rv  r  statusorderStatusry  r   r|  rz  r}  triggerPricer  executePrice)r  	fillPricer  r  r  accBaseVolume
cumExecQtynewSizecumExecValuerx   buybaseSizeqtyr   fillTotalAmountr  fillNotionalUsdr   r   r  clientOrderId	clientOidr   r   lastTradeTimestampru  r  r  timeInForceforcepostOnlyr  r~  r   filled)	remainingr  r|  rd  )r   r   r   r  r   r   r  r   r  r  r  	omit_zerosafe_string_lower_n
string_sub
safe_orderr   safe_string_upper_2parse_ws_order_status)rk   r  rx   isSpotisMarginrt  r   r   r|   	rawStatusorderFeer|  	feeAmount	feeObjectfeeCurrencyr	  isTriggerOrderr  r  r  r  r  newSizeValueisMarketOrderisBuytotalAmountfilledAmountr~  r  totalFilledpartialFillAmounts                                  rm   r  zbitget.parse_ws_order  s   v  5(%'))%<<vFxH%%eXx@@!!(F33''wFF	!&&uhFF	??5+r::ooh**$$S%00		 **3	::K))'*<Y*G*GHH 33K@@ I ''~>>&d2 	<$$UG44EE 	< 	<$$UN;;E>>$":":5BgBgBg"h"hiiv..{33t'9'9%R^'_'_``~~d&8&8	>&Z&Z[[)	((NN 	P N"00
EJJ"00lSS))%1BNSS$($4$4UL$I$I!$0#4LL#.L  N 3&3+&2 #'"4"4UIu"M"MKK  --e\<PPL,,UFEBBK%%e->OOD&{K@@	  
E 
f 
 $""5)44 
 T--e[AA	 

  
 Y// 
 !$"5"5eWm"T"T 
 D 
 433E7MRR 
  
 D 
 U 
 L 
 k 
 D 
  x! 
" l# 
$ #00;;+ 
  
  
, -  	rn   c                 B    ddddddd}|                      |||          S )Nr   closedcanceled)newlivepartially_filledr  	cancellednot_trigger)r   )rk   r  statusess      rm   r"  zbitget.parse_ws_order_status|  s:     &#!
 
 &&999rn   c                 l  K   |                                   d{V  d}d}|%|                     |          }|d         }|dz   |z   }d}|                     d||          \  }}d}d}	|                     |ddd          \  }	}|	|dk    rd	}n|                     d||	|          \  }}|	rd
}d|z   }
d|i}|	rdnd}d||<   |	sd|d<   n|                     |ddi          }|                     |	||
||           d{V }| j        r|                    ||          }| 	                    ||||d          S )a  
        watches trades made by the user

        https://www.bitget.com/api-doc/contract/websocket/private/Fill-Channel
        https://www.bitget.com/api-doc/uta/websocket/private/Fill-Channel

        :param str symbol: unified market symbol
        :param int [since]: the earliest time in ms to fetch trades for
        :param int [limit]: the maximum number of trades structures to retrieve
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :param boolean [params.uta]: set to True for the unified trading account(uta), defaults to False
        :returns dict[]: a list of `trade structures <https://docs.ccxt.com/?id=trade-structure>`
        NmyTradesr|   r   r*   rR   Fr   rr   r  zfill:rs   r   r   fillr  r   T)
r   rx   r  ru   r{   r   r  r   r   r  )rk   r|   r   r   ry   rx   r   r  rs   rR   r  r   r   rd  s                 rm   watch_my_tradeszbitget.watch_my_trades  s      !!!!!!!!! [[((FH%F%+f4K99/6SYZZf33FOUTYZZV>dfnnHH#11/63PVWWHf 	H"X-
 %(6Y%^ 	8&DNN[[%77F))#{<LdTZ[[[[[[[[? 	3OOFE22E00tTTTrn   c                    | j         0|                     | j        dd          }t          |          | _         | j         }|                     |dg           }t          |          }d}t          d|          D ]W}||         }	|                     |	          }
|                    |
           |
d         }d|z   }|	                    ||           X|	                    ||           d S )NrO   r<   r   r<  r   r|   z	myTrades:)
r<  r   re   r   rl  r   r   rm  r   r   )rk   r   r   r   r  r   rn  r   r   r_  r  r|   symbolSpecificMessageHashs                rm   handle_my_tradeszbitget.handle_my_trades  s    \ = %%dlM4HHE&u--DM~~gvr22T q&!! 	> 	>AGE((//FMM&!!!H%F(3f(<%NN6#<====v{+++++rn   c                   K   d}|                      |ddd          \  }}d}|                     dd|          \  }}d}|                     d|          \  }}d}d}|dk    s|dk    rd}n|d	}|s|d
k    rd}nd}n|sd}|                      |dd|          \  }}|rd}d|i}|rdnd}|||<   |sd|d<   n|                     |ddi          }d|                                z   }	|                     ||	|	||           d{V S )a  
        watch balance and get the amount of funds available for trading or funds locked in orders

        https://www.bitget.com/api-doc/spot/websocket/private/Account-Channel
        https://www.bitget.com/api-doc/contract/websocket/private/Account-Channel
        https://www.bitget.com/api-doc/margin/cross/websocket/private/Margin-Cross-Account-Assets
        https://www.bitget.com/api-doc/margin/isolated/websocket/private/Margin-isolated-account-assets
        https://www.bitget.com/api-doc/uta/websocket/private/Account-Channel

        :param dict [params]: extra parameters specific to the exchange API endpoint
        :param str [params.type]: spot or contract if not provided self.options['defaultType'] is used
        :param str [params.instType]: one of 'SPOT', 'MARGIN', 'USDT-FUTURES', 'USDC-FUTURES', 'COIN-FUTURES', 'SUSDT-FUTURES', 'SUSDC-FUTURES' or 'SCOIN-FUTURES'
        :param str [params.marginMode]: 'isolated' or 'cross' for watching spot margin balances
        :param boolean [params.uta]: set to True for the unified trading account(uta), defaults to False
        :returns dict: a `balance structure <https://docs.ccxt.com/?id=balance-structure>`
        Nr)   rR   Faccountrp   rq   r  r  r  account-isolatedaccount-crossedrr   rs   r  r   r   r  coinTbalance:)ru   r  r  r   rv   r  )
rk   ry   rR   r  r  rs   r   r   r   r   s
             rm   watch_balancezbitget.watch_balance4  s     " 33FNESXYYV99.$PVWWf
!??PVWW
FFNN 0 0%HH#H 0++0GG/G 	H88Q[]eff& 	H
 %(6Y&^ 	8$DLL[[%77F 8>>#3#33''[+tVTTTTTTTTTrn   c                    |                      |di           }|                     |d          }|                     |dg           }t          dt	          |                    D ]>}||         }|dk    r3|                     |dg           }t          dt	          |                    D ]}	||	         }
|                     |
d          }|                     |          }|| j        v r| j        |         n| 	                                }|                     |
d          }|                     |
d          }||t          j        ||          |d	<   |                     |
d
          |d<   |                     |
d          |d<   |                     |
d          |d<   || j        |<   E|                     |dd          }|                     |          }|| j        v r| j        |         n| 	                                }|                     |d          }|.|                     |d          }t          j        ||          |d	<   d|v rdnd
}|                     ||          |d<   |                     |d          |d<   |                     |d          |d<   || j        |<   @|                     | j                  | _        d|z   }|                    | j        |           d S )Nr   rs   r   r   rR   rF  borrowdebtsdebtr  freelockedusedbalancer  
marginCoininterestmaxTransferOutequityfrozenrG  )r   r   r   r   r   rl  r   r  rP  rC  r   
string_addr   safe_balancer   )rk   r   r   r   rs   r   r   
rawBalancecoinsjentry
currencyIdcoderC  rJ  rK  rR  	freeQueryr   s                      rm   handle_balancezbitget.handle_balanceg  s   r nnWeR00))#z::w33q#d))$$ 	- 	-AaJ5  z62>>q#e**-- 1 1A!!HE!%!1!1%!@!@J22:>>D59T\5I5Idl400PTP\P\P^P^G!--eX>>F ,,UG<<E*0A*1*<VU*K*K&*&6&6uk&J&JGFO&*&6&6uh&G&GGFO'+'7'7y'I'IGG$)0DL&&1 "//
FLQQ
..z::151E1E$,t,,DLLNN))*h??%#//
JGGH&-&8&J&JGFO1AZ1O1O,,Va	"&"2"2:y"I"I#'#3#3J#I#I "&"2"2:x"H"H%,T""((66 8+t|[11111rn   c                   K   |r| j         d         d         d         n| j         d         d         d         }|                     | j        ddd          }|r]|                     |d          }|d	k    rA|d
k    r;|dk    r5|r| j         d         d         d         }n| j         d         d         d         }d|gd}|                     ||          }	|                     |||	|           d {V S )Nr6   r!   r9   r7   sandboxModesandboxFrs   SCOIN-FUTURESSUSDT-FUTURESSUSDC-FUTURESr;   	subscribeopr   rd   safe_bool_2re   r   r   watch
rk   rR   r   r   ry   urlra  rs   requestr   s
             rm   r   zbitget.watch_public  s     58^dit$[11di>Nt>TU]>^&&t|]IuUU 	=''j99HO++(o2M2MT\`oToTo =)E*62;?CC)E*628<CF
 
 ++gv..ZZ[';GGGGGGGGGrn   c                   K   |r| j         d         d         d         n| j         d         d         d         }|                     | j        ddd          }|r]|                     |d          }|d	k    rA|d
k    r;|dk    r5|r| j         d         d         d         }n| j         d         d         d         }d|gd}|                     ||          }	|                     |||	|           d {V S )Nr6   r!   r9   r7   ra  rb  Frs   rc  rd  re  r;   unsubscriberg  ri  rl  s
             rm   r"  zbitget.un_watch_public  s     58^dit$[11di>Nt>TU]>^&&t|]IuUU 	=''j99HO++(o2M2MT\`oToTo =)E*62;?CC)E*628<CF
 
 ++gv..ZZ[';GGGGGGGGGrn   c                   K   |r| j         d         d         d         n| j         d         d         d         }|                     | j        ddd          }|rs|                     |di           }|                     |d	          }|d
k    r@|dk    r:|dk    r4|r| j         d         d         d         n| j         d         d         d         }d|d}	|                     |	|          }
|                     |||
|           d {V S )Nr6   r!   r9   r7   ra  rb  Fr   rs   rc  rd  re  r;   rf  rg  )rd   rj  re   r   r   r   watch_multiple)rk   rR   r   	argsArrayry   rm  ra  argsArrayFirstrs   rn  r   s              rm   r   zbitget.watch_public_multiple	  s.     58^dit$[11di>Nt>TU]>^&&t|]IuUU 	k!^^Iq"==N''
CCHO++(o2M2MT\`oToTo?Bjdi&v.{;;	RWHXY_H`aiHj
 
 ++gv..((mWmTTTTTTTTTrn   c                 r  K   |                                   |                     |d          }|                     |          }d}|                    |          }|                     |j        |          }|t          |                                           }|dz   dz   }|                     | 	                    |          | 	                    | j
                  t          j        d          }	d}
|
| j        | j        ||	dgd}|                     ||          }|                     ||||           | d {V S )	Nrm  authenticatedGETz/user/verifybase64login)apiKey
passphraser   signrg  )check_required_credentialsr   r   reusableFuturer   subscriptionsr  secondshmacencodesecrethashlibsha256rz  passwordr   rk  )rk   ry   rm  r   r   rq   rv  r   auth	signature	operationrn  r   s                rm   authenticatezbitget.authenticate	  s>     '')))vu--S!!%&&{33(<kJJ DLLNN++Iu$~5D		$++d"3"3T[[5M5Mw~_ghhII #'+&*m%. )	 
 
G kk'622GJJsK+>>>||||||rn   c                   K   |r| j         d         d         d         n| j         d         d         d         }|                     | j        ddd          }|r]|                     |d          }|d	k    rA|d
k    r;|dk    r5|r| j         d         d         d         }n| j         d         d         d         }|                     d|i           d {V  d|gd}	|                     |	|          }
|                     |||
|           d {V S )Nr6   r!   r:   r8   ra  rb  Frs   rc  rd  re  r;   rm  rf  rg  )rd   rj  re   r   r  r   rk  )rk   rR   r   r  r   ry   rm  ra  rs   rn  r   s              rm   r  zbitget.watch_private1	  sF     69`dit$\22ty?OPT?UV_?`&&t|]IuUU 	>''j99HO++(o2M2MT\`oToTo >)E*62<@CC)E*629=C---------F
 
 ++gv..ZZ[';KLLLLLLLLLrn   c                 j    d}|                      |j        |          }|                    d           d S )Nrv  T)r   futuresr   )rk   r   r   r   rq   s        rm   handle_authenticatezbitget.handle_authenticateC	  s6     &==trn   c                 p   |                      |d          }	 |dk    r|                      |d          }| j        dz   |                     |          z   }|                     | j        d         d         ||           |                      |dd          }|                     | j        d         d	         ||           t          |          d
S # t          $ r_}t          |t                    r*d}|
                    ||           ||j        v r|j        |= n|
                    |           Y d }~dS d }~ww xY w)NeventrP  r]  rM  r!   ra   msgr   rb   Frv  T)r   r   jsonthrow_exactly_matched_exceptionrg   throw_broadly_matched_exceptionr   	Exception
isinstancer   rO  r  )	rk   r   r   r  r]  feedbackr  er   s	            rm   handle_error_messagezbitget.handle_error_messageK	  sV      '22	''887S=499W+=+==44T_T5J75SUY[cddd&&wr::44T_T5J75SUXZbccc#H---5 		 		 		!011 !-a---&"666,[9 a   44444		s   B2C 
D5AD00D5c                 $   |                      ||          rd S |                     |d          }|dk    r|                     ||           d S |dk    r|                     ||           d S |                     |d          }|dk    r|                     ||           d S |dk    r|                     ||           d S |dk    r|                     ||           d S i d| j        d| j        d	| j        d
| j        d| j	        d| j	        d| j	        d| j	        d| j	        d| j	        d| j
        d| j        d| j        d| j
        d| j
        d| j        }|                     |di           }|                     |ddd          }|                     ||          }| |||           |                    d          dk    r|                     ||           |                    d          dk    r|                     ||           d S d S )Nr   pongr  ry  rf  rp  r   r_  r^  r=  r  r  r  r  r  r  rC  r  r  rD  rE  r   r   r   r   r   r   r   r  )r  r   handle_pongr  handle_subscription_statushandle_un_subscription_statusr   rq  rA  r  r_  r  r  r   safe_value_2findrK  )	rk   r   r   contentr  methodsr   r   methods	            rm   handle_messagezbitget.handle_messaged	  s   b $$VW55 	F""7I66fVW---FfVW---F  '22G$$VW555FK++FG<<<FM!!..vw???F
d(
T'
 4-
 D)	

 T&
 d'
 $+
 4,
 d/
 t0
 t*
 -
 .
  3
 t2
  T&!
$ oogub11!!#y'2>>%00F67###::h1$$fg...::g!##""6733333 $#rn   c                     dS )NrS    )rk   r   s     rm   rS   zbitget.ping	  s    vrn   c                 8    |                                  |_        |S N)millisecondslastPongrk   r   r   s      rm   r  zbitget.handle_pong	  s    ++--rn   c                     |S r  r  r  s      rm   r  z!bitget.handle_subscription_status	  s	     rn   c                    |                      |di           }|                     |d          }|dk    rdnd}|                     |d          }|                     |d d |          }|d         }d|d         z   }	d|z   }
|| j        v r| j        |= |
|j        v r|j        |
= |	|j        v r|j        |	= t          | j        d	z   |z             }|
|j        v r|	                    ||
           |
                    d
|	           d S )Nr   rs   r   r   r   r|   zunsubscribe:orderbook:r$  z orderbook T)r   r   r   r   r/  r  r   r   r  rO  r   rk   r   r   r   rs   r  r   rx   r|   r   subMessageHashrP  s               rm   !handle_order_book_un_subscriptionz(bitget.handle_order_book_un_subscription	  s/    nnWeR00))#z::"f,,vv:!!#x00!!&$d;;!.1AA%.T_$$'V111$^4&...$[1 =!86!ABBV^++MM%000t[)))))rn   c                    |                      |di           }|                     |d          }|dk    rdnd}|                     |dd          }|                     |d d |          }|d         }d|d         z   }	d|z   }
|| j        v r| j        |= |
|j        v r|j        |
= |	|j        v r|j        |	= t          | j        d	z   |z             }|
|j        v r|	                    ||
           |
                    d
|	           d S )Nr   rs   r   r   r   r|   zunsubscribe:trade:r`  z trades T)r   r   r   r   rd  r  r   r   r  rO  r   r  s               rm   handle_trades_un_subscriptionz$bitget.handle_trades_un_subscription
  s1    nnWeR00))#z::"f,,vv:##C8<<!!&$d;;!*VH-==!F*T[  F#V111$^4&...$[1 :!5!>??V^++MM%000t[)))))rn   c                    |                      |di           }|                     |d          }|dk    rdnd}|                     |dd          }|                     |d d |          }|d         }d|d         z   }	d|z   }
|| j        v r| j        |= |
|j        v r|j        |
= |	|j        v r|j        |	= t          | j        d	z   |z             }|
|j        v r|	                    ||
           |
                    d
|	           d S )Nr   rs   r   r   r   r|   zunsubscribe:ticker:r~   z ticker T)r   r   r   r   r   r  r   r   r  rO  r   r  s               rm   handle_ticker_un_subscriptionz$bitget.handle_ticker_un_subscription"
  s1    nnWeR00))#z::"f,,vv:##C8<<!!&$d;;!+fX.>>"V+T\!!V$V111$^4&...$[1 :!5!>??V^++MM%000t[)))))rn   c                    |                      |di           }|                     |d          }|dk    rdnd}|                     |dd          }|                     |dd          }|                     |d	          }d }	|d
}	|                    dd          }nd}	|                     | j        d          }
|                     ||
          }|                     |d d |          }|d         }d }d }|	rd|z   }d|z   }nd|z   dz   |z   }d|z   dz   |z   }|| j	        v r|| j	        |         v r| j	        |         |= | 
                    |||           d S )Nr   rs   r   r   r   r|   r   r   r   Fr   r   TrQ   zunsubscribe:kline:r   zunsubscribe:candles:r   r   )r   r   r   r   r  r   re   r  r   r   clean_unsubscription)rk   r   r   r   rs   r  r   r   r   r  rQ   r   rx   r|   r   r  s                   rm   handle_ohlcv_un_subscriptionz#bitget.handle_ohlcv_un_subscription9
  s    nnWeR00))#z::"f,,vv:##C8<<$$S)W==##C44Ex44HHE__T\<@@
''*==	!!&$d;;! 	C.7K%.NN09<sBVKK')3c9FBNT[  DK///K'	2!!&.+FFFFFrn   c                    |                      |d          }||                     |di           g}t          dt          |                    D ]*}||         }|                     |dd          }|                    d          dk    r|                     ||           R|                    d          dk    s|                    d          dk    r|                     ||           |                    d	          dk    r|                     ||           |                    d
          dk    r| 	                    ||           |                    d          dk    r| 	                    ||           ,|S )Nr   r   r   r   r   r  r_  r^  r   r   r   )
rl  r   r   r   r   r  r  r  r  r  )rk   r   r   argsListr   r   r   s          rm   r  z$bitget.handle_un_subscription_status^
  s   ( >>'622wr::;Hq#h--(( 	C 	CA1+C((iAAG||G$$))66vwGGGG,,w''1,,',,}2M2MQR2R2R2267CCCCh''1,,2267CCCCh''1,,11&'BBBBg&&!++11&'BBBrn   r  )K__name__
__module____qualname__r   rj   boolr{   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  rK  r7  rW  r2  r   r\  r[  rj  rq  rm  r   r  r  r  r   r   r  r  r  r"  r>  rA  r   rH  r_  r   r"  r   r  r  r  r	   r  r  rS   r  r  r  r  r  r  r  __classcell__)rl   s   @rm   r   r      s       V# V V V V V Vp =B" " "T " " " " 68 G G GF G G G G< 9; ^ ^C ^s ^ ^ ^ ^ 6:" 'E 'E7 'E 'E 'E 'E 'ER=,F =, =, =, =,~@ @ @ @D 8<B 'F 'FW 'F7 'F 'F 'F 'FR,V , , , ,   , ?CQUdhqs *H *H *H *H3 *H^a *Hx|  ~B  yC *H *H *H *HX BFb *_ *_3 *_3 *_UX *_ *_ *_ *_XY,6 Y, Y, Y, Y,v!
 !
D !
 !
 !
 !
F @DB P PS P PS\ P P P P  =? c c c3 c c c c, pr J JS J3 JRU Jcf Jvy J J J J, SW_a - -$s) -C -fo - - - -^p= p= p= p= p=d*6 *3 *UX * * * *
$ $ $3 3 3 <@d[] S S SS S Sbfglbm S S S S" OSaenp 2 2d3i 2 2[^ 2uyz  vA 2 2 2 2h 9; a aC as a a a a$7,F 7, 7, 7, 7,rB B B BH 8<$]ajl *] *]W *]C *]WZ *]quv~q *] *] *] *]Xg>v g> g> g> g>R] ] ] ]~ 04$UYbd XU XU XUC XUs XUimnsit XU XU XU XUtB86 B8 B8 B8 B8Hv v v vp	: 	: 	: 37TX\eg -U -UC -Us -URU -Ulpqvlw -U -U -U -U^|,v |, |, |, |,| *, 1U 1U 1U 1U 1U 1Uf|2V |2 |2 |2 |2| AC H H H H" DF H H H H" QS U U U U )+    6 TV M M M M$&    6 t    2~4V ~4 ~4 ~4 ~4@6    &        * * * * *.*F * * * *.*F * * * *.#G6 #G #G #G #GJ%F % % % % % % % %rn   r   )%ccxt.async_supportccxt ccxt.async_support.base.ws.cacher   r   r   r   r  ccxt.base.typesr   r   r	   r
   r   r   r   r   r   r   r   r   !ccxt.async_support.base.ws.clientr   typingr   ccxt.base.errorsr   r   r   r   r   r   r   ccxt.base.preciser   async_supportr   r  rn   rm   <module>r     s       A  A  A  A  A  A  A  A  A  A  A  A  v v v v v v v v v v v v v v v v v v v v v v v v v v v v 4 4 4 4 4 4       * * * * * * 0 0 0 0 0 0 . . . . . . ' ' ' ' ' ' . . . . . . * * * * * * - - - - - - % % % % % %m) m) m) m) m)T& m) m) m) m) m)rn   