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 i¥Zd!d"„ Zd&d$d%„Zd#S )'aº  
r2_setup.py -- [2026-10-22] The user's setup R2 "CALM-STOCK BASE AT A HIGHER LOW" (from the SNAP 12/30/2024 practice chart:
calm stock, ATR 4.8%, gap -2.5%; opening dip to 10.56 at 9:32, bounce to 10.77, slow drift back into a flat base at
10.61-10.66 (a HIGHER low) where buyers kept absorbing the selling (15-min buy share 53-58%, price flat), then the climb to
the previous close 10.99 = the gap fill. Best entry inside the base, 10.62-10.63 at 10:07-10:10 -> +2% at 11:04, +3.4% high).
On a calm stock a 2% move needs an EARLY entry: R2 buys inside the base, it does not wait for the breakout bar.
  0 CALM         daily ATR % < max_atr (8%)
  1 SUPPORT      the lowest low so far, made >= min_age bars ago, then a bounce >= max(bounce_pct, bounce_atr x ATR %)
  2 HIGHER LOW   the low of the base is AT or ABOVE the support (no break below by more than break_pct) and no more than
                 hl_atr x ATR % above it -- [10/22, NVAX 12/13/2024] tightened 0.25 -> 0.10 ATR: the base must sit AT the
                 support (NVAX 10:01 base 1.0% above, mid-range -> -1% first; NVAX 11:00-11:06 base 0.2% above = the user's
                 entry -> +2% first; SNAP 0.43% above, limit 0.48%)
  3 FLAT BASE    the last base_bars bars stay inside a range <= base_atr x ATR % (price holding, not sliding)
  4 ABSORPTION   buy share over the base >= base_bs (50%): buyers matching or beating the sellers while price holds
  5 NOT FALLING  the trigger bar closes >= the bar before and its buy share >= trig_bs (50%)
  5b LOW IN BASE the close is in the lower max_base_pos (50%) of the base range (on a calm stock the 2% is made by buying
                 the bottom of the base, not its top)
  6 ROOM         the highest target above (VWAP, the open, the previous close, today's high so far) >= room_pct above
  7 STORY        >= min_spm shares/min; no more than max_under_vwap under VWAP; not a fade (day high after the first 15 min,
                 >= 60 min ago, price under the open); 9:45-14:30; one trigger per base (cool-down)
When R2 is true these v2 rules do NOT apply: RVOL >= 1.5x, 15-min buy share >= 55%, 15-min steady trend, price >= VWAP,
below the open, calm-stock (min_atr) avoid rule. Pure function, no orders.
Úmax_atrg       @Úmin_ageé
   Ú
bounce_pctgš™™™™™é?Ú
bounce_atrg      Ð?Ú	break_pctgš™™™™™¹?Úhl_atrÚ	base_barsÚbase_atrgš™™™™™É?Úbase_bsg      à?Útrig_bsÚroom_pctg       @Úmin_spmiÐ  Úmax_under_vwapg      ø?Úmax_base_posÚfade_ageé<   Úfirst_mé   Úlast_mi,  Úcooldowné   c                 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   r   Úzoneinfor   Ú
isinstanceÚfromtimestampÚutcÚ
astimezoneÚhourÚminute)Útr   r   r   © r$   úr2_setup.pyÚ_minute   s   
r&   Nc                    s¸  i t ¥|pi ¥}|pi }tˆƒ}||d k s&||d ks&||d |d  k r*di fS | d¡r>| d¡r>|d |d  d nd }|d u sJ||d	 krPdd
difS |d |d ‰‰||d  }ˆ|d  d … }ˆd ||d  … ‰tttˆƒƒ‡fdd„d�}	ˆ|	 d }
|d |	 |d k r–dd
difS tdd„ ˆ|	||d  d … D ƒƒ}t|d |d | ƒ}||
 d d |k rÓdd
d||
 d d d›d|d›d�ifS tdd„ |D ƒƒ‰ tdd„ |D ƒƒ}ˆ |
 d d }||d  k sý||d | k�rdd
d |d!›d"|d | d#›d$�ifS |ˆ  d d }||d% | k�r4dd
d&|d#›d'|d% | d#›d�ifS t||d  d |d ƒ}t‡fd(d„|D ƒƒt‡fd)d„|D ƒƒ}}|| �rc|||  nd*}||d+ k �rvdd
d,|d-›�ifS ˆ| ˆ|  }ˆd. d/ ˆd0 d/ k �s™|�r™ˆ| | |d1 k �rŸdd
d2ifS ˆd. d/ }|ˆ k�rÈ|ˆ  |ˆ   |d3 k�rÈdd
d4|ˆ  |ˆ   d-›d5�ifS td6d„ ˆD ƒƒ}td7d„ ˆD ƒƒ}|�rá|| n|}|ˆd8 d9 td:d„ ˆD ƒƒg| d¡�rý|d gng  }t|ƒ| d d }||d; k �r!dd
d<|d›d|d; › d�ifS td=d„ ˆd>d … D ƒƒd? |d@ k �r;dd
dAifS || d d |dB  k �rZdd
dC|| d d dD›dE�ifS tt|ƒ‡fdFd„d�}tˆ| dG ƒdHk�r�|tˆ| dG ƒ |dI k�r�|ˆd8 d9 k �r�dd
dJifS | dK¡�p–g ‰‡fdLd„‰t‡ ‡‡‡‡‡fdMd„t||d  |ƒD ƒƒ}dN||
tˆ|	 dG ƒˆ |t	|dOƒt	|dOƒt	|dOƒt	|dOƒt	|dPƒt	|dƒdQœfS )RNr   r   r   r   FÚatr14Ú
prev_closeéd   r   Úwhy_notz0 not a calm stockÚbuyÚsellé   c                    ó   ˆ |  d S )NÚlr$   )Úi)Úprer$   r%   Ú<lambda>5   ó    zcheck.<locals>.<lambda>)Úkeyr/   z1 support too youngc                 s   ó   � | ]}|d  V  qdS ©ÚhNr$   ©Ú.0Úxr$   r$   r%   Ú	<genexpr>9   ó   € zcheck.<locals>.<genexpr>r   r   z	1 bounce z.1fz% < ú%c                 s   r5   )r/   Nr$   r8   r$   r$   r%   r;   =   r<   c                 s   r5   r6   r$   r8   r$   r$   r%   r;   =   r<   r   r   z2 base low z+.2fz% vs support (allowed 0..z.2fz%)r	   z3 base not flat: range z% > c                 3   ó   � | ]}ˆ | V  qd S ©Nr$   ©r9   Úk)Úfbr$   r%   r;   E   r<   c                 3   r>   r?   r$   r@   )Úfsr$   r%   r;   E   r<   g        r
   z 4 no absorption: base buy share z.0%éÿÿÿÿÚcéþÿÿÿr   z 5 trigger bar falling or sellersr   z5b price high in the base (ú)c                 s   s4   � | ]}|d  |d  |d  d |d  V  qdS )r7   r/   rE   é   ÚvNr$   r8   r$   r$   r%   r;   O   s   €2 c                 s   r5   ©rI   Nr$   r8   r$   r$   r%   r;   O   r<   r   Úoc                 s   r5   r6   r$   r8   r$   r$   r%   r;   Q   r<   r   z6 room c                 s   r5   rJ   r$   r8   r$   r$   r%   r;   U   r<   éûÿÿÿé   r   z7 thinr   z7 z+.1fz% under VWAPc                    r.   )Nr7   r$   ©Új©Údoner$   r%   r2   Y   r3   r#   r   r   z7 fadeÚvol_per_minc                    s   t ˆ |  d ƒS )Nr#   )r&   rN   rP   r$   r%   r2   ]   s    c              	   3   s˜   � | ]G}ˆoGˆ| d  dt ˆtˆ|ƒtˆƒd ƒ dƒ koGˆˆ|ƒ ˆˆ|ƒ  oGˆˆ|ƒ ˆˆ|ƒ ˆˆ|ƒ   dkoGˆ| d ˆ d kV  qdS )rI   é   r-   gÍÌÌÌÌÌä?r/   gÕxé&1ð?N)ÚmaxÚminÚlen)r9   rO   )ÚblorQ   rB   rC   ÚmjÚvpmr$   r%   r;   ^   s   € Lÿ&ÿ
ÿTrS   é   )Údefended_lowÚsupportÚ	support_mÚbase_loÚbase_hiÚ
base_rangeÚ
higher_lowr
   ÚroomÚvwapÚatr)
ÚDEFAULTSrV   ÚgetrU   ÚrangerT   Úsumr&   ÚanyÚround)rQ   ÚflowÚrefÚmrE   ÚnÚatrpÚm0ÚbaseÚi0ÚsupÚbounceÚneed_bÚbhiÚhlÚrngÚksÚBÚSÚbs_baseÚt_ÚPÚpvÚcvrc   Útargetsrb   ÚjhÚdefendedr$   )rW   rQ   rB   rC   rX   r1   rY   r%   Úcheck(   sx   ,,&*$ &&.6$"$: ("Fÿ  þr„   r?   )Ú__doc__re   r&   r„   r$   r$   r$   r%   Ú<module>   s8    &ÿÿÿÿÿÿþþþþþþ