
    E#i,7                       S SK rS SKJrJrJrJr  S SKrS SKJ	r	J
r
JrJrJrJrJrJrJrJrJrJrJrJrJr  S SKJr  S SKJr  S SKJr  S SKJr  S SKJr  S S	KJ r   S S
K!J"r"   " S S\RF                  RH                  5      r$g)    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                   *	  ^  \ rS rSrS\4U 4S jjrS rS rS\4S jr	SS\
S	\
4S
 jjrSS0 4S\S\S\S\\   4S jjrSS0 4S\\   S\S\S\\   4S jjrS\4S jrSS jrSS0 4S\S\S\S\\   4S jjrSS0 4S\\   S\S\S\\   4S jjrS\4S jrS0 4S\S\S\4S jjrS0 4S\\   S\S\4S jjr0 4S\\   S\4S jjr0 4S\S\4S jjrS0 4S\S\S\4S jjrS\4S jrS rS rS r S\4S  jr!S\4S! jr"S\4S" jr#S\4S# jr$S\S$\%4S% jr&SS0 4S\\   S\S\S\\'   4S& jjr(0 4S\\   S\4S' jjr)0 4S\S\4S( jjr*SS0 4S\S\S\S\\'   4S) jjr+SS\'4S* jjr,S\4S+ jr-S,SS0 4S\S-\S\S\S\\.   4
S. jjr/SS0 4S/\\\      S\S\4S0 jjr00 4S/\\\      S\4S1 jjr1S,0 4S\S-\S\4S2 jjr2S\4S3 jr30 4S\S\44S4 jjr5S,SS0 4S\S-\S\S\S\\.   4
S5 jjr6S\4S6 jr70 4S\S\44S7 jjr80 4S\S\44S8 jjr9S0 4S\:S\;4S9 jjr<S0 4S\:S\;4S: jjr=S0 4S\:S\4S; jjr>S0 4S\:S\4S< jjr?0 4S\S\4S= jjr@0 4S\S\4S> jjrAS0 4S\:S\;4S? jjrBS0 S@4SA\S\:SB\C4SC jjrDSD rES\4SE jrFS\4SF jrGS\4SG jrHS\4SH jrIS\4SI jrJ0 4SJ jrKSSK\4SL jjrLS\4SM jrM0 4SN jrN0 4SO jrOSS\4SP jjrPSQ rQ0 4S\R4SR jjrSS\4SS jrTS\4ST jrU0 4S\S\\V   4SU jjrWS0 4S\:S\\V   4SV jjrXS\4SW jrY0 4S\R4SX jjrZS\4SY jr[SZ\\   S\4S[ jr\0 4S\ jr]S0 4S\S\^S]\_S^\`S_\aS\b4S` jjrcS\4Sa jrdS\4Sb jreSS0 4Sc\S\S\^S]\_S^\aS_\aS\b4Sd jjrfS\4Se jrgS0 4Sc\S\
S\b4Sf jjrhS0 4S\
4Sg jjriS0 4Sc\S\
S\b4Sh jjrjSSS0 4S\
S\S\S\\b   4Si jjrkSSS0 4S\
S\S\S\\b   4Sj jjrlSSS0 4S\
S\S\S\\b   4Sk jjrmSSS0 4S\
S\S\S\\b   4Sl jjrnSSm jroS\4Sn jrpSSS0 4S\:S\S\S\\V   4So jjrqSS\S\:4Sp jjrrSq rsSr rtSSs jruSSS0 4S\
S\S\S\\'   4St jjrvSS0 4S\S\S\S\\'   4Su jjrwS\4Sv jrxSSS0 4S\
S\S\S\\'   4Sw jjryS\4Sx jrzS\4Sy jr{Sz r|S\4S{ jr}S\4S| jr~S\4S} jrS~rU =r$ )binance   returnc                 f   > [         [        U ]  5       nU R                  XR	                  5       5      $ N)superr   describedeep_extenddescribe_data)selfsuperDescribe	__class__s     J/home/james-whalen/.local/lib/python3.13/site-packages/ccxt/pro/binance.pyr$   binance.describe   s,    gt57/A/A/CDD    c                    0 SS_SS_SS_SS_SS_SS_SS_S	S_S
S_SS_SS_SS_SS_SS_SS_SS_SS_0 SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_S S_S!S_S"S_S#S_S$S_E0 S%S_S&S_S'S_S(S_S)S_S*S_S+S_S,S_S-S_S.S_S/S_S0S_S1S_S2S_S3S_S4S_S5S_ESSSSS6.ESS7S7S8S9S:S;S<S=.S>.0SS?S?S8S9S@S;S<S=.S>.0SSASASBSCSDSESFS=.SGSH.0SISJ.SKSL00 SMS_SNSOSOSOSOSP._SQSRSRSRSRSP._SSU R                  5       _STSU_SVSW_SXSY_SZSY_S[SY_S\SY_S]SY_S^U R                  5       _S_SY_SS`Sa0_SS`Sb0_SS`Sb0_S
S`Sc0_SdSSe.SSSf.SgSh0SSSi.SjSkSlSm0SbSnSoSpSqSrSsSt.Su.ESv.$ )wN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fetchWithdrawalsWsunWatchTickerunWatchTickersunWatchOHLCVunWatchOHLCVForSymbolsunWatchOrderBookunWatchOrderBookForSymbolsunWatchTradesunWatchTradesForSymbolsunWatchMyTrades)unWatchOrdersunWatchPositionsunWatchMarkPricesunWatchMarkPricez&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)re   marginrf   rg   ws-apiz wss://demo-stream.binance.com/wsz'wss://demo-ws-api.binance.com/ws-api/v3z 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)re   rh   rf   rg   ri   papiz!https://developers.binance.com/en)testdemoapidoc	keepAlivei  returnRateLimitsstreamLimits2   )re   rh   rf   rg   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   )r8   r/   r1   r<   walletlistenKeyRefreshRater.   tickerChannelsMap)hasurls	streamingoptions)create_safe_dictionary)r'   s    r*   r&   binance.describe_data   s   8d88 $T8 .t	8
 &t8 08  8  8 d8 '8 !$8 +D8 t8 (8 !$8  t!8" #8$ "4%8& !$'8( t)8* (+8,  -8. t/80  182 !%384 $T586 !$788 "598: !%;8< "4=8> ?8@ #DA8B $TC8D E8F  G8H "4I8J +DK8L #DM8N  O8P  Q8R %eS8T %eU8V  W8X !$Y8Z [8\ )$]8^ #D_8` -da8b  c8d *4e8f "5g8h "'$)%)$(o8v  H"J"F$H$S&R(T#
  B"D"F$H$M&R(T#
  B"D"@$B$L&L(N#
 !B ;S*X 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Qm
 m	
r,   c                     U R                  U R                  SU R                  5       5      nU R                  X!S5      nU R	                  US5      nX@R                  S   U'   U$ )Nr   r      )	safe_dictr   r   safe_integersum)r'   urlr   previousValuenewValues        r*   
request_idbinance.request_id   sY    ..{D<W<W<YZ))':88M1-)1[!#&r,   clientc                     UR                   R                  S5      S:  =(       d    UR                   R                  S5      S:  $ )Nz/streamrw   zdemo-stream)r   find)r'   r   s     r*   is_spot_urlbinance.is_spot_url   s2    zzy)B.XFJJOOM4RUW4WXr,   typesubscriptionHashc                    U R                  U R                  SU R                  5       5      nU R                  XB5      nUGc4  U R	                  U R                  SS5      nU R                  U R                  S5      nU R	                  Xq5      nUS-   nXh-  n	X`R                  S'   U R                  U	5      nXPR                  S   U'   U R                  U R                  S5      n
U
c  U R                  5       U R                  S'   U R	                  U R                  S   US5      nX-   nU R	                  U R                  S   US	5      nX:  a  [        U R                  S
-   5      eX-   U R                  S   U'   U$ )Nru   rv   rw   rq   r   numSubscriptionsByStreamr   rs   rt   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   numSubscriptionsru   streamrv   rq   streamLimitnormalizedIndexsubscriptionsByStreamssubscriptionsByStreamnewNumSubscriptionsrs   s                 r*   r   binance.stream   sx   $(NN4<<A\^b^y^y^{$|!!!";N>++DLL-LK??4<<HL++L?K%/K)7O*5LL'**?;FJPLL456FG%)__T\\C]%^"%-;?;V;V;X78$($5$5dllC]6^`fhi$j!"7"J(,(9(9$,,Gb:ceikn(o%">   ,Q  "Q  R  R?T?gDLL34V<r,   Nsymbolsincelimitc                 F   #    U R                  U/X#U5      I Sh  vN $  N7f)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_liquidationsbinance.watch_liquidations   s%      88&5QWXXXXs   !!symbolsc                 D  #    U R                  5       I Sh  vN   / n/ nSnU R                  USSS5      nU R                  U5      (       a#  UR                  S5        UR                  S5        Os[	        S[        U5      5       HC  nU R                  X   5      n	UR                  U	S   S-   5        UR                  SX   -   5        ME     US	S
R                  U5      -   -  nU R                  U5      n
SnU R                  SX5      u  pUS:X  a  [        U R                  S-   5      eSnU R                  SX5      u  pU R                  X5      (       a  SnOU R                  X5      (       a  Sn[        U5      nU R                  S   S   U   S-   U R!                  XU5      -   nU R#                  U5      nSUUS.nSU0nU R%                  XU R'                  UU5      UU5      I Sh  vN nU R(                  (       a  U$ U R+                  U R,                  XUS5      $  GN N87f)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::::,r1   re   z> watchLiquidationsForSymbols is not supported for spot symbolsrf   rg   rm   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   r   request	subscribenewLiquidationss                      r*   r   &binance.watch_liquidations_for_symbols   s5     !!!#
%%gtT4@==!!%%&<=  01c'l+WZ0"))&*?-*OP$$%5
%BC , $'!222J227;99:WYdm6>TWW'gghh99:WYdm==''D^^D**D12iit$T*S04;;tQa3bbOOC(	!(
 )
	 !% 3 3CT[]cHdfx  {D  !E  E??""11$2C2CWUZ\`aaO 	"H Es"   H HGH %H&6H H c                    U R                  US0 5      nU R                  US5      nU R                  US SS5      nUS   nU R                  X55      nU R                  U R                  U5      nUc(  U R                  U R                  SS5      n	[        U	5      nUR                  U5        XR                  U'   UR                  U/S5        UR                  U/S	U-   5        g )
Nos contractr   rz   r{   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_liquidation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                 d   U R                  US5      nU R                  X2S S5      nU R                  US5      nU R                  UU R	                  X25      U R                  US5      U R                  US5      U R                  US5      U R                  US5      S S UU R                  U5      S.
5      $ )	Nr   r   TlcontractSizeapS)
infor   	contractsr  priceside	baseValue
quoteValue	timestampdatetime)r   r   r   safe_liquidationsafe_symbolsafe_numbersafe_string_loweriso8601)r'   r  r   r   r  s        r*   r   binance.parse_ws_liquidatione  s    V ##K5!!(D&A%%k37	$$&&x8))+s; ,,V^D%%k48**;<"Y/&
  	r,   c                 2   #    U R                  U/X#U5      $ 7f)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_liquidationsbinance.watch_my_liquidations  s      55vhfUUs   c                 
  #    U R                  5       I Sh  vN   U R                  USSSS5      nU R                  U5      nS/nU R                  U5      (       d4  [	        S[        U5      5       H  nX   nUR                  SU-   5        M     Sn	U R                  SXT5      u  pSn
U R                  SXT5      u  pU R                  X5      (       a  Sn	OU R                  X5      (       a  Sn	U R                  U5      I Sh  vN   U R                  S	   S
   U	   S-   U R                  U	   S   -   nSnU R                  XX/5      I Sh  vN nU R                  (       a  U$ U R!                  U R"                  XU5      $  GNf N N97f)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::r3   rf   rg   rm   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  )binance.watch_my_liquidations_for_symbols  s     !!!%%gtT4F--g6)*}}W%%1c'l+ $$%7&%@A , 99:Y[aj99:Y[aj==''D^^D**D'''iit$T*S04<<3Ek3RR $ 3 3CQW XX??""11$2C2CWUZ[[- 	"  	( Ys5   FE<C(F?E? AFF5F?FFc                    U R                  US5      nUS:w  a  g U R                  US5      nU R                  US S S5      nU R                  XE5      nU R                  X%5      nU R	                  U R
                  U5      nUc(  U R                  U R                  SS5      n	[        U	5      nUR                  U5        XR
                  U'   UR                  U/S5        UR                  U/SU-   5        g )	Nr   LIQUIDATIONr   r   r|   r{   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_liquidationbinance.handle_my_liquidation  s    Z $$Wc2	%##GS1!!(D$?!!(3//@)<)<fE!%%dll4I4PE'.Nk*&4F#}&67}&86&ABr,   c                 D   #    U R                  U/X#5      I Sh  vN $  N7f)an  
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_bookbinance.watch_order_book1  s#     j 66xOOOO     c                   #    U R                  5       I Sh  vN   U R                  USSSS5      nU R                  US   5      nUS   nUS   (       a  US   (       a  SOS	nS
nSnUb@  [        U5      nUS:  a  [	        U R
                  S-   5      eUSSR                  U5      -   -  nU R                  U R                  SS5      n	/ n
/ n[        S[        U5      5       HS  nX   nU R                  U5      nUR                  SU-   5        US   S-   U-   nUS-   U	-   S-   nU
R                  U5        MU     [        U5      nU R                  S   S   U   S-   U R                  XWU5      -   nU R                  U5      nSU
US.n[        U5      UUU R                  UUUS.nU R!                  UXR#                  UU5      UU5      I Sh  vN nUR%                  5       $  GN N7f)aQ  
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   linearrf   rg   depthmultipleOrderbookrt   z} watchOrderBookForSymbols() accepts 200 symbols at most. To watch more symbols call watchOrderBookForSymbols() multiple timesr   r   rx   100orderbook::r   @msrm   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   strhandle_order_book_subscriptionr   r   r   )r'   r   r   r   r   r   r   r   symbolsLengthrx   	subParamsr   r   r   r   r   
symbolHashmessageHashesLengthr   r   r   subscription	orderbooks                          r*   r(  $binance.watch_order_book_for_symbolsh  s      !!!%%gtUD$Gkk'!*-6"z"*848*D(
LMs"   ,k  "k  l  l$'!222J!--dll<PRWX	q#g,'AZF[[(F  !78%m4s:TA)C/2DDtKJZ( ( "-0iit$T*S04;;tQd3eeOOC(	!
 i.99
 --c=++gW]B^`mo{||	  S 	"P }s"   G GF/G GG G c                 h  #    U R                  5       I Sh  vN   U R                  USSSS5      nU R                  US   5      nUS   nUS   (       a  US   (       a  SOS	nS
nSnUb  USSR                  U5      -   -  nU R	                  U R
                  SS5      n/ n/ n	/ n
[        S[        U5      5       Hg  nX   nU R                  U5      nU	R                  SU-   5        U
R                  SU-   5        US   S-   U-   nUS-   U-   S-   nUR                  U5        Mi     [        U	5      nU R                  S   S   U   S-   U R                  XFU5      -   nU R                  U5      nSUUS.nS[        U5      UU	U
SS.nU R                  UXR                  UU5      U
U5      I Sh  vN $  GN N7f)a  
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-  rf   rg   r.  r/  r   r   rx   r0  r1  zunsubscribe:orderbook:r   r2  r3  rm   r.   r   UNSUBSCRIBEr   r;  )unsubscriber   r   subMessageHashesr   topic)r   r   r   r   r   r   r   r   r   r   r   r   r4  r   r   )r'   r   r   r   r   r   r   rx   r7  r@  r   r   r   r   r   r8  r9  r   r   r   r:  s                        r*   un_watch_order_book_for_symbols'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ZF[[(F##MF$:;  !9F!BC%m4s:TA)C/2DDtKJZ( ( ""23iit$T*S04;;tQd3eeOOC(	#
  i. 0* 
 ((m[[RX=Y[hjvwwwM 	"L xs"   F2F-FF2(F0)F20F2c                 D   #    U R                  U/U5      I Sh  vN $  N7f)a	  
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)rB  r'   r   r   s      r*   un_watch_order_bookbinance.un_watch_order_book  s"      996(FKKKKr+  c                 T  #    U R                  5       I Sh  vN   U R                  U5      nSUS   0nUb  X%S'   U R                  SXC5      nUS:w  a  [        U R                  S-   5      eU R
                  S   S	   S
   U   nU R                  U5      n[        U5      n	Sn
U R                  USSS5      u  pXS'   U R                  US5      nU	SU R                  U R                  XS5      5      S.nSU R                  0nU R                  XyXU5      I Sh  vN nUS   US'   U$  GN N7f)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   rM   rf   z, fetchOrderBookWs only supports swap marketsrm   r.   ri   FrC   rp   rk   r.  r   r   r   r   )r   r   get_market_typer   r   r   r   r4  handle_option_and_paramsomitsign_paramsr   handle_fetch_order_bookwatch)r'   r   r   r   r   payload
marketTyper   r   messageHashrp   r   r:  r;  s                 r*   fetch_order_book_wsbinance.fetch_order_book_ws  sU     !!!V$fTl
 $G))*<fM
!TWW'UUVViit$X.z:OOC(	)n #'#@#@Zlns#t &6"#66*&&t{{7'CD
 d22
 **Sw\ZZ	$X.	(7 	"2 [s"   D(D#C=D(D&D(&D(c                     U R                  US5      nU R                  US5      nU R                  US5      nU R                  US U5      nU R	                  USS5      US'   UR                  Xc5        g )Nr   resultr  lastUpdateIdunonce)r   r   r   parse_order_booksafe_integer_2r   )r'   r   r   rR  rV  r  r;  s          r*   rN  binance.handle_fetch_order_book  sq    0 &&w52%%fc2	))&$	B	!00M	'y.r,   c                 
  #    U R                  US5      nSU-   n U R                  U R                  SS5      nU R                  US5      nU R                  USU5      nU R                  US5      n	U R	                  XHU	5      I S h  vN n
U R                  U R
                  U5      c  g U R
                  U   nUR                  U
5        UR                  n/ Ul        [        S[        U5      5       H  nX   nU R                  US	5      nU R                  US
5      nU R                  US5      nUS:X  a;  UUS   :  a  MN  XS   ::  a	  UUS   :  d	  UUS   :X  a  U R                  XU5        M|  M~  UUS   ::  a  M  US-
  US   ::  d  M  US-
  US   :  d  M  U R                  XU5        M     XR
                  U'   UR                  X5        g  GNF! [         a)  nUR                  U	 UR                  UU5         S nAg S nAff = f7f)Nr   r1  r   r{   r   r   r   r   UrX  purf   rY  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   rR  defaultLimitr   r   r   snapshotr;  messagesr   messageItemr^  rX  r_  es                      r*   fetch_order_book_snapshot!binance.fetch_order_book_snapshot3  s    !!,9#f,(	*,,T\\;PRVWL??<8D%%lG\JE__\8<F "<<VFSSHt7?/IOOH% H IO1c(m,&k%%k37%%k37&&{D98#9W-- w//a9W;M6MSUYbcjYkSk66vIV Tl Ig.. Q9W#55QUyQXGY<Y66vIV% -& '0OOF#NN92; T<  	*$$[1MM![))	*sZ   HA)G G
!G &H'CG 
G 5G 	H
G 
H G;6H;H  Hc                 n    U R                  US5      nU R                  US5      nUR                  X45        g )Nr   r   )
safe_floatstore)r'   booksidedeltar  amounts        r*   handle_deltabinance.handle_delta`  s.    q)*u%r,   c                 d    [        S[        U5      5       H  nU R                  XU   5        M     g )Nr   )r   r   ru  )r'   rr  deltasr   s       r*   handle_deltasbinance.handle_deltase  s)    q#f+&Ahq	2 'r,   c                    U R                  US5      nU R                  US   U R                  US/ 5      5        U R                  US   U R                  US/ 5      5        XCS'   U R                  US5      nXSS'   U R                  U5      US	'   U$ )
NrX  asksabidsbrY  Er  r  )r   ry  r   r  )r'   r   r   r;  rX  r  s         r*   rd  !binance.handle_order_book_messagei  s    gs+9V,doogsB.OP9V,doogsB.OP'%%gs3	!*+ $Y 7	*r,   c                    U R                  U5      nU(       a  SOSnU R                  US5      nU R                  US S U5      nUS   nSU-   nXpR                  ;  a  g U R                  U   n	U R	                  U	S5      n
U
c  U	R
                  R                  U5        g  U R	                  US5      nU R	                  US5      nU R	                  US	5      nUc  XS   :  a  U R	                  U	S
5      nS nUc  US-
  U	S   :*  =(       a    US-
  U	S   :  nOUS-
  U	S   :H  nU(       a-  U R                  XU	5        XS   :  a  UR                  X5        g g U R                  SSS5      nU(       a*  [        U R                  S-   U R                  U5      -   5      eg g XS   :  a  XS   ::  d  XS   :X  a-  U R                  XU	5        XS   ::  a  UR                  X5        g g U R                  SSS5      nU(       a*  [        U R                  S-   U R                  U5      -   5      eg g ! [         a6  nU R                  U	 UR                  U	 UR                  UU5         S nAg S nAff = f)Nre   r   r   r   r1  rY  r^  rX  r_  r  r   r8   r   T )r   r   r   ra  r   rc  r   rd  r   handle_optionr   r   orderbook_checksum_messagere  rf  rg  )r'   r   r   isSpotrQ  r   r   r   rR  r;  rY  r^  rX  r_  r  conditionalr   rl  s                     r*   handle_order_bookbinance.handle_order_books  s   & !!&) &VV
##GS1!!(D$
C!#f,//) OOF+	!!)W5=OO""7++.%%gs3%%gs3&&w5: W--$($5$5i$M	&*$,,-Ei6H+H*mPQTUPUZcdkZlOlK -.Ei6H+HK& ::6IV$'99 &y F  : (,'9'9:JJX\']H'&3DGGcMDDcDcdjDk4k&l l  ( .* g.. 7!33?Q9Q ::6IV$'(:: &y F  ; (,'9'9:JJX\']H'&3DGGcMDDcDcdjDk4k&l l  ( /  .OOF+((5a--.s.   B3H$ AH$ AH$ AH$ $
I$.,II$c                    U R                  U R                  SS5      nU R                  US5      nU R                  USU/5      nU R                  USU5      n[	        S[        U5      5       Hs  nXh   n	XR                  ;   a  U R                  U		 U R                  0 U5      U R                  U	'   U R                  USU	05      nU R                  U R                  XU5        Mu     g )Nr   r{   r   r   r   r   )r   r   r   r   r   r   ra  
order_bookr   spawnrm  )
r'   r   r   r:  rh  symbolOfSubscriptionr   r   r   r   s
             r*   r5  &binance.handle_order_book_subscription  s    ((7LdS#//hG//,	<P;QR!!,Fq#g,'AZF(OOF+&*oob%&@DOOF#;;|h5GHLJJt55vU (r,   c                    U R                  US5      nU R                  UR                  S5      nU R                  XC0 5      nU R                  US5      nUb	  U" XU5        U R	                  USS5      nU(       a  U R                  X5        U$ )Nr   r   r?  F)r   index_byrf  r   	safe_boolhandle_un_subscription)r'   r   r   r   subscriptionsByIdr:  r   isUnSubMessages           r*   handle_subscription_status"binance.handle_subscription_status  s     gt, MM&*>*>E'8bAx86L1mUK''=r,   r:  c                     U R                  US/ 5      nU R                  US/ 5      n[        S[        U5      5       H  nX5   nXE   nU R                  XU5        M     U R	                  U5        g )Nr   r@  r   )	safe_listr   r   clean_unsubscriptionclean_cache)r'   r   r:  r   r@  j	unsubHashsubHashs           r*   r  binance.handle_un_subscription  sn    |_bI>>,8JBOq#m,-A%(I&)G%%fyA . 	&r,   c                 L  #    U R                  5       I Sh  vN   U R                  USSSS5      nSnUb@  [        U5      nUS:  a  [        U R                  S-   5      eUSSR                  U5      -   -  nSnU R                  US	S
S5      u  ptU R                  US5      nU R                  US   5      nUS   n	US   (       a  US   (       a  SOSn	/ n
/ n[        S[        U5      5       HH  nX   nU R                  U5      nU
R                  SU-   5        US   S-   U-   nUR                  U5        MJ     U R                  US5      n[        U5      nU R                  S   S   U	   S-   U R                  XU5      -   nU R                  U5      nSUUS.nSU0nU R                  UXR                  UU5      U
U5      I Sh  vN nU R                   (       a6  U R#                  US5      nU R%                  US5      nUR'                  UU5      nU R)                  UX#SS5      $  GN	 Nb7f)a  
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multipleTradesrt   w watchTradesForSymbols() accepts 200 symbols at most. To watch more symbols call watchTradesForSymbols() multiple timesr   r   rB   r   r   callerMethodNamer   r   r   r-  rf   rg   trade::r   r2  rm   r.   r   r   r   r   r   r  )r   r   r   r   r   r   rK  rL  r   r   r   r   r   r   r   r   r   r   r   getLimitfilter_by_since_limit)r'   r   r   r   r   r   r6  r   r   r   r   r7  r   r   r   rawHashquerysubParamsLengthr   r   r   r   tradesfirsttradeSymbols                            r*   watch_trades_for_symbols binance.watch_trades_for_symbols  sM      !!!%%gtUD$G%
LMs"   ,e  "e  f  f$'!222J44V=TV\^ef6#56kk'!*-6"z"*848*D	q#g,'AZF[[(F  V!34]+c1D8GW% ( 		&&)i.iit$T*S04;;tQ`3aaOOC(	!
 )
	 **3{{7TY?Z\iktuu??OOFA.E**5(;KOOK7E))&%TRRQ 	"F vs#   H$HF(H$?H" A H$"H$c                   #    U R                  5       I Sh  vN   U R                  USSSS5      nSnUb@  [        U5      nUS:  a  [        U R                  S-   5      eUSSR                  U5      -   -  nSnU R                  US	S
S5      u  pRU R                  US5      nU R                  US   5      nUS   nUS   (       a  US   (       a  SOSn/ n/ n	/ n
[        S[        U5      5       H\  nX   nU R                  U5      nUR                  SU-   5        U
R                  SU-   5        US   S-   U-   nU	R                  U5        M^     U R                  US5      n[        U	5      nU R                  S   S   U   S-   U R                  XsU5      -   nU R                  U5      nSU	US.nS[        U5      UU
USS.nU R                  UXR!                  UU5      U
U5      I Sh  vN $  GN N7f)aA  
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>`
NFTr  rt   r  r   r   rB   r   r   r  r   r   r   r-  rf   rg   r  zunsubscribe:trade:r   r2  rm   r.   r   r>  r   r  r?  r   r@  r   r   rA  )r   r   r   r   r   r   rK  rL  r   r   r   r   r   r   r4  r   r   )r'   r   r   r   r6  r   r   r   r@  r7  r   r   r   r   r  r  r  r   r   r   r:  s                        r*   un_watch_trades_for_symbols#binance.un_watch_trades_for_symbols.  s)     !!!%%gtUD$G%
LMs"   ,e  "e  f  f$'!222J44V=TV\^ef6#56kk'!*-6"z"*848*D	q#g,'AZF[[(F##I$67  !5!>?]+c1D8GW% ( 		&&)i.iit$T*S04;;tQ`3aaOOC(	#
  i. 0*
 ((m[[RW=XZgiuvvvU 	"T ws"   G-G(GG-#G+$G-+G-c                 x   #    U R                  5       I Sh  vN   U R                  U/U5      I Sh  vN $  N N7f)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  rE  s      r*   un_watch_tradesbinance.un_watch_tradesh  s9      !!!55vhGGG 	"Gs   :6:8::c                 P   #    SUS'   U R                  U/X#U5      I Sh  vN $  N7f)a  
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>`
rA   r  N)r  r   s        r*   watch_tradesbinance.watch_tradesy  s/       &3!"22F8U6RRRRs   &$&c                 ~   U R                  US5      nUS:H  nU(       d  U R                  X5      $ U R                  USS5      nU R                  US5      nU R                  USS5      nU R                  US5      nU(       a  U R                  US	U5      nU R                  US
5      n	U	c  Ub  Ub  [        R
                  " Xx5      n	U R                  US5      n
SU;   a  SOSnU R                  U
S S U5      nU R                  US5      nS nU R                  US5      nSU;   a  Uc  US   (       a  SOSnUS   (       a  SOSnS nU R                  US5      nUb(  U R                  US5      nU R                  U5      nUUS.nU R                  US5      nU R                  UUU R                  U5      UUUUUUUUU	US.5      $ )NxTRADEtr}  r  Lpqr  Yr   psr   re   r	  r   msellbuymakertakernNr   currencyr   )r
  r  r  r   r   orderr   takerOrMakerr  r  rt  r   fee)r   parse_tradesafe_string_2r   r   
string_mulr  r  safe_currency_code
safe_trader  )r'   r   r   executionTypeisTradeExecutionr   r  r  rt  r   r   rQ  r   r  r  orderIdr  feeCostfeeCurrencyIdfeeCurrencyCoder   s                        r*   parse_ws_tradebinance.parse_ws_trade  s   R ((4)W4##E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                    U R                  U5      nU(       a  SOSnU R                  US5      nU R                  US S U5      nUS   nSU-   nU R                  X&5      n	U R	                  U R
                  U5      n
U
c(  U R                  U R                  SS5      n[        U5      n
U
R                  U	5        XR
                  U'   UR                  X5        g )Nre   r   r   r   r  r}   r{   )r   r   r   r  r   r  r   r   r   r   r   )r'   r   r   r  rQ  r   r   r   rR  r   tradesArrayr   s               r*   handle_tradebinance.handle_trade'  s     !!&) &VZ
##GS1!!(D$
C!&(##G4oodkk6:%%dllM4HE$U+K5!)F{0r,   1m	timeframec                    #    U R                  5       I Sh  vN   U R                  U5      nUS   nSUS'   U R                  X//X4U5      I Sh  vN nXq   U   $  NC N7f)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 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   r6   r  )r   r   watch_ohlcv_for_symbols)r'   r   r  r   r   r   r   rV  s           r*   watch_ohlcvbinance.watch_ohlcv9  sr       !!!V$!%1!"33f5H4I5Y_``~i(( 	" as!   AA7AAAAsymbolsAndTimeframesc                   #    U R                  5       I Sh  vN   SnU R                  USSS5      u  pTU R                  US5      nU R                  USSSS5      nU R	                  US   5      nUS   n	US	   (       a  US
   (       a  SOSn	U	S:H  n
SnU R                  USS5      u  pUSL=(       a#    US:H  =(       d    [        R                  " US5      n/ n/ n[        S[        U5      5       H  nX   nUS   nUS   nU R                  U R                  UU5      nU R	                  U5      nUS   nUS:X  a  UR                  SS5      nU=(       a    U
nSnU(       a  UOSnUR                  US-   U-   S-   U-   U-   5        UR                  SUS   -   S-   U-   5        M     U R                  S   S   U	   S-   U R                  U	S5      -   nU R!                  U5      nS UUS!.nS"U0nU R#                  US#5      nU R%                  UXR'                  UU5      UU5      I Sh  vN nUu  nnn U R(                  (       a  U R+                  UU5      nU R-                  U X#SS5      n!U R/                  UUU!5      $  GNa NW7f)$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channelr   r   r   FTr   r   r-  rf   rg   re   timezone+08:008r   r   indexPriceKline_perpr   @+08:00r2  _ohlcv::r   r   rm   r.   r   multipleOHLCVr   r   r   r  )r   handle_param_string_2get_list_from_object_valuesr   r   handle_param_stringr   	string_eqr   r   r   
timeframesreplacer   r   r   r   rL  r   r   r   r  r  create_ohlcv_object)"r'   r  r   r   r   	klineTyper   marketSymbolsr   r   r  r  isUtc8	rawHashesr   r   symAndTfsymbolStringtimeframeStringintervalr   r   shouldUseUTC8suffix	utcSuffixr   r   r   r   resr   r  candlesfiltereds"                                     r*   r  binance.watch_ohlcv_for_symbolsP  s     !!!	 66vy&RYZ	223GK++GT5%Nkk-"236"z"*848*D&.33FJM$&hh(.B-gwGXGXYacfGg	q#234A+.H#A;L&qkO''/ZH[[.Fm,H--#++GR8#.MF"/RIX^i7#=H9TU  VH-=!=!D!VW 5 iit$T*S04;;t_3UUOOC(	!
 )
	 6#56'']KKQW<XZgirss%("	7??$$VU3E--guQM''	8DD] 	"R ts#   I<I7HI<"I:#AI<:I<c                   #    U R                  5       I Sh  vN   SnU R                  USSS5      u  p2U R                  US5      nU R                  USSSS5      nU R	                  US   5      nUS   nUS	   (       a  US
   (       a  SOSnUS:H  nSn	U R                  USS5      u  pU	SL=(       a#    U	S:H  =(       d    [        R                  " U	S5      n
/ n/ n/ n[        S[        U5      5       H  nX   nUS   nUS   nU R                  U R                  UU5      nU R	                  U5      nUS   nUS:X  a  UR                  SS5      nU
=(       a    UnSnU(       a  UOSnUR                  US-   U-   S-   U-   U-   5        UR                  SUS   -   S-   U-   5        UR                  SUS   -   S-   U-   5        M     U R                  S   S   U   S-   U R                  US 5      -   nU R!                  U5      nS!UUS".nS[#        U5      UUUUS#S$.nU R%                  US%5      nU R'                  UXR)                  UU5      UU5      I Sh  vN $  GN? N7f)&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  r   r   r   FTr   r   r-  rf   rg   re   r  r  r  r   r   r  r  r   r  r2  r  r  r   r   zunsubscribe::ohlcv::rm   r.   r   r  r>  r   ohlcv)r?  r   r   r  r@  r   rA  r  )r   r  r  r   r   r  r   r  r   r   r   r  r  r   r   r   r   r4  rL  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   s                               r*   un_watch_ohlcv_for_symbols"binance.un_watch_ohlcv_for_symbols  s     !!!	 66vy&RYZ	223GK++GT5%Nkk-"236"z"*848*D&.33FJM$&hh(.B-gwGXGXYacfGg	q#234A+.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 5  iit$T*S04;;t_3UUOOC(	#
  i.$8 0*
	 6#56((m[[RX=Y[hjstttc 	"b us"   IIH9IIIIc                    #    U R                  5       I Sh  vN   U R                  U5      nUS   nSUS'   U R                  X//U5      I Sh  vN $  N; N7f)aj  
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   r6   r  )r   r   r  )r'   r   r  r   r   s        r*   un_watch_ohlcvbinance.un_watch_ohlcv  sc      !!!V$!%1!"44v6I5JFSSS	 	" Ts!   AA6AAAAc           	         U R                  US5      nSSS.nU R                  XCU5      nU R                  US5      nU R                  USS5      nUS:X  a  U R                  US5      nU R                  US5      nU R                  U5      nU R	                  US	5      U R                  US
5      U R                  US5      U R                  US5      U R                  US5      U R                  US5      /n	U R                  U5      n
U
(       a  SOSnU R                  US S U5      nSU-   S-   U-   nU R                  U R                  U0 5      U R                  U'   U R                  U R                  U   U5      nUc9  U R	                  U R                  SS5      n[        U5      nXR                  U   U'   UR                  U	5        XU/nUR                  UU5        g )Nrl  r  markPriceKline)indexPrice_klinemarkPrice_klinekr   r  r   r  r   hr  cvre   r   r  r   r   r{   )r   r   r  find_timeframer   rp  r   r  ohlcvsr   r   r   r   )r'   r   r   eventeventMapr   r   r  unifiedTimeframeparsedr  rQ  r   rR  storedr   resolveDatas                    r*   handle_ohlcvbinance.handle_ohlcv  s   6   #. 1/
   %8-%%eS$7%%''6H##E3/..x8eS)OOE3'OOE3'OOE3'OOE3'OOE3'
 !!&) &VZ
!!(D$
C&(4/2BB"oodkk62FFV!46FG>%%dllL$GE*51F4:KK 01f8{K0r,   c                 f  #    U R                  5       I Sh  vN   U R                  U5      nSUS   0nU R                  SX25      nUS:w  a  [        U R                  S-   5      eU R
                  S   S   S	   U   nU R                  U5      n[        U5      nS
U R                  0n	Sn
U R                  USSS5      u  pXS'   U R                  US5      nSnU R                  USS
S5      u  pUUU R                  U R                  XB5      5      S.nU R                  XhXU	5      I Sh  vN nU$  GN N	7f)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   rT   rf   z) fetchTickerWs only supports swap marketsrm   r.   ri   r   Frp   rk   zticker.bookrI  )r   r   rJ  r   r   r   r   r4  handle_ticker_wsrK  rL  rM  r   rO  )r'   r   r   r   rP  r   r   r   rR  r:  rp   r   r   r   s                 r*   fetch_ticker_wsbinance.fetch_ticker_ws"  sU     !!!V$fTl
 ##OVD8TWW'RRSSiit$X.t4OOC(	)nd++
 !#'#@#@Zlns#t &6"#66*66vPXZgh&&t{{7'CD

 zz#G,WW5 	"2 Xs"   D1D,DD1%D/&D1/D1c                   #    U R                  5       I Sh  vN   U R                  U5      nU R                  SXe5      nUS:w  a  US:w  a  [        U R                  S-   5      eU R
                  S   S   S   U   nU R                  U5      n	[        U	5      n
S	nU R                  USS
S	5      u  pU R                  U5      UU R                  U   S.nU R                  US5      nU R                  US5      nUb  X<S'   Ub  XLS'   Ub  XS'   U
SU R                  X5      S.nSU R                  0nU R                  XXU5      I Sh  vN $  GN1 N7f)aT  
       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
       NrL   re   rf   z0 fetchOHLCVWs only supports spot or swap marketsrm   r.   ri   Frp   )r   rp   r  until	startTimer   endTimeklinesrI  r   )r   r   rJ  r   r   r   r   r4  rK  	market_idr  r   rL  r   handle_fetch_ohlcvrO  )r'   r   r  r   r   r   r   rQ  r   r   rR  rp   rP  r$  r   r:  s                   r*   fetch_ohlcv_wsbinance.fetch_ohlcv_wsG  ss    " !!!V$)).&I
J($:TWW'YYZZiit$X.z:OOC(	)n #'#@#@Ykmr#s nnV, 0	2

 !!&'267+#(K $G!&Ikk'2
 d--
 ZZ'UUU? 	"> Vs"   EED+EE
E
Ec                     U R                  US5      nU R                  U5      nU R                  US5      nUR                  XE5        g )NrV  r   )r  parse_ohlcvsr   r   )r'   r   r   rV  r  rR  s         r*   r)  binance.handle_fetch_ohlcvy  sC    @ 2""6*&&w5v+r,   c                    #    U R                  5       I Sh  vN   U R                  U5      nU R                  U/U R                  USS05      5      I Sh  vN nX1   $  NF N
7f)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>`
Nr  r=   )r   r   watch_tickersr   r'   r   r   tickerss       r*   watch_tickerbinance.watch_ticker  s`       !!!V$**F8T[[J\^kIl5mnn 	"n!   A!A=A!A	A!A!c                    #    U R                  5       I Sh  vN   U R                  U5      nU R                  U/U R                  USS05      5      I Sh  vN nX1   $  NF N
7f)a  
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>`
Nr  r@   )r   r   watch_mark_pricesr   r1  s       r*   watch_mark_pricebinance.watch_mark_price  s`      !!!V$..xVN`brMs9tuu 	"ur5  c                    #    SnU R                  USSS5      u  p2U R                  SX1U5      I Sh  vN nU R                  (       a  U$ U R                  U R                  SU5      $  N47f)a!  
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?   r   r   r   )rK  watch_multi_ticker_helperr   filter_by_arrayr2  r'   r   r   channelName
newTickerss        r*   r7  binance.watch_mark_prices  so       #;;FDUW]_jk99:K[cijj
??##DLL(GDD ks   /A(A&5A(c                   #    SnU R                  USSS5      u  p2US:X  a  [        U R                  S-   5      eU R                  SX1U5      I Sh  vN nU R                  (       a  U$ U R                  U R                  SU5      $  N47f)aT  
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>   r   r   r   V deprecation notice - to subscribe for bids-asks, use watch_bids_asks() method insteadr   )rK  r   r   r;  r   r<  r2  r=  s        r*   r0  binance.watch_tickers  s      ";;FNTZ\de,&TWW'  A  A99.+`fgg
??##DLL(GDD hs   ABB5Bc                    #    SnU R                  USSS5      u  p2US:X  a  [        U R                  S-   5      eU R                  SX1US5      I Sh  vN $  N7f)	aV  
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>   r   r   r   rB  rY   T)rK  r   r   r;  r'   r   r   r>  s       r*   un_watch_tickersbinance.un_watch_tickers  sj      ";;FNTZ\de,&TWW'  A  A334Dk\bdhiiiis   AAAAc                    #    SnU R                  USSS5      u  p2U R                  5       I Sh  vN   U R                  SX1US5      I Sh  vN $  N  N7f)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/derivatives/usds-margined-futures/websocket-market-streams/Mark-Price-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>`
Nr?   r   r   rc   T)rK  r   r;  rE  s       r*   un_watch_mark_pricesbinance.un_watch_mark_prices  s_      ";;FDUW]_jk!!!334G_egklll 	"ls!   ,AAA
AAAc                 D   #    U R                  U/U5      I Sh  vN $  N7f)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/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
:returns dict: a `ticker structure <https://docs.ccxt.com/#/?id=ticker-structure>`
N)rI  rE  s      r*   un_watch_mark_pricebinance.un_watch_mark_price  s"      ..x@@@@r+  c                 D   #    U R                  U/U5      I Sh  vN $  N7f)aS  
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)rF  rE  s      r*   un_watch_tickerbinance.un_watch_ticker!  s"      **F8V<<<<r+  c                   #    U R                  5       I Sh  vN   U R                  USSSS5      nU R                  SSX5      I Sh  vN nU R                  (       a  U$ U R	                  U R
                  SU5      $  Nd N67f)au  
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>`
NTFr4   r   r   )r   r   r;  r   r<  bidsasks)r'   r   r   rV  s       r*   watch_bids_asksbinance.watch_bids_asks2  sv      !!!%%gtT5$G55o|U\ee??M##DMM8WEE 	"es!   A?A;/A?A=5A?=A?Fr>  isUnsubscribec                 *  #    U R                  5       I S h  vN   U R                  US SSS5      nUS:H  nUS:H  nU R                  USS5      nS n	S n
US LnU(       a  U R                  US   5      n	U(       a  SOS nU R	                  XXL5      u  pS nU R                  XU5      u  pS nU R                  X5      (       a  SnO@U R                  X5      (       a  S	nO'U
S
:X  a  U
nO[        U R                  S-   U-   S-   5      eU(       a=  U R                  U
SS/5      (       d$  [        U R                  S-   U-   S-   U
-   S-   5      e/ n/ n/ nSnU(       a  U(       a  SOSnS nU(       a  SnOU(       a  SnOSnU(       a  [        S[        U5      5       H|  nUU   nU R                  U5      nUR                  US   S-   U-   U-   5        UR                  US-   U-   S-   U-   5        U(       d  M\  UR                  SU-   S-   U-   S-   U-   5        M~     OU(       a9  U
S
:X  a  [        U R                  S-   U-   S-   5      eUR                  SU-   5        O9U(       a  UR                  SU-   S-   U-   5        OUR                  SU-   S-   5        UR                  US-   U-   5        UR                  SU-   5        UnU(       a  US-   SR                  U5      -   nU R                   S   S   U   S-   U R#                  UU5      -   nU R%                  U5      nU(       a  S OS!UUS".nUnS#U0nU(       a  S['        U5      UUUSS$.nUnU R)                  UUU R+                  UU5      UU5      I S h  vN nU(       a  U$ U(       d  U$ 0 nUUUS%   '   U$  GNx N%7f)&NTFr   r   	use1sFreqr   r   rf   rg   re   r  z#() does not support options marketsz() does not support z markets yetr   z@1sbidaskr   r   r2  :zunsubscribe::z5() requires symbols for self channel for spot markets!@arrs:r   r   rm   r.   r   r>  r   r   r   r  r   )r   r   r  r   r   r   r   r   r   r   in_arrayr   r   r   r   r   r   r   r   r4  r   r%   )r'   
methodNamer>  r   r   rU  isBidAskisMarkPricerW  r   rQ  symbolsDefineddefaultMarketr   rawMarketTypesubscriptionArgsr   unsubscribeMessageHashesr   unifiedPrefixr   r   r   r   r   r   r   hashesr:  rV  newDicts                                  r*   r;  !binance.watch_multi_ticker_helperE  s    !!!%%gtT5$G</"k1NN6;=	
!-++gaj1K#.T!??
Y_o
99*SYZ==--$M^^J00&M6!&Mtww}z9<aabbt}}Z&(9KLLtww}z9<RRU__bppqq#% (UrF!$M'M$M1c'l+ V, ''}(=(Ck(QTZ(Z[$$]S%8;%F%Lv%UV =,33Om4SVY4Y\g4gjm4mpv4vw , '+DGGcMJ,F  JA  -A  B  B ''k(9: ''k(9F(BV(KL ''k(9F(BC  !5!CD$++Ok,IJ 
$t+chhw.??Jiit$]3c9DKKWa<bbOOC(	'4m+&

 )
 #)n$1!9"!L .F**38H8HRX8Y[acoppM MG(.GF8$%Nw 	"b qs)   NNGN.E=N+N,#NNc                 j   U R                  USS5      nU R                  US S U5      nU R                  USS5      nUS:X  a  SnUS:X  ag  U R                  UU R	                  US5      U R                  U R	                  US5      5      UU R                  US	5      U R                  US
5      S.5      $ S nUS:X  a  U R                  USS5      nOU R                  U/ SQ5      nU R                  US S U5      nU R                  USS5      nU R                  0 SU_SU_SU R                  U5      _SU R                  US5      _SU R                  US5      _SU R                  USS5      _SU R                  USS5      _SU R                  USS5      _SU R                  US S!5      _S"U R                  US#5      _S$U R                  US%5      _S&U_S'U_S(U R                  US)5      _S*U R                  US	5      _S+U R                  US,5      _S-S _U R                  US.5      U R                  US/5      US0.EU5      $ )1Nr   r   rl  r   r   r   r   r  r  r   )r   r  r  r
  r   
indexPricetime)Cr  rl  r  r  r  r  highr  lowr  bidr  bidPrice	bidVolumeBbidQtyaskr}  askPrice	askVolumeAaskQtyvwapwopenr   closelastpreviousCloser  change
percentagePaverager  r  )
baseVolumequoteVolumer
  )	r  r  r   safe_tickerr   r  r[  safe_integer_nr   )	r'   r   rQ  r   r   r  r  r   r~  s	            r*   parse_ws_ticker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                     U R                  US5      nU R                  US0 5      nU R                  US5      nUR                  XS5        g )Nr   rV  rf   )r   r   r  r   )r'   r   r   rR  rV  r   s         r*   r   binance.handle_ticker_ws  sF    6 &&w5(B7%%fh7v+r,   c                 (    U R                  XS5        g )Nbidaskshandle_tickers_and_bids_asksr'   r   r   s      r*   handle_bids_asksbinance.handle_bids_asks7  s     	))&9Er,   c                 (    U R                  XS5        g )Nr2  r  r  s      r*   handle_tickersbinance.handle_tickersF  s    < 	))&9Er,   c                 (    U R                  XS5        g )N
markPricesr  r  s      r*   handle_mark_pricesbinance.handle_mark_pricesf  s    ))&<Hr,   c                    U R                  U5      nU(       a  SOSnUS:H  nUS:H  nS nU(       a  SnOU(       a  SnOSnS n	/ n
/ n0 n[        U[        5      (       a  UnOUR                  U5        [	        S[        U5      5       H  nX   nU R                  US	5      nU(       a  S
nU R                  U R                  S   X5      n	U	c  MF  U R                  X5      nUS   nUUU'   U(       a  UU R                  U'   OUU R                  U'   US-   U	-   S-   U-   nU
R                  U5        UR                  UU5        M     [        U
5      nUS:  a  US-   U	-   nUR                  UU5        g g )Nre   r   r  r  rX  r   r   r   rl  r   r   r   rY  r2  r\  )r   
isinstancelistr   r   r   r   r   r  rR  r2  r   )r'   r   r   
methodTyper  rQ  r_  r`  rf  r>  resolvedMessageHashes
rawTickersr?  r   r   r  parsedTickerr   rR  lengthbatchMessageHashs                        r*   r  $binance.handle_tickers_and_bids_asksi  s   !!&) &VZ
)+!\1!$M'M$M "

gt$$ Jg&q#j/*A]F$$VS1E$**4<<8K+Le[K"//CL!(+F!-Jv(4f%'3V$'#-;cAFJK!((5NN<5# +& *+A:,t3kANN:'78 r,   c                    U R                  5         U R                  U R                  S5      nUb  X!S'   U R                  US5      nUb  X1S'   U R                  U R	                  5       U R
                  S.U5      nU R                  U5      nU R                  U5      nS nU R                  R                  S5      S:  ac  [        U R                  5      S:  a  U R                  XPR                  S5      nOvU R                  U R                  U5      U R                  S5      nOIU R                  U R                  U5      U R                  U R                  5      [        R                   5      nXdS'   U$ )	N
recvWindow)r  apiKeyzPRIVATE KEYrw   x   sha256ed25519	signature)check_required_credentialsr   r   r   rY  r  keysort	rawencodesecretr   r   rsaeddsaencodehmachashlibr  )r'   r   defaultRecvWindowr  extendedParamsr  r  s          r*   rM  binance.sign_params  s6   '') --dllLI(#4< &&v|<
!#-< kk&
  n5~.	;;M*R/4;;#% HHUKKB	 JJt{{5'94;;	R			$++e"4dkk$++6NPWP^P^_I&/{#r,   rQ  c                   #    U R                   S   S   S   U   nU R                  U5      nUR                  n[        UR	                  5       5      nU R                  U5      nXa:X  a  gSUR                  U'   U R                  U5      n[        U5      nUSU R                  0 5      S.n	UU R                  US.n
U R                  X(XU
5      I Sh  vN   g N7f)	a  
       watches best bid & ask for symbols
@param marketType {string} only support on 'spot'

       {@link https://developers.binance.com/docs/binance-spot-api-docs/websocket-api/user-data-stream-requests#subscribe-to-user-data-stream-through-signature-subscription-user_data Binance User Data Stream Documentation}

       :returns: Promise<number> The subscription ID for the user data stream
       rm   r.   ri   NTz"userDataStream.subscribe.signaturerI  )r   r   r:  )r   r   rf  r  keys#get_account_type_from_subscriptionsr   r4  rM  !handle_user_data_stream_subscriberO  )r'   rQ  r   r   rf  subscriptionsKeysaccountTyper   rR  r   r:  s              r*   .ensure_user_data_stream_ws_subscribe_signature6binance.ensure_user_data_stream_ws_subscribe_signature  s      iit$X.z:S!,, !3!3!56>>?PQ$+/Z(OOC(	)n:&&r*
 <<&

 jj7NNNs   CCCCc                 B   U R                  US5      nUR                  n[        UR                  5       5      nU R	                  U5      nU R                  US0 5      nU R                  US5      nUc  UR                  U	 UR                  X&5        UR                  X#5        g )Nr   rV  subscriptionId)	r   rf  r  r  r  r   r   rg  r   )	r'   r   r   rR  rf  r  r  rV  r  s	            r*   r  )binance.handle_user_data_stream_subscribe  s     &&w5,, !3!3!56>>?PQ26**63CD!$$[1MM'/w,r,   c                   #    U R                  5       nS nU R                  SS U5      u  p1S nU R                  SS U5      u  pAS nU R                  USSSS5      u  pQU R	                  X45      (       a  SnOU R                  X45      (       a  SnUS:X  a  U R                  S5      I S h  vN   g S nU R                  SU5      u  paUS:H  nUS	:H  =(       d    US L nU R                  US
5      n	U R                  US
5      nU R                  U R                  U0 5      n
U R                  U
SS5      nU R                  U R                  SS5      nU R                  US5      nX+-
  U:  Gag  S nU(       a.  U R                  U5      I S h  vN nU R                  USS05      nOUS:X  a  U R!                  U5      I S h  vN nOUS:X  a  U R#                  U5      I S h  vN nOUS:X  a!  U(       a  U R%                  U5      I S h  vN nOzU(       aZ  U	c  ['        U R(                  S-   5      eU R+                  U	5      nU R                  US
U05      nU R-                  U5      I S h  vN nOU R/                  U5      I S h  vN nU R                  U
U R                  US5      US.5      U R                  U'   U R1                  XR2                  U5        g g  GN% GNN GN GN  N N| Nd7f)Nr   rj   portfolioMarginFrf   rg   re   isolatedcrossr   lastAuthenticatedTimer   r   r   i'  Trh   zC authenticate() requires a symbol argument for isolated margin moder  r  r  )millisecondsr   r   handle_option_and_params_2r   r   r  handle_margin_mode_and_paramsr   rL  r   r   r   r   papiPostListenKeyr   fapiPrivatePostListenKeydapiPrivatePostListenKeysapiPostUserDataStreamr   r   r(  sapiPostUserDataStreamIsolatedpublicPostUserDataStreamdelaykeep_alive_listen_key)r'   r   rl  r   r   isPortfolioMargin
marginModeisIsolatedMarginisCrossMarginr   r   r  r   r  responser   s                   r*   r   binance.authenticate  s      "99.$PVW99.$PVW $($C$CFN\bduw|$}!==''D^^D**D6>EEfMMM
!??PVW
&*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+-G-GP/ 0 N @ GFD MFs   B#K%K
&CK?K 3K3K4KK&K:K;A KKK5K6AKKKKKKKc                   #    U R                  U R                  SSS5      nU R                  USU5      nS nU R                  USSSS5      u  p1U R	                  SS U5      nUS	   nU R                  X%5      (       a  S
nOU R                  X%5      (       a  SnU R                  U R                  U0 5      nU R                  US5      nUc  g 0 nU R                  US5      n	U R                  USS/5      nU R                  5       n
 U(       a=  U R                  U R                  X5      5      I S h  vN   U R                  USS05      nOUS
:X  a)  U R                  U R                  X5      5      I S h  vN   OUS:X  a)  U R                  U R                  X5      5      I S h  vN   O`XxS'   US:X  a-  XS'   U R                  U R                  X5      5      I S h  vN   O)U R                  U R                  X5      5      I S h  vN    U R                  UUU
S.5      U R                  U'   ['        U R2                  R5                  5       5      nU R7                  U R                  SS5      n[-        S	[/        U5      5       Hr  nUU   n['        UR8                  R+                  5       5      n[-        S	[/        U5      5       H.  nUU   nUU:X  d  M  U R;                  UU R<                  U5            g    Mt     g  GN GN GNZ GN& N! [          a  nUnU(       a  SnU R"                  S   S   U   S-   U R                  U   S   -   nU R%                  U5      n['        UR(                  R+                  5       5      n[-        S	[/        U5      5       H  nUU   nUR1                  UU5        M     U R                  US S	S.5      U R                  U'    S nAg S nAff = f7f)Nr   r   re   r   keepAliveListenKeyrj   r  Fr   rf   rg   r  r   Trh   rm   r.   r   r  r   r   )r  r   r   r  r   r   r   r   rL  r  papiPutListenKeyr   fapiPrivatePutListenKeydapiPrivatePutListenKeysapiPutUserDataStreampublicPutUserDataStreamre  r   r   r  futuresr  r   r   rg  clientsvaluesr   rf  r  r  )r'   r   r   r  subTypeInfor   r   r  r   r   rl  errorurlTyper   r   r   r   rR  r  r   subscriptionKeysr  subscribeTypes                          r*   r  binance.keep_alive_listen_key	  s    !!$,,~vV5 $($C$CFL`bhj{  ~C  %D!556JDRXYa.==''D^^D**D//$,,b9$$Wk:	!!&(36FH#56  "	 ++DKK,HIIIV.?-FG!224;;w3OPPP#224;;w3OPPP'0$8#(.H%44T[[5QRRR66t{{77S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	 5 (G J QP
 ST 	G  ))E"4(1C7$,,t:L[:YYC[[%F !4!4!67M1c-01+A.e[1 2 "&W!)*7 "DLL 	s   C?O*L  ,K2-L  O)L  /K50L  4O5)L  K8L  #O$1L  K;L  O#L  >K>?L  CO'O2L  5L  8L  ;L  >L   
O
B?O	OOOc                 T   X!R                   ;   a  X R                  ;   a  g U R                  U R                  S5      nU R	                  USS5      nU(       aD  US-   nXaR
                  ;  a/  UR                  U5        U R                  U R                  XX#5        g g 0 U R                  U'   g )Nr/   r   F:fetchBalanceSnapshot)	rf  balancer   r   r  r  rf   r  load_balance_snapshot)r'   r   r   r  r   r   rR  s          r*   set_balance_cachebinance.set_balance_cacheX	  s    (((t||/C//$,,?#~~g7MuU!88K>>1k*

455vDd 2 "$DLLr,   c                 V  #    SU0nU(       a  SUS'   U R                  U5      I S h  vN nU R                  X`R                  U R                  U0 5      5      U R                  U'   UR                  U   nUR                  5         UR                  U R                  U   US-   5        g  N7f)Nr   Tr  :balance)fetch_balancer   r   r  r  r   )r'   r   rR  r   r  r   r  rf   s           r*   r  binance.load_balance_snapshote	  s     D
 (,F$%++F33![[??4<<QUWY3Z[T,t||D)4*+<= 4s   %B)B'B B)c                 L  #    U R                  5       I Sh  vN   U R                  SSU5      nUS:w  a$  US:w  a  US:w  a  [        U R                  S-   5      eU R                  S   S   S	   U   nU R                  U5      n[        U5      nS
nU R                  USSS
5      u  paSU0nSnU R                  USSS5      u  pUUU R                  U R                  Xq5      5      S.n	SUS:X  a  U R                  OU R                  0n
U R                  X5XU
5      I Sh  vN $  GN	 N7f)a  
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>`
NrH   re   rf   rg   z2 fetchBalanceWs only supports spot or swap marketsrm   r.   ri   Frp   r   zaccount.statusrI  )r   rJ  r   r   r   r   r4  rK  rM  r   handle_account_status_wshandle_balance_wsrO  )r'   r   r   r   r   rR  rp   rP  r   r   r:  s              r*   fetch_balance_wsbinance.fetch_balance_wsr	  sJ     !!!##$4dFC6>dh.4:3ETWW'[[\\iit$X.t4OOC(	)n #'#@#@IY[mot#u  0
 66v?OQY[kl&&t{{7'CD
 BR8Rd33Y]YoYo
 ZZ'UUU- 	", Vs"   D$DDD$D"D$"D$c                    U R                  US5      nS n[        US   [        5      (       a  U R                  US/ 5      nO&U R	                  US0 5      nU R                  US/ 5      nU R                  U5      nUR                  Xc5        g )Nr   rV  assets)r   r  r  r  r   parseBalanceCustomr   )r'   r   r   rR  
rawBalancerV  parsedBalancess          r*   r  binance.handle_balance_ws	  s     &&w5
gh'..2>J ^^GXr:F"=J00<~3r,   c                     U R                  US5      nU R                  US0 5      nU R                  U5      nUR                  XS5        g Nr   rV  )r   r   r  r   )r'   r   r   rR  rV  r  s         r*   r   binance.handle_account_status_ws	  sE    ^ &&w526008~3r,   c                 D   #    U R                  U/U5      I Sh  vN $  N7f)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_wsrE  s      r*   fetch_position_wsbinance.fetch_position_ws	  s"      ,,fXv>>>>r+  c                   #    U R                  5       I Sh  vN   0 nSnU R                  USSSS5      nUb-  [        U5      nUS:X  a  U R                  US   5      nUS   US'   U R	                  SXB5      nUS	:w  a  US
:w  a  [        U R                  S-   5      eU R                  S   S   S   U   nU R                  U5      n[        U5      n	Sn
U R                  USSS5      u  pXS'   SnU R                  USSS5      u  pU	UU R                  U R                  X25      5      S.nSU R                  0nU R                  XyXU5      I Sh  vN nU R                  USUS5      $  GNM N7f)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   rS   rf   rg   z, fetchPositionsWs only supports swap marketsrm   r.   ri   Frp   r   zaccount.positionrI  )r   r   r   r   rJ  r   r   r   r   r4  rK  rM  r   handle_positions_wsrO  filter_by_array_positions)r'   r   r   rP  r   r6  r   r   r   rR  rp   r   r   r:  rV  s                  r*   r  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{{7'CD
 d..
 zz#G,WW--fhOO; 	"8 Xs"   E(E#D3E(
E&E(&E(c                 H   U R                  US5      nU R                  US/ 5      n/ n[        S[        U5      5       HQ  nU R	                  XF   5      nU R                  US5      nUS:w  d  M0  US:w  d  M8  US:w  d  M@  UR                  U5        MS     UR                  XS5        g )Nr   rV  r   
entryPrice0z0.0z
0.00000000)r   r  r   r   parse_position_riskr   r   )	r'   r   r   rR  rV  	positionsr   r  r
  s	            r*   r  binance.handle_positions_ws
  s    > &&w526	q#f+&A--fi8F))&,?Jc!
e(;*P\B\  (	 '
 	y.r,   c                   #    U R                  5       I Sh  vN   U R                  U5      I Sh  vN   U R                  U R                  SS5      nU R                  USU5      nSnU R	                  SSU5      u  pASnU R                  USSSS5      u  pQU R                  X45      (       a  S	nOU R                  X45      (       a  S
nSnUnUS:X  a  U R                  S   S   S   U   nO4U(       a  SnU R                  S   S   U   S-   U R                  U   S   -   nU R                  U5      nU R                  XU5        U R                  XSU5        U R                  U R                  S5      n	U R                  U	SS5      n
U R                  U	SS5      nU
(       a#  U(       a  UR                  US-   5      I Sh  vN   US-   nSnU R                  XlX5      I Sh  vN $  GN GN N+ N7f)aw  
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   re   r   r/   rj   r  Frf   rg   r   rm   r.   ri   r   r  r   r   Tr  r  )r   r   r   r   r   r  r   r   r   r   r  set_positions_cacher   r  rf   rO  )r'   r   r   r   r   r  r   r  r   r   r   r   rR  r   s                 r*   watch_balancebinance.watch_balance=
  s     !!!'''&&t||]FK<99.$PVW $($C$CFN\bduw|$}!==''D^^D**D6>))E"4(248C  ))E"4(1C7$,,t:L[:YYCS!v->?  t5FG..~>#~~g7MuU#~~g7MtT$8--'> >???Z'ZZ'@@@? 	"'6 @ AsD   G)GG)G"F	G)9G%: G)G'G)"G)%G)'G)c                 v   U R                  U R                  SS5      nUR                  n[        UR	                  5       5      nU R                  U5      nUS-   nU R                  U   c  0 U R                  U'   X R                  U   S'   U R                  US5      nUS:X  a  U R                  US5      n	U R                  U	5      n
U R                  5       nU R                  US5      nXR                  U   ;   aU  U R                  U   U
   S	   n[        U[        5      (       d  U R                  U5      n[        R                  " X5      US	'   OXS	'   XR                  U   U
'   OU R                  USU5      nU R                  US
5      n[!        S[#        U5      5       H  nX   nU R                  US5      n	U R                  U	5      n
U R                  5       nU R                  US5      US	'   U R                  US5      US'   U R                  UU5      US'   XR                  U   U
'   M     U R%                  US5      nUU R                  U   S'   U R'                  U5      U R                  U   S'   U R)                  U R                  U   5      U R                  U'   UR+                  U R                  U   U5        g )Nr   r   r  r
  rl  balanceUpdater}  dfreers  r   fr  usedtotalr  r  r  )r   r   rf  r  r  r  r  r  accountr  r4  r   r   
string_addr   r  r   r   r   r  safe_balancer   )r'   r   r   r   rf  r  r  rR  r  
currencyIdcoder  rs  r   rs  r   entryr  s                     r*   handle_balancebinance.handle_balancee
  s   D !!$,,$?,, !3!3!56>>?PQ!J.<<$,(*DLL%,3[!&)  #.O#))'37J**:6DllnG$$Wc2E||K00 $[ 9$ ? G!-55$($9$9-$HM")"4"4]"J"'.5LL%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,   rf  c                     Sn[        S[        U5      5       H$  nX   nUS:X  d  US:X  d  US:X  d  US:X  d  M!  Un  U$    U$ )Nr   r   re   rh   rf   rg   )r   r   )r'   rf  r  r   r:  s        r*   r  +binance.get_account_type_from_subscriptions
  sc    q#m,-A(+L&LH,D,ZbJbht  yC  iC* .
 r,   c                     S nU R                  XU5      u  pCS nU R                  XU5      u  pSU R                  XE5      (       a  SnU$ U R                  XE5      (       a  SnU$ )Nrf   rg   )r   r   r   r   )r'   r   r   r   r   r   s         r*   rJ  binance.get_market_type
  sj    99&&Q99&&Q==''D  ^^D**Dr,   r  rt  r  c                 
  #    U R                  5       I Sh  vN   U R                  U5      nU R                  SXv5      nUS:w  a$  US:w  a  US:w  a  [        U R                  S-   5      eU R
                  S   S   S	   U   n	U R                  U	5      n
[        U
5      nU R                  US
SS5      nU R                  US
S5      nU R                  XX4XV5      nSnU R                  USSS5      u  pXS'   U R                  USS5      nU R                  US5      nUSU R                  U R                  X5      5      S.nU(       a  U(       a  SUS'   OSUS'   SU R                  0nU R!                  XUUU5      I Sh  vN $  GNh N7f)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>`
NrC   re   rf   rg   z1 createOrderWs only supports spot or swap marketsrm   r.   ri   sorSORFrp   rk   zorder.placerI  zsor.order.testr   z
order.test)r   r   rJ  r   r   r   r   r4  safe_bool_2rL  create_order_requestrK  r  rM  r   handle_order_wsrO  )r'   r   r   r  rt  r  r   r   rQ  r   r   rR  r'  rP  rp   rk   r   r:  s                     r*   create_order_wsbinance.create_order_ws
  s    $ !!!V$))/6J
J($:zZ?WTWW'ZZ[[iit$X.z:OOC(	)nvueU;65%0++F$V #'#@#@Zlns#t &6"#~~ffe466*#&&t{{7'CD

 $4!$0!d**
 ZZ';UUU; 	": Vs"   FE>E"F9F:FFc                     U R                  US5      nU R                  US0 5      nU R                  U5      nUR                  XS5        g r  r   r   parse_orderr   )r'   r   r   rR  rV  r  s         r*   r+  binance.handle_order_ws  sE    ` &&w526  (u*r,   c                     U R                  US5      nU R                  US/ 5      nU R                  U5      nUR                  XS5        g r  )r   r  parse_ordersr   )r'   r   r   rR  rV  orderss         r*   handle_orders_wsbinance.handle_orders_wsJ  sE    L &&w526""6*v+r,   r   c           	        #    U R                  5       I Sh  vN   U R                  U5      nU R                  SX5      n	U	S:w  a$  U	S:w  a  U	S:w  a  [        U R                  S-   5      eU R
                  S   S   S	   U	   n
U R                  U
5      n[        U5      nU	S:H  =(       d    U	S:H  nSnU	S:X  a  U R                  XX4XVU5      nOU(       a  U R                  XX4XVU5      nS
nU R                  USSS
5      u  pXS'   UU(       a  SOSU R                  U R                  X5      5      S.nSU R                  0nU R                  XUUU5      I Sh  vN $  GNB N7f)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>`
NrD   re   rf   rg   z/ editOrderWs only supports spot or swap marketsrm   r.   ri   Frp   zorder.modifyzorder.cancelReplacerI  r   )r   r   rJ  r   r   r   r   r4  editSpotOrderRequesteditContractOrderRequestrK  rM  r   handle_edit_order_wsrO  )r'   r   r   r   r  rt  r  r   r   rQ  r   r   rR  isSwaprP  rp   r   r:  s                     r*   edit_order_wsbinance.edit_order_wsu  sw    " !!!V$))-H
J($:zZ?WTWW'XXYYiit$X.z:OOC(	)n(DJ*,D//DW]^G33BF[abG #'#@#@Xjlq#r &6"#)/n6K&&t{{7'CD
 d//
 ZZ';UUU3 	"2 Vs"   EED<EEEEc                     U R                  US5      nU R                  US0 5      nU R                  US5      nS nUb  U R                  U5      nOU R                  U5      nUR                  Xc5        g )Nr   rV  newOrderResponser/  )r'   r   r   rR  rV  newSpotOrderr  s          r*   r:  binance.handle_edit_order_ws  sq    F &&w526~~f.@A#$$\2E$$V,Eu*r,   c                   #    U R                  5       I Sh  vN   Uc  [        U R                  S-   5      eU R                  U5      nU R	                  SXC5      nU R
                  S   S   S   U   nU R                  U5      n[        U5      nSn	U R                  USSS5      u  pU R                  U5      U	S	.n
U R                  US
S5      nUb  XS
'   OU R                  U5      U
S'   U R                  US
S/5      nUSU R                  U R                  X5      5      S.nSU R                  0nU R!                  XhXU5      I Sh  vN $  GN3 N7f)a  
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 symbolrE   rm   r.   ri   Frp   r   rp   origClientOrderIdclientOrderIdr  zorder.cancelrI  r   )r   r   r   r   rJ  r   r   r4  rK  r(  r  parse_to_intrL  rM  r   r+  rO  r'   r   r   r   r   r   r   r   rR  rp   rP  rF  r   r:  s                 r*   cancel_order_wsbinance.cancel_order_ws  sk     !!!>TWW'IIJJV$##OVDiit$X.t4OOC(	)n #'#@#@Zlns#t nnV, 0
 **63FX$+8'(!%!2!22!6GI6$7#IJ$&&t{{7'CD
 d**
 ZZ'UUU9 	"8 Vs"   EE	D-EEEEc                   #    U R                  5       I Sh  vN   U R                  U5      nU R                  SX25      nUS:w  a  [        U R                  S-   5      eU R
                  S   S   S   U   nU R                  U5      n[        U5      nSnU R                  USS	S5      u  pU R                  U5      US
.n	USU R                  U R                  X5      5      S.n
SU R                  0nU R                  XWXU5      I Sh  vN $  N N7f)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>`
NrG   re   z- cancelAllOrdersWs only supports spot marketsrm   r.   ri   Frp   rD  zopenOrders.cancelAllrI  r   )r   r   rJ  r   r   r   r   r4  rK  r(  rM  r   r5  rO  )r'   r   r   r   r   r   r   rR  rp   rP  r   r:  s               r*   cancel_all_orders_wsbinance.cancel_all_orders_ws:  s      !!!V$##$7H6>TWW'VVWWiit$X.t4OOC(	)n #'#@#@I\^prw#x nnV, 0

 ,&&t{{7'CD
 d++
 ZZ'UUU- 	", Vs"   DD	C-DDDDc                   #    U R                  5       I Sh  vN   Uc  [        U R                  S-   5      eU R                  U5      nU R	                  SXC5      nUS:w  a$  US:w  a  US:w  a  [        U R                  S-   5      eU R
                  S   S	   S
   U   nU R                  U5      n[        U5      nSn	U R                  USSS5      u  pU R                  U5      U	S.n
U R                  USS5      nUb  XS'   OU R                  U5      U
S'   USU R                  U R                  X5      5      S.nSU R                  0nU R                  XhXU5      I Sh  vN $  GNI N7f)an  
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>`
NrC  rO   re   rf   rg   z0 fetchOrderWs only supports spot or swap marketsrm   r.   ri   Frp   rD  rE  rF  r  zorder.statusrI  r   )r   r   r   r   rJ  r   r   r4  rK  r(  r  rG  rM  r   r+  rO  rH  s                 r*   fetch_order_wsbinance.fetch_order_ws\  s     !!!>TWW'IIJJV$##NFC6>dh.4:3ETWW'YYZZiit$X.t4OOC(	)n #'#@#@Ykmr#s nnV, 0
 **63FX$+8'(!%!2!22!6GI$&&t{{7'CD
 d**
 ZZ'UUU; 	": Vs"   E$EEE$E"E$"E$c                 z  #    U R                  5       I Sh  vN   Uc  [        U R                  S-   5      eU R                  U5      nU R	                  SXT5      nUS:w  a  [        U R                  S-   5      eU R
                  S   S   S   U   nU R                  U5      n[        U5      n	S	n
U R                  US
SS	5      u  pU R                  U5      U
S.nU	SU R                  U R                  X5      5      S.nSU R                  0nU R                  XyXU5      I Sh  vN nU R                  XX#5      $  GN  N7f)a5  
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 symbolrP   re   z) fetchOrdersWs only supports spot marketsrm   r.   ri   FrO   rp   rD  	allOrdersrI  r   )r   r   r   r   rJ  r   r   r4  rK  r(  rM  r   r5  rO  filter_by_symbol_since_limitr'   r   r   r   r   r   r   r   r   rR  rp   rP  r   r:  r4  s                  r*   fetch_orders_wsbinance.fetch_orders_ws  sJ      !!!>TWW'IIJJV$##OVD6>TWW'RRSSiit$X.t4OOC(	)n #'#@#@Ykmr#s nnV, 0

 !&&t{{7'CD
 d++
 zz#G,WW00NN3 	"0 Xs"   D;D6DD;D9 D;9D;c                    #    U R                  XX45      I Sh  vN n/ n[        S[        U5      5       H#  nXW   nUS   S:X  d  M  UR                  U5        M%     U$  ND7f)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)rU  r   r   r   )	r'   r   r   r   r   r4  closedOrdersr   r  s	            r*   fetch_closed_orders_wsbinance.fetch_closed_orders_ws  se      ++F5IIq#f+&AIEX(*##E* '  Js   AA)AAc                 ^  #    U R                  5       I Sh  vN   U R                  U5      nU R                  SXT5      nUS:w  a  US:w  a  [        U R                  S-   5      eU R
                  S   S   S   U   nU R                  U5      n[        U5      n	S	n
U R                  US
SS	5      u  pSU
0nUb  U R                  U5      US'   U	SU R                  U R                  X5      5      S.nSU R                  0nU R                  XyXU5      I Sh  vN nU R                  XX#5      $  GN N7f)a  
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>`
NrN   re   rf   z5 fetchOpenOrdersWs only supports spot or swap marketsrm   r.   ri   FrO   rp   r   zopenOrders.statusrI  r   )r   r   rJ  r   r   r   r   r4  rK  r(  rM  r   r5  rO  rS  rT  s                  r*   fetch_open_orders_wsbinance.fetch_open_orders_ws  sH     !!!V$##$7H6>dh.TWW'^^__iit$X.t4OOC(	)n #'#@#@Ykmr#s  0
  $v 6GH)&&t{{7'CD
 d++
 zz#G,WW00NN1 	". Xs"   D-D(C:D-D+D-+D-c                   #    U R                  5       I Sh  vN   SnSnUb  U R                  U5      nUS   nUSU-   -  nSnU R                  SXd5      u  ptSnU R                  SXd5      u  pU R	                  Xx5      (       a  SnOU R                  Xx5      (       a  SnU R                  XGXS.5      nU R                  U5      I Sh  vN   Sn	U R                  SU5      u  pUn
US	:X  d	  US
:X  a  U	b  S
n
SnU R                  USSSS5      u  pSnUS
:X  a  U R                  S   S   S   U   nO4U(       a  Sn
U R                  S   S   U
   S-   U R                  U   S   -   nU R                  U5      nU R                  XU5        U R                  XSU5        SnU R                  XX5      I Sh  vN nU R                   (       a  UR#                  X5      nU R%                  XX#S5      $  GN GN& N?7f)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>`
Nr4  r   rY  r:   rf   rg   )r   r   r   rh   re   rj   r  Fr   rm   r.   ri   r   r  T)r   r   r   r   r   r   r   r   r  r  r   r   r   r  r  rO  r   r  rS  )r'   r   r   r   r   rR  r   r   r   r  r  r  r   r   r   r4  s                   r*   watch_ordersbinance.watch_orders  s      !!![[(FH%F3<'K99-X99-X==''D^^D**DVf%YZ'''
!??vV
H46>
8NG $($C$CFM[actv{$|!6>))E"4(248C  ))E"4(1C7$,,t:L[:YYCS!v->?  t5FGzz#GBB??OOF2E00tTTO 	"  	(( Cs5   G0G(B.G0G+C(G0.G./:G0+G0.G0c                    U R                  US5      nU R                  US5      nU R                  US5      nSU;   a  SOSnU R                  US S U5      nU R                  US5      nU R                  US5      n	S n
US	:X  d  US
:X  d  US:X  a  Uc  U	nOUS:X  a  U	n
U	nS nU R                  US5      nUbD  [        R                  " US5      (       a(  U R                  US5      nU R                  U5      nUUS.nU R                  US5      nU R                  US5      nU R                  US5      nU R                  US5      nU R                  US5      nU R                  US5      nU R                  US5      nU R                  US5      nU R                  U5      nS nU R                  US5      nUb  [        U5      S:X  a  U R                  US5      nU R                  USS5      nU R                  US5      nUS:X  a  S nU R                  0 S!U_S"U_S#U_S$U_S%U_S&U R                  U5      _S'U
_S(U_S)U_S*U_S+S _S,U R                  US-5      _S.U_S/U_S0U_S1U_S2U_UUUS UUUS3.E5      $ )4Nr  r   r   r  r   re   Or  NEW	AMENDMENTCANCELEDr  r  r  r  r  r  r  r	  r   zZr  Xrm  r   r  r  spr  GTXPOr
  r   r   rF  r  r  lastTradeTimestamplastUpdateTimestampr   timeInForcepostOnly
reduceOnlyRr  r  	stopPricetriggerPricert  )r   r  filled	remainingrX  r  r  )r   r  r   r   	string_gtr  r  parse_order_statusr   r  
safe_orderr  r  )r'   r  r   r  r  r   rQ  r   r  r  rn  ro  r  r  r  feeCurrencyr  rt  r  r   rv  r   r  	rawStatusrX  r  rF  rt  rp  s                                r*   parse_ws_orderbinance.parse_ws_order&  s=   Z ((4""5#.##E3/$(EMZ
!!(D$
C%%eS1	eS)!E!]k%A]V`E` 	g%!"""5#.g&7&7&E&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                     U R                  US5      nUS:X  a  U R                  USU5      nU R                  X5        U R                  X5        U R	                  X5        g )Nrl  ORDER_TRADE_UPDATEr   )r   r   handle_my_tradehandle_orderr%  )r'   r   r   rl  s       r*   handle_order_updatebinance.handle_order_update  s\    d Wc*$$nnWc7;GV-&*""63r,   c                   #    U R                  5       I Sh  vN   SnSnU R                  U5      nU R                  U5      (       d%  U R                  U5      nSSR	                  U5      -   nSnU R                  SXT5      u  ptUS:X  d  US:X  a  SnSnU R                  SXT5      u  pU R                  Xx5      (       a  SnOU R                  Xx5      (       a  S	n0 n	XyS
'   XS'   U R                  U R                  X5      5      I Sh  vN   US-   U-   nSn
U R                  USSSS5      u  pUnU
(       a  SnU R                  S   S   U   S-   U R                  U   S   -   nU R                  U5      nU R                  XU
5        U R!                  XX5        U R#                  SSS5      nU R#                  SSS5      nU R%                  U R&                  U5      nU(       a:  U(       a3  Uc0  UR)                  US-   5      I Sh  vN nU R+                  UXUS5      $ U R-                  XSU5      I Sh  vN nU R.                  (       a  U$ U R+                  UXUS5      $  GNC GNY N` N37f)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<   re   rh   rf   rg   r   r   
:positionsrj   r  Frm   r.   r   r  r   Tr   :fetchPositionsSnapshot)r   r   r   r   r   r   r   r   r   r   r   r  r   r   r   r  r  r  r   r  rf   r   rO  r   )r'   r   r   r   r   r   rR  r   r   marketTypeObjectr  r  r   r   r   r   rc  ri  newPositionss                      r*   watch_positionsbinance.watch_positions  sv     !!!%%g.}}W%%11':F'!22K99:JF[6>TX-D99:JF[==''D^^D**D!##' &-#,< EFFF\)K7 $($C$CFL\^dfwy~$!Giit$W-3dll46H6UUS!v->?  w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?.I#-I!.,I#I#I#!I#c                 D   US:X  a  g U R                   c  0 U l         X R                   ;   a  g U R                  SSS5      nU(       aD  US-   nXaR                  ;  a/  UR                  U5        U R	                  U R
                  XX$5        g g [        5       U R                   U'   g )Nre   r<   r   Fr  )r  r  r  rf   r  load_positions_snapshotr   )r'   r   r   r   r  r   rR  s          r*   r  binance.set_positions_cacheD  s    6>>>!DN>>!!%!3!34DF^`e!f!!::K>>1k*

477df 2 $<#=DNN4 r,   c                   #    SU0nU(       a  SUS'   U R                  S U5      I S h  vN n[        5       U R                  U'   U R                  U   n[        S[	        U5      5       H3  nXh   n	U R                  U	SS5      n
U
S:  d  M"  UR                  U	5        M5     UR                  U   nUR                  U5        UR                  XsS-   5        g  N7f)Nr   Tr  r   r  z	:position)	fetch_positionsr   r  r   r   r  r   r  r   )r'   r   rR  r   r  r   r  rc  r   positionr  rf   s               r*   r  binance.load_positions_snapshotT  s     D
 (,F$%..tV<<	79tt$q#i.)A |H((;BI1}X&	 * ,uu[01 =s   &CCAC
ACc                    UR                   n[        UR                  5       5      nU R                  U5      nU R                  c  0 U l        XPR                  ;  a  [        5       U R                  U'   U R                  U   nU R                  US0 5      nU R                  US/ 5      n/ n	[        S[        U5      5       Hd  n
X   nU R                  U5      nU R                  US5      nXS'   U R                  U5      US'   U	R                  U5        UR                  U5        Mf     U R                  XS-   5      n[        S[        U5      5       Hl  n
X   nUR                  S5      nUS	   nUR                  S
5      nU R!                  U	SUS5      nU R#                  U5      (       a  MZ  UR%                  UU5        Mn     UR%                  XS-   5        g )Nr}  r  r   r  r  r  z:positions::r   r   r   r   Fr  )rf  r  r  r  r  r   r   r  r   r   parse_ws_positionr   r  r   find_message_hashessplitr<  r   r   )r'   r   r   rf  r  r  rc  datarawPositionsr  r   rawPositionr  r  r   rR  partssymbolsStringr   r  s                       r*   handle_positionsbinance.handle_positionsg  s   < ,, !3!3!56>>?PQ>>!DN~~-*B*DDNN;'{+~~gsB/~~dC4q#l+,A&/K--k:H))'37I$-[!#'<<	#:HZ )LL" - 00~9UVq#m,-A'*K%%d+E!!HM#))#.G,,\8WeTI==++y+6 . 	|<%?@r,   c           
         U R                  US5      nU R                  US5      n[        R                  " U R                  US5      5      nU R                  US5      nSnUS:X  a?  Sn[        R                  " US5      (       d!  [        R
                  " US5      (       a  SnOS	nU R                  0 S
U_SS _SU R                  US S S5      _SS _SU R                  US5      _SS _SU R                  US5      _SU R                  US5      _SS _SU R                  U5      _SS _SS _SU_SU_SS _SS _SS _S S S S S S S.E5      $ ) Nr   par  TbothFr  shortlongr
  r   r   r   notionalr  mtliquidationPricer
  epunrealizedPnlupr  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  binance.parse_ws_position  s    ##Hc2$$Xt4	))$*:*:8T*JK--h=6!F$$Y44$$Y44#*L#)L!! #
H#
$#
 d&&xtVD#
 	#

 $**8T:#
 #
 $**8T:#
 T--h=#
 $#
 **<8#
 D#
 #
 L#
 f#
 #
  !#
"  ##
$ ,0!'+/#
  	r,   c                   #    U R                  5       I Sh  vN   Uc  [        U R                  S-   5      eU R                  U5      nU R	                  SXT5      nUS:w  a$  US:w  a  [        U R                  S-   U-   S-   5      eU R
                  S   S	   S
   U   nU R                  U5      n[        U5      n	Sn
U R                  USSS5      u  pU R                  U5      U
S.nUb  X+S'   Ub  X;S'   U R                  US5      nUb  Ub  [        U R                  S-   5      eU	SU R                  U R                  X5      5      S.nSU R                  0nU R                  XyXU5      I Sh  vN nU R                  XX#5      $  GNj N7f)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 symbolrK   re   rf   z" fetchMyTradesWs does not support  marketsrm   r.   ri   Frp   rD  r%  r   fromIdz_ fetchMyTradesWs does not support fetching by both fromId and since parameters at the same timemyTradesrI  r   )r   r   r   r   rJ  r   r   r4  rK  r(  r   rM  r   handle_trades_wsrO  rS  )r'   r   r   r   r   r   r   r   r   rR  rp   rP  r  r   r:  r  s                   r*   fetch_my_trades_wsbinance.fetch_my_trades_ws  s     !!!>TWW'KKLLV$##$5vF6>dh.TWW'KKdRU__``iit$X.t4OOC(	)n #'#@#@IZ\npu#v nnV, 0
 #(K $G""684%"3TWW  (I  I  J  J &&t{{7'CD
 d++
 zz#G,WW00NNA 	"> Xs"   FF EF)F*FFc                 L  #    U R                  5       I Sh  vN   U R                  U5      nU R                  SXT5      nUS:w  a$  US:w  a  [        U R                  S-   U-   S-   5      eU R
                  S   S   S	   U   nU R                  U5      n[        U5      n	S
n
U R                  USSS
5      u  pU R                  U5      U
S.nUb  X;S'   U	SU R                  X5      S.nSU R                  0nU R                  XyXU5      I Sh  vN nU R                  XU5      $  GN	 N7f)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>`
       NrU   re   rf   z  fetchTradesWs does not support r  rm   r.   ri   Frp   rD  r   ztrades.historicalrI  r   )r   r   rJ  r   r   r   r   r4  rK  r(  r   r  rO  r  )r'   r   r   r   r   r   r   r   r   rR  rp   rP  r   r:  r  s                  r*   fetch_trades_wsbinance.fetch_trades_ws  sE     !!!V$##OVD6>dh.TWW'IIDPS]]^^iit$X.t4OOC(	)n #'#@#@Zlns#t nnV, 0
 $G)kk'2
 d++
 zz#G,WW))&??3 	"0 Xs"   D$DC1D$D"	D$"D$c                     U R                  US5      nU R                  US/ 5      nU R                  U5      nUR                  XS5        g r  )r   r  parse_tradesr   )r'   r   r   rR  rV  r  s         r*   r  binance.handle_trades_ws0  sE    \ &&w526""6*v+r,   c                   #    U R                  5       I Sh  vN   SnSnUb  U R                  U5      nUS   nU R                  SXd5      u  pTSnU R                  SXd5      u  ptU R	                  XW5      (       a  SnOU R                  XW5      (       a  SnSnUb0  U R                  U5      nUSU-   -  nU R                  XFS   US	.5      nU R                  U R                  XWS
.U5      5      I Sh  vN   Un	US:X  a  Sn	Sn
U R                  USSSS5      u  pSnUS:X  a  U R                  S   S   S   U   nO4U
(       a  Sn	U R                  S   S   U	   S-   U R                  U   S   -   nU R                  U5      nU R                  XU
5        U R                  XSU
5        SnU R                  XX5      I Sh  vN nU R                   (       a  UR#                  X5      nU R%                  XX#S5      $  GN GN N?7f)a)  
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   r5   rf   rg   r  rY  r   )r   r   )r   r   rh   re   rj   r  Fr   rm   r.   ri   r   r  T)r   r   r   r   r   r   r   r   r   r  r   r   r   r  r  rO  r   r  rS  )r'   r   r   r   r   r   r   r   rR  r  r  r   r   r   r  s                  r*   watch_my_tradesbinance.watch_my_tradesc  s     !!![[(FH%F99/6Z99/6Z==''D^^D**D [[(F3<'K[[.F)STFT,NPV WXXX8G $($C$CFO]cevx}$~!6>))E"4(248C  ))E"4(1C7$,,t:L[:YYCS!v->?  t5FGzz#GBB??OOF2E00tTTM 	"$ 	Y" Cs5   G:G2CG:.G5/C	G:8G89:G:5G:8G:c                 h   SnU R                  US5      nUS:X  Ga  U R                  U5      nU R                  US5      nU R                  US0 5      nU R                  0 U5      nU R                  US5      nUGb  UGb  UGb  U R                  n	U	Gb  U R                  U	R                  U0 5      n
U R                  X5      nUGbW  U R                  US5      nU R                  US5      nU R                  U5      (       d  Sn[        S	[        U5      5       HV  nX   nUS
   US
   :X  d  M  U R                  US   US   5      n[        U R                  US
   U5      5      US   U   S'   Sn  O   U(       a  US   R                  U5        OgUb`  US
   US
   :X  a=  U R                  US   US   5      n[        U R                  US
   U5      5      US   S'   OUS
   c  X{S'   OX/US'   S US'   OX{S'   U R                  US/ 5      nUR                  U5        UUS'   U R                  c-  U R!                  U R"                  SS5      n[%        U5      U l        U R                  nUR                  U5        UR'                  U R                  U5        US-   U-   nUR'                  U R                  U5        g g )Nr  r  r  r  r  r   feesTr   r  r   Fr  r}   r{   rY  )r   r  r   r   r4  r   hashmapr   r   r   r   floatcurrency_to_precisionr   r  r  r   r   r   r   )r'   r   r   rR  r  r   r  tradeFeer   cachedOrdersr4  r  r  r  insertNewFeeCurrencyr   orderFeer  orderTradesr   r  messageHashSymbols                         r*   r  binance.handle_my_trade  s    ((#6G#''0E&&ug6G~~eUB7H{{2x0H%%eX6F"x';@R#{{+!__\-A-A62NF OOF<E(#uf="ooeU;#}}T22370%*1c$i%8+/7#+J#78J;O#O.2hhx7GRXIY.ZG?DTE_E_`his`tv}E~?E&M!$4V$<;@$8$) &9  4 %f 4 4X > _":(:2FF*.((3v;@P*Q7<T=W=WX`akXlnu=v7weV 4!$Z!8/7e14f/3e+3%L&*nnUHb&I#**51*5h }}$))$,,tL 6u =}}HOOE"NN4==+6 +c 1F :NN4==*;<e $r,   c                    U R                  U5      nU R                  US5      nU R                  US5      nUGb8  U R                  c-  U R                  U R                  SS5      n[        U5      U l        U R                  nU R                  UR                  U0 5      nU R                  X5      n	U	b  U R                  U	S5      n
U
b  XS'   U R                  U	S5      nUb  XS'   U R                  U	S5      US'   U R                  US5      nUc*  U R                  U	S5      US'   U R                  U	S	5      US	'   UR                  U5        S
nSU-   nUR                  X}5        UR                  X~5        g g )Nr   r   r~   r{   r  r  r  r  r  r4  zorders:)
r}  r   r4  r   r   r   r   r  r   r   )r'   r   r   r  r   r  r   r  r4  r  r  r  r  rR  symbolSpecificMessageHashs                  r*   r  binance.handle_order  sn   $$W-!!&(3""640{{"))$,,tL4U;;;L__\%9%962FFOOF4E ooeU3?$'5Muf5#%)6N#'??5(#Cx  --fkB	$*.*;*;E;*OF;')-)9)9%)LF:&'"K(1F(:%NN<5NN<C/ r,   c                 H    U R                  X5        U R                  X5        g r"   )r   r  r  s      r*   handle_acount_updatebinance.handle_acount_update  s    F,f.r,   c                 <   U R                  US5      nSnU R                  US0 5      nU R                  US5      nU R                  US5      n U R                  XgUR                  S0 U R                  U5      U0 0 5	        U(       d  UR                  X#5        U R                  US5      nUb  US   S
:X  a  UR                  U5        g g g ! [         a  nSnUR                  X5        [        UR                  R                  5       5      n	[        S[        U	5      5       Ho  n
X   nU R                  UR                  U   S5      nU R                  UR                  U   S	5      nX<:X  d  ML  UR                  X5        Uc  Mb  UR                  U	 Mq      S nAGNS nAff = f)Nr   Fr  r  msgr   Tr   r:  5)r   r   r   handle_errorsr   jsonre  rg  r  rf  r  r   r   rb  )r'   r   r   r   rejectedr  r  r  rl  r  r   r   r  r:  
codeStrings                  r*   handle_ws_errorbinance.handle_ws_error  s    gt,w4  /ue,	?t&**b"dii>NPUWY[]^ MM'&%%eV4
"A#)=LL! *>"%  	?HMM! #F$8$8$=$=$?@1c"234#3#6 !%!1!1&2F2FGW2XZ^!_#//0D0DEU0VXfg'MM!6#/"00> 5	?s%   1C 
FBF)F?FFc                     U R                  US5      nUR                  n[        UR                  5       5      nU R	                  U5      nUS:X  a  UR                  U	 UR                  X&5        g g )Nrl  eventStreamTerminated)r   rf  r  r  r  rg  )r'   r   r   r  rf  r  r  s          r*   handle_event_stream_terminated&binance.handle_event_stream_terminated  sn       #.,, !3!3!56>>?PQ++$$[1MM'/ ,r,   c                 >   U R                  US5      nUb  UnU R                  US5      nU R                  US5      nUc	  Ub  US:w  a  U R                  X5        g U R                  US5      nU R                  UR                  U5      nU R                  US5      nUb	  U" X5        g 0 SU R
                  _SU R                  _S	U R                  _S
U R                  _SU R                  _SU R                  _SU R                  _SU R                  _SU R                  _SU R                  _SU R                  _SU R                  _SU R                  _SU R                  _SU R                  _SU R                  _SU R                  _U R                  U R                  U R                  U R                  U R                  U R                  U R                  U R                  U R                  U R                  S.
En	U R                  US5      n
[!        U["        5      (       a  US   nU R                  US5      S-   n
U R                  X5      nUcK  U R                  US5      nUb  U R%                  X5        g U
c   SU;   a  SU;   a  U R                  X5        g g g g U" X5        g )Nr  rX  r  200r   r   depthUpdater   aggTrader   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  
forceOrderr  externalLockUpdaterl  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   eventMsgrX  r  r   rf  r   methodsr  r  r   s                r*   handle_messagebinance.handle_message"  s   >>'73G!!'8416#5&E/  1gt,(<(<bA96#
411
T&&
 ))
 T&&	

 t00
  1 1
 D//
 D//
 D//
 d11
 !$"5"5
 ++
 ++
 ++
 $--
  d11!
" t66#
$ $(#:#://'+':':!00"77#77"&":":11%)%H%H"&"5"57
:   #.gt$$1:D$$T3/&8E0>(($7I$//@ }#.sg~%%f6 8F.} 6#r,   )r  r4  r  )r   r"   )re   )F)NF)__name__
__module____qualname____firstlineno__r   r$   r&   r   r   r   r   r   r4  r	   r   r
   r   r   r  r   r  r  r%  r   r)  r(  rB  rF  rS  rN  rm  ru  ry  rd  r  r5  r  dictr  r   r  r  r  r  r  r  r  r  r  r  r  r  r   r!  r*  r)  r3  r8  r   r   r7  r0  rF  rI  rL  rO  rS  boolr;  r  r   r  r  r  r  rM  r  r  r   r  r  r  r   r  r  r  r   r  r  r  r  r   r  rJ  r   r   r  r   r   r,  r+  r5  r<  r:  rI  rL  rO  rU  r[  r^  ra  r}  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  __static_attributes____classcell__)r)   s   @r*   r   r      s
   E# En
`Y& Y3 # . BFTXac Ys Y3 Yc Yhlmxhy Y UYgktv 4bDI 4bc 4bad 4b{  AL  |M 4bl4A 4AlYv 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$ ?CQUdhqs ) ) )3 )^a )x|  ~B  yC ). aesw  AC =E$tCy/ =EZ] =Emp =E~ ^` >uT$s)_ >udg >u@ BFb T3 T3 TUX T(=16 =1~ 9; #C #v #J BFTXgktv 0V3 0V3 0Vc 0Vad 0V{  AE  |F 0Vd$, $,L 68  F * :< S    :>b Ew EW E* 6:" E7 E E0 9=R jg jC j* =A m' mPS m =? 
A 
A3 
A 9; =C =s =" 8<B FW F7 F& `dln  GL \s \U\ \  @D \|rh,v ,@Fv FFV F@I I*96 *9X "$ 2Os O>- -* )+ 1Qf 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Pi?V i?Vc s  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 7U 7UC 7Us 7Uimnsit 7Ur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&:Ax1f 6:[_hj .Os .O# .OUX .Oostyoz .O` ?CQU^` (@C (@ (@3 (@eijoep (@T1,v 1,f 37TX\eg 0UC 0Us 0URU 0Ulpqvlw 0Ud5=f 5=nD6 D:/$"f $"L0V 0G$V G$ G$r,   r   )%ccxt.async_supportccxt ccxt.async_support.base.ws.cacher   r   r   r   r  ccxt.base.typesr   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   !ccxt.async_support.base.ws.clientr   typingr   ccxt.base.errorsr   r   r   r   ccxt.base.preciser   async_supportr    r,   r*   <module>r     sh     A  A  Y  Y  Y  Y  Y 4  . ' ) * %VA$d  (( VA$r,   