o
    ªöÊj¹  ã                   @   sÎ   d Z ddlZddlZddlZddlZddlmZ ddlZee	ƒ 
¡ jZejd Zej deeƒ¡ ddlmZmZ ed Zg d¢Zdd	„ Zddd„Zdd„ Zedkredejv rZeƒ neejdd… Ž  dS dS )a‰  
deepv_outcomes.py -- [2026-10-10] The user: for the Vs, how many REVERSED and how much they went up (not
necessarily positive at the close), and how many KEPT GOING DOWN. Every V of the day, not only the deepest:
  V        a bar whose low is the lowest of the 3 bars on each side, after a >= 1% drop from the 30-bar high,
           9:45-15:15 (confirmed when the 3 bars after it are closed = the earliest a bot could know it)
  outcome  the highest high after the V until its low breaks (or 15:55):
             bounce from the low      -> KEPT DOWN (< 1% then broke the low) / 1-2% / 2-3% / 3-5% / >= 5%
             gain from the confirmation close (3 bars after the low) -> what a bot buying then could get at best
           held the low to the close or not; the close vs the V low and vs the confirmation price
  split    drop into the V (1-2 / 2-3 / 3-5 / 5+ %), the V's number in the day (1st, 2nd, 3rd+), scanner-like
Universe: all $5-15 stocks >= 500k shares (bars10y) and the v3 top 30. HINDSIGHT outcome counts (no trading rule).
    python3 deepv_outcomes.py [START] [END]     default 2024-10-10 .. 2026-10-09   (-> deepv_outcomes.csv.gz / .log)
é    N)ÚPathÚopen_window)Úv3_top30Úarrayszdeepv_outcomes.csv.gz)ú	kept downú1-2%ú2-3%ú3-5%ú>=5%c                 C   s  | d | d | d | d f\}}}}g }d}t ddƒD ]ñ}|| }|||d |d …  ¡ ksD|td|d ƒ|…  ¡ | d d	 d
k rEq|dkrR||d  |krRq|d7 }t ||d d… |k ¡d }	t|	ƒrs|d t|	d ƒ nd}
||d |
… }t|ƒr‹| ¡ | d d	 nd}||d  }||d |
… }t|ƒr©| ¡ | d d	 nd}|dk r±dn|dk r·dn|dk r½dn|dk rÃdnd}| |||td|d ƒ|…  ¡ | d d	 ||d  d d	 |||tt|	ƒ ƒt|	ƒrõ|
| nd|d | d d	 |d | d d	 dœ¡ q|S )Nr   é   é   é   é   éZ  é   é   éd   g      ð?i‚  g        r   r   r   é   r	   r
   éÿÿÿÿi�  )ÚmÚnÚdropÚvs_openÚbounceÚclsÚup_confÚheldÚbroke_afterÚclose_vs_lowÚclose_vs_conf)ÚrangeÚminÚmaxÚnpÚwhereÚlenÚintÚappend)ÚaÚOÚHÚLÚCÚoutÚn_vÚjÚloÚbrkÚendÚsegr   ÚcpÚseg2r   r   © r6   ú:/var/www/screener/trade/reports/playbook/deepv_outcomes.pyÚvs_of   s0   $F   4>$
ýr8   ú
2024-10-10ú
2026-10-09c              	      s6  ‡ ‡fdd„t td  d¡ƒD ƒ}tjtddd��u}d }|D ]h}|jd d… }t t |d	¡¡}t	|ƒ}d
}	|d  
¡ D ]A\}
}t|ƒ}|d u rIq<t|ƒD ]/}||
t|
|v ƒdœdd„ | 
¡ D ƒ¥}|d u rstj|t|ƒd�}| ¡  | |¡ |	d7 }	qMq<~t||	dd� qW d   ƒ n1 s‘w   Y  tƒ  d S )Nc                    s2   g | ]}ˆ|j d d…   krˆ krn n|‘qS )Né
   )Úname)Ú.0Úf©r2   Ústartr6   r7   Ú
<listcomp>:   s   2 zmain.<locals>.<listcomp>Úbars10yz*/*.json.gzÚwtÚ )Únewliner;   Úrtr   Úbars)ÚdayÚsymÚv3c                 S   s0   i | ]\}}|t |ttjfƒrt|d ƒn|“qS )r   )Ú
isinstanceÚfloatr#   ÚfloatingÚround)r=   ÚxÚyr6   r6   r7   Ú
<dictcomp>F   s   0 zmain.<locals>.<dictcomp>)Ú
fieldnamesr   T)Úflush)ÚsortedÚOWÚglobÚgzipÚopenÚOUTr<   ÚjsonÚloadr   Úitemsr   r8   r&   ÚcsvÚ
DictWriterÚlistÚwriteheaderÚwriterowÚprintÚreport)r@   r2   ÚfilesÚfhÚwr>   rH   ÚdÚtopÚkÚsrG   r(   ÚvÚrowr6   r?   r7   Úmain9   s.   "ÿûòþ
rm   c                     sL  dd„ dD ƒ} t  td¡�}t |¡D ]}| D ]‰| ˆ  |ˆ ¡ qqW d   ƒ n1 s.w   Y  dd„ |  ¡ D ƒ‰ dD ]‰ˆ ˆ  t¡ˆ ˆ< q>ˆ d dkˆ d< ˆ d	 dkˆ d	< t	t
ˆ d
 ƒƒ}tdt	ˆ d
 ƒ› d|› d�ƒ dˆ d fdt t	ˆ d
 ƒt¡ffD �]\}}td|› d| ¡ | d›d�ƒ tddd›ddd›d�d dd„ tD ƒ¡ d ƒ d|fg}dD ]$\}}| d |› d!|d"k rÈ|nd#› d$�|ˆ d% |k@ ˆ d% |k @ f¡ q¸d&ˆ d' d(kfd)ˆ d' d*kfd+ˆ d' d,kffD ]\}	}
| |	||
@ f¡ qôd-D ]\}}}| ||ˆ d. |k@ ˆ d. |k @ f¡ �q|D ]O\}‰ˆ ¡ }|�s-�q td|d›d|| d/›d�d ‡ ‡fd0d„tD ƒ¡ dt ˆ d	 ˆ ¡d1›dt ˆ d2 ˆ ¡d3›d4t ˆ d5 ˆ d6k¡d7›� ƒ �q |ˆ d% d,k@ ‰td8ˆ ¡ | d›d9t ˆˆ d: d,k@ ¡| d›d;t ˆˆ d< d=k@ ¡| d›d>�ƒ qƒd S )?Nc                 S   s   i | ]}|g “qS r6   r6   )r=   ri   r6   r6   r7   rQ   P   s    zreport.<locals>.<dictcomp>)rH   rJ   r   r   r   r   r   r   r   r   r   rF   c                 S   s   i | ]
\}}|t  |¡“qS r6   )r#   Úarray)r=   ri   rk   r6   r6   r7   rQ   U   s    )r   r   r   r   r   r   r   rJ   Ú1r   rH   Ú
z Vs, z¯ days. HINDSIGHT outcomes: bounce = highest high after the V low until the low breaks; 'from confirm' = from the close 3 bars after the low (the earliest a bot can know the V)zSCANNER-LIKE (v3 top 30)zALL $5-15 STOCKSz
===== z: z.1fz Vs per day =====z  rD   Ú22ú zVs/dayz>7z | c                 s   s   � | ]}|d ›V  qdS )z>9Nr6   ©r=   Úcr6   r6   r7   Ú	<genexpr>^   s   € zreport.<locals>.<genexpr>z; | held to close | median up from confirm | close > confirmzall Vs))r   r   )r   r   )r   r   )r   éc   zdrop ú-rv   ú+ú%r   z1st V of the dayr   r   z2nd Vr   z3rd+ Vr   ))zV 9:45-11:00r   éZ   )zV 11:00-13:00rz   éÒ   )zV 13:00-15:15r{   r   r   z7.1fc                 3   s*   � | ]}t  ˆ d  ˆ |k¡d›V  qdS )r   z9.0%N)r#   Úmeanrs   ©ÚDri   r6   r7   ru   k   s   €( z13.0%r   z+21.2fz% | r   r   z14.0%z  drop >= 3%: per day z -> reversed >= 3% r   z/day, kept down r   r   z/day)rW   rX   rY   r]   Ú
DictReaderr'   r\   ÚastyperL   r%   Úsetrb   r#   ÚonesÚboolÚsumÚjoinÚCLASSESr|   Úmedian)Úcolsre   ÚrÚndaysÚlabÚbaseÚgroupsr0   ÚhiÚnthÚselÚtlr(   ÚbÚgr   r6   r}   r7   rc   O   sH   ÿÿÿ . 2
B6*4H
ÿ2ÿîrc   Ú__main__z--reportr   r   )r9   r:   )Ú__doc__r]   rW   rZ   ÚsysÚpathlibr   Únumpyr#   Ú__file__ÚresolveÚparentÚHERErU   ÚpathÚinsertÚstrÚup2_styles_buildr   r   rY   r†   r8   rm   rc   Ú__name__Úargvr6   r6   r6   r7   Ú<module>   s*    

# € ÿ