o
    ÜtÛj–  ã                   @   s´   d Z 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Z	ee
ƒ ¡ jZej deeƒ¡ ej dd¡ ddlZddlmZ edƒZd\ZZd	d
„ Zdd„ ZedkrXeƒ  dS dS )aO  
exit_trail_noise.py -- [2026-10-23] the user: test the 1% trailing stop COMBINED with noise2 (the Star bot's noise filter),
on the SAME 3,809 setup buys (premarket/data/star_exit_setups.jsonl: R1 R1A R3 R4 R5 R7 R8 R9, 1 year, buy at the next
open, 0.2% cost).
  T0  plain 1% trailing stop, intrabar (stop 1% under the buy, raised to 1% under every new high; sold at the stop)
  T1  the same trail checked at the BAR CLOSE (close below the trail stop -> sell at the next open) -- no noise filter
  T2  T1 + noise2 as in the Star bot: if the close is >= VWAP AND the dip from the high since the buy < 2 x the mean 1-min
      range of the last 15 bars -> HOLD (the trail stays); + the 3% safety stop (intrabar) like the Star bot
  T3  T1 + noise2 WITHOUT the VWAP condition (only "the dip is normal 1-min noise") + the 3% safety stop
  T4  T3 but the hard floor is UNDER THE SETUP'S SUPPORT (support x 0.998, clamped 0.5%..4% under the buy) instead of 3%
Also shown: Star's sells and the 2-year test's plain trail on the same trades.
    nice -n 19 python3 exit_trail_noise.py   -> exit_trail_noise.log
é    N)Údefaultdict)ÚPathz/var/www/screener/trade)ÚMODSz7/var/www/screener/premarket/data/star_exit_setups.jsonl)gš™™™™™É?g       @c                 C   s‚  |}|d }t dd„ | d |… D ƒƒ}t dd„ | d |… D ƒƒ}	t|t| ƒƒD �]}
| |
 }||d |d  |d  d |d	  7 }|	|d	 7 }	|	rN||	 n|d }|d
kry|d |krx|
|krgt||d ƒn|}|| d d t |
df  S n$|r�|d |kr�|
|krŒt||d ƒn|}|| d d t |
df  S |d |kr®|d }t||d ƒ}||
 dkrÅ|d | d d t |
df  S |d
k�r+|d |k �r+|
d t| ƒk �r+|dv �rt dd„ | td|
d ƒ|
d … D ƒƒtd|
d ƒ }||d  t| k �o|dk�p|d |k}|�rq'| |
d  d | d d t |
d df  S q'| d d | d d t t| ƒd dfS )Nç®Gáz®ï?c                 s   s4   � | ]}|d  |d  |d  d |d  V  qdS )ÚhÚlÚcé   ÚvN© ©Ú.0Úbr   r   úexit_trail_noise.pyÚ	<genexpr>!   ó   €2 zsim.<locals>.<genexpr>c                 s   s   � | ]}|d  V  qdS )r
   Nr   r   r   r   r   r   !   s   € r   r   r   r	   r
   ÚT0Úoé   éd   ÚtrailÚfloori�  z15:55©ÚT2ÚT3ÚT4c                 s   s    � | ]}|d  |d  V  qdS )r   r   Nr   )r   Úxr   r   r   r   3   s   € r   é   é   r   ztrail (close)éÿÿÿÿÚend)ÚsumÚrangeÚlenÚminÚCOSTÚmaxÚK)ÚbarsÚminsÚbiÚPÚmoder   Úhir   ÚpvÚcvÚjr   ÚvwÚpxÚr1Únoiser   r   r   Úsim   s4   4Hþ"*
6,.€*r5   c                     sš  dd„ t tƒD ƒ} ttƒ}| D ]‰|ˆd   ˆ¡ qg }t|ƒD ]ú}t t  t	j
d d|› d� d¡¡}|| D ]Ú‰ˆd ‰t	j |ˆ¡}t t	j
d	 | ˆ› d
�  ¡ ¡}dd„ t	 |d ˆ d ¡D ƒ}dd„ |D ƒ}t‡fdd„t|ƒD ƒd ƒ}	t‡fdd„t|ƒD ƒd ƒ}
|	d u s’|
d u r“q9ˆd }tˆd   |d |	d … ||ˆd ¡\}}|p°i  d¡p·|d }tt|d |d ƒ|d ƒ}ˆd |ˆd ˆd ˆd dœ}ddd |d! fd"|d! fd#|ffD ]&\}}t|||
|||ƒ\}}}|||< || ˆd$  ||d% < |||d& < qç| |¡ q9t	jj ¡  ~q!td'd(„ |D ƒƒ}|t|ƒd)  ‰ ‡ fd*d+„}tt|ƒ› d,t|ƒ› d-|d. › d/|d0 › d1t› d2�
ƒ td3d4d5›d6d7d8›d9d:d;›d9d<d;›d9d=d;›d9d>d;›d9d?d;›d9d@d;›�ƒ |dA|ƒ tD ]‰|ˆ‡fdBd„|D ƒƒ �qydCD ]\}‰‰||‡‡fdDd„|D ƒƒ �q‹dED ]%}ttƒ}|D ]}|||d&    d7  < �q©td3|› dFt|ƒ› �ƒ �q¡tdGƒ d S )HNc                 S   s   g | ]}t  |¡‘qS r   )ÚjsonÚloads)r   r   r   r   r   Ú
<listcomp><   s    zmain.<locals>.<listcomp>Údayr(   Úbars_z.json.gzÚrtÚsymÚflowz.jsonc                 S   s2   g | ]}d t  |d ¡  krdk rn n|‘qS )r   Úti†  ©ÚRÚminuter   r   r   r   r8   E   s   2 Úsymbolsc                 S   s   g | ]	}t  |d  ¡‘qS ©r>   r?   r   r   r   r   r8   F   s    c                 3   s$   � | ]\}}|ˆ d  kr|V  qdS )ÚmNr   ©r   ÚirD   rC   r   r   r   G   ó   €" zmain.<locals>.<genexpr>c                 3   s$   � | ]\}}|ˆ d  kr|V  qdS )Úbuy_mNr   rE   rC   r   r   r   H   rG   ÚbuyÚsetupr   rD   Úsupportr   gV-²�ïï?g¸…ëQ¸î?g×£p=
×ï?ÚatrÚplr   )rJ   r9   rL   ÚstarÚtrail2y)r   N)ÚT1Nr   g
×£p=
ï?r   r   rH   Ú_holdÚ_whyc                 S   s   h | ]}|d  ’qS ©r9   r   ©r   Úrr   r   r   Ú	<setcomp>U   ó    zmain.<locals>.<setcomp>é   c              	      sæ   ˆ sd S t  ‡fdd„ˆ D ƒ¡}g }dD ]7‰t  ‡fdd„ˆ D ƒ¡}| | ¡ d›d| ¡ r4||  ¡ ndd›d	|  ¡ rD||   ¡ ndd›d
�¡ qd ‡ fdd„dD ƒ¡}td| d›dtˆ ƒd›d�d |¡ d|› � ƒ d S )Nc                    s   g | ]}|d  ˆ k ‘qS rS   r   rT   ©Úhalfr   r   r8   Y   ó    z&main.<locals>.line.<locals>.<listcomp>)rN   r   rP   r   r   r   c                    s   g | ]}|ˆ  ‘qS r   r   rT   ©Úkr   r   r8   \   rW   z+.3fz% (r   z+.2fú/ú)ú c                 3   s4   � | ]‰ ˆ › d t  ‡ fdd„ˆD ƒ¡d›�V  qdS )r`   c                    s   g | ]}|ˆ d   ‘qS )rQ   r   rT   r\   r   r   r8   ^   r[   z0main.<locals>.line.<locals>.<genexpr>.<listcomp>z.0fN)ÚnpÚmedian)r   )ÚXr\   r   r   ^   r   z%main.<locals>.line.<locals>.<genexpr>)r   r   r   ú   Ú6Ú5ú | z | median hold min: )ra   ÚarrayÚappendÚmeanÚanyÚjoinÚprintr#   )Úlabrc   r   ÚcellsÚaÚholdsrY   )rc   r]   r   ÚlineV   s   T6zmain.<locals>.linez	 trades, z days r   z..r   z; per trade after z% cost (half 1 / half 2)rd   Ú re   r`   Únz>5rg   z
STAR sellsz^22zT0 1% trailzT1 trail at closezT2 + noise2 (VWAP)zT3 + noise2 no VWAPzT4 = T3, floor supportÚALLc                    s   g | ]
}|d  ˆ kr|‘qS )rJ   r   rT   )Úsr   r   r8   e   s    ))zATR<6r   é   )z6-10rw   é
   )z>=10rx   iç  c                    s,   g | ]}ˆ|d    krˆ k rn n|‘qS )rL   r   rT   )r-   Úlor   r   r8   g   s   , r   z exits: ÚTRAILNOISE_DONE)ÚopenÚSRCr   Úlistri   Úsortedr6   ÚloadÚgzipr@   ÚOWÚREFr7   Ú	read_textÚbars_ofÚnextÚ	enumerater   ÚcheckÚgetr$   r&   r5   Ú_CACHEÚclearr#   rm   r%   ÚintÚdict)ÚTÚbyÚrowsr9   ÚdÚrefÚflr(   r)   Útir*   r+   ÚokÚinfoÚsupÚsfloorÚrowr,   r   rM   r0   ÚwhyÚdaysrr   rn   r]   ÚwrU   r   )rZ   r-   ry   rv   r>   r   Úmain;   sX   $8 , *."6
4ÿÿ
rœ   Ú__main__)Ú__doc__r€   r6   ÚsysÚcollectionsr   Úpathlibr   Únumpyra   Ú__file__ÚresolveÚparentÚHEREÚpathÚinsertÚstrÚv2_bar_replayr@   Ú	exit_pepgr   r|   r%   r'   r5   rœ   Ú__name__r   r   r   r   Ú<module>   s$     5
ÿ