
    isX                         d dl Zd dlmZ d dlZd dl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)Int	OrderBookTickerTrade)Client)Listc            
            e Zd Z 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 )ndaxc           	          |                      t          t          |                                           ddddddddiddidd          S )NT)wswatchOrderBookwatchTrades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    S/var/www/html/crypto-bot/backend/venv/lib/python3.11/site-packages/ccxt/pro/ndax.pyr   zndax.describe   sz    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   symbolreturnc                   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
        :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'   r3   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 )Nr1   r$   r(   r)   r*   )
safe_valueparse_tickerr3   tickersresolve)	r   r@   r>   r<   tickerr$   r3   r9   r:   s	            r   handle_tickerzndax.handle_tickerI   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
        :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"   r2   r3   r   r#   r4   r5   r6   r7   
newUpdatesgetLimitfilter_by_since_limit)r   r$   rH   rI   r8   r'   r3   r9   r:   r;   r   r<   r=   r>   tradess                  r   watch_tradeszndax.watch_tradesl   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 )
Nr1   rK   r   r$   tradesLimit  Tr)   r*   )rB   rangelenparse_traderR   r!   r"   r   appendlistkeysr3   rE   )r   r@   r>   r<   r9   updatesr/   trader$   tradesArrayrI   symbolsr3   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   1mc           	        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 )aQ  
        watches historical candlestick data containing the open, high, low, and close price, and the volume of a market
        :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   rL   )r+   r,   IntervalrM   r   r-   T)r!   r"   r2   r3   r   r#   r4   safe_string
timeframesr5   r6   r7   rO   rP   rQ   )r   r$   	timeframerH   rI   r8   r'   r3   r9   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 )Nr1   r      r$   rV      r            T
OHLCVLimitrd   r)   )rB   rW   rX   rf   safe_marketohlcvsr[   rg   r\   r4   r!   parse_to_int
safe_floatmaxminr    rZ   r"   poprE   )r   r@   r>   r<   r]   r/   ri   marketIdr3   r$   r\   jrh   intervaldurationrN   parsedstoredlengthpreviousrI   r9   	marketIdsrg   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
        :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   rL   )r+   r,   Depthr   r-   )r*   r:   r9   r$   ry   methodrI   r8   )r!   r"   r2   r3   r   r#   r4   r5   handle_order_book_subscriptionr6   r7   rI   )r   r$   rI   r8   r'   r3   r9   r:   r;   r   r<   r=   subscriptionr>   	orderbooks                  r   watch_order_bookzndax.watch_order_book/  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 )Nr1   r      r$   rn   rm      rl   	   bidsasksr   noncerN   datetimer   r)   )rB   rf   rr   
orderbooksrW   rX   r!   rv   ru   storeiso8601rE   )r   r@   r>   r<   firstBidAskry   r3   r$   r   rN   r   r/   bidasknewTimestampnewNoncetypepriceamountsideorderbookSider9   r:   s                         r   handle_order_bookzndax.handle_order_bookZ  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 )Nr1   r$   rI   r:   )rB   rf   parse_order_bookr!   
order_bookr   rE   )
r   r@   r>   r   r<   r$   snapshotrI   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*   r/   r   )index_bysubscriptionsr!   rB   )r   r@   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 )Nr1   )r   r(   Level2UpdateEventLevel1UpdateEventrK   TradeDataUpdateEventrd   TickerDataUpdateEventr0   )	rf   r5   loadsr   rG   r   ra   r   rB   )r   r@   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#   strr   r?   r   rG   r   r	   r   rS   ra   r[   rj   r   r   r   r   r   r   r   __classcell__)r   s   @r   r   r      s$           0  
 68 H H HF H H H H8!,F !, !, !, !,F <@d[]  S  S  SS  S  Sbfglbm  S  S  S  SD'5F '5 '5 '5 '5R 8<$]ajl "H "H "HC "HWZ "Hquvzq{ "H "H "H "HHR46 R4 R4 R4 R4h @DB )! )!S )! )!S\ )! )! )! )!V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   r5   ccxt.base.typesr   r   r   r   !ccxt.async_support.base.ws.clientr   typingr	   async_supportr    r   r   <module>r      s        7 7 7 7 7 7  9 9 9 9 9 9 9 9 9 9 9 9 4 4 4 4 4 4      l$ l$ l$ l$ l$4" l$ l$ l$ l$ l$r   