
    ɤ,g_                     :   d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZmZ  ed      Z	e	d   Z
e
d   Ze
d   Ze
d	   Z ej                         Zej                  d
 ee      id dd      Zej                  d
 ee      id dd      Zed   Z ee      ZdefdZy)    N)
KiteTicker)MongoClient)datedatetimez7mongodb://jenya:DJenya$Mongo%40St0ckDB@127.0.0.1:27017/trade_iqzerodha_credentialsordersholdingsr      )_idenctoken)r   	websocketr   )
socket_urlc                 0   | D ci c]  }|d   |
 c}t               adat        dt	              d      D cg c]!  }t        j                               ||dz    # c}fd}fd}d }d }d }d	 }|t        _        |t        _	        |t        _
        |t        _        |t        _        |t        _        t        j                  d
       d}	t	        t              t	              k  rt        j                         rW|	t	              k  rI|	   }
t!        j"                  d|	dz           t        j%                  t        j&                  |
       |	dz  }	t)        j*                  d       t	        t              t	              k  ry y c c}w c c}w )Ninstrument_tokenr   d   c                    |D ]a  }|d   }|j                  d      }|v s|t        vs(|+|   }|d   }|d   }||z  }|d   }	t        j                         j	                  d      }
t
        j                  d|i      }|sgt        t        j                               |dg ddddd	t        j                         t        j                         d
||	d	}t
        j                  |       |d   d   j                  |||t        j                         d       |d   d   d   |z   }|d   d   d   |z   }|dkD  r||z  nd}|t        |d      t        |d      d|d   d<   t        j                         |d<   t
        j                  d|id|i       t        j                  d| d| d| d       t        j!                  |       t"        dz  at%        t              t%              k(  r8t        j                  d       | j'                         | j)                           y t"        dz  dk(  s,t        j                  d       | j'                         | j)                           y  y )Nr   
last_pricesymbolquantityexchangez%Y-%m-%dT%H:%M:%Sr   )total_quantitytotal_avg_pricetotal_invested)summarytotali  )	r   r   total_current_valuer
   
created_atlast_updatedallocated_balancer   r   r
   r   )r   	avg_priceinvested_amountexecuted_atr   r   r      r   z$setzExecuted paper trade for z
 at price z with quantity .r   z3All orders have been executed. Stopping the ticker.   z6200 executions completed. Unsubscribing current batch.)getexecuted_ordersr   utcnowstrftimeholdings_collectionfind_onestruuiduuid4
insert_oneappendround
update_onelogginginfoaddexecution_countlenunsubscribeclose)wstickstickr   ltporderr   r   r"   r   	timestampholding_entryr   r   r   	order_maptokens_batchess                   /var/www/html/trade_iq/orders.pyon_tickszplace_order.<locals>.on_ticks)   s     P	D#$67((<(C9,1A1X]`]l!"23x ,"*S. ,$OO-667JK	 !4 < <>PRb=c d$"4::<0,</0')233423&% '/oo&7(0(9-1"($,!%M$ (22=Aj))4;; (!$'6#+??#4	=  "/z!:7!CDT!UX`!`!.z!:7!CDT!UXg!gESVWEW.>"A]^ '5',_a'@&+NA&>6j)'2 190An- $..')9:]+
 8
3%_g^hhijk  ##$451$ '3y>9LL!VWNN>2HHJ #S(A-LL!YZNN>2HHJaP	    c                 `    t        j                  d       D ]  }| j                  |        y )NzConnected successfully.)r4   r5   	subscribe)r;   responsebatchrC   s      rD   
on_connectzplace_order.<locals>.on_connect   s+    ./# 	 ELL	 rF   c                 :    t        j                  d| d|        y )NzConnection closed:  - r4   r5   r;   codereasons      rD   on_closezplace_order.<locals>.on_close   s    *4&F8<=rF   c                 :    t        j                  d| d|        y )NzConnection error: rM   rN   rO   s      rD   on_errorzplace_order.<locals>.on_error   s    )$s6(;<rF   c                 4    t        j                  d|        y )NzReconnecting: rN   )r;   attempts_counts     rD   on_reconnectz!place_order.<locals>.on_reconnect   s    ~n%567rF   c                 .    t        j                  d       y )NzReconnect failed.rN   )r;   s    rD   on_noreconnectz#place_order.<locals>.on_noreconnect   s    ()rF   T)threadedz### Setting LTP mode for batch r   )setr(   r7   ranger8   listkeystickerrE   rR   rT   rK   rW   rY   connectis_connectedr4   r5   set_modeMODE_LTPtimesleep)r	   r?   irE   rK   rR   rT   rW   rY   current_batchrJ   rB   rC   s              @@rD   place_orderrh      sc    @FFe)*E1FI eOO BGq#i.Z]A^_Ad9>>+,Qq3w7_NSl >=8* FOFOFO"F&F*F NNDN! M
o
Y
/ ]S5H%H"=1ELL:=1;L:MNOOOFOOU3QM

1 o
Y
/E G `s
   F&F)r4   rd   r.   zerodha.zerodha_tickerr   pymongor   r   r   clientdatabasecreds_collectionorders_collectionr+   todayr,   r-   enctoken_objwebsocket_objr   r_   r]   rh    rF   rD   <module>rs      s       -  # 
N	O*12 X& z* 

  ((&#e*)=qVW?XY ))63u:*>XY@Z[+&		y	)L LrF   