o
    ºÎÚj  ã                   @   s”   d Z i dd“dd“dd“dd“d	d
“dd“dd“dd“dd“dd“dd“dd“dd“dd“dd“dd“dd“dd d
d!d"d#œ¥Zd$d%„ Zd)d'd(„Zd&S )*aŽ  
r1_setup.py -- [2026-10-22] The user's dynamic setup R1 "RETEST REVERSAL AT SUPPORT" (from the WOLF 11/06/2024 practice chart:
open 13.41, low 12.60 at 9:36, bounce died at VWAP, retest 12.61 at 9:57-9:58 held, 9:59 buyers 40% -> 73% while sell
volume collapsed, close above the base -> buy 12.83, +5% by 13:05). The bot reads the WHOLE movement since the open:
  1 SUPPORT      the lowest low so far, made at least min_age bars ago, followed by a bounce >= bounce_pct
  2 RETEST HELD  the low of the last retest_bars bars came back within near_pct of the support and did not break it
                 by more than break_pct
  3 SELLERS GONE sell volume of the signal bar <= sell_dry x the mean sell volume of the 3 bars before
                 (total volume does NOT need to rise)
  4 BUYERS UP    1-min buy share >= bs_min and >= bs_jump above the mean of the 3 bars before
  5 PRICE UP     the close is above the highest high of the base_bars bars before
  6 ROOM         the higher of VWAP and the open is >= room_pct above the close (they are TARGETS, not blocks)
  safety         >= min_spm shares/min over the last 5 bars; 9:40-14:30; one trigger per support (cool-down)
  [v2, 10/22 after CRML 10/03/2025 (-1% first) vs WOLF] the stock's STORY must fit too:
  7 REAL BAR     the trigger bar's volume >= trig_vol_x (0.5) x the stock's normal for that minute (buy share on a
                 1,690-share bar is noise)
  8 NEAR VWAP    price no more than max_under_vwap (2%) under VWAP (far under = the sellers own the day)
  9 TESTED       the bounce off the support before the retest >= max(bounce_pct, bounce_atr x daily ATR %) (0.25 ATR)
  10 NO FAILED PUSH  since the support no bar with >= push_vol_x (2x) normal volume and >= push_bs (75%) buyers that
                 moved the price < push_move (0.3%) (a seller absorbing the buying above)
  11 NOT A FADE  not: the day high made after the first 15 min, >= fade_age (60) min ago, and the price now under the
                 open (the morning rally is being given back)
When R1 is true these v2 rules do NOT apply: RVOL >= 1.5x, 15-min buy share, 15-min steady trend, price >= VWAP, below
the open. Pure function, no orders.
Úmin_ageé   Ú
bounce_pctg      ð?Úretest_barsé   Únear_pctgš™™™™™Ù?Ú	break_pctg333333Ó?Úsell_dryg      à?Úbs_mingÍÌÌÌÌÌä?Úbs_jumpgš™™™™™É?Ú	base_barsÚroom_pctg       @Úmin_spmiÐ  Úfirst_mé
   Úlast_mi,  Úcooldowné   Ú
trig_vol_xÚmax_under_vwapÚ
bounce_atrg      Ð?g      è?é<   T)Ú
push_vol_xÚpush_bsÚ	push_moveÚfade_ageÚstoryc                 C   sZ   ddl m }m} ddlm} t| |ƒs| | |j¡} |  |dƒ¡} | jd d | j	 d S )z¸minutes since 9:30 ET of a bar time (datetime or epoch seconds) -- [2026-10-22] flow is looked up by the bar's own
    minute, not its position (thin stocks have minutes without a bar)é    )ÚdatetimeÚtimezone)ÚZoneInfozAmerica/New_Yorké	   r   é   )
r   r   Úzoneinfor   Ú
isinstanceÚfromtimestampÚutcÚ
astimezoneÚhourÚminute)Útr   r   r   © r*   úr1_setup.pyÚ_minute!   s   
r,   Nc                    sD  i t ¥|pi ¥}tˆƒ}||d k s$||d ks$||d |d  d k r(di fS |d |d ‰‰ˆd	||d  … ‰tttˆƒƒ‡fd
d„d�}ˆ| d }|d | |d |d  d k rcdi fS |||d  k r~tdd„ ˆ|||d  d … D ƒƒn|}|pƒi  d¡rž|pŠi  d¡rž|p‘i  d¡|p—i  d¡ d nd}	|d r¯t|d |d |	 ƒn|d }
|| d d |
k rÒddd|| d d d›d|
d›d�ifS tdd„ ˆ|d  d	… D ƒƒ}|| d d }||d ks÷||d  k rûdi fS |d k �rdi fS ˆ| tˆ|d  |… ƒd  }}‡‡fd!d„‰‡‡fd"d„‰ ˆ |ƒt‡ fd#d„t|d  |ƒD ƒƒd  }}|d$k�rH||d% | k�sLdi fS ||d& k�r\|| |d' k�s`di fS td(d„ ˆd)|d*  d)… D ƒƒ}ˆd) d+ }||k�r€di fS td,d„ ˆD ƒƒ}td-d„ ˆD ƒƒ}|�r™|| n|}t|ˆd$ d. ƒ}|| d d }||d/ k �r·di fS td0d„ ˆd1d	… D ƒƒd2 |d3 k �rÏdi fS |d �rö|�pØi  d4¡�pÞg ‰‡fd5d„}||ƒ�rˆd) d6 |d7 ||ƒ k �rddd8ˆd) d6 d9›d:ˆd) d6 ||ƒ d›d;�ifS || d d |d<  k �r3ddd=|| d d d>›d�ifS ||d  }t|d |d ƒD ]n}ˆ| }t|d? ƒ}ˆ| ˆ|  }||ƒ�r¯|d6 |d@ ||ƒ k�r¯|�r¯ˆ| | |dA k�r¯|d+ |d.  d d |dB k �r¯dddC|› dD|d6 d9›dEˆ| | dF›dG|d+ |d.  d d dH›d�	if  S �qBtt|ƒ‡fdId„d�}tˆ| d? ƒdJk�rö|tˆ| d? ƒ |dK k�rö|ˆd$ d. k �rödddLˆ| dM dN›dO|d | › dP�ifS dQ|tˆ| d? ƒ|t|dƒ|t|ƒt|dƒt|dƒ|t|dRƒˆd$ d. t|dƒdSœfS )Tz«done: finished bars (index = minute since 9:30 for complete data); flow: {'buy': [...], 'sell': [...]} per minute;
    m = minute of the last finished bar. -> (bool, info)r   r   r   r   é   FÚbuyÚsellNc                    ó   ˆ |  d S )NÚlr*   )Úi)Úprer*   r+   Ú<lambda>6   ó    zcheck.<locals>.<lambda>)Úkeyr1   é   c                 s   ó   � | ]}|d  V  qdS ©ÚhNr*   ©Ú.0Úxr*   r*   r+   Ú	<genexpr>:   ó   € zcheck.<locals>.<genexpr>Úatr14Ú
prev_closeéd   ç        r   r   r   Úwhy_notz9 support not tested: bounce z.1fz% < ú%c                 s   r8   )r1   Nr*   r;   r*   r*   r+   r>   @   r?   r   r   é   c                    s   ˆ |  ˆ|   S ©Nr*   ©Úk)ÚfbÚfsr*   r+   r4   H   s    c                    s   ˆ| ƒrˆ |  ˆ| ƒ S dS )NrC   r*   rH   )rJ   Útotr*   r+   r4   I   s    c                 3   s   � | ]}ˆ |ƒV  qd S rG   r*   )r<   rI   )Úbsr*   r+   r>   J   r?   r   r   r	   r
   c                 s   r8   r9   r*   r;   r*   r*   r+   r>   P   r?   éÿÿÿÿr   Úcc                 s   s4   � | ]}|d  |d  |d  d |d  V  qdS )r:   r1   rO   rF   ÚvNr*   r;   r*   r*   r+   r>   U   s   €2 c                 s   r8   ©rP   Nr*   r;   r*   r*   r+   r>   U   r?   Úor   c                 s   r8   rQ   r*   r;   r*   r*   r+   r>   [   r?   éûÿÿÿr   r   Úvol_per_minc                    s   ˆ rˆ t | tˆ ƒd ƒ S dS )Nr7   r   )ÚminÚlenrH   )Úvpmr*   r+   r4   _   s    rP   r   z7 trigger bar too small: z.0fz
 shares = zx normalr   z8 too far under VWAP: z+.1fr)   r   r   r   z 10 failed buying push at minute z: z sh z.0%z buyers, price z+.2fc                    r0   )Nr:   r*   )Új)Údoner*   r+   r4   j   r5   r   r   z11 fade: day high r:   z.2fú z min ago, price under the openTé   )ÚsupportÚ	support_mÚ
retest_lowr   Úsell_nowÚ	sell_prevÚbs_nowÚbs_prevÚbase_hiÚvwapÚopenÚroom)	ÚDEFAULTSrV   rU   ÚrangeÚmaxÚgetÚsumr,   Úround)rY   ÚflowÚrefÚmrO   ÚnÚi0ÚsupÚbounceÚatrpÚneed_bounceÚrlÚnearÚs_nowÚs_prevra   rb   rc   ÚPÚpvÚcvrd   Útargetrf   ÚnormÚm0rX   ÚbrI   Út_Újhr*   )rM   rY   rJ   rK   r3   rL   rW   r+   Úcheck,   s|   0 :@&* 
". "
$(
(6"bP€F,þrƒ   rG   )Ú__doc__rg   r,   rƒ   r*   r*   r*   r+   Ú<module>   s:    &ÿÿÿÿÿÿþþýýýü