
    ":hY                         d dl Zd dlmZ d dlZd dlmZmZmZm	Z	m
Z
 d dlmZ d dlmZ  G d dej                  j                         Zy)    N)
ArrayCache)AnyInt	OrderBookTickerTrade)Client)Listc            
            e Zd Zdef fdZd Zi fdedefdZ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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e	fdZde	fdZde	fdZde	fdZ xZS )ndaxreturnc           
      j    | j                  t        t        |          dddddddddiddidd      S )	NTF)wswatchOrderBookwatchTradeswatchTradesForSymbolswatchTicker
watchOHLCVr   z7wss://ndaxmarginstaging.cdnhop.net:10456/WSAdminGatewa/zwss://api.ndax.io/WSGateway)testapi)hasurls)deep_extendsuperr   describe)self	__class__s    G/var/www/html/turnos/venv/lib/python3.12/site-packages/ccxt/pro/ndax.pyr   zndax.describe   s]    dD : <"&#).#" S 7	?
  	    c                 ~    | j                  | j                  | j                  dd      d      }|| j                  d<   |S )N	requestIdr      )sumsafe_integeroptions)r   r!   s     r   
request_idzndax.request_id)   s9    HHT..t||[!LaP	$-[!r   symbolc                   K   | j                  | j                  dd      }| j                          d{    | j                  |      }d}|dz   |d   z   }| j                  d   d   }| j                         }|t        |d         d	}	d
||| j                  |	      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://apidoc.ndax.io/#subscribelevel1

        :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>`
        omsIdr"   NSubscribeLevel1:idr   r   )OMSIdInstrumentIdr   mino)
r$   r%   load_marketsmarketr   r&   intjsonextendwatch)r   r'   paramsr)   r5   namemessageHashurlr!   payloadrequestmessages               r   watch_tickerzndax.watch_ticker.   s      !!$,,;!!!V$ Sj6$</iit$OO%	t-
 7#	
 ++gv.ZZ[';GGG% 	"$ Hs"   1CCBCC	CCclientc                     | j                  |di       }| j                  |      }|d   }| j                  |      }|| j                  |<   d}|dz   |d   z   }|j	                  ||       y )Nr3   r'   r*   r+   r,   )
safe_valueparse_tickerr5   tickersresolve)	r   rB   r@   r>   tickerr'   r5   r;   r<   s	            r   handle_tickerzndax.handle_tickerM   sq    //'334 ""7+!V$%V Sj6$</v{+r   Nsincelimitc                   K   | j                  | j                  dd      }| j                          d{    | j                  |      }|d   }d}|dz   |d   z   }| j                  d   d	   }	| j                         }
|t        |d         d
d}d|
|| j                  |      d}| j                  ||      }| j                  |	|||       d{   }| j                  r|j                  ||      }| j                  |||dd      S 7 7 9w)a  
        get the list of most recent trades for a particular symbol

        https://apidoc.ndax.io/#subscribetrades

        :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
        :returns dict[]: a list of `trade structures <https://docs.ccxt.com/#/?id=public-trades>`
        r)   r"   Nr'   SubscribeTradesr+   r,   r   r   d   )r-   r.   IncludeLastCountr   r/   	timestampT)r$   r%   r4   r5   r   r&   r6   r7   r8   r9   
newUpdatesgetLimitfilter_by_since_limit)r   r'   rJ   rK   r:   r)   r5   r;   r<   r=   r!   r>   r?   r@   tradess                  r   watch_tradeszndax.watch_tradesp   s     !!$,,;!!!V$! Sj6$</iit$OO%	t- #
 7#	
 ++gv.zz#{G[II??OOFE2E))&%TRR- 	"& Js"   1D
DBD
D8D
D
c                 r   | j                  |dg       }d}i }t        dt        |            D ]  }| j                  ||         }|d   }| j                  | j                  |      }	|	(| j                  | j                  dd      }
t        |
      }	|	j                  |       |	| j                  |<   d||<    t        |j                               }t        dt        |            D ]Q  }||   }| j                  |      }|dz   |d	   z   }| j                  | j                  |      }	|j                  |	|       S y )
Nr3   rM   r   r'   tradesLimit  Tr+   r,   )rD   rangelenparse_traderT   r$   r%   r   appendlistkeysr5   rG   )r   rB   r@   r>   r;   updatesr1   trader'   tradesArrayrK   symbolsr5   r<   s                 r   handle_tradeszndax.handle_trades   s/   //'33( !q#g,' 		#A$$WQZ0E8_F//$++v>K"))$,,tL(/u%"-DKK"GFO		# w||~&q#g,' 	5AQZF[[(F*vd|3K//$++v>KNN;4	5r   1mc           	      n  K   | j                  | j                  dd      }| j                          d{    | j                  |      }|d   }d}|dz   |z   dz   |d   z   }	| j                  d   d	   }
| j                         }|t        |d         t        | j                  | j                  ||            d
d}d||| j                  |      d}| j                  ||      }| j                  |
|	||	       d{   }| j                  r|j                  ||      }| j                  |||dd      S 7 7 9w)a  
        watches historical candlestick data containing the open, high, low, and close price, and the volume of a market

        https://apidoc.ndax.io/#subscribeticker

        :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
        r)   r"   Nr'   SubscribeTickerr+   r,   r   r   rN   )r-   r.   IntervalrO   r   r/   T)r$   r%   r4   r5   r   r&   r6   safe_string
timeframesr7   r8   r9   rQ   rR   rS   )r   r'   	timeframerJ   rK   r:   r)   r5   r;   r<   r=   r!   r>   r?   r@   ohlcvs                   r   watch_ohlcvzndax.watch_ohlcv   sA     !!$,,;!!!V$! Sj9,s2VD\Aiit$OO%	t-D,,T__iST #	
 7#	
 ++gv.jjk7KHH??NN651E))%q$GG/ 	"( Is"   1D5D1CD59D3:8D53D5c                    | j                  |dg       }i }t        dt        |            D ]\  }||   }| j                  |d      }| j	                  |      }|d   }	i ||<   | j                  | j
                  |	i       | j
                  |	<   t        | j                  j                               }
t        dt        |
            D ]  }|
|   }| j                  | j                  ||      }t        |      dz  }| j                  |d      }| j                  ||z  |z        | j                  |d      | j                  |d      | j                  |d      | j                  |d	      | j                  |d
      g}| j                  | j
                  |	   |g       }t        |      }|ro|d   ||dz
     d   k(  r^||dz
     }|d   |d   t        |d   |d         t        |d   |d         |d	   | j                  |d
   |d
         g||dz
  <   d||   |<   na|r|d   ||dz
     d   k  rd|j!                  |       | j                  | j"                  dd      }||k\  r|j%                  d       d||   |<   || j
                  |	   |<    _ d}t        |j                               }t        dt        |            D ]  }||   }t        ||   j                               }t        dt        |            D ]]  }||   }|dz   |z   dz   |z   }| j	                  |      }|d   }	| j                  | j
                  |	   |g       }|j'                  ||       _  y )Nr3   r      r'   rX      r"            T
OHLCVLimitrf   r+   )rD   rY   rZ   rh   safe_marketohlcvsr]   ri   r^   r6   r$   parse_to_int
safe_floatmaxminr#   r\   r%   poprG   )r   rB   r@   r>   r_   r1   rk   marketIdr5   r'   r^   jrj   intervaldurationrP   parsedstoredlengthpreviousrK   r;   	marketIdsri   r<   s                            r   handle_ohlcvzndax.handle_ohlcv   su    //'33" q#g,' +	8AAJE''q1H%%h/FH%F "GH"&//$++vr"JDKK,,./D1c$i( #8 G	++DOOY	Rx=4/ --eQ7	%%y8';x&GHOOE1-OOE1-OOE1-OOE1-OOE1- V)<iLVvayF6A:,>q,AA%fqj1Hq	 F1Ix{3F1Ix{3q	HQK8*F6A:& 48GH%i06!9vfqj/A!/D#D f- $ 1 1$,,d S!U?"JJqM7;))417F#I.G#8+	8X !(	q#i.) 		4A |Hgh/4467J1c*o. 4&qM	"Sj94s:XE))(3)V)<iLv{34		4r   c           	        K   | j                  | j                  dd      }| j                          d{    | j                  |      }|d   }d}|dz   |d   z   }| j                  d   d	   }| j                         }	|d
n|}|t        |d         |d}
d|	|| j                  |
      d}|	||||d   | j                  ||d}| j                  ||      }| j                  |||||       d{   }|j                         S 7 7 w)a%  
        watches information on open orders with bid(buy) and ask(sell) prices, volumes and other data

        https://apidoc.ndax.io/#subscribelevel2

        :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
        r)   r"   Nr'   SubscribeLevel2r+   r,   r   r   rN   )r-   r.   Depthr   r/   )r,   r<   r;   r'   r{   methodrK   r:   )r$   r%   r4   r5   r   r&   r6   r7   handle_order_book_subscriptionr8   r9   rK   )r   r'   rK   r:   r)   r5   r;   r<   r=   r!   r>   r?   subscriptionr@   	orderbooks                  r   watch_order_bookzndax.watch_order_book9  s2     !!$,,;!!!V$! Sj6$</iit$OO%	Et-	
 7#	
 &t99	
 ++gv.**S+w\ZZ	  A 	"> [s"   1DDB9D-D.DDc                    | j                  |dg       }| j                  |dg       }| j                  |d      }|y | j                  |      }|d   }| j                  | j                  |      }|y d }	d }
t	        dt        |            D ]  }||   }|	| j                  |d      }	n| j                  |d      }t        |	|      }	|
| j                  |d      }
n| j                  |d      }t        |
|      }
| j                  |d      }| j                  |d      }| j                  |d      }| j                  |d	      }|dk(  r|d
   n|d   }|dk(  r|j                  ||       |dk(  r|j                  ||       |dk(  s|j                  |d        |
|d<   |	|d<   | j                  |	      |d<   d}|dz   |z   }|| j                  |<   |j                  ||       y )Nr3   r      r'   rp   ro      rn   	   bidsasksr"   noncerP   datetimer   r+   )rD   rh   rt   
orderbooksrY   rZ   r$   rx   rw   storeiso8601rG   )r   rB   r@   r>   firstBidAskr{   r5   r'   r   rP   r   r1   bidasknewTimestampnewNoncetypepriceamountsideorderbookSider;   r<   s                         r   handle_order_bookzndax.handle_order_bookg  s    //'33 oogq"5##K3!!(+!OODOOV<		q#g,' 	.AQZF  --fa8	#00;	<8	}))&!4,,VQ7E8,$$VQ/DOOFA.E__VQ/F$$VQ/D26!)If-)FBSMqy##E62##E62##E1-3	.4 #	'!*	+ $Y 7	* Sj8+"+y+.r   c                     | j                  |dg       }| j                  |d      }| j                  ||      }| j                  |d      }| j	                  ||      }|| j
                  |<   | j                  |d      }	|j                  ||	       y )Nr3   r'   rK   r<   )rD   rh   parse_order_bookr$   
order_bookr   rG   )
r   rB   r@   r   r>   r'   snapshotrK   r   r<   s
             r   r   z#ndax.handle_order_book_subscription  s     //'33" !!,9((&9!!,8OOHe4	"+&&|]Cy+.r   c                     | j                  |j                  d      }| j                  |d      }| j                  ||      }| | j                  |d      }| ||||       y y y )Nr,   r1   r   )index_bysubscriptionsr$   rD   )r   rB   r@   subscriptionsByIdr,   r   r   s          r   handle_subscription_statuszndax.handle_subscription_status  sp     !MM&*>*>Ew,'8"=#__\8<F!vw5 " $r   c           	      t   | j                  |d      }|y t        j                  |      |d<   | j                  | j                  | j
                  | j                  | j                  | j                  | j                  | j                  d}| j                  |d      }| j                  ||      }|
 |||       y y )Nr3   )r   r*   Level2UpdateEventLevel1UpdateEventrM   TradeDataUpdateEventrf   TickerDataUpdateEventr2   )	rh   r7   loadsr   rI   r   rc   r   rD   )r   rB   r@   r>   methodseventr   s          r   handle_messagezndax.handle_message  s    . ""7C0?zz'*#>>#11!%!7!7!%!3!3#11$($6$6#00%)%6%6	
   #.%067# r   )__name__
__module____qualname__r   r   r&   strr   rA   r	   rI   r   r
   r   rU   rc   r]   rl   r   r   r   r   r   r   r   __classcell__)r   s   @r   r   r      s&   # 2
 68 H HF H>!,F !,F <@d[] #S #SS #S #Sbfglbm #SJ'5F '5R 8<$]ajl %H %HC %HWZ %Hquvzq{ %HNR46 R4h @DB ,!S ,! ,!S\ ,!\C/ C/J /V  /D6 6"($V ($r   r   )ccxt.async_supportccxt ccxt.async_support.base.ws.cacher   r7   ccxt.base.typesr   r   r   r   r   !ccxt.async_support.base.ws.clientr	   typingr
   async_supportr    r   r   <module>r      s6     7  > > 4 y$4"" y$r   