
    ei                         d dl Zd dlmZmZmZ d dlZd dlmZm	Z	m
Z
mZmZmZmZmZmZmZmZ 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)ArrayCacheBySymbolByIdArrayCacheBySymbolBySideArrayCacheByTimestamp)AnyBalancesIntMarketOrder	OrderBookPositionStrStringsTickerTickers)Client)List)ExchangeError)ArgumentsRequired)Precisec            
       (    e Zd Zdef fdZdefdZd Zi i fdZi 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i fdedefdZd Zdef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
defdZddi fdeee
                  dedefdZi fdeee
                  defdZd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di fdee
         dedefdZi fdee
         defdZ defdZ!d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%defd"Z&d5d#e'd$e(de#fd%Z)dddi fdedededee*         fd&Z+d' Z,d5d(Z-d)e"de"fd*Z.i fde/fd+Z0defd,Z1d- Z2defd.Z3defd/Z4ded0e'fd1Z5defd2Z6defd3Z7defd4Z8 xZ9S )6bydfi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iidddddddddddd d!d"d#d$d%d&d'd(d)d*| j        d+d,d-          S ).NwsTwatchBalancewatchBidsAsksFwatchMyTrades
watchOHLCVwatchOHLCVForSymbolswatchOrderBookwatchOrderBookForSymbolswatchOrderswatchOrdersForSymbolswatchPositionswatchTickerwatchTickerswatchTradeswatchTradesForSymbolsunwatchBidsAsksunwatchOHLCV)
unwatchOHLCVForSymbolsunwatchOrderBookunwatchOrderBookForSymbolsunwatchTickerunwatchTickersunWatchTradesunWatchTradesForSymbolsunWatchOrdersunWatchOrdersForSymbolsunWatchPositionsapiz%wss://stream.bydfi.com/v1/public/fapi1001000ms)depth	frequency)fetchBalanceSnapshotawaitBalanceSnapshot1m3m5m15m30m1h2h4h6h8h12h1d1w1M)r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   )r!   r   
timeframesi )ping	keepAlive)hasurlsoptions	streaming)deep_extendsuperr   describerK   )self	__class__s    L/var/www/html/volatility/venv/lib/python3.11/site-packages/ccxt/pro/bydfi.pyrS   zbydfi.describe   s   eT 2 2 ; ; = =d    	
 d ' !$ +D t ( !$ t  u (  "5!" #$ +/$(.2!%"&!&+0!&+0$)7  < A #!)- -
 -2,0! !
      6 	# {A@
 A@
 A A A	    clientc                 0    |                                  ddS )NrK   )idmethod)
request_id)rT   rX   s     rV   rK   z
bydfi.pingW   s!    //##
 
 	
rW   c                     |                                   |                     |                     | j        dd          d          }|| j        d<   |                                  |S )Nreqidr      )lock_idsumsafe_integerrO   	unlock_id)rT   r^   s     rV   r\   zbydfi.request_id]   sW    **4<!DDaHH %WrW   c           
      p  K   | j         d         d         }|                                 }d|i}|                     |dd          }d}	|r"d}	|                     |d          }d|d<   ||d	<   ||	|d
}
|                     |||                     |
|          ||                     ||                     d {V S )Nr5   r   rZ   unsubscribeF	SUBSCRIBEUNSUBSCRIBETmessageHashesrZ   r[   params)rN   r\   	safe_boolomitwatch_multiplerQ   extend)rT   rh   channelsrj   subscriptionurlrZ   subscriptionParamsre   r[   messages              rV   watch_publiczbydfi.watch_publicd   s     it$__"$
 nnV]EBB 	@"FYYv}55F04}-2?/
 

 ((mT=M=MgW]=^=^`mosozoz  |N  P\  p]  p]  ^  ^  ^  ^  ^  ^  ^  ^  	^rW   c                 l  K   |                                   | j        d         d         }d}|                     |          }|                     |j        |          }i }||                                 }t          |                                           }	| j        |	z   }
| 	                    | 
                    |
          | 
                    | j                  t          j        d          }|d| j        |	|dd}|                     ||          }||d<   |                     |||dg|           d {V S )	Nr5   r   privatehexLOGIN)apiKey	timestampsignri   rZ   )check_required_credentialsrN   rX   
safe_valuesubscriptionsr\   strmillisecondsry   hmacencodesecrethashlibsha256rQ   rm   )rT   rh   rj   rq   subHashrX   privateSubscriptionrp   rZ   rz   payload	signaturerequests                rV   watch_privatezbydfi.watch_privatex   sD     '')))it$S!!"oof.BGLL&""BD--//00IkI-G		$++g"6"6DK8P8PRYR`bghhI!"k!*%  G %%gv66F!#L((mVi[R^_________rW   symbolc                    K   |                                   d{V  |                     |          }|d         }d|z   }|dz   }|                     |g|g|           d{V S )a  
        watches a price ticker, a statistical calculation with the information calculated over the past 24 hours for a specific market

        https://developers.bydfi.com/en/futures/websocket-market#ticker-by-symbol

        :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>`
        NrZ   ticker::@ticker)load_marketsmarketrt   )rT   r   rj   r   marketIdmessageHashchannels          rV   watch_tickerzbydfi.watch_ticker   s       !!!!!!!!!V$$$< 6)Y&&&}wiHHHHHHHHHrW   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://developers.bydfi.com/en/futures/websocket-market#ticker-by-symbol

        :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_tickersrT   r   rj   s      rV   un_watch_tickerzbydfi.un_watch_ticker   s2       **F8V<<<<<<<<<rW   Nsymbolsc                 $  K   |                                   d{V  |                     |dd          }g }d}g }d}|.|                    |dz              |                    d           nmt          dt	          |                    D ]O}||         }|                     |          }	|                    ||z              |                    |	|z              P|                     |||           d{V  |                     | j        d|          S )	aF  
        watches a price ticker, a statistical calculation with the information calculated over the past 24 hours for all markets of a specific list

        https://developers.bydfi.com/en/futures/websocket-market#ticker-by-symbol
        https://developers.bydfi.com/en/futures/websocket-market#market-wide-ticker

        :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>`
        NTr   r   allnot ticker@arrr   r   )	r   market_symbolsappendrangelen	market_idrt   filter_by_arraytickers)
rT   r   rj   rh   r   ro   r   ir   r   s
             rV   watch_tickerszbydfi.watch_tickers   s;      !!!!!!!!!%%gtT:: ?  u!4555OO,----1c'll++ 4 4 >>&11$$[6%9:::7 23333x@@@@@@@@@##DL(GDDDrW   c                 V  K   |                      |dd          }g }d}g }d}ddi}||                                 }d|d<   t          dt          |                    D ]y}	|                     ||	          }
|
_|
                    d	          }|                     |d
          }|dk    rL|                     |          }|                    ||z              z|                    |           |                    d           nrt          dt          |                    D ]O}	||	         }|                     |          }|                    ||z              |                    ||z              P||d<   |                     |ddi          }| 	                    ||||           d{V S )aH  
        unWatches a price ticker, a statistical calculation with the information calculated over the past 24 hours for all markets of a specific list

        https://developers.bydfi.com/en/futures/websocket-market#ticker-by-symbol
        https://developers.bydfi.com/en/futures/websocket-market#market-wide-ticker

        :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>`
        NTzunsubscribe::ticker::r   topictickersubHashIsPrefixr   ::r_   r   r   r   re   )
r   -get_message_hashes_for_tickers_unsubscriptionr   r   safe_stringsplitr   r   rn   rt   )rT   r   rj   rh   r   ro   r   rp   	subHashesr   r   partsr   r   s                 rV   r   zbydfi.un_watch_tickers   s      %%gtT::-X
 ? JJLLI.2L*+1c)nn-- 8 8**9a88&#MM$//E!--eQ77F #~~f55HOOHw$6777  ---OO,----1c'll++ 4 4 >>&11$$[6%9:::7 23333&-L#VmT%:;;&&}hUUUUUUUUUrW   c                 b   | j         d         d         d         }|                     |          }|j        }g }t          |                                          }t          dt          |                    D ]8}||         }|                    d          dk    r|                    |           9|S )Nr5   r   publicr   r   )	rN   rX   r~   listkeysr   r   findr   )rT   rq   rX   r~   rh   r   r   keys           rV   r   z3bydfi.get_message_hashes_for_tickers_unsubscription   s    it$X.S!!,M&&(())q#d))$$ 	* 	*Aq'Cxx
##q(($$S)))rW   c                     |                      |          }|d         }d|z   }|| j        |<   |                    | j        |         |           |                    | j        d           d S )Nr   r   zticker::all)parse_tickerr   resolve)rT   rX   rs   r   r   r   s         rV   handle_tickerzbydfi.handle_ticker  sn     ""7++! 6)%Vt|F+[999t|]33333rW   r<   sincelimitc                 d   K   |                      ||gg|||           d{V }||         |         S )a  
        watches historical candlestick data containing the open, high, low, close price, and the volume of a market

        https://developers.bydfi.com/en/futures/websocket-market#candlestick-data

        :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
        N)watch_ohlcv_for_symbols)rT   r   	timeframer   r   rj   results          rV   watch_ohlcvzbydfi.watch_ohlcv  sL       33fi5H4I5RWY_````````f~i((rW   r   c                 D   K   |                      ||gg|           d{V S )a  
        watches historical candlestick data containing the open, high, low, and close price, and the volume of a market

        https://developers.bydfi.com/en/futures/websocket-market#candlestick-data

        :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
        N)un_watch_ohlcv_for_symbols)rT   r   r   rj   s       rV   un_watch_ohlcvzbydfi.un_watch_ohlcv(  s8       44vy6I5JFSSSSSSSSSrW   symbolsAndTimeframesc                 n  K   t          |          }|dk    st          |d         t                    st          | j        dz             |                                  d{V  g }g }t          dt          |                    D ]}||         }	|                     |	d          }
|                     |
          }|                     |	d          }| 	                    | j
        di           }|                     |||          }|                    |d         dz   |z              |                    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://developers.bydfi.com/en/futures/websocket-market#candlestick-data

        :param str[][] symbolsAndTimeframes: array of arrays containing unified symbols and timeframes to fetch OHLCV data for, example [['BTC/USDT', '1m'], ['LTC/USDT', '5m']]
        :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
        r   z_ watchOHLCVForSymbols() requires a an array of symbols and timeframes, like  ['ETH/USDC', '1m']Nr_   rJ   rZ   @kline_ohlcv::r   r   T)r   
isinstancer   r   rZ   r   r   r   r   	safe_dictrO   r   rt   
newUpdatesgetLimitfilter_by_since_limitcreate_ohlcv_object)rT   r   r   r   rj   symbolsLengthro   rh   r   symbolAndTimeframer   r   tfrJ   intervalr   r   candlesfiltereds                      rV   r   zbydfi.watch_ohlcv_for_symbols5  s      011AZ0DQ0G%N%N#DG  /P  %P  Q  Q  Q!!!!!!!!!q#23344 	Q 	QA!5a!8''(:A>>H[[**F!!"4a88BlBGGJ''
B;;HOOF4L94x?@@@  VH-=!=!Dx!OPPPP+/+<+<]HV\+]+]%]%]%]%]%]%]"	7? 	4$$VU33E--gueQMM''	8DDDrW   c                   K   t          |          }|dk    st          |d         t                    st          | j        dz             |                                  d{V  g }g }t          dt          |                    D ]}||         }|                     |d          }|                     |          }	|                     |d          }
|                     | j	        |
|
          }|
                    |	d         dz   |z              |
                    d|	d         z   d	z   |z              |                     |d
di          }d|d}|                     ||||           d{V S )a1  
        unWatches historical candlestick data containing the open, high, low, and close price, and the volume of a market

        https://developers.bydfi.com/en/futures/websocket-market#candlestick-data

        :param str[][] symbolsAndTimeframes: array of arrays containing unified symbols and timeframes to fetch OHLCV data for, example [['BTC/USDT', '1m'], ['LTC/USDT', '5m']]
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns int[][]: A list of candles ordered, open, high, low, close, volume
        r   za unWatchOHLCVForSymbols() requires a an array of symbols and timeframes, like  ['ETH/USDC', '1m']Nr_   rZ   r   zunsubscribe::ohlcv::r   r   re   Tohlcv)r   r   )r   r   r   r   rZ   r   r   r   r   rJ   r   rn   rt   )rT   r   rj   r   ro   rh   r   r   r   r   r   r   rp   s                rV   r   z bydfi.un_watch_ohlcv_for_symbolsV  s      011AZ0DQ0G%N%N#DG  /R  %R  S  S  S!!!!!!!!!q#23344 	^ 	^A!5a!8''(:A>>H[[**F!!"4a88B''R@@HOOF4L94x?@@@  !7&:J!JT!QT\!\]]]]VmT%:;;$8
 
 &&}hUUUUUUUUUrW   c                 x   |                      |d          }|                     |          }|d         }|                      |d          }|                     | j        di           }|                     ||          }|| j        vr
i | j        |<   || j        |         vr;|                     | j        dd          }	t          |	          }
|
| j        |         |<   | j        |         |         }|                     |          }|	                    |           d|z   dz   |z   }|
                    |||g|           d S )	Nsr   r   rJ   
OHLCVLimit  r   r   )r   safe_marketr   rO   find_timeframeohlcvsrb   r   parse_ws_ohlcvr   r   )rT   rX   rs   r   r   r   r   rJ   r   r   storedr   parsedr   s                 rV   handle_ohlcvzbydfi.handle_ohlcvu  sD    ##GS11!!(++!##GS11^^DL,CC
''*==	$+%%"$DKT[000%%dlL$GGE*511F-3DK	*F#I.$$W--V&(4/);	51;?????rW   c                 B   K   |                      |g||           d{V S )ag  
        watches information on open orders with bid(buy) and ask(sell) prices, volumes and other data

        https://developers.bydfi.com/en/futures/websocket-market#limited-depth-information

        :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(default and maxi is 100)
        :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
        N)watch_order_book_for_symbols)rT   r   r   rj   s       rV   watch_order_bookzbydfi.watch_order_book  s4       66xOOOOOOOOOrW   c                 @   K   |                      |g|           d{V S )a  
        unWatches information on open orders with bid(buy) and ask(sell) prices, volumes and other data

        https://developers.bydfi.com/en/futures/websocket-market#limited-depth-information

        :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
        N)un_watch_order_book_for_symbolsr   s      rV   un_watch_order_bookzbydfi.un_watch_order_book  s2       996(FKKKKKKKKKrW   c                 L  K   |                                   d{V  |                     |dd          }d}|                     |dd|          \  }}d}|                     |dd|          \  }}d}|dk    rd	}g }g }t          d
t	          |                    D ][}	||	         }
|                     |
          }|                    |d         dz   |z   |z              |                    d|
z              \|                     |||           d{V }|                                S )aI  
        watches information on open orders with bid(buy) and ask(sell) prices, volumes and other data

        https://developers.bydfi.com/en/futures/websocket-market#limited-depth-information

        :param str[] symbols: unified array of symbols
        :param int [limit]: the maximum amount of order book entries to return(default and max is 100)
        :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
        NFr6   r!   r8   100msr9    @100msr   rZ   @depthorderbook::)	r   r   handle_option_and_paramsr   r   r   r   rt   r   )rT   r   r   rj   r8   r9   channelSuffixro   rh   r   r   r   	orderbooks                rV   r   z"bydfi.watch_order_book_for_symbols  s`      !!!!!!!!!%%gtU;;55f>XZachiiv	 99&B\^iktuu	6$Mq#g,,'' 	9 	9AQZF[[((FOOF4L83e;mKLLL  !78888++M8VLLLLLLLL	   rW   c                 `  K   |                                   d{V  |                     |dd          }d}|                     |dd|          \  }}d}|                     |dd|          \  }}d}|dk    rd	}g }g }t          d
t	          |                    D ][}||         }	|                     |	          }
|                    |
d         dz   |z   |z              |                    d|	z              \d|d}|                     |ddi          }|                     ||||           d{V S )ay  
        unWatches information on open orders with bid(buy) and ask(sell) prices, volumes and other data

        https://developers.bydfi.com/en/futures/websocket-market#limited-depth-information

        :param str[] symbols: unified array of symbols
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :param str [params.method]: either '/market/level2' or '/spotMarket/level2Depth5' or '/spotMarket/level2Depth50' default is '/market/level2'
        :returns dict: A dictionary of `order book structures <https://docs.ccxt.com/?id=order-book-structure>` indexed by market symbols
        NFr6   r!   r8   r   r9   r   r   r   rZ   r   zunsubscribe::orderbook::r   )r   r   re   T)	r   r   r   r   r   r   r   rn   rt   )rT   r   rj   r8   r9   r   ro   rh   r   r   r   rp   s               rV   r   z%bydfi.un_watch_order_book_for_symbols  s      !!!!!!!!!%%gtU;;55f>XZachiiv	 99&B\^iktuu	6$Mq#g,,'' 	F 	FAQZF[[((FOOF4L83e;mKLLL  !;f!DEEEE 
 
 VmT%:;;&&}hUUUUUUUUUrW   c                    |                      |d          }|                     |          }|                     |d          }|| j        vr|                                 | j        |<   | j        |         }|                     |||dd          }|                    |           d|z   }|| j        |<   |                    ||           d S )Nr   Ebar   )r   safe_symbolrb   
orderbooks
order_bookparse_order_bookresetr   )	rT   rX   rs   r   r   rz   r   r   r   s	            rV   handle_order_bookzbydfi.handle_order_book  s     ##GS11!!(++%%gs33	$/))&*oo&7&7DOF#OF+	&&w	3LL#f,"+y+.....rW   c                 P   K   d}||g}|                      ||||           d{V S )aF  
        watches information on multiple orders made by the user

        https://developers.bydfi.com/en/futures/websocket-account#order-trade-update-push

        :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)watch_orders_for_symbols)rT   r   r   r   rj   r   s         rV   watch_orderszbydfi.watch_orders  sF       hG227E5&QQQQQQQQQrW   c                   K   |                                   d{V  |                     |dd          }g }||                    d           n@t          dt	          |                    D ]"}||         }|                    d|z              #|                     ||           d{V }| j        rB|                     |d          }	|                     |	d          }
|	                    |
|          }| 
                    |||dd          S )aB  
        watches information on multiple orders made by the user

        https://developers.bydfi.com/en/futures/websocket-account#order-trade-update-push

        :param str[] symbols: unified symbol of the market to fetch orders for
        :param int [since]: the earliest time in ms to fetch orders for
        :param int [limit]: the maximum number of trade 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>`
        NTordersr   zorders::r   rz   )r   r   r   r   r   r   r   r}   r   r   r   )rT   r   r   r   rj   rh   r   r   r   firsttradeSymbols              rV   r   zbydfi.watch_orders_for_symbols  s0      !!!!!!!!!%%gtT::?  ****1c'll++ : : $$Z&%89999))-@@@@@@@@? 	8OOFA..E**5(;;KOOK77E))&%TRRRrW   c                    |                      |di           }|                     |d          }|                     |          }|d         }d}|dz   |z   }| j        0|                     | j        dd          }	t          |	          | _        | j        }
|                     ||          }|                     |d          }||d	<   |
                    |           |	                    |
|           |	                    |
|           d S )
Nor   r   r   r   ordersLimitr   TlastUpdateTimestamp)
r   r   r   r   rb   rO   r   parse_ws_orderr   r   )rT   rX   rs   rawOrderr   r   r   r   symbolMessageHashr   r   orderr  s                rV   handle_orderzbydfi.handle_order2  s   : >>'333##Hc22!!(++!'$.7;%%dlM4HHE077DK##Hf55"//==':#$ev{+++v011111rW   r  r   c                    |                      |d          }|                     ||          }|                      |d          }|                      |d          }d }|                      |d          }|t          j        |          |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!|          S )"Nr   sttfeequote)costcurrencyinforZ   r  clientOrderIdcidrz   datetimelastTradeTimestampr  statusr   typetimeInForcepostOnly
reduceOnlyrosideSpriceptriggerPricestopLossPricevevqtyap)takeProfitPriceamountfilled	remainingr  tradesr  average)
r   r   r   
string_abs
safe_orderparse_order_statusparseOrderTyperk   safe_string_lower	omit_zero)rT   r  r   r   	rawStatusrawTyper  feeCosts           rV   r  zbydfi.parse_ws_order`  sg   0 ##E3//!!(F33$$UD11	""5#..""5%00*733"7O C   
E 
$""5#.. 
 T--eU;; 
 	 

  
 !$ 
 "4 
 d--i88 
 fX& 
 D''00 
 4 
  
 $..55 
 D**5#66 
 T%%eS11 
  D! 
" T# 
$  $&&uc22&&ud33))%77~~d&6&6ud&C&CDD3 
  
  
4 5  	rW   c                   K   |                                   d{V  |                     |dd          }g }d}||                    |           nCt          dt	          |                    D ]%}||         }|                    |dz   |z              &|                     ||           d{V }	| j        r|	S |                     | j        |||d          S )a-  
        watch all open positions

        https://developers.bydfi.com/en/futures/websocket-account#balance-and-position-update-push

        :param str[] [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
        :returns dict[]: a list of `position structure <https://docs.ccxt.com/en/latest/manual.html#position-structure>`
        NT	positionsr   r   )	r   r   r   r   r   r   r   filter_by_symbols_since_limitr9  )
rT   r   r   r   rj   rh   r   r   r   r9  s
             rV   watch_positionszbydfi.watch_positions  s      !!!!!!!!!%%gtT::!?  ----1c'll++ B B $$[4%7&%@AAAA,,]FCCCCCCCC	? 	11$.'5RWY]^^^rW   c                 d   |                      |di           }|                     |dg           }|                      |di           }|                     |d          }|                     |          }|d         }d}	|	dz   |z   }
| j        t                      | _        | j        }|                     ||          }|                     |d          }||d	<   |                     |          |d
<   |	                    |           |
                    |g|	           |
                    |g|
           d S )Nr   r"  r   r   r   r9  r   r  rz   r  )r   	safe_listr   r   r9  r   parse_ws_positionrb   iso8601r   r   )rT   rX   rs   datapositionsDatarawPositionr   r   r   r   r
  cacheparsedPositionrz   s                 rV   handle_positionszbydfi.handle_positions  s8   R ~~gsB//tS"55nn]Ar::##K55!!(++!!'$.7>!577DN//VDD%%gs33	&/{#%)\\)%<%<z"^$$$'555'):;;;;;rW   c                    |                      |d          }|                     ||          }|                      |d          }|                      |d          }|                     i d|d|                      |d          d|d         d|                     |                      |d                    d	d d
d dd dd dd d|                     |                      |d                    d|                     |          d|                     |                      |d                    d|                     |                      |d                    dd dd dd d|dk    d d |                     |                      |d                    d |                     |d          |                     |                      |d                    d |                     |d          d d	          S )Nr   r   ptr  rZ   r   
entryPricer(  	markPrice	lastPricenotional
collateralunrealizedPnlrealizedPnlrpr  	contractsr%  contractSizeuqrz   r  r  hedgedONEWAYpmllqmt)	maintenanceMarginmaintenanceMarginPercentageinitialMargininitialMarginPercentageleverageliquidationPricemarginRatio
marginMode
percentage)r   r   safe_positionparse_numberparse_ws_position_siderb   r3  )rT   positionr   r   rawPositionSidepositionModes         rV   r>  zbydfi.parse_ws_position  se   * ##Hc22!!(F33**8S99''$77!! #
H#
$""8T22#
 fX&#
 $++D,<,<Xt,L,LMM	#

 #
 #
 #
 $#
 T#
 4,,T-=-=h-M-MNN#
 D//@@#
 **4+;+;Hc+J+JKK#
 D--d.>.>x.N.NOO#
 #
 #
  "4!#
" |x/##
$ "&+/!..t/?/?$/O/OPP'+))(C88 $ 1 1$2B2B8T2R2R S S004@@5#
 #
 #
   	rW   rf  c                 :    ddd}|                      |||          S )Nlongshort)12)r   )rT   rf  sidess      rV   rd  zbydfi.parse_ws_position_side,  s.    
 
 HHHrW   c                   K   |                                   d{V  | j        d         d         }|                     |          }|                     |           |                     | j        d          }|                     |dd          }|                     |dd          }|r|r|                    d           d{V  d	}|                     |g|           d{V S )
a  
        watch balance and get the amount of funds available for trading or funds locked in orders

        https://developers.bydfi.com/en/futures/websocket-account#balance-and-position-update-push

        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: a `balance structure <https://docs.ccxt.com/?id=balance-structure>`
        Nr5   r   r   r:   Fr;   Tbalance)	r   rN   rX   fetch_balance_snapshotr   rO   rk   futurer   )rT   rj   rq   rX   rO   r:   r;   r   s           rV   watch_balancezbydfi.watch_balance3  s	      !!!!!!!!!it$S!!##F+++..~>>#~~g7MuUU#~~g7MtTT 	8$8 	8-- 6777777777''v>>>>>>>>>rW   c                     |                      | j        d          }|                     |dd          }|r>d}||j        vr5|                    |           |                     | j        ||           d S d S d S )Nr   r:   F)r}   rO   rk   futuresrq  spawnload_balance_snapshot)rT   rX   rO   r:   r   s        rV   rp  zbydfi.fetch_balance_snapshotH  s    //$,??#~~g7MuUU 	L0K6>11k***

45v{KKKKK		L 	L11rW   c                    K   ddi}|                      |           d {V }|                     || j                  | _        |j        |         }|                                 |                    | j        d           d S )Nr  swapro  )fetch_balancern   ro  rt  r   )rT   rX   r   rj   responserq  s         rV   rv  zbydfi.load_balance_snapshotQ  s      F
 ++F33333333{{8T\::,t|Y/////rW   c                    d}||j         v rM|                     |di           }|                     |dg           }|                     |d          }|||                     |          d}t          dt          |                    D ]}||         }	|                     |	d          }
|                     |
          }| 	                                }|                     |	d          |d<   |                     |	d	          |d
<   |||<   | 
                    |          }|                     | j        |          | _        |                    | j        |           d S d S )Nro  r   Br  )r  rz   r  r   wbtotaltfmused)rt  r   r=  rb   r?  r   r   r   safe_currency_codeaccountsafe_balancern   ro  r   )rT   rX   rs   r   r@  balancesrz   r   r   ro  
currencyIdcoder  parsedBalances                 rV   handle_balancezbydfi.handle_balance\  sb   R  &.((>>'333D~~dC44H))'377I& LL33 F
 1c(mm,, ' '"1+!--gs;;
..z::,,..#'#3#3GT#B#B "&"2"27E"B"B&t --f55M;;t|]CCDLNN4<55555' )(rW   c                     |                      |d          }|                     |j        d          }|                     ||i           }|                     |dd          }|r|                     ||           |S )NrZ   re   F)r   index_byr~   r   rk   handle_un_subscription)rT   rX   rs   rZ   subscriptionsByIdrp   isUnSubMessages          rV   handle_subscription_statusz bydfi.handle_subscription_status  s~     gt,, MM&*>EE~~&7R@@mUKK 	>''===rW   rp   c                 8   |                      |dg           }|                     |dd          }t          dt          |                    D ]8}||         }|                    dd          }|                     ||||           9|                     |           d S )Nrh   r   Fr   zunsubscribe::r   )r=  rk   r   r   replaceclean_unsubscriptionclean_cache)rT   rX   rp   rh   r   r   	unsubHashr   s           rV   r  zbydfi.handle_un_subscription  s    |_bII..7H%PPq#m,,-- 	S 	SA%a(I''<<G%%fgy/RRRR&&&&&rW   c                 8    |                                  |_        |S N)r   lastPong)rT   rX   rs   s      rV   handle_pongzbydfi.handle_pong  s     ++--rW   c                    |                      |d          }|                      |d          }| j        dz   |                     |          z   }|                     | j        d         ||           |                     | j        d         ||           |                     | j        d         ||           t          |          )Nr  msg exactbroad)r   rZ   jsonthrow_exactly_matched_exception
exceptionsthrow_broadly_matched_exceptionr   )rT   rX   rs   r  r  feedbacks         rV   handle_error_messagezbydfi.handle_error_message  s     00w..7S=499W#5#55,,T_W-EsHUUU,,T_W-EsHUUU,,T_W-EtXVVVH%%%rW   c                 p   |                      |d          }||dk    r|                     ||           |                      |d          }|dk    r|                     ||           d S ||                     ||           d S |                      |d          }|dk    r|                     ||           d S |dk    r|                     ||           d S |dk    r|                     ||           d S |d	k    r|                     ||           d S |d
k    r|                     |di           }| 	                    |dg           }t          |          }|dk    r|                     ||           | 	                    |dg           }	t          |	          }
|
dk    r|                     ||           d S d S d S )Nr  0r   ponge
24hrTickerklinedepthUpdateORDER_TRADE_UPDATEACCOUNT_UPDATEr   r|  r   r"  )r   r  r  r  r   r   r   r  r   r=  r   r  rE  )rT   rX   rs   r  r   eventr  r  balancesLengthr9  positionsLengths              rV   handle_messagezbydfi.handle_message  s   00%%fg666!!'844VVW-----++FG<<<<<$$Wc22E$$""6733333'!!!!&'22222-''&&vw77777...!!&'22222***..#r::>>'3;;!$X!A%%''888 NN7C<<	"%i.."Q&&))&'::::: +* '&rW   r  ):__name__
__module____qualname__r   rS   r   rK   r\   rt   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   r  dictr	   r  r   r;  rE  r>  rd  r   rr  rp  rv  r  r  r  r  r  r  __classcell__)rU   s   @rV   r   r      s       B# B B B B B BH
6 
 
 
 
   BDRT ^ ^ ^ ^( 9; ` ` ` `2 68 I I IF I I I I" 9; 
= 
=C 
=s 
= 
= 
= 
= 6:" E E7 E E E E E: 9=R +V +Vg +VC +V +V +V +VZ
 
 
4F 4 4 4 4( 8<$]ajl ) ) )C )WZ )quvzq{ ) ) ) )  BFb T T3 T3 TUX T T T T aesw  AC E E$tCy/ EZ] Emp E E E EB ^` V VT$s)_ Vdg V V V V>@6 @ @ @ @B @DB P PS P PS\ P P P P =? 
L 
L 
L3 
L 
L 
L 
L SW_a ! !$s) !C !fo ! ! ! !< PR  V  VT#Y  VVY  V  V  V  VD/ / / / /, 04$UYbd R R RC Rs Rimnsit R R R R" OSaenp S Sd3i S S[^ Suyz  vA S S S S8,26 ,2 ,2 ,2 ,2\= =D =& =E = = = =~ 8<$]ajl _ _W _C _WZ _quv~q _ _ _ _6:< :< :<x4 4 4 4lIc Ic I I I I *, ? ? ? ? ? ?*LV L L L L	0 	0 	0=6V =6 =6 =6 =6~    'V '4 ' ' ' '&    &6 & & & &;V ; ; ; ; ; ; ; ;rW   r   )ccxt.async_supportccxt ccxt.async_support.base.ws.cacher   r   r   r   ccxt.base.typesr   r   r   r	   r
   r   r   r   r   r   r   !ccxt.async_support.base.ws.clientr   typingr   ccxt.base.errorsr   r   ccxt.base.preciser   async_supportr    rW   rV   <module>r     s&       t t t t t t t t t t  q q q q q q q q q q q q q q q q q q q q q q q q q q 4 4 4 4 4 4       * * * * * * . . . . . . % % % % % %W; W; W; W; W;D$ W; W; W; W; W;rW   