o
    f®já  ã                   @   s|   d Z ddlmZmZ ddlmZmZmZ dZ	dZ
dddd	d
œZeG dd„ dƒƒZdd„ Zddededededef
dd„ZdS )aÖ  
exit_fast_slope_experimental.py

[2026-09-18] NOT wired into monitor.py's live loop -- backtest-only.
User's proposed design, deliberately simpler than
exit_giveback_experimental.py's multi-vote sequence: sell the instant
price's own SHORT-TERM trajectory turns down (with real volume behind
it, not a one-print wiggle), and trust the existing entry gate
(smart_engine.evaluate() -- already allows immediate same-symbol
re-entry, no cooldown, per the 2026-09-18 GLOO decision) to buy back in
once the stock shows renewed strength. No new logic on the entry side
at all -- Stage 1's constructive check + Stage 2's 18s persistence hold
+ Stage 3's imbalance/resistance confirmation already ARE "volume and
all the enter indicators needed to allow entry."

Uses stream.py's SymbolBuffer.get_bars_sub() -- the 30-second-bucket
rolling buffer that's been built and fed by every trade tick since
2026-09-05 but never actually consumed by anything (its own docstring:
"inert until a stream-features consumer is built"). This is that
consumer. 30s buckets react roughly 2x faster than the 1-min bars
exit.py's existing deterioration layer reads, without going all the way
down to raw-tick noise.

Single-read trigger, no confirm-read wait -- deliberately, since the
whole point is speed. The regression slope over several sub-bars is
already a smoothed measure (unlike a raw two-tick delta), so it doesn't
need an additional multi-poll hysteresis layer on top the way the
imbalance-decline gate does.
é    )Ú	dataclassÚfield)Únormalized_slope_pctÚclassify_slopeÚatrÚHOLDÚEXITé   gš™™™™™©?g      Ð?g      à?)Úsub_bars_lookbackÚmin_flat_threshold_pctÚflat_threshold_atr_fractionÚvolume_floor_ratioc                   @   sL   e Zd ZU eed< eZeed< dZeed< dZ	eed< e
ed�Zeed< d	S )
ÚFastSlopeDecisionÚsymbolÚstateFÚshould_exitÚ Úreason)Údefault_factoryÚmetricsN)Ú__name__Ú
__module__Ú__qualname__ÚstrÚ__annotations__Ú
STATE_HOLDr   r   Úboolr   r   Údictr   © r   r   ú7/var/www/screener/trade/exit_fast_slope_experimental.pyr   .   s   
 r   c                 C   s   i t ¥| pi ¥S ©N)ÚDEFAULT_CONFIG)Úcfgr   r   r   Ú
_merge_cfg7   s   r#   Nr   ÚbarsÚsub_barsr"   Úreturnc                 C   sæ  t |ƒ}t|ƒdk st|ƒ|d d k rt| dd�S |d d }t|tdtdt|ƒd	 ƒƒd
�}|r8|| d nd}t|d ||d  ƒ}||d  d… }dd„ |D ƒ}	t|	ƒ}
t|
|ƒ}tdd„ |D ƒƒ}||d d  d… }|r…tdd„ |D ƒƒt|ƒ |d  nd}|dkp’||d | k}|t	|dƒt	|dƒt	|
dƒ|t	|d	ƒt	|d	ƒ|dœ}|dkrÒ|rÒt| t
dd|
d›d|d › d|d›d|d ›d!|d ›d"�|d#�S |dkrãt| td$|
d›d%�|d&�S t| td'|
d›d(|› d)�|d&�S )*a9  
    bars: standard 1-min rolling window (only used for ATR, to scale the
        flat threshold the same way the rest of the codebase does).
    sub_bars: stream.SymbolBuffer.get_bars_sub() -- 30s-bucket bars,
        oldest first, each {"t","o","h","l","c","v","tick_count",
        "upticks","downticks"}.
    é   r
   é   zinsufficient sub-bar history)r   r   éÿÿÿÿÚcé   é   )Úperiodg      Y@g        r   r   Nc                 S   s   g | ]}|d  ‘qS )r*   r   ©Ú.0Úbr   r   r   Ú
<listcomp>N   s    zevaluate.<locals>.<listcomp>c                 s   ó   � | ]}|d  V  qdS ©ÚvNr   r.   r   r   r   Ú	<genexpr>R   ó   € zevaluate.<locals>.<genexpr>c                 s   r2   r3   r   r.   r   r   r   r5   T   r6   r   r   r	   )ÚpriceÚatr_pctÚflat_threshÚfast_slope_pctÚ
fast_trendÚrecent_sub_volÚtrailing_avg_volÚvolume_confirmedÚnegativeTzfast slope down: z.3fz% over last z sub-bars (flat band z%), volume confirmed (z.0fz vs z trailing avg))r   r   r   r   r   zslope down (z,%) but volume too thin to confirm -- holding)r   r   r   r   zhealthy (fast slope z%, ú))r#   Úlenr   Úcalc_atrÚminÚmaxr   r   ÚsumÚroundÚ
STATE_EXITr   )r   r$   r%   r"   r7   Úar8   r9   ÚwindowÚclosesr:   r;   Ú
recent_volÚtrailingÚtrailing_avgr>   r   r   r   r   Úevaluate;   sT     
*
ýÿþþûýþrN   r    )Ú__doc__Údataclassesr   r   Ú
indicatorsr   r   r   rB   r   rG   r!   r   r#   r   Úlistr   rN   r   r   r   r   Ú<module>   s    ü$