
    ":h                       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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"  G d dejF                  jH                        Z$y)    N)
ArrayCacheArrayCacheBySymbolByIdArrayCacheBySymbolBySideArrayCacheByTimestamp)AnyBalancesIntLiquidationNumOrder	OrderBook	OrderSide	OrderTypePositionStrStringsTickerTickersTrade)Client)List)ArgumentsRequired)
BadRequest)NotSupported)ChecksumError)Precisec                       e Zd Zdef fdZd Zd ZdrdedefdZddi fd	e	d
e
de
dee   fdZdddi fdee	   d
e
de
dee   fdZdefdZdsd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defdZdi fd	e	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i fd	e	defdZdi fd	e	de
defdZdefdZd Zd Zd ZdefdZdefdZ defd Z!defd!Z"ded"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e	   defd%Z'i fd	e	defd&Z(ddi fd	e	d
e
de
dee%   fd'Z)dsd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e	      d
e
de
fd-Z.i fd,eee	      defd.Z/d*i fd	e	defd/Z0defd0Z1i fd	e	de2fd1Z3d*ddi fd	e	d
e
de
dee,   fd2Z4defd3Z5i fd	e	de2fd4Z6i fd	e	de2fd5Z7di fde8de9fd6Z:di fde8de9fd7Z;di fde8defd8Z<i fd	e	defd9Z=di fde8de9fd:Z>di fd;e	de8fd<Z?d= Z@defd>ZAdefd?ZBdefd@ZCdefdAZDd;e	d	edBeEfdCZFi fdDZGi fdEZHi fdFZIdtdefdGZJdH ZKi fdeLfdIZMdefdJZNdefdKZOi fd	e	deeP   fdLZQdi fde8deeP   fdMZRdefdNZSi fdeLfdOZTdefdPZUi fdQZVdi fd	e	deWdReXdSeYdTeZde[fdUZ\defdVZ]defdWZ^ddi fdXe	d	e	deWdReXdSeZdTeZde[fdYZ_defdZZ`di fdXe	d	ede[fd[Zadi fd	efd\Zbdi fdXe	d	ede[fd]Zcdddi fd	ed
e
de
dee[   fd^Zddddi fd	ed
e
de
dee[   fd_Zedddi fd	ed
e
de
dee[   fd`Zfdddi fd	ed
e
de
dee[   fdaZgdsdbZhdefdcZidddi fde8d
e
de
deeP   fddZjdudede8fdeZkdf Zldg ZmdsdhZndddi fd	ed
e
de
dee%   fdiZodddi fd	ed
e
de
dee%   fdjZpdefdkZqdddi fd	ed
e
de
dee%   fdlZrdefdmZsdefdnZtdo ZudefdpZvdefdqZw xZxS )vbinancereturnc                 h    t         t        |          }| j                  || j	                               S N)superr   describedeep_extenddescribe_data)selfsuperDescribe	__class__s     J/var/www/html/turnos/venv/lib/python3.12/site-packages/ccxt/pro/binance.pyr#   zbinance.describe   s.    gt57t/A/A/CDD    c                 6   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i ddddddddddddddddddddddddd dd!dd"dd#dd$dddddddddd%dd&d&d'd(d)d*d+d,d-idd.d.d/d0d1d2d3d,d4d5id6d7d8d9ii d:dd;d<d<d<d<d=d>d?d?d?d?d=d@| j                         dAdBdCdDdEdFdGdFdHdFdIdFdJdFdK| j                         dLdFddMdNiddMdOiddMdOid
dMdPidQddRdddSdTdUidddVdWdXdYdZidOd[d\d]d^d_d`dadbdcS )dNwsTwatchBalancewatchLiquidationswatchLiquidationsForSymbolswatchMyLiquidationswatchMyLiquidationsForSymbolswatchBidsAskswatchMyTrades
watchOHLCVwatchOHLCVForSymbolswatchOrderBookwatchOrderBookForSymbolswatchOrderswatchOrdersForSymbolswatchPositionswatchTickerwatchTickerswatchMarkPriceswatchMarkPricewatchTradeswatchTradesForSymbolscreateOrderWseditOrderWscancelOrderWscancelOrdersWsFcancelAllOrdersWsfetchBalanceWsfetchDepositsWsfetchMarketsWsfetchMyTradesWsfetchOHLCVWsfetchOrderBookWsfetchOpenOrdersWsfetchOrderWs)fetchOrdersWsfetchPositionWsfetchPositionForSymbolWsfetchPositionsWsfetchTickerWsfetchTradesWsfetchTradingFeesWsfetchWithdrawalsWsz&wss://stream.testnet.binance.vision/wsz"wss://fstream.binancefuture.com/wsz"wss://dstream.binancefuture.com/wsz-wss://ws-api.testnet.binance.vision/ws-api/v3z*wss://testnet.binancefuture.com/ws-fapi/v1z*wss://testnet.binancefuture.com/ws-dapi/v1)spotfuturedelivery)rV   marginrW   rX   ws-apiz wss://stream.binance.com:9443/wszwss://fstream.binance.com/wszwss://dstream.binance.com/wsz&wss://ws-api.binance.com:443/ws-api/v3z$wss://ws-fapi.binance.com/ws-fapi/v1z$wss://ws-dapi.binance.com/ws-dapi/v1zwss://fstream.binance.com/pm/ws)rV   rY   rW   rX   rZ   papiz!https://developers.binance.com/en)testapidoc	keepAlivei  returnRateLimitsstreamLimits2   )rV   rY   rW   rX   subscriptionLimitByStream   streamBySubscriptionsHashstreamIndexwatchOrderBookRated   liquidationsLimit  myLiquidationsLimittradesLimitordersLimit
OHLCVLimit	requestIdwatchOrderBookLimitnametradetickerkline   )
maxRetrieschecksum)fetchBalanceSnapshotawaitBalanceSnapshotdefaultTypeswap)fetchPositionsSnapshotawaitPositionsSnapshotwbO cost   
miniTicker	markPrice	ticker_1h	ticker_4h	ticker_1d
bookTicker)
24hrTicker24hrMiniTickermarkPriceUpdate1hTicker4hTicker1dTickerr   )r6   r-   r/   r:   walletlistenKeyRefreshRater,   tickerChannelsMap)hasurls	streamingoptions)create_safe_dictionary)r&   s    r)   r%   zbinance.describe_data   s   +d++ $T+ .t	+
 &t+ 0+  +  + d+ '+ !$+ +D+ t+ (+ !$+  t!+" #+$ "4%+& !$'+( t)+* (++,  -+. t/+0  1+2 !%3+4 $T5+6 !$7+8 "59+: !%;+< "4=+> ?+@ #DA+B $TC+D E+F "&#',0$(!%!%&+&+U+\  H"J"F$H$S&R(T#
  B"D"@$B$L&L(N#
 !B ;9> VD"ED   "	!D ,!! #	.D ,T-H-H-JD rD$ %c%D& $T'D( &t)D* t+D, t-D. d/D0 T88:1D2 &t3D4 G 5D: H ;D@ H!ADF GGDN #$ $#
 -2,0!
 "60 /3.2# (/A #+&2'2 + + +".	&uD]S
 S	
r*   c                     | j                  | j                  d| j                               }| j                  ||d      }| j	                  |d      }|| j                  d   |<   |S )Nrp   r      )	safe_dictr   r   safe_integersum)r&   urlr   previousValuenewValues        r)   
request_idzbinance.request_id   s]    ..{D<W<W<YZ))'3:88M1-)1[!#&r*   typesubscriptionHashc                    | j                  | j                  d| j                               }| j                  ||      }|9| j	                  | j                  dd      }| j                  | j                  d      }| j	                  ||      }|dz   }||z  }	|| j                  d<   | j                  |	      }|| j                  d   |<   | j                  | j                  d      }
|
| j                         | j                  d<   | j	                  | j                  d   |d      }||z   }| j	                  | j                  d   |d	      }||kD  rt        | j                  d
z         ||z   | j                  d   |<   |S )Nre   rf   rg   ra   r   numSubscriptionsByStreamr   rc   rd   z reached the limit of subscriptions by stream. Increase the number of streams, or increase the stream limit or subscription limit by stream if the exchange allows.)	r   r   r   safe_stringr   
safe_valuenumber_to_stringr   id)r&   r   r   numSubscriptionsre   streamrf   ra   streamLimitnormalizedIndexsubscriptionsByStreamssubscriptionsByStreamnewNumSubscriptionsrc   s                 r)   r   zbinance.stream   s   $(NN4<<A\^b^y^y^{$|!!!";=MN>++DLL-LK??4<<HL++L$?K%/K)K7O*5DLL'**?;FJPDLL456FG%)__T\\C]%^"%-;?;V;V;X78$($5$5dllC]6^`fhi$j!"7:J"J(,(9(9$,,Gb:ceikn(o%"%>>   ,Q  "Q  R  R?TWg?gDLL34V<r*   Nsymbolsincelimitc                 H   K   | j                  |g|||       d{   S 7 w)a  
        watch the public liquidations of a trading pair

        https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Liquidation-Order-Streams
        https://developers.binance.com/docs/derivatives/coin-margined-futures/websocket-market-streams/Liquidation-Order-Streams

        :param str symbol: unified CCXT market symbol
        :param int [since]: the earliest time in ms to fetch liquidations for
        :param int [limit]: the maximum number of liquidation structures to retrieve
        :param dict [params]: exchange specific parameters for the bitmex api endpoint
        :returns dict: an array of `liquidation structures <https://github.com/ccxt/ccxt/wiki/Manual#liquidation-structure>`
        N)watch_liquidations_for_symbolsr&   r   r   r   paramss        r)   watch_liquidationszbinance.watch_liquidations   s(      88&5%QWXXXXs   " "symbolsc                 ,  K   | j                          d{    g }g }d}| j                  |ddd      }| j                  |      r#|j                  d       |j                  d       nst	        dt        |            D ]D  }| j                  ||         }	|j                  |	d   dz          |j                  d||   z          F |d	d
j                  |      z   z  }| j                  |      }
d}| j                  d|
|      \  }}|dk(  rt        | j                  dz         d}| j                  d|
|      \  }}| j                  ||      rd}n| j                  ||      rd}t        |      }| j                  d   d   |   dz   | j!                  |||      z   }| j#                  |      }d||d}d|i}| j%                  ||| j'                  ||      ||       d{   }| j(                  r|S | j+                  | j,                  |||d      S 7 7 4w)a  
        watch the public liquidations of a trading pair

        https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/All-Market-Liquidation-Order-Streams
        https://developers.binance.com/docs/derivatives/coin-margined-futures/websocket-market-streams/All-Market-Liquidation-Order-Streams

        :param str[] symbols: list of unified market symbols
        :param int [since]: the earliest time in ms to fetch liquidations for
        :param int [limit]: the maximum number of liquidation structures to retrieve
        :param dict [params]: exchange specific parameters for the bitmex api endpoint
        :returns dict: an array of `liquidation structures <https://github.com/ccxt/ccxt/wiki/Manual#liquidation-structure>`
        NliquidationsTz!forceOrder@arrr   lowercaseIdz@forceOrderliquidations::::,r/   rV   z> watchLiquidationsForSymbols is not supported for spot symbolsrW   rX   r]   r,   /	SUBSCRIBEmethodr   r   r   )load_marketsmarket_symbolsis_emptyappendrangelenmarketjoinget_market_from_symbolshandle_market_type_and_paramsr   r   handle_sub_type_and_paramsisLinear	isInverser   r   r   watch_multipleextend
newUpdatesfilter_by_symbols_since_limitr   )r&   r   r   r   r   subscriptionHashesmessageHashes
streamHashir   firstMarketr   subTyper   r   rp   request	subscribenewLiquidationss                      r)   r   z&binance.watch_liquidations_for_symbols   sM     !!!#
%%gtT4@==!%%&<=  01c'l+ DWQZ0"))&*?-*OP$$%5
%BCD $'!222J227;99:WYdflmf6>TWW'gghh99:WYdflm==w'D^^D'*D12iit$T*S04;;tZQa3bbOOC(	!(
 )
	 !% 3 3CT[]cHdfx  {D  !E  E??""11$2C2CWeUZ\`aaO 	"H Es"   HHGHH2HHclientc                    | j                  |di       }| j                  |d      }| j                  |d dd      }|d   }| j                  ||      }| j                  | j                  |      }|(| j                  | j                  dd      }	t        |	      }|j                  |       || j                  |<   |j                  |gd       |j                  |gd	|z          y )
Nos contractr   rj   rk   r   r   )
r   r   safe_marketparse_ws_liquidationr   r   r   r   r   resolve)
r&   r   messagerawLiquidationmarketIdr   r   liquidationr   r   s
             r)   handle_liquidationzbinance.handle_liquidation  s    P #r:##NC8!!(D"jA!//Gt'8'8&A%%dll4GNE%e,LK($0&!}n5}&6&?@r*   c                 B   | j                  |d      }| j                  ||      }| j                  |d      }| j                  || j	                  ||      | j                  |d      | j                  |d      | j                  |d      d d || j                  |      d	      S )Nr   TlcontractSizeap)	infor   	contractsr   price	baseValue
quoteValue	timestampdatetime)r   r   r   safe_liquidationsafe_symbolsafe_numberiso8601)r&   r   r   r   r   s        r)   r   zbinance.parse_ws_liquidationH  s    V ##K5!!(F3%%k37	$$&&x8))+s; ,,V^D%%k48"Y/
&
 
 
	r*   c                 4   K   | j                  |g|||      S w)a  
        watch the private liquidations of a trading pair

        https://developers.binance.com/docs/derivatives/usds-margined-futures/user-data-streams/Event-Order-Update
        https://developers.binance.com/docs/derivatives/coin-margined-futures/user-data-streams/Event-Order-Update

        :param str symbol: unified CCXT market symbol
        :param int [since]: the earliest time in ms to fetch liquidations for
        :param int [limit]: the maximum number of liquidation structures to retrieve
        :param dict [params]: exchange specific parameters for the bitmex api endpoint
        :returns dict: an array of `liquidation structures <https://github.com/ccxt/ccxt/wiki/Manual#liquidation-structure>`
        )!watch_my_liquidations_for_symbolsr   s        r)   watch_my_liquidationszbinance.watch_my_liquidations  s!      55vhufUUs   c                   K   | j                          d{    | j                  |dddd      }| j                  |      }dg}| j                  |      s3t	        dt        |            D ]  }||   }|j                  d|z           d}	| j                  d||      \  }	}d}
| j                  d||      \  }
}| j                  |	|
      rd}	n| j                  |	|
      rd}	| j                  |       d{    | j                  d	   d
   |	   dz   | j                  |	   d   z   }d}| j                  ||||	g       d{   }| j                  r|S | j!                  | j"                  |||      S 7 Z7 }7 5w)a  
        watch the private liquidations of a trading pair

        https://developers.binance.com/docs/derivatives/usds-margined-futures/user-data-streams/Event-Order-Update
        https://developers.binance.com/docs/derivatives/coin-margined-futures/user-data-streams/Event-Order-Update

        :param str[] symbols: list of unified market symbols
        :param int [since]: the earliest time in ms to fetch liquidations for
        :param int [limit]: the maximum number of liquidation structures to retrieve
        :param dict [params]: exchange specific parameters for the bitmex api endpoint
        :returns dict: an array of `liquidation structures <https://github.com/ccxt/ccxt/wiki/Manual#liquidation-structure>`
        NTmyLiquidationsr   myLiquidations::r1   rW   rX   r]   r,   r   	listenKey)r   r   r   r   r   r   r   r   r   r   r   authenticater   r   r   r   r   r   )r&   r   r   r   r   r   r   r   r   r   r   r   r   r   s                 r)   r   z)binance.watch_my_liquidations_for_symbols  s     !!!%%gtT4F--g6)*}}W%1c'l+ B $$%7&%@AB 99:Y[acijf99:Y[acij==w'D^^D'*D'''iit$T*S04<<3Ek3RR $ 3 3CRVQW XX??""11$2C2CWeUZ[[- 	"  	( Ys5   E7E0CE75E36A	E7?E5 1E73E75E7c                    | j                  |d      }|dk7  ry | j                  |d      }| j                  |      }| j                  |      }| j                  ||      }| j	                  | j
                  |      }|(| j                  | j                  dd      }	t        |	      }|j                  |       || j
                  |<   |j                  |gd       |j                  |gd|z          y )Nr   LIQUIDATIONr   rl   rk   r   r   )r   r   r   r   r   r   r   r   r   r   r   )
r&   r   r   	orderTyper   r   r   r   r   r   s
             r)   handle_my_liquidationzbinance.handle_my_liquidation  s    Z $$Wc2	%##GS1!!(+!!(+//@)<)<fE!%%dll4I4PE'.Nk*&4F#}&67}&86&ABr*   c                 F   K   | j                  |g||       d{   S 7 w)a  
        watches information on open orders with bid(buy) and ask(sell) prices, volumes and other data

        https://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams#partial-book-depth-streams
        https://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams#diff-depth-stream
        https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Partial-Book-Depth-Streams
        https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Diff-Book-Depth-Streams
        https://developers.binance.com/docs/derivatives/coin-margined-futures/websocket-market-streams/Partial-Book-Depth-Streams
        https://developers.binance.com/docs/derivatives/coin-margined-futures/websocket-market-streams/Diff-Book-Depth-Streams

        :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
        :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)r&   r   r   r   s       r)   watch_order_bookzbinance.watch_order_book  s&     j 66xOOOOs   !!c                   K   | j                          d{    | j                  |dddd      }| j                  |d         }|d   }|d   r	|d   rdnd	}d
}d}|?t        |      }|dkD  rt	        | j
                  dz         |ddj                  |      z   z  }| j                  | j                  dd      }	g }
g }t        dt        |            D ]S  }||   }| j                  |      }|j                  d|z          |d   dz   |z   }|dz   |	z   dz   }|
j                  |       U t        |      }| j                  d   d   |   dz   | j                  |||      z   }| j                  |      }d|
|d}t        |      ||| j                  |||d}| j!                  ||| j#                  ||      ||       d{   }|j%                         S 7 7 w)a  
        watches information on open orders with bid(buy) and ask(sell) prices, volumes and other data

        https://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams#partial-book-depth-streams
        https://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams#diff-depth-stream
        https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Partial-Book-Depth-Streams
        https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Diff-Book-Depth-Streams
        https://developers.binance.com/docs/derivatives/coin-margined-futures/websocket-market-streams/Partial-Book-Depth-Streams
        https://developers.binance.com/docs/derivatives/coin-margined-futures/websocket-market-streams/Diff-Book-Depth-Streams

        :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
        :returns dict: A dictionary of `order book structures <https://docs.ccxt.com/#/?id=order-book-structure>` indexed by market symbols
        NFTr   r   r   linearrW   rX   depthmultipleOrderbookrd   z} watchOrderBookForSymbols() accepts 200 symbols at most. To watch more symbols call watchOrderBookForSymbols() multiple timesr   r   rh   100orderbook::r   @msr]   r,   r   r   r   )r   rr   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   strhandle_order_book_subscriptionr   r   r   )r&   r   r   r   r   r   rr   r   symbolsLengthrh   	subParamsr   r   r   r   r   
symbolHashmessageHashesLengthr   rp   r   subscription	orderbooks                          r)   r  z$binance.watch_order_book_for_symbolsJ  s(      !!!%%gtUD$Gkk'!*-6"z"*848*D(
LMs"   ,k  "k  l  l$'!222J!--dll<PRWX	q#g,' 	)AQZF[[(F  !78%m4s:TA)C/2DDtKJZ(	) "-0iit$T*S04;;tZQd3eeOOC(	!
 i.99
 --c=$++gW]B^`mo{||	  S 	"P }s"   GGF$G;G<GGc                 T  K   | j                          d{    | j                  |dddd      }| j                  |d         }|d   }|d   r	|d   rdnd	}d
}d}||ddj                  |      z   z  }| j	                  | j
                  dd      }g }g }	g }
t        dt        |            D ]g  }||   }| j                  |      }|	j                  d|z          |
j                  d|z          |d   dz   |z   }|dz   |z   dz   }|j                  |       i t        |	      }| j                  d   d   |   dz   | j                  |||      z   }| j                  |      }d||d}dt        |      ||	|
dd}| j                  ||
| j                  ||      |
|       d{   S 7 7 w)ad  
        unWatches information on open orders with bid(buy) and ask(sell) prices, volumes and other data

        https://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams#partial-book-depth-streams
        https://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams#diff-depth-stream
        https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Partial-Book-Depth-Streams
        https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Diff-Book-Depth-Streams
        https://developers.binance.com/docs/derivatives/coin-margined-futures/websocket-market-streams/Partial-Book-Depth-Streams
        https://developers.binance.com/docs/derivatives/coin-margined-futures/websocket-market-streams/Diff-Book-Depth-Streams

        :param str[] symbols: 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
        NFTr   r   r   r
  rW   rX   r  r  r   r   rh   r  r  zunsubscribe:orderbook:r   r  r  r]   r,   r   UNSUBSCRIBEr   r  )unsubscriber   r   subMessageHashesr   topic)r   r   r   r   r   r   r   r   r   r   r   r   r  r   r   )r&   r   r   r   r   rr   r   rh   r  r  r   r   r   r   r   r  r  r   rp   r   r  s                        r)   un_watch_order_book_for_symbolsz'binance.un_watch_order_book_for_symbols  s     !!!%%gtUD$Gkk'!*-6"z"*848*D(
$'!222J!--dll<PRWX	q#g,' 	)AQZF[[(F##MF$:;  !9F!BC%m4s:TA)C/2DDtKJZ(	) ""23iit$T*S04;;tZQd3eeOOC(	#
  i. 0* 
 ((mT[[RX=Y[hjvwwwM 	"L xs"   F(F#FF(F&F(&F(c                 D   K   | j                  |g|       d{   S 7 w)aa  
        unWatches information on open orders with bid(buy) and ask(sell) prices, volumes and other data

        https://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams#partial-book-depth-streams
        https://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams#diff-depth-stream
        https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Partial-Book-Depth-Streams
        https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Diff-Book-Depth-Streams
        https://developers.binance.com/docs/derivatives/coin-margined-futures/websocket-market-streams/Partial-Book-Depth-Streams
        https://developers.binance.com/docs/derivatives/coin-margined-futures/websocket-market-streams/Diff-Book-Depth-Streams

        :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)r  r&   r   r   s      r)   un_watch_order_bookzbinance.un_watch_order_book  s#      996(FKKKK     c                 ^  K   | j                          d{    | j                  |      }d|d   i}|||d<   | j                  d||      }|dk7  rt        | j                  dz         | j
                  d   d	   d
   |   }| j                  |      }t        |      }	d}
| j                  |ddd      \  }
}|
|d<   | j                  |d      }|	d| j                  | j                  ||            d}d| j                  i}| j                  ||	||	|       d{   }|d   |d<   |S 7 7 w)a  
        fetches information on open orders with bid(buy) and ask(sell) prices, volumes and other data

        https://developers.binance.com/docs/binance-spot-api-docs/websocket-api/market-data-requests#order-book
        https://developers.binance.com/docs/derivatives/usds-margined-futures/market-data/websocket-api/Order-Book

        :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
        :returns dict: A dictionary of `order book structures <https://docs.ccxt.com/#/?id=order-book-structure>` indexed by market symbols
        Nr   r   r   rK   rW   z, fetchOrderBookWs only supports swap marketsr]   r,   rZ   FrA   r`   r\   r  r   r   r   r   )r   r   get_market_typer   r   r   r   r  handle_option_and_paramsomitsign_paramsr   handle_fetch_order_bookwatch)r&   r   r   r   r   payload
marketTyper   rp   messageHashr`   r   r  r  s                 r)   fetch_order_book_wszbinance.fetch_order_book_ws  se     !!!V$fTl
 $GG))*<ffM
!TWW'UUVViit$X.z:OOC(	)n #'#@#@Zlns#t &&6"#66*&&t{{7F'CD
 d22
 **S+w\ZZ	$X.	(7 	"2 [s"   D-D(DD-D+D-+D-c                     | j                  |d      }| j                  |d      }| j                  |d      }| j                  |d |      }| j	                  |dd      |d<   |j                  ||       y )Nr   resultr   lastUpdateIdunonce)r   r   r   parse_order_booksafe_integer_2r   )r&   r   r   r-  r0  r   r  s          r)   r)  zbinance.handle_fetch_order_book  ss    0 &&w52%%fc2	))&$	B	!00M	'y+.r*   c                   K   | j                  |d      }d|z   }	 | j                  | j                  dd      }| j                  |d      }| j                  |d|      }| j                  |d      }	| j	                  |||	       d {   }
| j                  | j
                  |      y | j
                  |   }|j                  |
       |j                  }g |_        t        dt        |            D ]  }||   }| j                  |d	      }| j                  |d
      }| j                  |d      }|dk(  r6||d   k  rL||d   k  r||d   k\  s	||d   k(  se| j                  |||       y||d   k  r|dz
  |d   k  s|dz
  |d   k\  s| j                  |||        || j
                  |<   |j                  ||       y 7 ;# t        $ r)}|j                  |= |j                  ||       Y d }~y d }~ww xY ww)Nr   r  rq   rk   r   r   r   r   Ur2  purW   r3  r   )r   r   r   r   fetch_rest_order_book_safe
orderbooksresetcacher   r   handle_order_book_messager   	Exceptionsubscriptionsreject)r&   r   r   r  r   r-  defaultLimitr   r   r   snapshotr  messagesr   messageItemr7  r2  r8  es                      r)   fetch_order_book_snapshotz!binance.fetch_order_book_snapshot  s    !!,9#f,(	*,,T\\;PRVWL??<8D%%lG\JE__\8<F "<<VUFSSHt7?/IOOH% H IO1c(m, W&qk%%k37%%k37&&{D98#9W-- Yw//a9W;M6MSUYbcjYkSk66v{IV Ig.. Q9W#55QUyQXGY<Y66v{IV%W& '0DOOF#NN9k2; T<  	*$$[1MM![))	*s`   G8A*G G  G &G8'B,G (G =G 	6G ?G8 G 	G5G0+G80G55G8c                 p    | j                  |d      }| j                  |d      }|j                  ||       y )Nr   r   )
safe_floatstore)r&   booksidedeltar   amounts        r)   handle_deltazbinance.handle_deltaB  s0    q)*uf%r*   c                 b    t        dt        |            D ]  }| j                  |||           y )Nr   )r   r   rM  )r&   rJ  deltasr   s       r)   handle_deltaszbinance.handle_deltasG  s0    q#f+& 	3Ahq	2	3r*   c                 "   | j                  |d      }| j                  |d   | j                  |dg              | j                  |d   | j                  |dg              ||d<   | j                  |d      }||d<   | j                  |      |d	<   |S )
Nr2  asksabidsbr3  Er   r   )r   rP  r   r   )r&   r   r   r  r2  r   s         r)   r=  z!binance.handle_order_book_messageK  s    gs+9V,doogsB.OP9V,doogsB.OP	'%%gs3	!*	+ $Y 7	*r*   c                    |j                   j                  d      dkD  }|rdnd}| j                  |d      }| j                  |d d |      }|d   }d|z   }|| j                  vry | j                  |   }	| j                  |	d      }
|
|	j                  j                  |       y 	 | j                  |d	      }| j                  |d
      }| j                  |d      }|||	d   kD  r3| j                  |	d      }d }||dz
  |	d   k  xr |dz
  |	d   k\  }n|dz
  |	d   k(  }|r.| j                  |||	       |
|	d   k  r|j                  |	|       y | j                  ddd      }|rt        | j                  dz   | j                  |      z         ||	d   k\  r||	d   k  s||	d   k(  r.| j                  |||	       |
|	d   k  rS|j                  |	|       y | j                  ddd      }|r*t        | j                  dz   | j                  |      z         y y y y y y # t        $ r6}| j                  |= |j                  |= |j!                  ||       Y d }~y d }~ww xY w)N/streamrg   rV   r   r   r   r  r3  r7  r2  r8  r   r   r6   rx   T )r   findr   r   r:  r   r<  r   r=  r   handle_optionr   r   orderbook_checksum_messager>  r?  r@  )r&   r   r   isSpotr,  r   r   r   r-  r  r3  r7  r2  r8  r   conditionalrx   rE  s                     r)   handle_order_bookzbinance.handle_order_bookU  s   & **//),r1 &VZ
##GS1!!(D$
C!#f,$//) OOF+	!!)W5=OO""7++.%%gs3%%gs3&&w5: 9W--$($5$5i$M	&*$,,-Ei6H+H*mPQTUPUZcdkZlOlK -.Ei6H+HK& ::67IV$y'99 &y+ F'+'9'9:JJX\']H'&3DGGcMDDcDcdjDk4k&l l Ig.. 7!33y?Q9Q ::67IV$	'(:: &y+ F'+'9'9:JJX\']H'&3DGGcMDDcDcdjDk4k&l l  (	  ; /  (	  : .B  .OOF+((5a--.s&   $B*H BH ?H 	I",IIc                    | j                  | j                  dd      }| j                  |d      }| j                  |d|g      }| j                  |d|      }t	        dt        |            D ]s  }||   }	|	| j                  v r| j                  |	= | j                  i |      | j                  |	<   | j                  |d|	i      }| j                  | j                  |||       u y )Nrq   rk   r   r   r   r   )r   r   r   r   r   r   r:  
order_bookr   spawnrF  )
r&   r   r   r  rA  symbolOfSubscriptionr   r   r   r   s
             r)   r  z&binance.handle_order_book_subscription  s    ((7LdS#//hG//,	<P;QR!!,Fq#g,' 	VAQZF(OOF+&*oob%&@DOOF#;;|h5GHLJJt55vwU	Vr*   c                    | j                  |d      }| j                  |j                  d      }| j                  ||i       }| j                  |d      }|
 ||||       | j	                  |dd      }|r| j                  ||       |S )Nr   r   r  F)r   index_byr?  r   	safe_boolhandle_un_subscription)r&   r   r   r   subscriptionsByIdr  r   isUnSubMessages           r)   handle_subscription_statusz"binance.handle_subscription_status  s     gt, MM&*>*>E'8"bAx867L1mUK''=r*   r  c                     | j                  |dg       }| j                  |dg       }t        dt        |            D ]  }||   }||   }| j                  |||       ! | j	                  |       y )Nr   r  r   )	safe_listr   r   clean_unsubscriptionclean_cache)r&   r   r  r   r  j	unsubHashsubHashs           r)   rg  zbinance.handle_un_subscription  s{    |_bI>>,8JBOq#m,- 	BA%a(I&q)G%%fgyA	B 	&r*   c                 0  K   | j                          d{    | j                  |dddd      }d}|?t        |      }|dkD  rt        | j                  dz         |ddj                  |      z   z  }d}| j                  |d	d
d      \  }}| j                  |d      }| j                  |d         }|d   }	|d   r	|d   rdnd}	g }
g }t        dt        |            D ]H  }||   }| j                  |      }|
j                  d|z          |d   dz   |z   }|j                  |       J | j                  |d      }t        |      }| j                  d   d   |	   dz   | j                  |	||      z   }| j                  |      }d||d}d|i}| j                  ||
| j                  ||      |
|       d{   }| j                   r6| j#                  |d      }| j%                  |d      }|j'                  ||      }| j)                  |||dd      S 7 7 ^w)a2  
        get the list of most recent trades for a list of symbols

        https://developers.binance.com/docs/binance-spot-api-docs/websocket-api/market-data-requests#aggregate-trades
        https://developers.binance.com/docs/binance-spot-api-docs/websocket-api/market-data-requests#recent-trades
        https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Aggregate-Trade-Streams
        https://developers.binance.com/docs/derivatives/coin-margined-futures/websocket-market-streams/Aggregate-Trade-Streams

        :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 str [params.name]: the name of the method to call, 'trade' or 'aggTrade', default is 'trade'
        :returns dict[]: a list of `trade structures <https://docs.ccxt.com/#/?id=public-trades>`
        NFTmultipleTradesrd   w watchTradesForSymbols() accepts 200 symbols at most. To watch more symbols call watchTradesForSymbols() multiple timesr   r   r@   rr   rs   callerMethodNamer   r   r   r
  rW   rX   trade::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   getLimitfilter_by_since_limit)r&   r   r   r   r   r   r  rr   r   r   r   r  r   r   r   rawHashquerysubParamsLengthr   rp   r   r   tradesfirsttradeSymbols                            r)   watch_trades_for_symbolsz binance.watch_trades_for_symbols  s^      !!!%%gtUD$G%
LMs"   ,e  "e  f  f$'!222J44V=TV\^eff6#56kk'!*-6"z"*848*D	q#g,' 	&AQZF[[(F  V!34]+c1D8GW%	& 		&&)i.iit$T*S04;;tZQ`3aaOOC(	!
 )
	 **3t{{7TY?Z\iktuu??OOFA.E**5(;KOOK7E))&%TRRQ 	"F vs#   HHFH5H6AHHc                   K   | j                          d{    | j                  |dddd      }d}|?t        |      }|dkD  rt        | j                  dz         |ddj                  |      z   z  }d}| j                  |d	d
d      \  }}| j                  |d      }| j                  |d         }|d   }|d   r	|d   rdnd}g }g }	g }
t        dt        |            D ]\  }||   }| j                  |      }|j                  d|z          |
j                  d|z          |d   dz   |z   }|	j                  |       ^ | j                  |d      }t        |	      }| j                  d   d   |   dz   | j                  |||      z   }| j                  |      }d|	|d}dt        |      ||
|dd}| j                  ||
| j!                  ||      |
|       d{   S 7 7 w)a  
        unsubscribes from the trades channel

        https://developers.binance.com/docs/binance-spot-api-docs/websocket-api/market-data-requests#aggregate-trades
        https://developers.binance.com/docs/binance-spot-api-docs/websocket-api/market-data-requests#recent-trades
        https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Aggregate-Trade-Streams
        https://developers.binance.com/docs/derivatives/coin-margined-futures/websocket-market-streams/Aggregate-Trade-Streams

        :param str[] symbols: unified symbol of the market to fetch trades for
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :param str [params.name]: the name of the method to call, 'trade' or 'aggTrade', default is 'trade'
        :returns dict[]: a list of `trade structures <https://docs.ccxt.com/#/?id=public-trades>`
        NFTrs  rd   rt  r   r   r@   rr   rs   ru  r   r   r   r
  rW   rX   rv  zunsubscribe:trade: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   r   )r&   r   r   r   r  rr   r   r   r  r  r   r   r   r   ry  rz  r{  r   rp   r   r  s                        r)   un_watch_trades_for_symbolsz#binance.un_watch_trades_for_symbols  s8     !!!%%gtUD$G%
LMs"   ,e  "e  f  f$'!222J44V=TV\^eff6#56kk'!*-6"z"*848*D	q#g,' 	&AQZF[[(F##I$67  !5!>?]+c1D8GW%	& 		&&)i.iit$T*S04;;tZQ`3aaOOC(	#
  i. 0*
 ((mT[[RW=XZgiuvvvU 	"T ws"   G#GGG#G!G#!G#c                 x   K   | j                          d{    | j                  |g|       d{   S 7 7 w)a  
        unsubscribes from the trades channel

        https://developers.binance.com/docs/binance-spot-api-docs/websocket-api/market-data-requests#aggregate-trades
        https://developers.binance.com/docs/binance-spot-api-docs/websocket-api/market-data-requests#recent-trades
        https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Aggregate-Trade-Streams
        https://developers.binance.com/docs/derivatives/coin-margined-futures/websocket-market-streams/Aggregate-Trade-Streams

        :param str symbol: unified symbol of the market to fetch trades for
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :param str [params.name]: the name of the method to call, 'trade' or 'aggTrade', default is 'trade'
        :returns dict[]: a list of `trade structures <https://docs.ccxt.com/#/?id=public-trades>`
        N)r   r  r   s      r)   un_watch_tradeszbinance.un_watch_tradesJ  s;      !!!55vhGGG 	"Gs   :6:8::c                 R   K   d|d<   | j                  |g|||       d{   S 7 w)a1  
        get the list of most recent trades for a particular symbol

        https://developers.binance.com/docs/binance-spot-api-docs/websocket-api/market-data-requests#aggregate-trades
        https://developers.binance.com/docs/binance-spot-api-docs/websocket-api/market-data-requests#recent-trades
        https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Aggregate-Trade-Streams
        https://developers.binance.com/docs/derivatives/coin-margined-futures/websocket-market-streams/Aggregate-Trade-Streams

        :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 str [params.name]: the name of the method to call, 'trade' or 'aggTrade', default is 'trade'
        :returns dict[]: a list of `trade structures <https://docs.ccxt.com/#/?id=public-trades>`
        r?   ru  N)r  r   s        r)   watch_tradeszbinance.watch_trades[  s2       &3!"22F8UE6RRRRs   '%'c                 F   | j                  |d      }|dk(  }|s| j                  ||      S | j                  |dd      }| j                  |d      }| j                  |dd      }| j                  |d      }|r| j                  |d	|      }| j                  |d
      }	|	||t	        j
                  ||      }	| j                  |d      }
d|v rdnd}| j                  |
d d |      }| j                  |d      }d }| j                  |d      }d|v r|	|d   rdnd}|d   rdnd}d }| j                  |d      }|(| j                  |d      }| j                  |      }||d}| j                  |d      }| j                  ||| j                  |      |||||||||	|d      S )NxTRADEtrS  r   Lpqr   Yr   psr   rV   Sr   msellbuymakertakernNr   currencyr   )r   r   r   r   r   orderr   takerOrMakersider   rL  r   fee)r   parse_tradesafe_string_2r   r   
string_mulr   safe_string_lowersafe_currency_code
safe_trader   )r&   rs   r   executionTypeisTradeExecutionr   r   r   rL  r   r   r,  r   r  r  orderIdr  feeCostfeeCurrencyIdfeeCurrencyCoder   s                        r)   parse_ws_tradezbinance.parse_ws_traden  s   R ((4)W4##E622sC0%%eS1	""5#s3!!%-%%eS&9Fs+<!(:))%8##E3/$(EMZ
!!(D$
C%%eS1""5#.%<|!&sv&+Cj7gL""5#. ,,UC8M"55mDO+C %%eS1"Y/( 
  	r*   c                    |j                   j                  d      dkD  }|rdnd}| j                  |d      }| j                  |d d |      }|d   }d|z   }| j	                  ||      }	| j                  | j                  |      }
|
(| j                  | j                  dd	      }t        |      }
|
j                  |	       |
| j                  |<   |j                  |
|       y )
NrX  rg   rV   r   r   r   rv  rm   rk   )r   rZ  r   r   r  r   r|  r   r   r   r   r   )r&   r   r   r]  r,  r   r   r   r-  rs   tradesArrayr   s               r)   handle_tradezbinance.handle_trade	  s     **//),r1 &VZ
##GS1!!(D$
C!&(##GV4oodkk6:%%dllM4HE$U+K5!)F{K0r*   1mc                    K   | j                          d{    | j                  |      }|d   }d|d<   | j                  ||gg|||       d{   }||   |   S 7 F7 w)aG  
        watches historical candlestick data containing the open, high, low, and close price, and the volume of a market

        https://developers.binance.com/docs/binance-spot-api-docs/websocket-api/market-data-requests#klines
        https://developers.binance.com/docs/derivatives/coin-margined-futures/websocket-market-streams/Kline-Candlestick-Streams
        https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Kline-Candlestick-Streams

        :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 dict [params.timezone]: if provided, kline intervals are interpreted in that timezone instead of UTC, example '+08:00'
        :returns int[][]: A list of candles ordered, open, high, low, close, volume
        Nr   r4   ru  )r   r   watch_ohlcv_for_symbols)r&   r   	timeframer   r   r   r   r0  s           r)   watch_ohlcvzbinance.watch_ohlcv  s{       !!!V$!%1!"33fi5H4I5RWY_``f~i(( 	" as!   A!A9A!AA!A!symbolsAndTimeframesc                   K   | j                          d{    d}| j                  |ddd      \  }}| j                  |d      }| j                  |dddd      }| j	                  |d         }|d   }	|d	   r	|d
   rdnd}	|	dk(  }
d}| j                  |dd      \  }}|duxr |dk(  xs t        j                  |d      }g }g }t        dt        |            D ]  }||   }|d   }|d   }| j                  | j                  ||      }| j	                  |      }|d   }|dk(  r|j                  dd      }|xr |
}d}|r|nd}|j                  |dz   |z   dz   |z   |z          |j                  d|d   z   dz   |z           | j                  d   d   |	   dz   | j                  |	d      z   }| j!                  |      }d ||d!}d"|i}| j#                  |d#      }| j%                  ||| j'                  ||      ||       d{   \  }}}| j(                  r|j+                  ||      }| j-                  |||dd      } | j/                  |||       S 7 =7 Qw)$a`  
        watches historical candlestick data containing the open, high, low, and close price, and the volume of a market

        https://developers.binance.com/docs/binance-spot-api-docs/websocket-api/market-data-requests#klines
        https://developers.binance.com/docs/derivatives/coin-margined-futures/websocket-market-streams/Kline-Candlestick-Streams
        https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Kline-Candlestick-Streams

        :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
        :param dict [params.timezone]: if provided, kline intervals are interpreted in that timezone instead of UTC, example '+08:00'
        :returns int[][]: A list of candles ordered, open, high, low, close, volume
        Nchannelrr   ru   r   FTr   r   r
  rW   rX   rV   timezone+08:008r   r   indexPriceKline_perpr   @+08:00r  _ohlcv::r   r   r]   r,   r   multipleOHLCVr   r   r   ru  )r   handle_param_string_2get_list_from_object_valuesr   r   handle_param_stringr   	string_eqr   r   r   
timeframesreplacer   r   r   r   r'  r   r   r   rw  rx  create_ohlcv_object)!r&   r  r   r   r   	klineTyper   marketSymbolsr   r   r]  r  isUtc8	rawHashesr   r   symAndTfsymbolStringtimeframeStringintervalr   r   shouldUseUTC8suffix	utcSuffixr   rp   r   r   r   r  candlesfiltereds!                                    r)   r  zbinance.watch_ohlcv_for_symbols2  s     !!!	 66vy&RYZ	6223GK++GT5%Nkk-"236"z"*848*D&.33FJM&$&hh(.B-gwGXGXYacfGg	q#234 	XA+A.H#A;L&qkO''/ZH[[.Fm,H--#++GR8#.MF"/RIX^i7#=H9TU  VH-=!=!D!VW	X iit$T*S04;;t_3UUOOC(	!
 )
	 6#56+/+>+>sMSWS^S^_fhnSoq~  AJ  ,K  &K"	7??$$VU3E--gueQM''	8DD[ 	"R &Ks#   IIG-IIAIIc                 |  K   | j                          d{    d}| j                  |ddd      \  }}| j                  |d      }| j                  |dddd      }| j	                  |d         }|d   }|d	   r	|d
   rdnd}|dk(  }d}	| j                  |dd      \  }	}|	duxr |	dk(  xs t        j                  |	d      }
g }g }g }t        dt        |            D ]  }||   }|d   }|d   }| j                  | j                  ||      }| j	                  |      }|d   }|dk(  r|j                  dd      }|
xr |}d}|r|nd}|j                  |dz   |z   dz   |z   |z          |j                  d|d   z   dz   |z          |j                  d|d   z   dz   |z           | j                  d   d   |   dz   | j                  |d       z   }| j!                  |      }d!||d"}dt#        |      ||||d#d$}| j%                  |d%      }| j'                  ||| j)                  ||      ||       d{   S 7 !7 w)&a  
        unWatches historical candlestick data containing the open, high, low, and close price, and the volume of a market

        https://developers.binance.com/docs/binance-spot-api-docs/websocket-api/market-data-requests#klines
        https://developers.binance.com/docs/derivatives/coin-margined-futures/websocket-market-streams/Kline-Candlestick-Streams
        https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Kline-Candlestick-Streams

        :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
        :param dict [params.timezone]: if provided, kline intervals are interpreted in that timezone instead of UTC, example '+08:00'
        :returns int[][]: A list of candles ordered, open, high, low, close, volume
        Nr  rr   ru   r   FTr   r   r
  rW   rX   rV   r  r  r  r   r   r  r  r   r  r  r  r  r   r   zunsubscribe::ohlcv::r]   r,   r   r  r  r   ohlcv)r  r   r   r  r  r   r  ru  )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]  r  r  r  r  r   r   r  r  r  r  r   r   r  r  r  r   rp   r   r   s                               r)   un_watch_ohlcv_for_symbolsz"binance.un_watch_ohlcv_for_symbolsp  s     !!!	 66vy&RYZ	6223GK++GT5%Nkk-"236"z"*848*D&.33FJM&$&hh(.B-gwGXGXYacfGg	q#234 	eA+A.H#A;L&qkO''/ZH[[.Fm,H--#++GR8#.MF"/RIX^i7#=H9TU##Ix0@$@4$G/$YZ  !7&:J!JT!QTc!cd	e  iit$T*S04;;t_3UUOOC(	#
  i.$8 0*
	 6#56((mT[[RX=Y[hjstttc 	"b us"   H<H7HH<2H:3H<:H<c                    K   | j                          d{    | j                  |      }|d   }d|d<   | j                  ||gg|       d{   S 7 <7 w)a  
        unWatches historical candlestick data containing the open, high, low, and close price, and the volume of a market

        https://developers.binance.com/docs/binance-spot-api-docs/websocket-api/market-data-requests#klines
        https://developers.binance.com/docs/derivatives/coin-margined-futures/websocket-market-streams/Kline-Candlestick-Streams
        https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Kline-Candlestick-Streams

        :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
        :param dict [params.timezone]: if provided, kline intervals are interpreted in that timezone instead of UTC, example '+08:00'
        :returns int[][]: A list of candles ordered, open, high, low, close, volume
        Nr   r4   ru  )r   r   r  )r&   r   r  r   r   s        r)   un_watch_ohlcvzbinance.un_watch_ohlcv  sg      !!!V$!%1!"44vy6I5JFSSS	 	" Ts!   AA7AAAAc           	         | j                  |d      }ddd}| j                  |||      }| j                  |d      }| j                  |dd      }|dk(  r| j                  |d      }| j                  |d      }| j                  |      }| j	                  |d	      | j                  |d
      | j                  |d      | j                  |d      | j                  |d      | j                  |d      g}	|j                  j                  d      dkD  }
|
rdnd}| j                  |d d |      }d|z   dz   |z   }| j                  | j                  |i       | j                  |<   | j                  | j                  |   |      }|:| j	                  | j                  dd      }t        |      }|| j                  |   |<   |j                  |	       |||g}|j                  ||       y )NrE  r  markPriceKline)indexPrice_klinemarkPrice_klinekr   r  r   r  r   hr   cvrX  rg   rV   r   r  r   ro   rk   )r   r   r  find_timeframer   rH  r   rZ  r   ohlcvsr   r   r   r   )r&   r   r   eventeventMapru   r   r  unifiedTimeframeparsedr]  r,  r   r-  storedr   resolveDatas                    r)   handle_ohlcvzbinance.handle_ohlcv  s   6   #. 1/
   5%8-%%eS$7%%''6H##E3/..x8eS)OOE3'OOE3'OOE3'OOE3'OOE3'
 **//),r1 &VZ
!!(D$
C&(4/2BB"oodkk62FFV!46FG>%%dllL$GE*51F4:DKK 01f/8{K0r*   c                 r  K   | j                          d{    | j                  |      }d|d   i}| j                  d||      }|dk7  rt        | j                  dz         | j
                  d   d   d	   |   }| j                  |      }t        |      }d
| j                  i}	d}
| j                  |ddd      \  }
}|
|d<   | j                  |d      }d}| j                  |dd
d      \  }}||| j                  | j                  ||            d}| j                  |||||	       d{   }|S 7 7 	w)a?  
        fetches a price ticker, a statistical calculation with the information calculated over the past 24 hours for a specific market
        :param str symbol: unified symbol of the market to fetch the ticker for
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :param str [params.method]: method to use can be ticker.price or ticker.book
        :param boolean [params.returnRateLimits]: return the rate limits for the exchange
        :returns dict: a `ticker structure <https://docs.ccxt.com/#/?id=ticker-structure>`
        Nr   r   rR   rW   z) fetchTickerWs only supports swap marketsr]   r,   rZ   r   Fr`   r\   zticker.bookr$  )r   r   r%  r   r   r   r   r  handle_ticker_wsr&  r'  r(  r   r*  )r&   r   r   r   r+  r   r   rp   r-  r  r`   r   r   rt   s                 r)   fetch_ticker_wszbinance.fetch_ticker_ws  se     !!!V$fTl
 ##OVVD8TWW'RRSSiit$X.t4OOC(	)nd++
 !#'#@#@Zlns#t &&6"#66*66vPXZgh&&t{{7F'CD

 zz#{G[,WW5 	"2 Xs"   D7D2DD7+D5,D75D7c                   K   | j                          d{    | j                  |      }| j                  d||      }|dk7  r|dk7  rt        | j                  dz         | j
                  d   d   d   |   }| j                  |      }	t        |	      }
d	}| j                  |dd
d	      \  }}| j                  |      || j                  |   d}| j                  |d      }| j                  |d      }|||d<   |||d<   |||d<   |
d| j                  ||      d}d| j                  i}| j                  ||
||
|       d{   S 7 47 w)a`  
        query historical candlestick data containing the open, high, low, and close price, and the volume of a market

        https://developers.binance.com/docs/binance-spot-api-docs/websocket-api/market-data-requests#klines

        :param str symbol: unified symbol of the market to query 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 int params['until']: timestamp in ms of the earliest candle to fetch

 EXCHANGE SPECIFIC PARAMETERS
        :param str params['timeZone']: default=0(UTC)
        :returns int[][]: A list of candles ordered, open, high, low, close, volume
        NrJ   rV   rW   z0 fetchOHLCVWs only supports spot or swap marketsr]   r,   rZ   Fr`   )r   r`   r  until	startTimer   endTimeklinesr$  r   )r   r   r%  r   r   r   r   r  r&  	market_idr  r   r'  r   handle_fetch_ohlcvr*  )r&   r   r  r   r   r   r   r,  r   rp   r-  r`   r+  r  r   r  s                   r)   fetch_ohlcv_wszbinance.fetch_ohlcv_ws(  s    " !!!V$)).&&I
J($:TWW'YYZZiit$X.z:OOC(	)n #'#@#@Ykmr#s &nnV, 0	2

 !!&'267+#(GK $GG!&GIkk'62
 d--
 ZZ[';UUU? 	"> Vs"   EE
D.EEEEc                     | j                  |d      }| j                  |      }| j                  |d      }|j                  ||       y )Nr0  r   )rl  parse_ohlcvsr   r   )r&   r   r   r0  r  r-  s         r)   r  zbinance.handle_fetch_ohlcvZ  sE    @ 2""6*&&w5v{+r*   c                    K   | j                          d{    | j                  |      }| j                  |g| j                  |ddi             d{   }||   S 7 G7 w)a  
        watches a price ticker, a statistical calculation with the information calculated over the past 24 hours for a specific market

        https://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams#individual-symbol-mini-ticker-stream
        https://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams#all-market-mini-tickers-stream
        https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Individual-Symbol-Ticker-Streams
        https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/All-Market-Mini-Tickers-Stream
        https://developers.binance.com/docs/derivatives/coin-margined-futures/websocket-market-streams/All-Market-Mini-Tickers-Stream
        https://developers.binance.com/docs/derivatives/coin-margined-futures/websocket-market-streams/Individual-Symbol-Ticker-Streams

        :param str symbol: unified symbol of the market to fetch the ticker for
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :param str [params.name]: stream to use can be ticker or miniTicker
        :returns dict: a `ticker structure <https://docs.ccxt.com/#/?id=ticker-structure>`
        Nru  r;   )r   r   watch_tickersr   r&   r   r   tickerss       r)   watch_tickerzbinance.watch_ticker  sd       !!!V$**F8T[[J\^kIl5mnnv 	"n!   A"A=A"A 
A" A"c                    K   | j                          d{    | j                  |      }| j                  |g| j                  |ddi             d{   }||   S 7 G7 w)aK  
        watches a mark price for a specific market

        https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Mark-Price-Stream

        :param str symbol: unified symbol of the market to fetch the ticker for
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :param boolean [params.use1sFreq]: *default is True* if set to True, the mark price will be updated every second, otherwise every 3 seconds
        :returns dict: a `ticker structure <https://docs.ccxt.com/#/?id=ticker-structure>`
        Nru  r>   )r   r   watch_mark_pricesr   r  s       r)   watch_mark_pricezbinance.watch_mark_price  sd      !!!V$..xVN`brMs9tuuv 	"ur  c                    K   d}| j                  |ddd      \  }}| j                  d|||       d{   }| j                  r|S | j                  | j                  d|      S 7 /w)aY  
        watches the mark price for all markets

        https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Mark-Price-Stream-for-All-market

        :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.use1sFreq]: *default is True* if set to True, the mark price will be updated every second, otherwise every 3 seconds
        :returns dict: a `ticker structure <https://docs.ccxt.com/#/?id=ticker-structure>`
        Nr=   rr   r   r   )r&  watch_multi_ticker_helperr   filter_by_arrayr  r&   r   r   channelName
newTickerss        r)   r  zbinance.watch_mark_prices  su       #;;FDUW]_jkV99:K[Zacijj
??##DLL(GDD ks   1A%A#0A%c                   K   d}| j                  |ddd      \  }}|dk(  rt        | j                  dz         | j                  d|||       d{   }| j                  r|S | j                  | j                  d|      S 7 /w)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://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams#individual-symbol-mini-ticker-stream
        https://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams#all-market-mini-tickers-stream
        https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Individual-Symbol-Ticker-Streams
        https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/All-Market-Mini-Tickers-Stream
        https://developers.binance.com/docs/derivatives/coin-margined-futures/websocket-market-streams/All-Market-Mini-Tickers-Stream
        https://developers.binance.com/docs/derivatives/coin-margined-futures/websocket-market-streams/Individual-Symbol-Ticker-Streams

        :param str[] symbols: unified symbol of the market to fetch the ticker for
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: a `ticker structure <https://docs.ccxt.com/#/?id=ticker-structure>`
        Nr<   rr   rt   r   V deprecation notice - to subscribe for bids-asks, use watch_bids_asks() method insteadr   )r&  r   r   r   r   r  r  r  s        r)   r  zbinance.watch_tickers  s      ";;FNTZ\deV,&TWW'  A  A99.+W^`fgg
??##DLL(GDD hs   ABB 0Bc                 |  K   d}| j                  |ddd      \  }}|dk(  rt        | j                  dz         | j                          d{    d}| j	                  |dddd      }d}d}|du}|r| j                  |d	         }| j                  |||      \  }}d}| j                  |||      \  }}d}	| j                  ||      rd
}	n;| j                  ||      rd}	n&|dk(  r|}	nt        | j                  dz   |z   dz         |dk(  }
g }g }g }|rt        d	t        |            D ]k  }||   }| j                  |      }|j                  |d   dz   |z          |j                  | j                  ||d   |
             |j                  d|z          m n|
r8|dk(  rt        | j                  dz   |z   dz         |j                  d|z          n|j                  d|z   dz          |j                  | j                  |d|
             |j                  d       |}|r|dz   dj!                  |      z   }| j"                  d   d   |	   dz   | j%                  |	|      z   }| j'                  |      }d||d}dt)        |      |||dd}| j+                  ||| j-                  ||      ||       d{   S 7 k7 w)a  
        unWatches a price ticker, a statistical calculation with the information calculated over the past 24 hours for all markets of a specific list

        https://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams#individual-symbol-mini-ticker-stream
        https://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams#all-market-mini-tickers-stream
        https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Individual-Symbol-Ticker-Streams
        https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/All-Market-Mini-Tickers-Stream
        https://developers.binance.com/docs/derivatives/coin-margined-futures/websocket-market-streams/All-Market-Mini-Tickers-Stream
        https://developers.binance.com/docs/derivatives/coin-margined-futures/websocket-market-streams/Individual-Symbol-Ticker-Streams

        :param str[] symbols: unified symbol of the market to fetch the ticker for
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: a `ticker structure <https://docs.ccxt.com/#/?id=ticker-structure>`
        Nr<   rr   rt   r   r  TFr   rW   rX   rV   rY  #() does not support options marketsr   r  r   zunsubscribe:ticker:5() requires symbols for self channel for spot markets!@arrzunsubscribe:tickerr   r   r]   r,   r   r  r   r  )r&  r   r   r   r   r   r   r   r   r   r   r   r   r   get_message_hashr   r   r   r   r   r  r   r   )r&   r   r   r  
methodNamer   r,  symbolsDefinedr   rawMarketTypeisBidAsksubscriptionArgsr  r   r   r   r   r   r   rp   r   r  s                         r)   un_watch_tickerszbinance.un_watch_tickers  s.     ";;FNTZ\deV,&TWW'  A  A!!!#
%%gtT5$G
!-++gaj1K!??
KY_`
F99*kSYZ==W-$M^^J0&M6!&Mtww}z9<aabb</1c'l+ E V, ''}(=(Ck(QR ''(=(=k6RZK[]e(fg$$%:V%CDE '+DGGcMJ,F  JA  -A  B  B ''k(9: ''k(9F(BC##D$9$9+tX$VW  !56 
$t+chhw.??Jiit$]3c9DKKWa<bbOOC(	#&
  i. 0-
 ((.>GU[@\^np|}}}u 	"t ~s%   A
J<J7I%J<2J:3J<:J<c                 D   K   | j                  |g|       d{   S 7 w)a  
        unWatches a price ticker, a statistical calculation with the information calculated over the past 24 hours for all markets of a specific list

        https://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams#individual-symbol-mini-ticker-stream
        https://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams#all-market-mini-tickers-stream
        https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Individual-Symbol-Ticker-Streams
        https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/All-Market-Mini-Tickers-Stream
        https://developers.binance.com/docs/derivatives/coin-margined-futures/websocket-market-streams/All-Market-Mini-Tickers-Stream
        https://developers.binance.com/docs/derivatives/coin-margined-futures/websocket-market-streams/Individual-Symbol-Ticker-Streams

        :param str symbol: unified symbol of the market to fetch the ticker for
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: a `ticker structure <https://docs.ccxt.com/#/?id=ticker-structure>`
        N)r  r   s      r)   un_watch_tickerzbinance.un_watch_ticker!  s#      **F8V<<<<r"  c                    K   | j                          d{    | j                  |dddd      }| j                  dd||       d{   }| j                  r|S | j	                  | j
                  d|      S 7 `7 1w)a  
        watches best bid & ask for symbols

        https://developers.binance.com/docs/binance-spot-api-docs/websocket-api/market-data-requests#symbol-order-book-ticker
        https://developers.binance.com/docs/derivatives/coin-margined-futures/websocket-market-streams/All-Book-Tickers-Stream
        https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/All-Book-Tickers-Stream

        :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>`
        NTFr2   r   r   )r   r   r   r   r  bidsasks)r&   r   r   r0  s       r)   watch_bids_askszbinance.watch_bids_asks2  s{      !!!%%gtT5$G55o|U\^dee??M##DMM8WEE 	"es!   A;A70A;A90A;9A;r  c                 T  K   | j                          d {    | j                  |d ddd      }|dk(  }|dk(  }| j                  |dd      }d }d }	|d u}
|
r| j                  |d         }|rdnd }| j	                  ||||      \  }	}d }| j                  |||      \  }}d }| j                  |	|      rd}n;| j                  |	|      rd	}n&|	d
k(  r|	}nt        | j                  dz   |z   dz         g }g }d}|r|rdnd}|
rst        dt        |            D ]Z  }||   }| j                  |      }|j                  |d   dz   |z   |z          |j                  | j                  ||d   |             \ n|r8|	d
k(  rt        | j                  dz   |z   dz         |j                  d|z          n4|r|j                  d|z   dz   |z          n|j                  d|z   dz          |j                  | j                  |d |             |}|
r|dz   dj                  |      z   }| j                   d   d   |   dz   | j#                  ||      z   }| j%                  |      }d||d}d|i}| j'                  ||| j)                  ||      ||       d {   }|
s|S i }|||d   <   |S 7 7 w)NTFr   r   	use1sFreqr   r|   rW   rX   rV   rY  r  r   z@1sr   r  r   r	  r
  r  r   r   r]   r,   r   r   r   r   )r   r   rf  r   r   r   r   r   r   r   r   r   r   r  r   r   r   r   r   r   r$   )r&   r  r  r   r   r  isMarkPricer  r   r,  r  defaultMarketr   r  r  r   r  r   r   r   r   r   rp   r   r   r0  newDicts                              r)   r   z!binance.watch_multi_ticker_helperE  s*    !!!%%gtT5$G</"k1NN6;=	
!-++gaj1K#.T!??
KY_ano
F99*kSYZ==W-$M^^J0&M6!&Mtww}z9<aabb(UrF1c'l+ e V, ''}(=(Ck(QTZ(Z[$$T%:%:;xHXZb%cd	e '+DGGcMJ,F  JA  -A  B  B ''k(9: ''k(9F(BV(KL ''k(9F(BC  !6!6{D(!ST 
$t+chhw.??Jiit$]3c9DKKWa<bbOOC(	!&
 )
	 **3t?O?OPWY_?`brt}~~ MG(.GF8$%NC 	"r s"   J(J#I7J(J&J(&J(c                 d   | j                  |dd      }| j                  |d d |      }| j                  |dd      }|dk(  rd}|dk(  rg| j                  || j	                  |d      | j                  | j	                  |d            || j                  |d	      | j                  |d
      d      S d }|dk(  r| j                  |dd      }n| j                  |g d      }| j                  |d d |      }| j                  |dd      }| j                  i d|d|d| j                  |      d| j                  |d      d| j                  |d      d| j                  |dd      d| j                  |dd      d| j                  |dd      d| j                  |d d!      d"| j                  |d#      d$| j                  |d%      d&|d'|d(| j                  |d)      d*| j                  |d	      d+| j                  |d,      d-d | j                  |d.      | j                  |d/      |d0|      S )1Nr   r   rE  r   r   rt   r   rV  r  r   )r   r   r   r   r   
indexPricetime)CrV  r  r  r   r   r   highr  lowr   bidrU  bidPrice	bidVolumeBbidQtyaskrS  askPrice	askVolumeAaskQtyvwapwopenr   closelastpreviousCloser  change
percentagePaverager  r  )
baseVolumequoteVolumer   )	r  r   r   safe_tickerr   r   r5  safe_integer_nr   )	r&   r   r,  r   r   r  r   r   r1  s	            r)   parse_ws_tickerzbinance.parse_ws_ticker  s   J %%gsH=!!(D$
C  #|<L E%%## !..w< LL):):7C)HI!--gs;"..w<%   	L ++GS&AI ++G5GHI!!(D$
C!!'38 !
f!
!
 Y/!
 D$$Wc2	!

 4##GS1!
 4%%gsJ?!
 ++GS(C!
 4%%gsJ?!
 ++GS(C!
 D$$Wc2!
 D$$Wc2!
 T!
 D!
 T--gs;!
 d&&w4!
  $**7C8!!
" t#!
$ **7C8++GS9)!
* + 	r*   c                     | j                  |d      }| j                  |di       }| j                  |d      }|j                  ||       y )Nr   r0  rW   )r   r   r;  r   )r&   r   r   r-  r0  rt   s         r)   r  zbinance.handle_ticker_ws  sH    6 &&w5(B7%%fh7v{+r*   c                 *    | j                  ||d       y )Nbidaskshandle_tickers_and_bids_asksr&   r   r   s      r)   handle_bids_askszbinance.handle_bids_asks  s     	))&'9Er*   c                 *    | j                  ||d       y )Nr  r?  rA  s      r)   handle_tickerszbinance.handle_tickers,  s    < 	))&'9Er*   c                    |j                   j                  d      dkD  }|rdnd}|dk(  }d }g }g }	i }
t        |t              r|}	n|	j	                  |       t        dt        |	            D ]  }|	|   }| j                  |d      }|rd}| j                  | j                  d	   ||      }|A| j                  ||      }|d
   }||
|<   |r|| j                  |<   n|| j                  |<   | j                  |||      }|j	                  |       |j                  ||        t        |      }|dkD  r&| j                  |d |      }|j                  |
|       y y )NrX  rg   rV   r   r>  r   rE  r   r   r   )r   rZ  
isinstancelistr   r   r   r   r   r;  r  r  r  r   )r&   r   r   
methodTyper]  r,  r  r  resolvedMessageHashes
rawTickersr  r   rt   r  parsedTickerr   r-  lengthbatchMessageHashs                      r)   r@  z$binance.handle_tickers_and_bids_asksL  su   **//),r1 &VZ
)+ "

gt$ Jg&q#j/* 	6A]F$$VS1E$**4<<8K+LeUZ[K"//
CL!(+F!-Jv(4f%'3V$//VXNK!((5NN<5#	6& *+A:#44[$QNN:'78 r*   r  c                 D    |rdnd}||dz   |z   dz   |z   S |dz   dz   |z   S )Nbidaskrt   :r  r    )r&   r  r   r  prefixs        r)   r  zbinance.get_message_hashp  s>    %8C<+-3f<<C<#%33r*   c                    | j                          | j                  | j                         | j                  d|      }| j	                  | j
                  d      }|||d<   | j	                  |d      }|||d<   | j                  |      }| j                  |      }d }| j                  j                  d      dkD  rct        | j                        dkD  r| j                  || j                  d      }nv| j                  | j                  |      | j                  d      }nI| j                  | j                  |      | j                  | j                        t        j                         }||d<   |S )	N)r   apiKey
recvWindowzPRIVATE KEYrg   x   sha256ed25519	signature)check_required_credentialsr   r3  rT  r   r   keysort	urlencodesecretrZ  r   rsaeddsaencodehmachashlibrW  )r&   r   extendedParamsdefaultRecvWindowrU  rz  rY  s          r)   r(  zbinance.sign_paramsw  s>   '')kk&
  !--dllLI(#4F< &&v|<
!#-F< n5~.	;;M*R/4;;#% HHUDKKB	 JJt{{5'94;;	R			$++e"4dkk$++6NPWP^P^_I&/{#r*   c                 >  K   | j                         }d }| j                  dd |      \  }}d }| j                  dd |      \  }}d }| j                  |dddd      \  }}| j	                  ||      rd}n| j                  ||      rd}d }| j                  d|      \  }}|dk(  }|dk(  xs |d u }| j                  |d	      }	| j                  |d	      }| j                  | j                  |i       }
| j                  |
d
d      }| j                  | j                  dd      }| j                  |d      }||z
  |kD  rUd }|r.| j                  |       d {   }| j                  |ddi      }n|dk(  r| j                  |       d {   }n|dk(  r| j!                  |       d {   }n|dk(  r|r| j#                  |       d {   }nt|rY|	t%        | j&                  dz         | j)                  |	      }| j                  |d	|i      }| j+                  |       d {   }n| j-                  |       d {   }| j                  |
| j                  |d      |d      | j                  |<   | j/                  || j0                  |       y y 7 >7 7 7 7 y7 aw)Nr  r[   portfolioMarginFrW   rX   isolatedcrossr   lastAuthenticatedTimer   r   r   i'  TrY   zC authenticate() requires a symbol argument for isolated margin moder   r   ri  )millisecondsr   r   handle_option_and_params_2r   r   handle_margin_mode_and_paramsr   r'  r   r   r   r   papiPostListenKeyr   fapiPrivatePostListenKeydapiPrivatePostListenKeysapiPostUserDataStreamr   r   r  sapiPostUserDataStreamIsolatedpublicPostUserDataStreamdelaykeep_alive_listen_key)r&   r   r  r   r   isPortfolioMargin
marginModeisIsolatedMarginisCrossMarginr   r   ri  r   rt  responser   s                   r)   r  zbinance.authenticate  s      "99.$PVWf99.$PVW $($C$CFN\bduw|$}!6==w'D^^D'*D
!??PVW
F&*4#w.GJ$4F!!&(368,//$,,b9 $ 1 1';RTU V#00?UW^_-u5''%/H !%!7!7!??V.?-FG!!%!>!>v!FF#!%!>!>v!FF!m!%!<!<V!DD!>+DGG6{,{||>>&1Vh-AB!%!D!DV!LL!%!>!>v!FF!%W!--hD)-7 "DLL JJ+T-G-GP/ 0 @ GFD MFsn   EJJ2JJJ#J$ JJAJJ J9J:AJJJJJJc                   K   | j                  | j                  ddd      }| j                  |d|      }d }| j                  |dddd      \  }}| j	                  dd |      }|d	   }| j                  ||      rd
}n| j                  ||      rd}| j                  | j                  |i       }| j                  |d      }|y i }| j                  |d      }	| j                  |ddg      }| j                         }
	 |r>| j                  | j                  ||             d {    | j                  |ddi      }n|d
k(  r*| j                  | j                  ||             d {    n|dk(  r*| j                  | j                  ||             d {    nb||d<   |dk(  r/|	|d<   | j                  | j                  ||             d {    n)| j                  | j                  ||             d {    | j                  |||
d      | j                  |<   t'        | j2                  j5                               }| j7                  | j                  dd      }t-        d	t/        |            D ]n  }||   }t'        |j8                  j+                               }t-        d	t/        |            D ],  }||   }||k(  s| j;                  || j<                  |         y  p y 7 7 7 W7 !7 # t         $ r}|}|rd}| j"                  d   d   |   dz   | j                  |   d   z   }| j%                  |      }t'        |j(                  j+                               }t-        d	t/        |            D ]  }||   }|j1                  ||        | j                  |d d	d      | j                  |<   Y d }~y d }~ww xY ww)Nr{   r  rV   r   keepAliveListenKeyr[   rf  Fr   rW   rX   r   r   TrY   r]   r,   r   rj  r   r   )r  r   r   rl  r   r   r   r   r'  rk  papiPutListenKeyr   fapiPrivatePutListenKeydapiPrivatePutListenKeysapiPutUserDataStreampublicPutUserDataStreamr>  r   r   rG  futureskeysr   r   r@  clientsvaluesr   r?  rt  ru  )r&   r   r   rv  subTypeInfor   r   r   r   r   r  errorurlTyper   r   r   r   r-  r  r   subscriptionKeysro  subscribeTypes                          r)   ru  zbinance.keep_alive_listen_key  s    !!$,,~vV5 $($C$CFL`bhj{  ~C  %D!6556JDRXYa.==w'D^^D'*D//$,,b9$$Wk:	!!&(36FH#56  "	 ++DKK,HIIIV.?-FG!224;;w3OPPP#224;;w3OPPP'0$8#(.GH%44T[[&5QRRR66t{{7F7STTT  "[["%)3
 T
 t||**,-#00?UW^_q#g,' 	AQZF#F$8$8$=$=$?@1c"234  0 3 D(JJ3T5O5OQWX		G J QP
 ST 	G  ))E"4(1C7$,,t:L[:YYC[[%F !4!4!67M1c-01 2+A.e[12 "&W!)*7 "DLL 	s   C7N?:&K1  K#!AK1 #K&$.K1 K)8K1 K,)K1 5K/6K1 :CN??$N?#K1 &K1 )K1 ,K1 /K1 1	N<:B8N72N?7N<<N?c                 ,   ||j                   v ry | j                  | j                  d      }| j                  |dd      }|rE|dz   }||j                  vr1|j                  |       | j                  | j                  ||||       y y i | j                  |<   y )Nr-   ry   F:fetchBalanceSnapshot)	r?  r   r   rf  r  rW   rb  load_balance_snapshotbalance)r&   r   r   rv  r   ry   r-  s          r)   set_balance_cachezbinance.set_balance_cache	  s    6'''//$,,?#~~g7MuU!88K6>>1k*

455v{DRcd 2 "$DLLr*   c                 N  K   d|i}|rd|d<   | j                  |       d {   }| j                  || j                  | j                  |i             | j                  |<   |j                  |   }|j                          |j                  | j                  |   |dz          y 7 w)Nr   Trf  :balance)fetch_balancer   r   r  r  r   )r&   r   r-  r   rv  r   rz  rW   s           r)   r  zbinance.load_balance_snapshot	  s     D
 (,F$%++F33![[4??4<<QUWY3Z[T,t||D)4*+<= 4s    B%B#BB%c                 N  K   | j                          d{    | j                  dd|      }|dk7  r"|dk7  r|dk7  rt        | j                  dz         | j                  d   d   d	   |   }| j                  |      }t        |      }d
}| j                  |ddd
      \  }}d|i}d}| j                  |ddd      \  }}||| j                  | j                  ||            d}	d|dk(  r| j                  n| j                  i}
| j                  |||	||
       d{   S 7 
7 w)ao  
        fetch balance and get the amount of funds available for trading or funds locked in orders

        https://developers.binance.com/docs/derivatives/usds-margined-futures/account/websocket-api/Futures-Account-Balance
        https://developers.binance.com/docs/binance-spot-api-docs/websocket-api/account-requests#account-information-user_data
        https://developers.binance.com/docs/derivatives/coin-margined-futures/account/websocket-api

        :param dict [params]: extra parameters specific to the exchange API endpoint
        :param str|None [params.type]: 'future', 'delivery', 'savings', 'funding', or 'spot'
        :param str|None [params.marginMode]: 'cross' or 'isolated', for margin trading, uses self.options.defaultMarginMode if not passed, defaults to None/None/None
        :param str[]|None [params.symbols]: unified market symbols, only used in isolated margin mode
        :param str|None [params.method]: method to use. Can be account.balance, account.status, v2/account.balance or v2/account.status
        :returns dict: a `balance structure <https://docs.ccxt.com/#/?id=balance-structure>`
        NrF   rV   rW   rX   z2 fetchBalanceWs only supports spot or swap marketsr]   r,   rZ   Fr`   r   zaccount.statusr$  )r   r%  r   r   r   r   r  r&  r(  r   handle_account_status_wshandle_balance_wsr*  )r&   r   r   r   rp   r-  r`   r+  r   r   r  s              r)   fetch_balance_wszbinance.fetch_balance_ws	  sV     !!!##$4dFC6>dh.4:3ETWW'[[\\iit$X.t4OOC(	)n #'#@#@IY[mot#u & 0
 66v?OQY[kl&&t{{7F'CD
 BR8Rd33Y]YoYo
 ZZ[';UUU- 	", Vs"   D%D DD%D#D%#D%c                    | j                  |d      }d }t        |d   t              r| j                  |dg       }n&| j	                  |di       }| j                  |dg       }| j                  |      }|j                  ||       y )Nr   r0  assets)r   rF  rG  rl  r   parseBalanceCustomr   )r&   r   r   r-  
rawBalancer0  parsedBalancess          r)   r  zbinance.handle_balance_wsC	  s     &&w5
gh'.2>J ^^GXr:F"=J00<~{3r*   c                     | j                  |d      }| j                  |di       }| j                  |      }|j                  ||       y Nr   r0  )r   r   r  r   )r&   r   r   r-  r0  r  s         r)   r  z binance.handle_account_status_wsR	  sG    ^ &&w526008~{3r*   c                 D   K   | j                  |g|       d{   S 7 w)a  
        fetch data on an open position

        https://developers.binance.com/docs/derivatives/usds-margined-futures/trade/websocket-api/Position-Information

        :param str symbol: unified market symbol of the market the position is held in
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: a `position structure <https://docs.ccxt.com/#/?id=position-structure>`
        N)fetch_positions_wsr   s      r)   fetch_position_wszbinance.fetch_position_ws	  s#      ,,fXv>>>>r"  c                   K   | j                          d{    i }d}| j                  |dddd      }|,t        |      }|dk(  r| j                  |d         }|d   |d<   | j	                  d||      }|d	k7  r|d
k7  rt        | j                  dz         | j                  d   d   d   |   }| j                  |      }t        |      }	d}
| j                  |ddd      \  }
}|
|d<   d}| j                  |ddd      \  }}|	|| j                  | j                  ||            d}d| j                  i}| j                  ||	||	|       d{   }| j                  |d|d      S 7 P7 w)a  
        fetch all open positions

        https://developers.binance.com/docs/derivatives/usds-margined-futures/trade/websocket-api/Position-Information
        https://developers.binance.com/docs/derivatives/coin-margined-futures/trade/websocket-api/Position-Information

        :param str[] [symbols]: list of unified market symbols
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :param boolean [params.returnRateLimits]: set to True to return rate limit informations, defaults to False.
        :param str|None [params.method]: method to use. Can be account.position or v2/account.position
        :returns dict[]: a list of `position structure <https://docs.ccxt.com/#/?id=position-structure>`
        Nr|   Tr   r   r   r   rQ   rW   rX   z, fetchPositionsWs only supports swap marketsr]   r,   rZ   Fr`   r   zaccount.positionr$  )r   r   r   r   r%  r   r   r   r   r  r&  r(  r   handle_positions_wsr*  filter_by_array_positions)r&   r   r   r+  r   r  r   r   rp   r-  r`   r   r   r  r0  s                  r)   r  zbinance.fetch_positions_ws	  s     !!!%%gvtT4HLM!WQZ0$*4L!##$6G8
 2TWW'UUVViit$X.t4OOC(	)n #'#@#@I[]oqv#w &&6"#66v?QS[]op&&t{{7F'CD
 d..
 zz#{G[,WW--fhOO; 	"8 Xs"   E+E&D6E+E)E+)E+c                 <   | j                  |d      }| j                  |dg       }g }t        dt        |            D ]K  }| j	                  ||         }| j                  |d      }|dk7  s/|dk7  s5|dk7  s;|j                  |       M |j                  ||       y )Nr   r0  r   
entryPrice0z0.0z
0.00000000)r   rl  r   r   parse_position_riskr   r   )	r&   r   r   r-  r0  	positionsr   r  r  s	            r)   r  zbinance.handle_positions_ws	  s    > &&w526	q#f+& 	)A--fQi8F))&,?Jc!
e(;*P\B\  (		)
 	y+.r*   c                 r  K   | j                          d{    | j                  |       d{    | j                  | j                  dd      }| j                  |d|      }d}| j	                  dd|      \  }}d}| j                  |dddd      \  }}| j                  ||      rd	}n| j                  ||      rd
}|}|rd}| j                  d   d   |   dz   | j                  |   d   z   }| j                  |      }| j                  |||       | j                  ||d|       | j                  | j                  d      }	| j                  |	dd      }
| j                  |	dd      }|
r|r|j                  |dz          d{    |dz   }d}| j                  ||||       d{   S 7 7 7 -7 w)a  
        watch balance and get the amount of funds available for trading or funds locked in orders
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :param boolean [params.portfolioMargin]: set to True if you would like to watch the balance of a portfolio margin account
        :returns dict: a `balance structure <https://docs.ccxt.com/#/?id=balance-structure>`
        Nr{   rV   r   r-   r[   rf  FrW   rX   r]   r,   r   r   ry   rz   Tr  r  )r   r  r   r   r   rl  r   r   r   r   r  set_positions_cacher   rf  rW   r*  )r&   r   r{   r   r   rv  r  r   r   r   ry   rz   r-  r   s                 r)   watch_balancezbinance.watch_balance	  s     !!!'''&&t||]FK<99.$PVW $($C$CFN\bduw|$}!6==w'D^^D'*DGiit$W-3dll46H6UUS!vt->?  t5FG..~>#~~g7MuU#~~g7MtT$8--'> >???Z'ZZ['4@@@5 	"', @ AsD   F7F-F7F0EF7F3"F7(F5)F70F73F75F7c                 P   | j                  | j                  dd      }t        |j                  j	                               }|d   }|dz   }| j
                  |   i | j
                  |<   || j
                  |   d<   | j                  |d      }|dk(  r| j                  |d      }| j                  |      }	| j                         }
| j                  |d	      }|	| j
                  |   v rP| j
                  |   |	   d
   }t        |t              s| j                  |      }t        j                  ||      |
d
<   n||
d
<   |
| j
                  |   |	<   n| j                  |d|      }| j                  |d      }t        dt!        |            D ]  }||   }| j                  |d      }| j                  |      }	| j                         }
| j                  |d      |
d
<   | j                  |d      |
d<   | j                  ||      |
d<   |
| j
                  |   |	<    | j#                  |d      }|| j
                  |   d<   | j%                  |      | j
                  |   d<   | j'                  | j
                  |         | j
                  |<   |j)                  | j
                  |   |       y )Nr   r   r   r  r   rE  balanceUpdaterS  dfreer&  fr   usedtotalrV  r   r   )r   r   rG  r?  r  r  r  accountrF  r  r   r   
string_addr   rl  r   r   r   r   safe_balancer   )r&   r   r   r   r?  accountTyper-  r  
currencyIdcoder  rK  r   r&  r   entryr   s                    r)   handle_balancezbinance.handle_balance

  s   t !!$,,$? V116689#A&!J.<<$,(*DLL%,3[!&)  #.O#))'37J**:6DllnG$$Wc2Et||K00 $[ 9$ ? G!-5$($9$9-$HM")"4"4]E"J"'.5DLL%d+nnWc7;Gw,A1c!f% :!!--eS9
..z:,,."&"2"25#">"&"2"25#">#'#3#3E6#B 29[)$/: %%gs3	1:[!+.04Y0G[!*-$($5$5dll;6O$P[!t||K0+>r*   c                     d }| j                  |||      \  }}d }| j                  |||      \  }}| j                  ||      rd}|S | j                  ||      rd}|S )NrW   rX   )r   r   r   r   )r&   r   r   r   r   r   s         r)   r%  zbinance.get_market_typem
  sp    99&&&Qf99&&&Q==w'D  ^^D'*Dr*   r  rL  r   c                    K   | j                          d{    | j                  |      }| j                  d||      }|dk7  r"|dk7  r|dk7  rt        | j                  dz         | j
                  d   d   d	   |   }	| j                  |	      }
t        |
      }| j                  |d
dd      }| j                  |d
d      }| j                  ||||||      }d}| j                  |ddd      \  }}||d<   | j                  |dd      }| j                  |d      }|d| j                  | j                  ||            d}|r|rd|d<   nd|d<   d| j                  i}| j!                  |	||||       d{   S 7 c7 w)a[  
        create a trade order

        https://developers.binance.com/docs/binance-spot-api-docs/websocket-api/trading-requests#place-new-order-trade
        https://developers.binance.com/docs/derivatives/usds-margined-futures/trade/websocket-api/New-Order
        https://developers.binance.com/docs/derivatives/coin-margined-futures/trade/websocket-api

        :param str symbol: unified symbol of the market to create an order in
        :param str type: 'market' or 'limit'
        :param str side: 'buy' or 'sell'
        :param float amount: how much of currency you want to trade in units of base currency
        :param float|None [price]: the price at which the order is to be fulfilled, in units of the quote currency, ignored in market orders
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :param boolean params['test']: test order, default False
        :param boolean params['returnRateLimits']: set to True to return rate limit information, default False
        :returns dict: an `order structure <https://docs.ccxt.com/#/?id=order-structure>`
        NrA   rV   rW   rX   z1 createOrderWs only supports spot or swap marketsr]   r,   rZ   sorSORFr`   r\   zorder.placer$  zsor.order.testr   z
order.test)r   r   r%  r   r   r   r   r  safe_bool_2r'  create_order_requestr&  rf  r(  r   handle_order_wsr*  )r&   r   r   r  rL  r   r   r   r,  r   rp   r-  r  r+  r`   r\   r   r  s                     r)   create_order_wszbinance.create_order_wsx
  s    $ !!!V$))/66J
J($:zZ?WTWW'ZZ[[iit$X.z:OOC(	)nvueU;65%0++FD$vV #'#@#@Zlns#t &&6"#~~ffe466*#&&t{{7F'CD

 $4!$0!d**
 ZZ[';UUU; 	": Vs"   E>E9EE>4E<5E><E>c                     | j                  |d      }| j                  |di       }| j                  |      }|j                  ||       y r  r   r   parse_orderr   )r&   r   r   r-  r0  r  s         r)   r  zbinance.handle_order_ws
  sG    ` &&w526  (uk*r*   c                     | j                  |d      }| j                  |dg       }| j                  |      }|j                  ||       y r  )r   rl  parse_ordersr   )r&   r   r   r-  r0  orderss         r)   handle_orders_wszbinance.handle_orders_ws
  sG    L &&w526""6*v{+r*   r   c           	        K   | j                          d{    | j                  |      }| j                  d||      }	|	dk7  r"|	dk7  r|	dk7  rt        | j                  dz         | j
                  d   d   d	   |	   }
| j                  |
      }t        |      }|	dk(  xs |	dk(  }d}|	dk(  r| j                  |||||||      }n|r| j                  |||||||      }d
}| j                  |ddd
      \  }}||d<   ||rdnd| j                  | j                  ||            d}d| j                  i}| j                  |
||||       d{   S 7 :7 w)a  
        edit a trade order

        https://developers.binance.com/docs/binance-spot-api-docs/websocket-api/trading-requests#cancel-and-replace-order-trade
        https://developers.binance.com/docs/derivatives/usds-margined-futures/trade/websocket-api/Modify-Order
        https://developers.binance.com/docs/derivatives/coin-margined-futures/trade/websocket-api/Modify-Order

        :param str id: order id
        :param str symbol: unified symbol of the market to create an order in
        :param str type: 'market' or 'limit'
        :param str side: 'buy' or 'sell'
        :param float amount: how much of the currency you want to trade in units of the base currency
        :param float|None [price]: the price at which the order is to be fulfilled, in units of the quote currency, ignored in market orders
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: an `order structure <https://docs.ccxt.com/#/?id=order-structure>`
        NrB   rV   rW   rX   z/ editOrderWs only supports spot or swap marketsr]   r,   rZ   Fr`   zorder.modifyzorder.cancelReplacer$  r   )r   r   r%  r   r   r   r   r  editSpotOrderRequesteditContractOrderRequestr&  r(  r   handle_edit_order_wsr*  )r&   r   r   r   r  rL  r   r   r   r,  r   rp   r-  isSwapr+  r`   r   r  s                     r)   edit_order_wszbinance.edit_order_ws	  s    " !!!V$))-H
J($:zZ?WTWW'XXYYiit$X.z:OOC(	)n(DJ*,D//FD$PUW]^G33BdFTY[abG #'#@#@Xjlq#r &&6"#)/n6K&&t{{7F'CD
 d//
 ZZ[';UUU3 	"2 Vs"   EED4EEEEc                     | j                  |d      }| j                  |di       }| j                  |d      }d }|| j                  |      }n| j                  |      }|j                  ||       y )Nr   r0  newOrderResponser  )r&   r   r   r-  r0  newSpotOrderr  s          r)   r  zbinance.handle_edit_order_ws5  ss    F &&w526~~f.@A#$$\2E$$V,Euk*r*   c                   K   | j                          d{    |t        | j                  dz         | j                  |      }| j	                  d||      }| j
                  d   d   d   |   }| j                  |      }t        |      }d}	| j                  |ddd      \  }	}| j                  |      |	d	}
| j                  |d
d      }|||
d
<   n| j                  |      |
d<   | j                  |d
dg      }|d| j                  | j                  |
|            d}d| j                  i}| j!                  |||||       d{   S 7 77 w)ak  
        cancel multiple orders

        https://developers.binance.com/docs/binance-spot-api-docs/websocket-api/trading-requests#cancel-order-trade
        https://developers.binance.com/docs/derivatives/usds-margined-futures/trade/websocket-api/Cancel-Order
        https://developers.binance.com/docs/derivatives/coin-margined-futures/trade/websocket-api/Cancel-Order

        :param str id: order id
        :param str [symbol]: unified market symbol, default is None
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :param str|None [params.cancelRestrictions]: Supported values: ONLY_NEW - Cancel will succeed if the order status is NEW. ONLY_PARTIALLY_FILLED - Cancel will succeed if order status is PARTIALLY_FILLED.
        :returns dict: an list of `order structures <https://docs.ccxt.com/#/?id=order-structure>`
        N  cancelOrderWs requires a symbolrC   r]   r,   rZ   Fr`   r   r`   origClientOrderIdclientOrderIdr  order.cancelr$  r   )r   r   r   r   r%  r   r   r  r&  r  r  parse_to_intr'  r(  r   r  r*  r&   r   r   r   r   r   r   rp   r-  r`   r+  r  r   r  s                 r)   cancel_order_wszbinance.cancel_order_ws  sy     !!!>TWW'IIJJV$##OVVDiit$X.t4OOC(	)n #'#@#@Zlns#t &nnV, 0
 **63FX$+8G'(!%!2!22!6GI6$7#IJ$&&t{{7F'CD
 d**
 ZZ[';UUU9 	"8 Vs"   EED1EE	EEc                 0  K   | j                          d{    | j                  |      }| j                  d||      }|dk7  r|dk7  rt        | j                  dz         | j
                  d   d   d   |   }| j                  |      }t        |      }d	}| j                  |dd
d	      \  }}| j                  |      |d}	|d| j                  | j                  |	|            d}
d| j                  i}| j                  |||
||       d{   S 7 7 w)a  
        cancel all open orders in a market

        https://developers.binance.com/docs/binance-spot-api-docs/websocket-api/trading-requests#cancel-open-orders-trade

        :param str [symbol]: unified market symbol of the market to cancel orders in
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict[]: a list of `order structures <https://docs.ccxt.com/#/?id=order-structure>`
        NrE   rV   rW   z5 cancelAllOrdersWs only supports spot or swap marketsr]   r,   rZ   Fr`   r  r  r$  r   )r   r   r%  r   r   r   r   r  r&  r  r(  r   r  r*  )r&   r   r   r   r   r   rp   r-  r`   r+  r   r  s               r)   cancel_all_orders_wszbinance.cancel_all_orders_ws  s3     !!!V$##$7H6>dh.TWW'^^__iit$X.t4OOC(	)n #'#@#@I\^prw#x &nnV, 0

 $&&t{{7F'CD
 d++
 ZZ[';UUU- 	", Vs"   DDC6DDDDc                   K   | j                          d{    |t        | j                  dz         | j                  |      }| j	                  d||      }|dk7  r"|dk7  r|dk7  rt        | j                  dz         | j
                  d   d	   d
   |   }| j                  |      }t        |      }d}	| j                  |ddd      \  }	}| j                  |      |	d}
| j                  |dd      }|||
d<   n| j                  |      |
d<   |d| j                  | j                  |
|            d}d| j                  i}| j                  |||||       d{   S 7 J7 w)a  
        fetches information on an order made by the user

        https://developers.binance.com/docs/binance-spot-api-docs/websocket-api/trading-requests#query-order-user_data
        https://developers.binance.com/docs/derivatives/usds-margined-futures/trade/websocket-api/Query-Order
        https://developers.binance.com/docs/derivatives/coin-margined-futures/trade/websocket-api/Query-Order

        :param str id: order id
        :param str [symbol]: unified symbol of the market the order was made in
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: An `order structure <https://docs.ccxt.com/#/?id=order-structure>`
        Nr  rM   rV   rW   rX   z0 fetchOrderWs only supports spot or swap marketsr]   r,   rZ   Fr`   r  r  r  r  zorder.statusr$  r   )r   r   r   r   r%  r   r   r  r&  r  r  r  r(  r   r  r*  r  s                 r)   fetch_order_wszbinance.fetch_order_ws  s     !!!>TWW'IIJJV$##NFFC6>dh.4:3ETWW'YYZZiit$X.t4OOC(	)n #'#@#@Ykmr#s &nnV, 0
 **63FX$+8G'(!%!2!22!6GI$&&t{{7F'CD
 d**
 ZZ[';UUU; 	": Vs"   E%E EE%E#E%#E%c                   K   | j                          d{    |t        | j                  dz         | j                  |      }| j	                  d||      }|dk7  rt        | j                  dz         | j
                  d   d   d   |   }| j                  |      }t        |      }	d	}
| j                  |d
dd	      \  }
}| j                  |      |
d}|	d| j                  | j                  ||            d}d| j                  i}| j                  ||	||	|       d{   }| j                  ||||      S 7 %7 w)a  
        fetches information on multiple orders made by the user

        https://developers.binance.com/docs/binance-spot-api-docs/websocket-api/trading-requests#order-lists

        :param str symbol: unified market symbol of the market orders were made in
        :param int|None [since]: the earliest time in ms to fetch orders for
        :param int|None [limit]: the maximum number of order structures to retrieve
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :param int [params.orderId]: order id to begin at
        :param int [params.startTime]: earliest time in ms to retrieve orders for
        :param int [params.endTime]: latest time in ms to retrieve orders for
        :param int [params.limit]: the maximum number of order structures to retrieve
        :returns dict[]: a list of `order structures <https://docs.ccxt.com/#/?id=order-structure>`
        Nz  fetchOrdersWs requires a symbolrN   rV   z) fetchOrdersWs only supports spot marketsr]   r,   rZ   FrM   r`   r  	allOrdersr$  r   )r   r   r   r   r%  r   r   r  r&  r  r(  r   r  r*  filter_by_symbol_since_limitr&   r   r   r   r   r   r   r   rp   r-  r`   r+  r   r  r  s                  r)   fetch_orders_wszbinance.fetch_orders_ws  sZ      !!!>TWW'IIJJV$##OVVD6>TWW'RRSSiit$X.t4OOC(	)n #'#@#@Ykmr#s &nnV, 0

 !&&t{{7F'CD
 d++
 zz#{G[,WW00NN3 	"0 Xs"   E D;DE "D>#E >E c                    K   | j                  ||||       d{   }g }t        dt        |            D ]!  }||   }|d   dk(  s|j                  |       # |S 7 Aw)a   
        fetch closed orders

        https://developers.binance.com/docs/binance-spot-api-docs/websocket-api/trading-requests#order-lists

        :param str symbol: unified market symbol
        :param int [since]: the earliest time in ms to fetch open orders for
        :param int [limit]: the maximum number of open orders structures to retrieve
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict[]: a list of `order structures <https://docs.ccxt.com/#/?id=order-structure>`
        Nr   statusclosed)r  r   r   r   )	r&   r   r   r   r   r  closedOrdersr   r  s	            r)   fetch_closed_orders_wszbinance.fetch_closed_orders_wsG  sq      ++FE5&IIq#f+& 	+A1IEX(*##E*	+  Js   AA+AAc                 f  K   | j                          d{    | j                  |      }| j                  d||      }|dk7  r|dk7  rt        | j                  dz         | j
                  d   d   d   |   }| j                  |      }t        |      }	d	}
| j                  |d
dd	      \  }
}d|
i}|| j                  |      |d<   |	d| j                  | j                  ||            d}d| j                  i}| j                  ||	||	|       d{   }| j                  ||||      S 7 7 w)aQ  
        fetch all unfilled currently open orders

        https://developers.binance.com/docs/binance-spot-api-docs/websocket-api/trading-requests#current-open-orders-user_data

        :param str symbol: unified market symbol
        :param int|None [since]: the earliest time in ms to fetch open orders for
        :param int|None [limit]: the maximum number of open orders 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>`
        NrL   rV   rW   z5 fetchOpenOrdersWs only supports spot or swap marketsr]   r,   rZ   FrM   r`   r   zopenOrders.statusr$  r   )r   r   r%  r   r   r   r   r  r&  r  r(  r   r  r*  r  r  s                  r)   fetch_open_orders_wszbinance.fetch_open_orders_ws[  sX     !!!V$##$7H6>dh.TWW'^^__iit$X.t4OOC(	)n #'#@#@Ykmr#s & 0
  $v 6GH)&&t{{7F'CD
 d++
 zz#{G[,WW00NN1 	". Xs"   D1D,C<D1D/D1/D1c                   K   | j                          d{    d}d}|| j                  |      }|d   }|d|z   z  }d}| j                  d||      \  }}d}| j                  d||      \  }}| j	                  ||      rd}n| j                  ||      rd}| j                  |||d      }| j                  |       d{    d}	| j                  d|      \  }	}|}
|d	k(  s|d
k(  r|	d
}
d}| j                  |dddd      \  }}|rd}
| j                  d   d   |
   dz   | j                  |   d   z   }| j                  |      }| j                  |||       | j                  ||d|       d}| j                  ||||       d{   }| j                   r|j#                  ||      }| j%                  ||||d      S 7 7 7 =w)a  
        watches information on multiple orders made by the user

        https://developers.binance.com/docs/binance-spot-api-docs/user-data-stream#order-update
        https://developers.binance.com/docs/margin_trading/trade-data-stream/Event-Order-Update
        https://developers.binance.com/docs/derivatives/usds-margined-futures/user-data-streams/Event-Order-Update

        :param str symbol: unified market symbol of the market the 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 str|None [params.marginMode]: 'cross' or 'isolated', for spot margin
        :param boolean [params.portfolioMargin]: set to True if you would like to watch portfolio margin account orders
        :returns dict[]: a list of `order structures <https://docs.ccxt.com/#/?id=order-structure>`
        Nr  r   rP  r8   rW   rX   r   r   rY   rV   r[   rf  Fr]   r,   r   r   T)r   r   r   r   r   r   r   r  rm  rl  r   r   r   r  r  r*  r   rw  r  )r&   r   r   r   r   r-  r   r   r   rw  r  rv  r   r   r   r  s                   r)   watch_orderszbinance.watch_orders  s      !!![[(FH%F3<'K99-QWXf99-QWX==w'D^^D'*DVdf%EF'''
!??vV
FH46>
8NG $($C$CFM[actv{$|!6Giit$W-3dll46H6UUS!vt->?  t5FGzz#{GTBB??OOFE2E00tTTE 	"  	( Cs5   GF?B*GGCGG8GGGc                    | j                  |d      }| j                  |d      }| j                  |d      }d|v rdnd}| j                  |d d |      }| j                  |d      }| j                  |d      }	d }
|d	k(  s
|d
k(  s|dk(  r|
|	}n|dk(  r|	}
|	}d }| j                  |d      }|>t        j                  |d      r(| j                  |d      }| j                  |      }||d}| j                  |d      }| j                  |d      }| j                  |d      }| j                  |d      }| j                  |d      }| j                  |d      }| j                  |d      }| j                  |d      }| j                  |      }d }| j                  |d      }|t        |      dk(  r| j                  |d      }| j                  |dd      }| j                  |d      }|dk(  rd }| j                  i d!|d"|d#|d$|d%|d&| j                  |      d'|
d(|d)|d*|d+d d,| j                  |d-      d.|d/|d0|d1|d2||||d |||d3      S )4Nr  r   r   r  r   rV   Or   NEW	AMENDMENTCANCELEDr  r  r  r  r  r  r  r  r   zZr   Xr   r   r  r5  spr  GTXPOr   r   r   r  r   r   lastTradeTimestamplastUpdateTimestampr   timeInForcepostOnly
reduceOnlyRr  r   	stopPricetriggerPricerL  )r   r6  filled	remainingr  r  r|  )r   r   r   r   	string_gtr  r  parse_order_statusr   r  
safe_orderr   rf  )r&   r  r   r  r  r   r,  r   r   r   r  r  r  r  r  feeCurrencyr   rL  r  r   r  r   r6  	rawStatusr  r|  r  r   r  s                                r)   parse_ws_orderzbinance.parse_ws_order  s9   Z ((4""5#.##E3/$(EMZ
!!(D$
C%%eS1	eS)!E!]k%A]V`E` 	g%!"""5#.g&7&7&E ,,UC8M11-@K'C   ,!!%-%%eS1%%eS1!!%-s+""5$/$$UC0	((3((4!s='9Q'> ,,UC8M&&uc48	&&uc2%K  
E 
f 
 ' 
 ]	 

  
 Y/ 
 !"4 
 "#6 
 D 
 ; 
  
 $..4 
 D 
 U 
  
  I! 
" f# 
$ 1 
  	r*   c                     | j                  |d      }|dk(  r| j                  |d|      }| j                  ||       | j                  ||       | j	                  ||       y )NrE  ORDER_TRADE_UPDATEr   )r   r   handle_my_tradehandle_orderr  )r&   r   r   rE  s       r)   handle_order_updatezbinance.handle_order_updateF  sb    d Wc*$$nnWc7;GVW-&'*""673r*   c                   K   | j                          d{    d}d}| j                  |      }| j                  |      s%| j                  |      }ddj	                  |      z   }d}| j                  d||      \  }}|dk(  s|dk(  rd}d}| j                  d||      \  }}| j                  ||      rd}n| j                  ||      rd	}i }	||	d
<   ||	d<   | j                  | j                  |	|             d{    |dz   |z   }d}
| j                  |dddd      \  }
}|}|
rd}| j                  d   d   |   dz   | j                  |   d   z   }| j                  |      }| j                  |||
       | j!                  ||||
       | j#                  ddd      }| j#                  ddd      }| j%                  | j&                  |      }|r5|r3|1|j)                  |dz          d{   }| j+                  ||||d      S | j-                  ||d|       d{   }| j.                  r|S | j+                  ||||d      S 7 -7 K7 ^7 /w)a  
        watch all open positions
        :param str[]|None symbols: list of unified market symbols
        :param number [since]: since timestamp
        :param number [limit]: limit
        :param dict params: extra parameters specific to the exchange API endpoint
        :param boolean [params.portfolioMargin]: set to True if you would like to watch positions in a portfolio margin account
        :returns dict[]: a list of `position structure <https://docs.ccxt.com/en/latest/manual.html#position-structure>`
        Nr   r   r   r:   rV   rY   rW   rX   r   r   
:positionsr[   rf  Fr]   r,   r   r   r}   Tr~   :fetchPositionsSnapshot)r   r   r   r   r   r   r   r   r   r  r   rl  r   r   r   r  r  r[  r   r  rW   r   r*  r   )r&   r   r   r   r   r   r-  r   r   marketTypeObjectrv  r  r   r   r}   r~   r<  rB  newPositionss                      r)   watch_positionszbinance.watch_positions  s     !!!%%g.}}W%11':F'!22K99:JFTZ[f6>TX-D99:JFTZ[==w'D^^D'*D!##' &-#,<f EFFF\)K7 $($C$CFL\^dfwy~$!6Giit$W-3dll46H6UUS!vt->?  w8IJ!%!3!34DF^`d!e!%!3!34DF^`d!e5!&<#]]42K+KLLH55hPUW[\\!ZZ[$EE??11%%PTUUQ 	"( 	G MEsG   IIC$I;I<C.I*I	+0II(II	IIc                 6   |dk(  ry | j                   i | _         || j                   v ry | j                  ddd      }|rE|dz   }||j                  vr1|j                  |       | j	                  | j
                  ||||       y y t               | j                   |<   y )NrV   r:   r}   Fr  )r  r[  r  rW   rb  load_positions_snapshotr   )r&   r   r   r   rv  r}   r-  s          r)   r  zbinance.set_positions_cache  s    6>>>!DN4>>!!%!3!34DF^`e!f!!::K6>>1k*

477dTef 2 $<#=DNN4 r*   c                   K   d|i}|rd|d<   | j                  d |       d {   }t               | j                  |<   | j                  |   }t        dt	        |            D ]1  }||   }	| j                  |	dd      }
|
dkD  s!|j                  |	       3 |j                  |   }|j                  |       |j                  ||dz          y 7 w)Nr   Trf  r   r   z	:position)	fetch_positionsr   r  r   r   r   r   r  r   )r&   r   r-  r   rv  r   r  r<  r   positionr   rW   s               r)   r  zbinance.load_positions_snapshot  s     D
 (,F$%..tV<<	79tt$q#i.) 	'A |H((;BI1}X&		' ,uud[01 =s   !CCACA
Cc                 |   t        |j                  j                               }|d   }| j                  i | _        || j                  vrt	               | j                  |<   | j                  |   }| j                  |di       }| j                  |dg       }g }t        dt        |            D ]e  }	||	   }
| j                  |
      }| j                  |d      }||d<   | j                  |      |d<   |j                  |       |j                  |       g | j                  ||dz         }t        dt        |            D ]f  }	||	   }|j                  d      }|d	   }|j                  d
      }| j                  |d|d      }| j!                  |      rU|j#                  ||       h |j#                  ||dz          y )Nr   rS  r5  rV  r   r   z:positions::r   r   r   r   Fr  )rG  r?  r  r  r   r   rl  r   r   parse_ws_positionr   r   r   find_message_hashessplitr  r   r   )r&   r   r   r?  r  r<  datarawPositionsr  r   rawPositionr  r   r   r-  partssymbolsStringr   r  s                      r)   handle_positionszbinance.handle_positions  s   < V116689#A&>>!DNt~~-*B*DDNN;'{+~~gsB/~~dC4q#l+, 	#A&q/K--k:H))'37I$-H[!#'<<	#:HZ )LL"	# 00~9UVq#m,- 	7A'*K%%d+E!!HM#))#.G,,\8WeTI==+y+6	7 	|[<%?@r*   c           
      h   | j                  |d      }| j                  |d      }t        j                  | j                  |d            }| j                  |d      }d}|dk(  r3d}t        j                  |d      st        j
                  |d      rd}nd	}| j                  i d
|dd d| j                  |d d d      dd d| j                  |d      dd d| j                  |d      d| j                  |d      dd d| j                  |      dd dd d|d|dd dd dd d d d d d d d      S ) Nr   par  TbothFr  shortlongr   r   r   r   notionalrw  mtliquidationPricer  epunrealizedPnlupr4  r   r   r   r  hedgedr   r   maintenanceMargin)maintenanceMarginPercentage
collateralinitialMargininitialMarginPercentageleveragemarginRatio)
r   r   
string_absr  r  	string_ltsafe_positionr   r   parse_number)r&   r  r   r   r   contractsAbspositionSider/  s           r)   r  zbinance.parse_ws_position1  s    ##Hc2$$Xt4	))$*:*:8T*JK--h=6!F$$Y4$$Y4#*L#)L!! #
H#
$#
 d&&xtZH#
 	#

 $**8T:#
 #
 $**8T:#
 T--h=#
 $#
 **<8#
 D#
 #
 L#
 f#
 #
  !#
"  ##
$ ,0!'+/#
  	r*   c                   K   | j                          d{    |t        | j                  dz         | j                  |      }| j	                  d||      }|dk7  r#|dk7  rt        | j                  dz   |z   dz         | j
                  d   d	   d
   |   }| j                  |      }t        |      }	d}
| j                  |ddd      \  }
}| j                  |      |
d}|||d<   |||d<   | j                  |d      }||t        | j                  dz         |	d| j                  | j                  ||            d}d| j                  i}| j                  ||	||	|       d{   }| j                  ||||      S 7 l7 w)a  
        fetch all trades made by the user

        https://developers.binance.com/docs/binance-spot-api-docs/websocket-api/account-requests#account-trade-history-user_data

        :param str symbol: unified market symbol
        :param int|None [since]: the earliest time in ms to fetch trades for
        :param int|None [limit]: the maximum number of trades structures to retrieve
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :param int [params.endTime]: the latest time in ms to fetch trades for
        :param int [params.fromId]: first trade Id to fetch
        :returns dict[]: a list of `trade structures <https://docs.ccxt.com/#/?id=trade-structure>`
        Nz" fetchMyTradesWs requires a symbolrI   rV   rW   z" fetchMyTradesWs does not support  marketsr]   r,   rZ   Fr`   r  r  r   fromIdz_ fetchMyTradesWs does not support fetching by both fromId and since parameters at the same timemyTradesr$  r   )r   r   r   r   r%  r   r   r  r&  r  r   r(  r   handle_trades_wsr*  r  )r&   r   r   r   r   r   r   r   rp   r-  r`   r+  r?  r   r  r|  s                   r)   fetch_my_trades_wszbinance.fetch_my_trades_wsd  s     !!!>TWW'KKLLV$##$5vvF6>dh.TWW'KKdRU__``iit$X.t4OOC(	)n #'#@#@IZ\npu#v &nnV, 0
 #(GK $GG""684%"3TWW  (I  I  J  J &&t{{7F'CD
 d++
 zz#{G[,WW00NNA 	"> Xs"   FFEF)F*FFc                   K   | j                          d{    |t        | j                  dz         | j                  |      }| j	                  d||      }|dk7  r#|dk7  rt        | j                  dz   |z   dz         | j
                  d   d	   d
   |   }| j                  |      }t        |      }	d}
| j                  |ddd      \  }
}| j                  |      |
d}|||d<   |	d| j                  ||      d}d| j                  i}| j                  ||	||	|       d{   }| j                  |||      S 7 '7 w)a  
        fetch all trades made by the user

        https://developers.binance.com/docs/binance-spot-api-docs/websocket-api/market-data-requests#recent-trades

        :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, default=500, max=1000
        :param dict [params]: extra parameters specific to the exchange API endpoint

 EXCHANGE SPECIFIC PARAMETERS
        :param int [params.fromId]: trade ID to begin at
        :returns dict[]: a list of `trade structures <https://docs.ccxt.com/#/?id=trade-structure>`
        Nz+ fetchTradesWs() requires a symbol argumentrS   rV   rW   z  fetchTradesWs does not support r>  r]   r,   rZ   Fr`   r  r   ztrades.historicalr$  r   )r   r   r   r   r%  r   r   r  r&  r  r   rA  r*  rx  )r&   r   r   r   r   r   r   r   rp   r-  r`   r+  r   r  r|  s                  r)   fetch_trades_wszbinance.fetch_trades_ws  so     !!!>TWW'TTUUV$##OVVD6>dh.TWW'IIDPS]]^^iit$X.t4OOC(	)n #'#@#@Zlns#t &nnV, 0
 $GG)kk'62
 d++
 zz#{G[,WW))&%??7 	"4 Xs"   ED=DE%E &E Ec                     | j                  |d      }| j                  |dg       }| j                  |      }|j                  ||       y r  )r   rl  parse_tradesr   )r&   r   r   r-  r0  r|  s         r)   rA  zbinance.handle_trades_ws  sG    \ &&w526""6*v{+r*   c                   K   | j                          d{    d}d}|| j                  |      }|d   }| j                  d||      \  }}d}| j                  d||      \  }}| j	                  ||      rd}n| j                  ||      rd}d}|1| j                  |      }|d|z   z  }| j                  ||d   |d	      }| j                  |       d{    |}	|d
k(  rd}	d}
| j                  |dddd      \  }
}|
rd}	| j                  d   d   |	   dz   | j                  |   d   z   }| j                  |      }| j                  |||
       | j                  ||d|
       d}| j                  ||||       d{   }| j                   r|j#                  ||      }| j%                  ||||d      S 7 7 7 <w)ai  
        watches information on multiple trades made by the user
        :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.portfolioMargin]: set to True if you would like to watch trades in a portfolio margin account
        :returns dict[]: a list of `trade structures <https://docs.ccxt.com/#/?id=trade-structure>`
        Nr   r3   rW   rX   r@  rP  r   r  rY   rV   r[   rf  Fr]   r,   r   r   T)r   r   r   r   r   r   r   r   r  rl  r   r   r   r  r  r*  r   rw  r  )r&   r   r   r   r   r   r   r   r-  r  rv  r   r   r   r|  s                  r)   watch_my_tradeszbinance.watch_my_trades  s     !!![[(FH%F99/6SYZf99/6SYZ==w'D^^D'*D [[(F3<'K[[&.F)STF'''8G $($C$CFO]cevx}$~!6Giit$W-3dll46H6UUS!vt->?  t5FGzz#{GTBB??OOFE2E00tTTE 	"$ 	( Cs5   F>F7C F>F:B'F>?F< 8F>:F><F>c                 D   d}| j                  |d      }|dk(  r| j                  |      }| j                  |d      }| j                  |di       }| j                  i |      }| j                  |d      }|||| j                  }	|	}| j                  |	j                  |i       }
| j                  |
|      }|K| j                  |d      }| j                  |d      }| j                  |      sd}t        d	t        |            D ]U  }||   }|d
   |d
   k(  s| j                  |d   |d         }t        | j                  |d
   |            |d   |   d<   d} n |r||d   j                  |       ng|`|d
   |d
   k(  r=| j                  |d   |d         }t        | j                  |d
   |            |d   d<   n|d
   ||d<   n||g|d<   d |d<   n||d<   | j                  |dg       }|j                  |       ||d<   | j                  -| j!                  | j"                  dd      }t%        |      | _        | j                  }|j                  |       |j'                  | j                  |       |dz   |z   }|j'                  | j                  |       y y )Nr@  r  r  r  r  r   feesTr   r  r   Fr|  rm   rk   rP  )r   r  r   r   r  r   hashmapr   r   r   r   floatcurrency_to_precisionr   rl  r@  r   r   r   r   )r&   r   r   r-  r  rs   r  tradeFeer   cachedOrdersr  r  rJ  r  insertNewFeeCurrencyr   orderFeer  orderTradesr   r@  messageHashSymbols                         r)   r  zbinance.handle_my_trade!  s    ((#6G#''0E&&ug6G~~eUB7H{{2x0H%%eX6F"x';@R#{{+!__\-A-A62NF OOFG<E(#uf="ooeU;#}}T2370%*1c$i%8 *+/7#+J#78J;O#O.2hhx7GRXIY.ZG?DTE_E_`his`tv}E~?E&M!$4V$<;@$8$)*  4 %f 4 4X > _":(:2FF*.((3v;@P*Q7<T=W=WX`akXlnu=v7weV 4!$Z!8/7e14hf/3e+3E%L&*nnUHb&I#**51*5h }}$))$,,tL 6u =}}HOOE"NN4==+6 +c 1F :NN4==*;<e $r*   c                    | j                  |      }| j                  |d      }| j                  |d      }|8| j                  -| j                  | j                  dd      }t        |      | _        | j                  }| j                  |j                  |i       }| j                  ||      }	|	| j                  |	d      }
|
|
|d<   | j                  |	d      }|||d<   | j                  |	d      |d<   | j                  |d      }|*| j                  |	d      |d<   | j                  |	d	      |d	<   |j                  |       d
}d|z   }|j                  ||       |j                  ||       y y )Nr   r   rn   rk   r  rJ  r|  r   r   r  zorders:)
r	  r   r  r   r   r   r   rK  r   r   )r&   r   r   r  r   r  r   rO  r  r  r  rJ  r   r-  symbolSpecificMessageHashs                  r)   r  zbinance.handle_orderX  sy   $$W-!!&(3""640{{"))$,,tL4U;;;L__\%9%962FFOOFG4E ooeU3?$'F5Muf5#%)F6N#'??5(#Cx  --fkB	$*.*;*;E;*OF;')-)9)9%)LF:&'"K(1F(:%NN<5NN<)BC/ r*   c                 L    | j                  ||       | j                  ||       y r!   )r  r#  rA  s      r)   handle_acount_updatezbinance.handle_acount_updateu  s"    FG,fg.r*   c                    | j                  |d      }d}| j                  |di       }| j                  |d      }| j                  |d      }	 | j                  |||j                  d d | j                  |      |d d 	       |s|j                  ||       | j                  |d      }||d   dk(  r|j                  |       y y y # t        $ r}d}|j                  ||       t        |j                  j                               }	t        dt        |	            D ]>  }
|	|
   }| j                  |j                  |   d      }||k(  s-|j                  ||       @ Y d }~d }~ww xY w)	Nr   Fr  r  msgTr   5)r   r   r   handle_errorsr   jsonr>  r@  rG  r?  r  r   r   r;  )r&   r   r   r   rejectedr  r  rY  rE  r  r   r   subscriptionId
codeStrings                 r)   handle_ws_errorzbinance.handle_ws_errory  sj    gt,w4  /ue,	7tS&**dD$))EBRTY[_aef MM'2&%%eV4
"A#)=LL! *>"  
	7HMM!R #F$8$8$=$=$?@1c"234 7#3A#6 !%!1!1&2F2FGW2XZ^!_'MM!%56	7
	7s   2C 	E#A8EEE#c           
         | j                  |d      }| j                  |d      }|||dk7  r| j                  ||       y | j                  |d      }| j                  |j                  |      }| j                  |d      }|
 |||       y 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| j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  d}| j                  |d      }	t        |t              r|d   }
| j                  |
d      dz   }	| j                  ||	      }|G| j                  |d      }|| j                  ||       y |	d|v rd|v r| j                  ||       y y y y  |||       y )Nr  r  200r   r   depthUpdaters   aggTraderu   r  r  z1hTicker@arrz4hTicker@arrz1dTicker@arrz24hrTicker@arrz24hrMiniTicker@arrr   r   r   r   r   r   )zmarkPriceUpdate@arrr   outboundAccountPositionr  ACCOUNT_UPDATEexecutionReportr  
forceOrderrE  r   r  rS  rU  )r   r   r`  r?  r_  r  r  rD  rB  r  rW  r  r   rF  rG  rj  )r&   r   r   r  r  r   r?  r   methodsr  r  rp   s               r)   handle_messagezbinance.handle_message  s   !!'8416#5&E/  1gt,(<(<bA967#
411
T&&
 ))
 T&&	

 t00
  1 1
 D//
 D//
 D//
 d11
 !$"5"5
 ++
 ++
 ++
 $--
  d11!
" t22#
$ $(#6#6//'+':':!00"77#77"&":":113
6   #.gt$1:D$$T3/&8E%0>(($7I$//@ }#.sg~%%fg6 8F.} 67#r*   )r   r!   )F)NF)y__name__
__module____qualname__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)  rF  rM  rP  r=  r_  r  rj  dictrg  r   r  r  r  r  r  r  rG  r  r  r  r  r  r   r  r  r  r  r  r   r   r  r  r  r  r  r   r;  r  rB  rD  r@  boolr  r(  r  ru  r  r  r   r  r  r  r   r  r  r  r  r  r%  r   r   rL  r   r   r  r  r  r  r  r  r  r  r  r  r  r  r	  r  r  r  r  r#  r  rB  rD  rA  rH  r  r  rW  r`  rj  __classcell__)r(   s   @r)   r   r      s
   E# ET
l3 # . BFTXac Ys Y3 Yc Yhlmxhy Y IM[_nr{} 4bDI 4bUX 4bhk 4b  CG  HS  CT 4bl4A 4AlXt EIW[df V# Vc VQT Vkop{k| V X\jnwy #\tCy #\QT #\dg #\  C  DO  P #\J;CF ;Cz @DB 5PS 5P 5PS\ 5Pn SW_a 9!$s) 9!C 9!fo 9!v PR 5xT#Y 5xVY 5xn =? L L3 L" CGr ' 'C 'V_ 'R/f />+*Z&
3 T. T.lVV V  $'V '4 ' OSaenp 8Sd3i 8S 8S[^ 8Suyz  vA 8St LN 8wc 8wRU 8wt 9; HC Hs H" <@d[] S SS S Sbfglbm S&YE Yv16 1$ 8<$]ajl ) )C )WZ )quvzq{ ). aesw  AC <E$tCy/ <EZ] <Emp <E| ^` >uT$s)_ >udg >u@ ;?r T3 Tc T(=16 =1~ 9; #C #v #J ;?T`dmo 0V3 0Vs 0VZ] 0Vtxy}t~ 0Vd$, $,L 68  F * :< S    :>b Ew EW E* 6:" E7 E E0 9=R M~g M~C M~^ 9; =C =s =" 8<B FW F7 F& `dln Bs BU\ BHrh,v ,@Fv FFV F@"96 "9H4C 4 4 4 "$ 2 )+ -Q^ 24 AF$ $> -/ %V8 %VN4 424v 24h ;= 
?c 
?h 
? ;?r *P *PdS[n *PX'/& '/R *, !A !AFa?V a?F 68 	 pt|~ /VC /Vy /V	 /V[` /Vil /V  DI /Vb3+f 3+j),v ),V im{  IK *Vc *V3 *Vi *Vy *Vbe *Vux *V  PU *VXk+6 k+Z <@ *V *VS *Vu *VX 8<B  V  VD ;?r *Vs *VC *Ve *VX 37TX\eg )OC )Os )ORU )Olpqvlw )OV :>D_cln 3 c Y\ swx}s~ ( 8<$]ajl $O $OC $OWZ $Oquv{q| $OL 04$UYbd 2U 2UC 2Us 2Uimnsit 2UhObW4& W4r 8<$]ajl 2VW 2VC 2VWZ 2Vquv~q 2Vh>& > > 2&9Av1f 6:[_hj .Os .O# .OUX .Oostyoz .O` 37TX\eg *@C *@s *@RU *@lpqvlw *@X1,v 1,f 37TX\eg ,UC ,Us ,URU ,Ulpqvlw ,U\5=f 5=nD6 D:/!"f !"FA$V A$r*   r   )%ccxt.async_supportccxt ccxt.async_support.base.ws.cacher   r   r   r   rb  ccxt.base.typesr   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   !ccxt.async_support.base.ws.clientr   typingr   ccxt.base.errorsr   r   r   r   ccxt.base.preciser   async_supportr   rQ  r*   r)   <module>rz     sf     A  A  Y  Y  Y  Y  Y 4  . ' ) * %J?$d  (( J?$r*   