
    eiY                         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dS )    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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           
          |                      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    K/var/www/html/volatility/venv/lib/python3.11/site-packages/ccxt/pro/ndax.pyr   zndax.describe   s}    dD 1 1 : : < <"&#).#"  S 7	 ?
 ?
   	    c                 z    |                      |                     | j        dd          d          }|| j        d<   |S )N	requestIdr      )sumsafe_integeroptions)r   r!   s     r   
request_idzndax.request_id)   s;    HHT..t|[!LLaPP	$-[!r   symbolc                   K   |                      | j        dd          }|                                  d{V  |                     |          }d}|dz   |d         z   }| j        d         d         }|                                 }|t          |d                   d	}	d
|||                     |	          d}
|                     |
|          }| 	                    ||||           d{V S )a  
        watches a price ticker, a statistical calculation with the information calculated over the past 24 hours for a specific market

        https://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$</it$OO%%	t--
 
 7##	
 
 ++gv..ZZ[';GGGGGGGGGr   clientc                     |                      |di           }|                     |          }|d         }|                     |          }|| j        |<   d}|dz   |d         z   }|                    ||           d S )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   s    //'3334 ""7++!V$$%V Sj6$</v{+++++r   Nsincelimitc                 T  K   |                      | j        dd          }|                                  d{V  |                     |          }|d         }d}|dz   |d         z   }| j        d         d	         }	|                                 }
|t          |d                   d
d}d|
||                     |          d}|                     ||          }| 	                    |	|||           d{V }| j
        r|                    ||          }|                     |||dd          S )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   sU      !!$,;;!!!!!!!!!V$$! Sj6$</it$OO%%	t-- #
 
 7##	
 
 ++gv..zz#{G[IIIIIIII? 	3OOFE22E))&%TRRRr   c                    |                      |dg           }d}i }t          dt          |                    D ]}|                     ||                   }|d         }|                      | j        |          }	|	+|                     | j        dd          }
t          |
          }	|	                    |           |	| j        |<   d||<   t          |
                                          }t          dt          |                    D ]^}||         }|                     |          }|dz   |d	         z   }|                      | j        |          }	|                    |	|           _d S )
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   sd   //'333( !q#g,,'' 		# 		#A$$WQZ00E8_F//$+v>>K"))$,tLL(//u%%%"-DK"GFOOw||~~&&q#g,,'' 	5 	5AQZF[[((F*vd|3K//$+v>>KNN;4444	5 	5r   1m	timeframec           	        K   |                      | j        dd          }|                                  d{V  |                     |          }|d         }d}|dz   |z   dz   |d         z   }	| j        d         d	         }
|                                 }|t          |d                   t          |                     | j        ||                    d
d}d||| 	                    |          d}| 
                    ||          }|                     |
|	||	           d{V }| j        r|                    ||          }|                     |||dd          S )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'   re   rJ   rK   r:   r)   r5   r;   r<   r=   r!   r>   r?   r@   ohlcvs                   r   watch_ohlcvzndax.watch_ohlcv   s~      !!$,;;!!!!!!!!!V$$! Sj9,s2VD\Ait$OO%%	t--D,,T_iSSTT #	
 
 7##	
 
 ++gv..jjk7KHHHHHHHH? 	2NN6511E))%q$GGGr   c                 .   |                      |dg           }i }t          dt          |                    D ]}||         }|                     |d          }|                     |          }|d         }	i ||<   |                      | j        |	i           | j        |	<   t          | j                                                  }
t          dt          |
                    D ]3}|
|         }|                     | j        ||          }t          |          dz  }| 
                    |d          }|                     ||z  |z            |                     |d          |                     |d          |                     |d          |                     |d	          |                     |d
          g}|                      | j        |	         |g           }t          |          }|r|d         ||dz
           d         k    r||dz
           }|d         |d         t          |d         |d                   t          |d         |d                   |d	         |                     |d
         |d
                   g||dz
  <   d||         |<   nv|r|d         ||dz
           d         k     r|                    |           | 
                    | j        dd          }||k    r|                    d           d||         |<   || j        |	         |<   5d}t          |                                          }t          dt          |                    D ]}||         }t          ||                                                   }t          dt          |                    D ]m}||         }|dz   |z   dz   |z   }|                     |          }|d         }	|                      | j        |	         |g           }|                    ||           nd S )Nr3   r      r'   rX      r"            T
OHLCVLimitrg   r+   )rD   rY   rZ   ri   safe_marketohlcvsr]   rj   r^   r6   r$   parse_to_int
safe_floatmaxminr#   r\   r%   poprG   )r   rB   r@   r>   r_   r1   rk   marketIdr5   r'   r^   jre   intervaldurationrP   parsedstoredlengthpreviousrK   r;   	marketIdsrj   r<   s                            r   handle_ohlcvzndax.handle_ohlcv   s    //'333" q#g,,'' +	8 +	8AAJE''q11H%%h//FH%F "GH"&//$+vr"J"JDK,,..//D1c$ii(( #8 #8 G	++DOY	RRx==4/ --eQ77	%%y8';x&GHHOOE1--OOE1--OOE1--OOE1--OOE1-- V)<iLLV <vayF6A:,>q,AAA%fqj1Hq	 F1Ix{33F1Ix{33q	HQK88*F6A:& 48GH%i00 <6!9vfqj/A!/D#D#D f--- $ 1 1$,d S S!U??"JJqMMM7;))417F#I..G#8H !((	q#i..)) 		4 		4A |Hgh/446677J1c*oo.. 4 4&qM	"Sj94s:XE))(33)V)<iLLv{33334		4 		4r   c           	      J  K   |                      | j        dd          }|                                  d{V  |                     |          }|d         }d}|dz   |d         z   }| j        d         d	         }|                                 }	|d
n|}|t          |d                   |d}
d|	||                     |
          d}|	||||d         | j        ||d}| 	                    ||          }| 
                    |||||           d{V }|                                S )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  sh      !!$,;;!!!!!!!!!V$$! Sj6$</it$OO%%	Et--	
 
 7##	
 
 &t9	
 	
 ++gv..**S+w\ZZZZZZZZ	   r   c                    |                      |dg           }|                      |dg           }|                     |d          }|d S |                     |          }|d         }|                      | j        |          }|d S d }	d }
t	          dt          |                    D ]O}||         }|	|                     |d          }	n&|                     |d          }t          |	|          }	|
|                     |d          }
n&|                     |d          }t          |
|          }
|                     |d          }|                     |d          }|                     |d          }|                     |d	          }|dk    r|d
         n|d         }|dk    r|	                    ||           |dk    r|	                    ||           3|dk    r|	                    |d           Q|
|d<   |	|d<   | 
                    |	          |d<   d}|dz   |z   }|| j        |<   |                    ||           d S )Nr3   r      r'   rp   ro      rn   	   bidsasksr"   noncerP   datetimer   r+   )rD   ri   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  ss    //'333 oogq"55##K33F!!(++!OODOV<<	F	q#g,,'' 	. 	.AQZF  --fa88		#00;;	<88	}))&!44,,VQ77E8,,$$VQ//DOOFA..E__VQ//F$$VQ//D26!))If--)FBSMqyy##E62222##E62222##E1---"	'!*	+ $Y 7 7	* Sj8+"+y+.....r   c                 P   |                      |dg           }|                     |d          }|                     ||          }|                     |d          }|                     ||          }|| j        |<   |                     |d          }	|                    ||	           d S )Nr3   r'   rK   r<   )rD   ri   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3" !!,99((&99!!,88OOHe44	"+&&|]CCy+.....r   c                     |                      |j        d          }|                     |d          }|                     ||          }|'|                     |d          }| ||||           d S d S d S )Nr,   r1   r   )index_bysubscriptionsr$   rD   )r   rB   r@   subscriptionsByIdr,   r   r   s          r   handle_subscription_statuszndax.handle_subscription_status  s     !MM&*>EEw,,'8"==#__\8<<F!vw55555 $#!!r   c           	      F   |                      |d          }|d S t          j        |          |d<   | j        | j        | j        | j        | j        | j        | j        | j        d}|                      |d          }|                     ||          }| |||           d S d S )Nr3   )r   r*   Level2UpdateEventLevel1UpdateEventrM   TradeDataUpdateEventrg   TickerDataUpdateEventr2   )	ri   r7   loadsr   rI   r   rc   r   rD   )r   rB   r@   r>   methodseventr   s          r   handle_messagezndax.handle_message  s    . ""7C00?Fz'**#>#1!%!7!%!3#1$($6#0%)%6	
 	
   #..%00F67##### 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      sB       #      2  
 68 H H HF H H H H>!,F !, !, !, !,F <@d[] #S #S #SS #S #Sbfglbm #S #S #S #SJ'5F '5 '5 '5 '5R ?CQUdhqs %H %H %H %H3 %H^a %Hx|  ~B  yC %H %H %H %HNR46 R4 R4 R4 R4h @DB ,! ,!S ,! ,!S\ ,! ,! ,! ,!\C/ C/ C/ C/ C/J /V  /  /  /  /D6 6 6 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      s        7 7 7 7 7 7  > > > > > > > > > > > > > > 4 4 4 4 4 4      y$ y$ y$ y$ y$4" y$ y$ y$ y$ y$r   