
    ":h                         d dl Zd dlmZmZmZ d dlmZmZm	Z	m
Z
mZmZmZmZmZmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d d	lmZ  G d
 dej4                  j6                        Zy)    N)
ArrayCacheArrayCacheBySymbolByIdArrayCacheByTimestamp)
AnyBalancesIntOrder	OrderBookStrStringsTickerTickersTrade)Client)List)ArgumentsRequired)
BadRequest)NotSupported)NetworkErrorc            
       .    e Zd Zdef fdZi fdedefdZdefdZ	d+dZ
di fd	edefd
Zdi fd	ee   dedefdZddi fdeee      dedefdZd+dedefdZd,dededefdZddi fdedededee   fdZdefdZdi fdededefdZd ZdefdZd+defdZdefdZdddi fdedededee   fdZdddi fdedededee    fdZ!dddi fdedededee   fdZ"i fde#fd Z$defd!Z%d" Z&d# Z'i fd$Z(i fd%Z)d& Z*d' Z+defd(Z,defd)Z-defd*Z. xZ/S )-bingxreturnc                     | j                  t        t        |          ddddddddddddddddddd	iid
ddiddddddddddddddddidddddd d!d"iddd#d$d%d&d$d%d&d'did(d)d*id+      S ),NTF)wswatchTradeswatchTradesForSymbolswatchOrderBookwatchOrderBookForSymbols
watchOHLCVwatchOHLCVForSymbolswatchOrderswatchMyTradeswatchTickerwatchTickerswatchBalanceapir   z"wss://open-api-ws.bingx.com/marketz)wss://open-api-swap.bingx.com/swap-marketz(wss://open-api-cswap-ws.bingx.com/marketspotlinearinversei 6 gunzip
timeframes1m3m5m15m30m1h2h4h6h12h1d3d1w1M)r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   1min5min15min30min60min1day)r-   r/   r0   r1   r2   r7   )fetchBalanceSnapshotawaitBalanceSnapshotd     )depthintervalignoreDuplicates)listenKeyRefreshRater   swapr(   r%   r   r   r   	keepAlivei@w )hasurlsoptions	streaming)deep_extendsuperr   describe)self	__class__s    H/var/www/html/turnos/venv/lib/python3.12/site-packages/ccxt/pro/bingx.pyrQ   zbingx.describe   s$   eT ; =#)."&,0"(,#!%# $ $  D"M#M )0d !"""$$""""$""""#& !$$&&%$#	 -1,1!
 ! ##
 ! #-
 ' [0d WSL@
 L L	    symbolc                 .  K   | j                          d{    | j                  |      }d}d}d}| j                  d||      \  }}| j                  d||d      \  }}|dk(  r#| j	                  | j
                  d   d   |      }n"| j	                  | j
                  d   d   |      }|d   dz   }| j                  d	|d
         }| j                         }	|	|d}
|dk(  rd|
d<   | j                  ||| j                  |
|      |       d{   S 7 7 w)a  
        watches a price ticker, a statistical calculation with the information calculated over the past 24 hours for a specific market

        https://bingx-api.github.io/docs/#/en-us/spot/socket/market.html#Subscribe%20to%2024-hour%20Price%20Change
        https://bingx-api.github.io/docs/#/en-us/swapV2/socket/market.html#Subscribe%20to%2024-hour%20price%20changes
        https://bingx-api.github.io/docs/#/en-us/cswap/socket/market.html#Subscribe%20to%2024-Hour%20Price%20Change

        :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>`
        Nr#   r)   rI   r&   r   id@tickertickerrV   rX   dataTypesubreqType)
load_marketsmarkethandle_market_type_and_paramshandle_sub_type_and_paramssafe_stringrL   get_message_hashuuidwatchextend)rR   rV   paramsr`   
marketTypesubTypeurlsubscriptionHashmessageHashre   requests              rT   watch_tickerzbingx.watch_tickerb   s7     !!!V$
!??vW]^
F99-QWYab""499U#3D#97CC""499U#3D#9:FC!$<)3++HfX6FGyy{(
 !&GIZZ[$++gv2NP`aaa) 	"( bs"   DDC5DDDDclientc                    | j                  |di       }| j                  |d      }|j                  j                  d      dk\  }|rdnd}| j	                  |d d |      }|d   }| j                  ||      }	|	| j                  |<   |j                  |	| j                  d|             | j                  |d      d	k(  r"|j                  |	| j                  d             y y )
NdatasrI   r   r(   rV   rZ   r\   
all@ticker)	
safe_valuerc   rk   findsafe_marketparse_ws_tickertickersresolverd   )
rR   rp   messagerr   marketIdisSwapri   r`   rV   rZ   s
             rT   handle_tickerzbingx.handle_ticker   s    n w3##D#.(A-%V6
!!(D$
C!%%dF3%Vvt44XvFGGZ0L@NN64#8#8#BC ArU   Nc                    | j                  |d      }| j                  |d      }| j                  ||      }| j                  |d      }| j                  i d|d   d|d| j	                  |      d| j                  |d      d	| j                  |d
      d| j                  |d      d| j                  |d      d| j                  |d      d| j                  |d      dd d| j                  |d      d|d|dd d| j                  |d      dd dd | j                  |d      | j                  |d      |d|      S ) NCrs   crV   	timestampdatetimehighhlowlbidB	bidVolumebaskA	askVolumeavwapopenocloselastpreviousClosechangep
percentageaveragevq)
baseVolumequoteVolumeinfo)safe_integerrc   rw   safe_tickeriso8601)rR   r{   r`   r   r|   r   s         rT   rx   zbingx.parse_ws_ticker   s   . %%gs3	##GS1!!(F3  #. !
fX&!
!
 Y/!
 D$$Wc2	!

 4##GS1!
 4##GS1!
 ))'37!
 4##GS1!
 ))'37!
 D!
 D$$Wc2!
 U!
 E!
 T!
 d&&w4!
  $!!
" t#!
$ **7C8++GS9)!
* + 	rU   symbolsc                   K   | j                          d{    | j                  |dddd      }d}d}d}|du}|r| j                  |d         }| j                  d||      \  }}| j	                  d||d      \  }}|dk(  rt        | j                  dz         |d	k(  rt        | j                  d
z         g }dg}|rUt        dt        |            D ]<  }	||	   }
| j                  |
      }|j                  | j                  d|d                > n |j                  | j                  d             | j                  | j                  d   d   |      }| j                         }|dd}|dk(  rd|d<   | j                  ||| j                  ||      |       d{   }| j                   ri }|||d   <   |S | j"                  S 7 7 +w)a6  
        watches a price ticker, a statistical calculation with the information calculated over the past 24 hours for all markets of a specific list

        https://bingx-api.github.io/docs/#/en-us/swapV2/socket/market.html#Subscribe%20to%2024-hour%20price%20changes%20of%20all%20trading%20pairs

        :param str[] symbols: unified symbol of the market to watch the tickers for
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: a `ticker structure <https://docs.ccxt.com/#/?id=ticker-structure>`
        NTFr   r$   r)   r(   z3 watchTickers is not supported for spot markets yetr*   z6 watchTickers is not supported for inverse markets yetrt   rZ   rV   r&   r   r[   rI   r]   r^   )r_   market_symbolsr`   ra   rb   r   rX   rangelenappendrd   rc   rL   re   watch_multiplerO   
newUpdatesry   )rR   r   rh   firstMarketri   rj   symbolsDefinedmessageHashessubscriptionHashesirV   r`   rk   re   rn   resultnewDicts                    rT   watch_tickerszbingx.watch_tickers   s     !!!%%gtT4G
!-++gaj1K!??P[]cd
F99.+W]_ghtww)^^__itww)aabb*^1c'l+ X V,$$T%:%:8VHEU%VWX
   !6!6x!@Atyy/5w?yy{$
 !&GI**3t?O?OPWY_?`btuu??G(.GF8$%N||I 	"> vs"   G	GFG	G)G	G	limitc                   K   | j                  |dddd      }d}d}d}|du}|r| j                  |d         }| j                  d||      \  }}| j                  d||d      \  }}|dk(  rt	        | j
                  dz         |d	k(  rt	        | j
                  d
z         | j                  ||      }d}| j                  |ddd      \  }}| j                  d|dg d       dt        |      z   dz   t        |      z   dz   }	d|	z   }
g }|rUt        dt        |            D ]<  }||   }| j                  |      }|j                  | j                  d|d                > n |j                  | j                  d             | j                  | j                  d   d   |      }| j!                         }||
d}|dk(  rd|d<   ||||d}| j#                  ||| j%                  ||      |
g|       d{   }|j'                         S 7 w)ab  
        watches information on open orders with bid(buy) and ask(sell) prices, volumes and other data

        https://bingx-api.github.io/docs/#/en-us/swapV2/socket/market.html#Subscribe%20Market%20Depth%20Data%20of%20all%20trading%20pairs

        :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
        NTFr   r   r)   r(   z? watchOrderBookForSymbols is not supported for spot markets yetr*   zB watchOrderBookForSymbols is not supported for inverse markets yetrF   rD   rC      rD     rE   @mszall@	orderbookrV   r&   r   r[   rI   r]   r^   )r   r   rF   rh   )r   r`   ra   rb   r   rX   #get_order_book_limit_by_market_typehandle_option_and_paramscheck_required_argumentstrr   r   r   rd   rc   rL   re   r   rO   r   )rR   r   r   rh   r   ri   rj   r   rF   channelNamerl   r   r   rV   r`   rk   re   rn   subscriptionArgsr   s                       rT   watch_order_book_for_symbolsz"bingx.watch_order_book_for_symbols*  s]     %%gtT4G
!-++gaj1K!??@Z\giop
F99:TVaciksttww)jjkkitww)mmnn88UK88A[]gilm&$$%?:WlmE
*S03x=@4G!K/1c'l+ [ V,$$T%:%:;xHX%YZ[
   !6!6{!CDtyy/5w?yy{(
 !&GI 	"
 --c=$BRBRSZ\bBcfvew  zJ  K  K	   Ks   G-H/H0HsymbolsAndTimeframessincec                   K   t        |      }|dk7  r+t        |d   t              st        | j                  dz         | j                          d{    g }d}d}d}	d}
|dk7  r;| j                  |d      }| j                  |dddd      }| j                  |d         }
| j                  d|
|      \  }}| j                  d|
|d      \  }}|dk(  rt        | j                  d	z         |d
k(  rt        | j                  dz         | j                  | j                  |      }| j                  |di       }t        dt        |            D ]|  }||   }|d   }|d   }| j                  |      }| j                  |||      }|	|}	n|	|k7  rt!        | j                  dz         |j#                  | j%                  d|d   |	             ~ d|	z   }| j                  | j&                  d   d   |      }| j)                         }||d}|dk(  rd|d<   ||d}| j+                  ||||g|       d{   \  }}}| j,                  r|j/                  ||      }| j1                  |||dd      }| j3                  |||      S 7 ,7 Qw)a  
        watches historical candlestick data containing the open, high, low, and close price, and the volume of a market

        https://bingx-api.github.io/docs/#/en-us/swapV2/socket/market.html#Subscribe%20K-Line%20Data%20of%20all%20trading%20pairs

        :param str[][] symbolsAndTimeframes: array of arrays containing unified symbols and timeframes to fetch OHLCV data for, example [['BTC/USDT', '1m'], ['LTC/USDT', '5m']]
        :param int [since]: timestamp in ms of the earliest candle to fetch
        :param int [limit]: the maximum amount of candles to fetch
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns int[][]: A list of candles ordered, open, high, low, close, volume
        r   zd watchOHLCVForSymbols() requires a an array like  [['BTC/USDT:USDT', '1m'], ['LTC/USDT:USDT', '5m']]NTFr    r)   r(   z; watchOHLCVForSymbols is not supported for spot markets yetr*   z> watchOHLCVForSymbols is not supported for inverse markets yetr,      z< watchOHLCVForSymbols requires all timeframes to be the sameohlcvrV   z
all@kline_r&   r   r[   rI   r]   r^   )r   rh   )r   
isinstancelistr   rX   r_   get_list_from_object_valuesr   r`   ra   rb   r   	safe_dictrM   r   rc   r   r   rd   rL   re   r   r   getLimitfilter_by_since_limitcreate_ohlcv_object)rR   r   r   r   rh   symbolsLengthr   ri   rj   chosenTimeframer   r   marketOptionsr,   r   symbolAndTimeframesymtfr`   rawTimeframerl   rk   re   rn   r   rV   	timeframecandlesfiltereds                                rT   watch_ohlcv_for_symbolszbingx.watch_ohlcv_for_symbolsa  s     01Aj1Ea1H$&O#DGG  /U  %U  V  V!!!
A667KQOG))'4tUKG++gaj1K!??@VXcekl
F99:PR]_egoptww)ffggitww)iijjt||Z@^^M<D
q#234 
	dA!5a!8$Q'C#A&B[[%F++JB?L&". L0 +i!ijj  !6!6wx@PRa!bc
	d (/9tyy/5w?yy{(
 !&GI"
 ,0+>+>sMSZ]m\n  qA  ,B  &B"	7??$$VU3E--gueQM''	8DD] 	"T &Bs&   AJI=GJ.J /AJ Jri   c                     |d}|S |dk(  s|dk(  r| j                  g d|      }|S |dk(  r| j                  ddg|      }|S )NrC   rI   future)   
      2   rC   r(   r   )find_nearest_ceiling)rR   ri   r   s      rT   r   z)bingx.get_order_book_limit_by_market_type  sc    =E 	 V#zX'=112FN  v%112s)UCrU   unifiedChannelextrac                 >    |}|	|d|z   z  }n|dz  }||d|z   z  }|S )Nz::rs    )rR   r   rV   r   hashs        rT   rd   zbingx.get_message_hash  s=    D6M!DCKDD5L DrU   c                   K   | j                          d{    | j                  |      }|d   }d}d}d}| j                  d||      \  }}| j                  d||d      \  }}|dk(  r#| j	                  | j
                  d   d   |      }n"| j	                  | j
                  d   d   |      }|d   d	z   }	d
|z   }
| j                         }||	d}|dk(  rd|d<   | j                  ||
| j                  ||      |
       d{   }| j                  r|j                  ||      }| j                  |||dd      }| j                  ddd      r%| j                  |      }| j                  |d      }|S |S 7 ]7 tw)a6  
        watches information on multiple trades made in a market

        https://bingx-api.github.io/docs/#/spot/socket/market.html#Subscribe%20to%20tick-by-tick
        https://bingx-api.github.io/docs/#/swapV2/socket/market.html#Subscribe%20the%20Latest%20Trade%20Detail
        https://bingx-api.github.io/docs/#/en-us/cswap/socket/market.html#Subscription%20transaction%20by%20transaction

        :param str symbol: unified market symbol of the market orders were made in
        :param int [since]: the earliest time in ms to fetch orders for
        :param int [limit]: the maximum number of order structures to retrieve
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict[]: a list of `order structures <https://docs.ccxt.com/#/?id=order-structure>`
        NrV   r   r)   rI   r&   r   rX   @tradetrade::r[   r]   r^   r   TrG   )r_   r`   ra   rb   rc   rL   re   rf   rg   r   r   r   handle_option!remove_repeated_trades_from_arraysort_by)rR   rV   r   r   rh   r`   ri   rj   rk   rawHashrm   re   rn   tradesr   r   s                   rT   watch_tradeszbingx.watch_trades  s     !!!V$!
!??vW]^
F99-QWYab""499U#3D#97CC""499U#3D#9:FC,)&(yy{
 !&GIzz#{DKK4PR]^^??OOFE2E++FE5+tTm-?F==fEH||Hk:HO; 	"* _s#   E8E3C*E8E6A2E86E8c                    | j                  |dg       }| j                  |d      }|j                  d      d   }|j                  j	                  d      dk\  }|rdnd}| j                  |d d |      }|d   }	d|	z   }
d }t        |t              r| j                  ||      }n| j                  ||      g}| j                  | j                  |	      }|7| j                  | j                  d	d
      }t        |      }|| j                  |	<   t        dt        |            D ]  }|j!                  ||           |j#                  ||
       y )Nrr   r\   r   r   rI   r(   rV   r   tradesLimitr   )ru   rc   splitrk   rv   rw   r   r   parse_tradesparse_trader   r   rM   r   r   r   r   rz   )rR   rp   r{   rr   r   r|   r}   ri   r`   rV   rm   r   storedr   js                  rT   handle_tradeszbingx.handle_trades  s=   d w3""7J7==%a((A-%V6
!!(D$
C!&(dD!&&tV4F&&tV45Ff5>%%dllM4HE&F"(DKKq#f+& 	%AMM&)$	%v{+rU   c                 X  K   | j                          d{    | j                  |      }d}d}d}| j                  d||      \  }}| j                  d||d      \  }}|dk(  r#| j	                  | j
                  d   d   |      }n"| j	                  | j
                  d   d   |      }| j                  ||      }dt        |      z   }d}	|dk7  rF|d	   sA| j                  |dd
d      \  }	}| j                  d|	d
g d       |dz   t        |	      z   dz   }|d   dz   |z   }
| j                  d|d         }| j                         }||
d}|dk(  rd|d<   i }|d	   r||d}n||	|d}| j                  ||| j                  ||      |
|       d{   }|j                         S 7 7 w)a8  
        watches information on open orders with bid(buy) and ask(sell) prices, volumes and other data

        https://bingx-api.github.io/docs/#/en-us/spot/socket/market.html#Subscribe%20Market%20Depth%20Data
        https://bingx-api.github.io/docs/#/en-us/swapV2/socket/market.html#Subscribe%20Market%20Depth%20Data
        https://bingx-api.github.io/docs/#/en-us/cswap/socket/market.html#Subscribe%20to%20Limited%20Depth

        :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)   rI   r&   r   rE   r(   r*   rF   rD   r   r   r   rX   r   rV   r[   r]   r^   )countrh   )levelrF   rh   )r_   r`   ra   rb   rc   rL   r   r   r   r   rd   re   rf   rO   r   )rR   rV   r   rh   r`   ri   rj   rk   r   rF   rl   rm   re   rn   r   r   s                   rT   watch_order_bookzbingx.watch_order_bookI  s     !!!V$
!??@PRXZ`a
F99:JFTZ\de""499U#3D#97CC""499U#3D#9:FC88UKE
*)$#'#@#@IY[egj#k &,,-=xUjk)C/#h-?$F!$<#-;++K9IJyy{(
 !&GI!#)   $  
 **S+t7G7GQW7XZjl|}}	  S 	"P ~s"   F*F%E9F*F(F*(F*c                 t    | j                  |dd      }| j                  |dd      }|j                  ||       y )Nr   r   r   r   )safe_float_2store)rR   booksidedeltapriceamounts        rT   handle_deltazbingx.handle_delta  s8    !!%C0""5!S1uf%rU   c           	         | j                  |di       }| j                  |d      }|j                  d      }|d   }|dk(  }| j                  |d|      }|j                  j	                  d      dk\  }	|	rdnd}
| j                  |d d |
      }|d   }| j                  | j                  |      }|B|}|j                  |   }| j                  |d	      }| j                  i |      | j                  |<   | j                  |   }d }| j                  |d
d      }| j                  |d
d|      }|d   r| j                  |||dddd      }n| j                  |||dddd      }| j                  |d      }||d<   |j                  |       | j                  d|      }|j                  ||       |r$| j                  d      }|j                  ||       y y )Nrr   r\   r   r   allrV   rI   r(   r   r   tsr*   bidsasksr   r   r   lastUpdateIdnoncer   )r   rc   r   rk   rv   rw   ru   
orderbookssubscriptionsr   
order_booksafe_integer_2parse_order_bookresetrd   rz   )rR   rp   r{   rr   r\   parts	firstPartisAllEndpointr|   r}   ri   r`   rV   r   rl   subscriptionr   snapshotr   r  rm   messageHashForAlls                         rT   handle_order_bookzbingx.handle_order_book  s   H ~~gvr2##GZ8s#!H	"e+##D(I>(A-%V6
!!(D$
C!OODOOV<	'!//0@AL%%lG<E&*oob%&@DOOF#OOF+	''dC	''k4K	),,T69ffVY[^_H,,T69ffVWYZ[H!!$7!!++K@y+. $ 5 5k BNN9&78 rU   c           	          |d   rdnd}|d   r	|d   rdnd}| j                  ||      | j                  |d      | j                  |d      | j                  |d      | j                  |d	      | j                  |d
      gS )Nr(   tTrI   r*   r   r   r   r   r   )r   safe_number)rR   r   r`   r   s       rT   parse_ws_ohlcvzbingx.parse_ws_ohlcv  s     #6NC	&> &y 1IeY/UC(UC(UC(UC(UC(
 	
rU   c                    |j                   j                  d      dk\  }| j                  |d      }|j                  d      }|d   }|dk(  }| j                  |d|      }|rdnd}	| j	                  |d d |	      }
d }|r.|
d   r| j                  |d	i       g}n;| j                  |d	g       }n'| j                  |d	i       }| j                  |d
i       g}|
d   }| j                  | j                  |i       | j                  |<   |j                  d      d   }| j                  | j                  |	      }| j                  |di       }| j                  ||      }| j                  | j                  |   |      >|}|j                  |   }| j                  |d      }t        |      | j                  |   |<   | j                  |   |   }t        dt        |            D ]*  }||   }| j!                  ||
      }|j#                  |       , |||g}| j%                  d||      }|j'                  ||       |r&| j%                  dd |      }|j'                  ||       y y )NrI   r   r\   r   r  rs   r(   r*   rr   KrV   _r   r,   r   r   )rk   rv   rc   r   rw   r   	safe_listru   ohlcvsrM   find_timeframer	  r   r   r   r   r  r   rd   rz   )rR   rp   r{   r}   r\   r  r  r  r|   ri   r`   r   rr   rV   r   r   r,   unifiedTimeframerl   r  r   r   r   candleparsedresolveDatarm   r  s                               rT   handle_ohlcvzbingx.handle_ohlcv  se   B (A-##GZ8s#!H	"e+##GS)<%V6
!!(D$
Ci >>'62>?..&"=>>'626D~~dC45G!"oodkk62FF~~c*1-t||Z@^^M<D
..|ZH??4;;v.=E'!//0@AL%%lG<E4I%4PDKK 01V$%56q#g,' 	"AQZF((8FMM&!	" /8++GV=MN{K0 $ 5 5gtEU VNN;(9: rU   r-   c                 z  K   | j                          d{    | j                  |      }d}d}d}	| j                  d||      \  }}| j                  d||d      \  }}|dk(  r#| j	                  | j
                  d   d   |      }	n"| j	                  | j
                  d   d   |      }	|	t        | j                  dz   |z   dz         | j                  | j                  |i       }
| j                  |
d	i       }| j	                  |||      }| j                  d
|d   |      }|d   dz   |z   }| j                         }||d}|dk(  rd|d<   ||d}| j                  |	|| j                  ||      ||       d{   }|d   }| j                  r|j                  ||      }| j!                  |||dd      S 7 7 ?w)a  
        watches historical candlestick data containing the open, high, low, and close price, and the volume of a market

        https://bingx-api.github.io/docs/#/en-us/spot/socket/market.html#K-line%20Streams
        https://bingx-api.github.io/docs/#/en-us/swapV2/socket/market.html#Subscribe%20K-Line%20Data
        https://bingx-api.github.io/docs/#/en-us/cswap/socket/market.html#Subscribe%20to%20Latest%20Trading%20Pair%20K-Line

        :param str symbol: unified symbol of the market to fetch OHLCV data for
        :param str timeframe: the length of time each candle represents
        :param int [since]: timestamp in ms of the earliest candle to fetch
        :param int [limit]: the maximum amount of candles to fetch
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns int[][]: A list of candles ordered, open, high, low, close, volume
        Nr   r)   rI   r&   r   z! watchOHLCV is not supported for z	 markets.r,   r   rV   rX   z@kline_r[   r]   r^   )rF   rh      r   T)r_   r`   ra   rb   rc   rL   r   rX   ru   rM   rd   re   rf   rg   r   r   r   )rR   rV   r   r   r   rh   r`   ri   rj   rk   rM   r,   r   rm   rl   re   rn   r   r   r   s                       rT   watch_ohlcvzbingx.watch_ohlcvq  s     !!!V$
!??fV\]
F99,PVX`a""499U#3D#97CC""499U#3D#9:FC;TWW'JJZWZeeff//$,,
B?__WlB?
''
IyI++GVH5EyQ!$<)3lByy{(
 !&GI$"
 zz#{DKK4PRbdtuuq	??NN651E))%q$GGC 	": vs"   F;F6E"F;9F9:=F;9F;c                    K   | j                          d{    | j                          d{    d}d}d}|| j                  |      }|d   }| j                  d||      \  }}| j	                  d||d      \  }}|dk(  }d}	d}
|r|	n|
}d}d	}|r|n|}||d
|z   z  }| j                         }d}d}|dk(  r@|dk(  rt        | j                  dz         | j                  | j                  d   d   |      }n(| j                  | j                  d   d   |      }|ddd}|dz   | j                  d   z   }| j                  ||||       d{   }| j                  r|j                  ||      }| j                  ||||d      S 7 p7 [7 =w)a!  
        watches information on multiple orders made by the user

        https://bingx-api.github.io/docs/#/en-us/spot/socket/account.html#Subscription%20order%20update%20data
        https://bingx-api.github.io/docs/#/en-us/swapV2/socket/account.html#Order%20update%20push
        https://bingx-api.github.io/docs/#/en-us/cswap/socket/account.html#Order%20update%20push

        :param str [symbol]: unified market symbol of the market orders are made in
        :param int [since]: the earliest time in ms to watch orders for
        :param int [limit]: the maximum number of order structures to retrieve
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict[]: a list of `order structures <https://docs.ccxt.com/#/?id=order-structure>`
        NrV   r!   r)   r(   spot:privateswap:private
spot:order
swap:order:rI   r*   z: watchOrders is not supported for inverse swap markets yetr&   r   r]   spot.executionReportrX   r^   r\   ?listenKey=	listenKeyTr_   authenticater`   ra   rb   re   r   rX   rc   rL   rM   rf   r   r   filter_by_symbol_since_limit)rR   rV   r   r   rh   typerj   r`   isSpotspotHashswapHashrl   spotMessageHashswapMessageHashrm   re   baseUrlrn   rk   orderss                       rT   watch_orderszbingx.watch_orders  s     !!!!!![[(FH%F99-QWXf99-QWYab&.!!'-88&&)/o_3<'Kyy{6>)#"477-i#ijj&&tyy'7'=wGG&&tyy'7'=tDG 2G
 %[(AAzz#{G=MNN??OOFE2E00tTTK 	"!B O2   FFFF	DFF8F	FFc                    K   | j                          d{    | j                          d{    d}d}d}|| j                  |      }|d   }| j                  d||      \  }}| j	                  d||d      \  }}|dk(  }d}	d}
|r|	n|
}d}d	}|r|n|}||d
|z   z  }| j                         }d}d}|dk(  r@|dk(  rt        | j                  dz         | j                  | j                  d   d   |      }n(| j                  | j                  d   d   |      }|ddd}|dz   | j                  d   z   }| j                  ||||       d{   }| j                  r|j                  ||      }| j                  ||||d      S 7 p7 [7 =w)a%  
        watches information on multiple trades made by the user

        https://bingx-api.github.io/docs/#/en-us/spot/socket/account.html#Subscription%20order%20update%20data
        https://bingx-api.github.io/docs/#/en-us/swapV2/socket/account.html#Order%20update%20push
        https://bingx-api.github.io/docs/#/en-us/cswap/socket/account.html#Order%20update%20push

        :param str [symbol]: unified market symbol of the market the trades are made in
        :param int [since]: the earliest time in ms to watch trades for
        :param int [limit]: the maximum number of trade structures to retrieve
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict[]: a list of `trade structures <https://docs.ccxt.com/#/?id=trade-structure>`
        NrV   r"   r)   r(   r)  r*  spot:mytradesswap:mytradesr-  rI   r*   z< watchMyTrades is not supported for inverse swap markets yetr&   r   r]   r.  r/  r0  r1  Tr2  )rR   rV   r   r   rh   r5  rj   r`   r6  r7  r8  rl   r9  r:  rm   re   r;  rn   rk   r   s                       rT   watch_my_tradeszbingx.watch_my_trades  s     !!!!!![[(FH%F99/6SYZf99/6SY[cd&.!!'-88)))/o_3<'Kyy{6>)#"477-k#kll&&tyy'7'=wGG&&tyy'7'=tDG 2G
 %[(AAzz#{G=MNN??OOFE2E00tTTK 	"!B Or>  c                 d  K   | j                          d{    | j                          d{    d}d}| j                  dd|      \  }}| j                  dd|d      \  }}|dk(  }d}d}d}d}|r|n|}	|r|n|}
d}d}| j	                         }|dk(  r@|d	k(  rt        | j                  d
z         | j                  | j                  d   d   |      }n'| j                  | j                  d   d   |      }|dd}|dz   | j                  d   z   }| j                  |      }| j                  ||||
|       d}d}| j                  |ddd      \  }}| j                  |ddd      \  }}|r|r|j                  |dz          d{    | j                  ||	||
       d{   S 7 7 {7 &7 w)a  
        query for balance and get the amount of funds available for trading or funds locked in orders

        https://bingx-api.github.io/docs/#/en-us/spot/socket/account.html#Subscription%20account%20balance%20push
        https://bingx-api.github.io/docs/#/en-us/swapV2/socket/account.html#Account%20balance%20and%20position%20update%20push
        https://bingx-api.github.io/docs/#/en-us/cswap/socket/account.html#Account%20balance%20and%20position%20update%20push

        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: a `balance structure <https://docs.ccxt.com/#/?id=balance-structure>`
        Nr%   r)   r(   zspot:balancer*  zswap:balancerI   r*   z; watchBalance is not supported for inverse swap markets yetr&   r   ACCOUNT_UPDATEr[   r0  r1  rA   TrB   F:fetchBalanceSnapshot)r_   r3  ra   rb   re   r   rX   rc   rL   rM   rp   set_balance_cacher   r   rf   )rR   rh   r5  rj   r6  spotSubHashswapSubHashr9  r:  rm   rl   rn   r;  re   rk   rp   rA   rB   s                     rT   watch_balancezbingx.watch_balance  s     !!!!!!99.$PVWf99.$PVX`a&.$$(()/o_*0;kyy{6>)#"477-j#jkk&&tyy'7'=wGG&&tyy'7'=tDG,G %[(AAS!vtW6FO##'+'D'DV^]suy'z$f'+'D'DV^]suz'{$f$8--'> >???ZZ[';KLLLG 	"!B @LsD   F0F&F0F)EF0F,F0!F."F0)F0,F0.F0c                     ||j                   v ry | j                  |ddd      }|rE|dz   }||j                  vr1|j                  |       | j	                  | j
                  ||||       y y i | j                  |<   y )Nr%   rA   TrE  )r	  r   futuresr   spawnload_balance_snapshotbalance)rR   rp   r5  rj   rl   rh   rA   rm   s           rT   rF  zbingx.set_balance_cache=  s    v333#<<V^Ukmqr!88K6>>1k*

455v{DRYZ 2 "$DLLrU   c                 >  K   | j                  ||d       d {   }| j                  || j                  | j                  |i             | j                  |<   |j                  |   }|j                          |j                  | j                  |   |dz          y 7 w)N)r5  rj   :balance)fetch_balancerg   ru   rN  rK  rz   )rR   rp   rm   r5  rj   responser   s          rT   rM  zbingx.load_balance_snapshotI  s     ++Tg,NOO![[4??4<<QUWY3Z[T,t||D)4*+<= Ps   BBBBc                     | j                  |d      }	 |A| j                  dz   | j                  |      z   }| j                  | j                  d   ||       y# t
        $ r}|j                  |       Y d }~yd }~ww xY w)Ncode exactT)rc   rX   jsonthrow_exactly_matched_exception
exceptions	Exceptionreject)rR   rp   r{   rT  feedbackes         rT   handle_error_messagezbingx.handle_error_messageQ  s     0	77S=499W+==44T__W5MtU]^   	MM!	s   AA 	A<!A77A<c                   K   | j                  | j                  d      }|y 	 | j                  d|i       d {    | j                  | j                  dd	      }| j                  || j                  |       y 7 ?# t        $ r}g d}t	        dt        |            D ]  }||   }| j                  d   d   |   dz   |z   }| j                  |      }t        |j                  j                               }	t	        dt        |	            D ]  }
|	|
   }|j                  ||         d | j                  d<   d| j                  d<   Y d }~y d }~ww xY ww)
Nr1  r'   r   r&   r   r0  lastAuthenticatedTimerH   6 )rc   rM    userAuthPrivatePutUserDataStreamrZ  r   r   rL   rp   r   rK  keysr[  r   delaykeep_alive_listen_key)rR   rh   r1  errortypesr   r5  rk   rp   r   r   rm   rH   s                rT   re  zbingx.keep_alive_listen_keyd  sP    $$T\\;?		77i8PQQQ  $00?UW^_

')C)CVL! R 	1E1c%j) 6Qxii&t,T2]BYNS) $V^^%8%8%: ;q#m"45 6A"/"2KMM%566 )-DLL%45DLL01	s>    EA; A9A; ;E9A; ;	ECE
E
EEc                 |  K   | j                         }| j                  | j                  dd      }| j                  | j                  dd      }||z
  |kD  rd| j                          d {   }| j	                  |d      | j                  d<   || j                  d<   | j                  || j                  |       y y 7 Qw)Nr`  r   rH   ra  r1  )millisecondsr   rM   !userAuthPrivatePostUserDataStreamrc   rd  re  )rR   rh   timer`  rH   rR  s         rT   r3  zbingx.authenticate|  s       " $ 1 1$,,@WYZ [#00?UW^_''*>>!CCEEH(,(8(8;(ODLL%48DLL01JJ+T-G-GP	 ?Es   A&B<(B:)AB<c                 v  K   	 |dk(  r|j                  d       d {    y | j                  |d      }| j                  |d      }|j                  ||d       d {    y 7 F7 # t        $ rE}t        | j                  dz   | j                  |      z         }|j                  |       Y d }~y d }~ww xY ww)NPingPongpingrk  )pongrk  z pong failed with error )sendrc   rZ  r   rX   rW  r  )rR   rp   r{   ro  rk  r]  rf  s          rT   rp  z
bingx.pong  s     	 & kk&)))''8''8kk  #   	 *  	  +E!E		RS!TUELL	 sZ   B9A( A$A( B9;A( A&A( #B9$A( &A( (	B61;B1,B91B66B9c                 t   d|v }| j                  |ddi       }| j                  -| j                  | j                  dd      }t	        |      | _        | j                  }| j                  |      }|j                  |       |d   }d}	d}
|r|	n|
}|j                  ||       |j                  ||d	z   |z          y )
Nr\   rr   r   ordersLimitr   rV   r+  r,  r-  )safe_value_2r<  r   rM   r   parse_orderr   rz   )rR   rp   r{   r6  rr   r   r   parsedOrderrV   r7  r8  rm   s               rT   handle_orderzbingx.handle_order  s    h '  &#r:;;%%dllM4HE07DK&&t,k"X&#)hv{+v{S069:rU   c                    d|v }| j                  |ddi       }| j                  }|/| j                  | j                  dd      }t	        |      }|| _        |rdnd}| j                  |d      }| j                  |d d	|      }	| j                  ||	      }
|
d
   }d}d}|r|n|}|j                  |
       |j                  ||       |j                  ||dz   |z          y )Nr\   rr   r   r   r   r(   rI   rs   -rV   r@  rA  r-  )
safe_dict_2myTradesr   rM   r   rc   rw   r   r   rz   )rR   rp   r{   r6  r   cachedTradesr   r5  r|   r`   r"  rV   r7  r8  rm   s                  rT   handle_my_tradeszbingx.handle_my_trades  s    r '!!'63;}}%%dllM4HE1%8L(DMvV##FC0!!(D#t<!!&&1!"""(hhF#|[1|[3%6%?@rU   c                 
   | j                  |di       }| j                  |dg       }| j                  |dd      }d|v rdnd}|| j                  vri | j                  |<   || j                  |   d<   || j                  |   d	<   | j	                  |      | j                  |   d
<   t        dt        |            D ]{  }||   }| j                  |d      }	| j                  |	      }
| j                         }||d<   | j                  |d      |d<   | j                  |d      |d<   || j                  |   |
<   } | j                  | j                  |         | j                  |<   |j                  | j                  |   |dz          y )Nr   r   r  EPrI   r(   r   r   r   r   lkusedwbfreerP  )r   r  r  rN  r   r   r   rc   safe_currency_codeaccountsafe_balancerz   )rR   rp   r{   r   rr   r   r5  r   rN  
currencyIdrT  r  s               rT   handle_balancezbingx.handle_balanceM  s~   J NN7C,~~ab)''c:	(v$!#DLL%)T6"*3T;')-i)@T:&q#d)$ 	/A1gG))'37J**:6DllnG%GFO"..w=GFO"..w=GFO'.DLLt$	/ "..t||D/ABTt||D)4*+<=rU   c                 :   | j                  ||      sy |dk(  sd|v r| j                  | j                  ||       y | j                  |dd      }|j	                  d      dk\  r| j                  ||       y |j	                  d      dk\  r| j                  ||       y |j	                  d      dk\  r| j                  ||       y |j	                  d	      dk\  r| j                  ||       y |j	                  d
      dk\  rO| j                  |di       }| j                  |d      }|dk(  r| j                  ||       | j                  ||       y | j                  |d      }|dk(  r| j                  ||       |dk(  re| j                  ||       | j                  |di       }| j                  |d      }| j                  |d      }|dk(  r|dk(  r| j                  ||       | j                  |d      }| j                  |d      }	|	dk(  r| j                  ||       y y )Nrm  ro  r\    z@depthr   rY   r   z@klineexecutionReportrr   xTRADEr]  rD  ORDER_TRADE_UPDATEr   XFILLED	24hTicker)r^  rL  rp  rc   rv   r  r~   r   r$  ru   r}  rw  r  )
rR   rp   r{   r\   rr   r5  r]  statusmsgDatamsgEvents
             rT   handle_messagezbingx.handle_message  s   ((9v6W#4JJtyy&'2##GZ<=="a'""673==#q(vw/=="a'vw/=="a'fg.==*+q0??7FB7D##D#.Dw%%fg6fg.Wc*  0$$fg.??7C4D##D#.D%%dC0Ff&8%%fg6//'62##GS1{"vw/ #rU   )N)NN)0__name__
__module____qualname__r   rQ   r   r   ro   r   r~   rx   r   r   r   r   r   r
   r   r   r   r   rd   r   r   r   r   r   r  r   r  r$  r'  r	   r=  rB  r   rI  rF  rM  r^  re  r3  rp  rw  r}  r  r  __classcell__)rS   s   @rT   r   r      s   M# M^ 68  b  bF  bDBDF BDH0d 6:" .7 . .` SW_a 5!$s) 5!C 5!fo 5!n aesw  AC =E$tCy/ =EZ] =Emp =E~c # s C s  <@d[] + +S + +bfglbm +Zf,F f,P @DB 6!S 6! 6!S\ 6!p&
e9 e9N
D 
4h;6 h;T 8<$]ajl 0H 0HC 0HWZ 0Hquvzq{ 0Hd 04$UYbd 3U 3UC 3Us 3Uimnsit 3Uj 37TX\eg 3UC 3Us 3URU 3Ulpqvlw 3Uj *, .M .M`
$ 
$>& 24 M0 )+ Q 0a;FJAv JAX8>V 8>t(0V (0rU   r   )ccxt.async_supportccxt ccxt.async_support.base.ws.cacher   r   r   ccxt.base.typesr   r   r   r	   r
   r   r   r   r   r   !ccxt.async_support.base.ws.clientr   typingr   ccxt.base.errorsr   r   r   r   async_supportr   r   rU   rT   <module>r     sE     f f f f f 4  . ' ) )^0D$$ ^0rU   