
    i                        U d Z ddlmZ ddlmZ ddlZddlm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Z	de
d<   	 d	 	 	 	 	 	 	 ddZy)u  
H1 market structure detection — port fedele da V15.

Classifies the H1 frame into BULLISH_EXPANSION / BEARISH_EXPANSION /
RANGING using:
  - HH/LL detection on last 7 bars vs 5 prior, with ATR-scaled threshold
  - HH/HL counts on last 6 bars (h1_struct_bull / h1_struct_bear flags)
  - bullish/bearish close on the latest bar to disambiguate HH+LL ties

The asset-specific multiplier (STRUCT_THRESHOLDS) widens the band for
indices and metals (deeper swings) and tightens it for FX (smaller).
    )annotations)AnyN)calc_atrMESg?MNQMYMESNQYMRTYMGCg      ?GCMCLCL6E333333?6B6A6J6Cdefaultzdict[str, float]STRUCT_THRESHOLDSc           	     8    |xs t         }t               }|j                  d   }|j                  ||j                  dd            }||z  }t	         d   j                  d    d   j                  d         } d   j                  dd j	                         }	||	|z   kD  }
t         d   j                  d    d   j                  d         } d   j                  dd j                         }|||z
  k  }|
r,|r* d   j                  d    d	   j                  d   kD  rd
nd}n|
rd
}n|rd}nd}t         fdt        ddd      D              }t         fdt        ddd      D              }t        dd      D cg c]  } d   j                  |    c}t        dd      D cg c]  } d   j                  |    c}t        fdt        dt                    D              }t        fdt        dt                    D              }t        fdt        dt                    D              }t        fdt        dt                    D              }|dk\  xr |dk\  }|dk\  xr |dk\  }|t	        ||      ||t        |      dS c c}w c c}w )ud  
    Returns a dict with:
        market_structure: "BULLISH_EXPANSION" | "BEARISH_EXPANSION" | "RANGING"
        trend_maturity:   max consecutive up/down H1 closes (last 5)
        h1_struct_bull:   bool — HH+HL pattern on last 6 H1 bars
        h1_struct_bear:   bool — LH+LL pattern on last 6 H1 bars
        atr_h1:           current H1 ATR (used by upstream regime detection)

    Args:
        df1: H1 OHLCV DataFrame (>= 7 bars).
        symbol: instrument root for threshold lookup.
        struct_thresholds: override map; defaults to module-level
                           STRUCT_THRESHOLDS.
    r   r   highilowcloseopenBULLISH_EXPANSIONBEARISH_EXPANSIONRANGINGc              3  r   K   | ].  }d    j                   |   d    j                   |dz
     kD  rd 0 ywr      Niloc.0idf1s     4/home/work/apex_v16/analysis/indicators/structure.py	<genexpr>z(calc_market_structure.<locals>.<genexpr>W   sC      w<Q#g,"3"3AE":: 	
   47ic              3  r   K   | ].  }d    j                   |   d    j                   |dz
     k  rd 0 ywr$   r&   r(   s     r,   r-   z(calc_market_structure.<locals>.<genexpr>[   sC      w<Q#g,"3"3AE":: 	
r.   r   c              3  @   K   | ]  }|   |d z
     kD  sd   ywr%   N r)   r*   h1_highss     r,   r-   z(calc_market_structure.<locals>.<genexpr>b   '     W!xPQTUPU9V1W   r%   c              3  @   K   | ]  }|   |d z
     kD  sd   ywr1   r2   r)   r*   h1_lowss     r,   r-   z(calc_market_structure.<locals>.<genexpr>c   %     T
WQQRU^8S1Tr6   c              3  @   K   | ]  }|   |d z
     k  sd   ywr1   r2   r3   s     r,   r-   z(calc_market_structure.<locals>.<genexpr>d   r5   r6   c              3  @   K   | ]  }|   |d z
     k  sd   ywr1   r2   r8   s     r,   r-   z(calc_market_structure.<locals>.<genexpr>e   r:   r6      )market_structuretrend_maturityh1_struct_bullh1_struct_bearatr_h1)
r   r   r'   getmaxminsumrangelenfloat)r+   symbolstruct_thresholds
thresholdsatr_h1_s
atr_h1_curstr_mult
struct_thrcurrent_or_prev_high
past_highshhcurrent_or_prev_low	past_lowsllstructh1_uph1_downr*   hh_counthl_countlh_countll_countr@   rA   r4   r9   s   `                       @@r,   calc_market_structurer^   (   s   & #7&7J}Hr"J~~fjnnY&EFHJ&Js6{//3S[5E5Eb5IJV!!"R(,,.J	
Z 7	7Bc%joob13u:??23FGE
2&**,I	y:5	5B	b 7|  $s6{'7'7';;  $ 	
 
$	$ R$ E  R$ G
 .32q\:F  #:H+0Q<8as5zq!8GWeAs8}5WWHTeAs7|4TTHWeAs8}5WWHTeAs7|4TTH!m5AN!m5AN #w/**!*-  ;8s   =J'J)N)r+   zpd.DataFramerJ   strrK   zdict[str, float] | Nonereturnzdict[str, Any])__doc__
__future__r   typingr   pandaspdanalysis.indicators.atrr   r   __annotations__r^   r2       r,   <module>ri      s!   #   ,
'	4''#T' 	4' ' #T' ,1$'
 
4'
 ' 
4' ' 	4' ' #T' ,0$' 9=t' t' # $ 26H	HH /H 	Hrh   