+
    `j4              
          R t ^ RIt^ RIt^ RIt^ RIt^ RIt^ RIHtHtHt ^ RI	H
t
  ^ RIt]
! ]4      P!                  4       P"                  ^,          t]R,          tR. RO3R. RO3R. RO3R	. RO3R
. RO3.t^t. R Ot]
! R4      ]R,          .tRtRt^tRR!RR"RR#RR$RR%/tR. R&OR. R'O/t. R(OtR tR tR t R t!R t"R t#]$R8X  d
   ]#! 4        R# R#   ] d    ]P                  ! R4        Li ; i))u  
build_forecast.py — writes forecast_data.json for the Daily Forecasts page.

ENGINE: replica of vol_range_forecast.py v3 (Parkinson daily variance,
EWMA lambda=0.94, multipliers = median/75th pct of range%/sigma over the
trailing 150 same-weekday sessions, event-conditioned on
FOMC/NFP/USCPI/ECB/BOE days when >=24 samples exist).

Deviations from the standalone tool, both for the rollover build time:
  * SESSION DATING — the forecast session rolls at 17:00 New York
    (= broker midnight). Built Tuesday 5pm NY, it forecasts Wednesday.
  * COMPLETED BARS ONLY — all statistics use completed daily bars,
    exactly matching the OOS validation. The forming bar (nearly empty
    at rollover) contributes only today's OPEN for level anchoring; if
    it hasn't printed yet (Gold/NQ open 18:00 NY), levels anchor to the
    prior close and the server retries until the real open exists.

v3 of this builder also ships M15/H1/H4/D1 candle sets per instrument
so the page can switch timeframes. Statistics remain D1-only.

Save to: src\execution\build_forecast.py
Run manually:   python src\execution\build_forecast.py
Normally invoked by dashboard_server.py just after the daily rollover.
Requires the MT5 terminal running on this machine.
N)datetime	timedeltatimezone)PathuF   MetaTrader5 package missing — run: python -m pip install MetaTrader5zforecast_data.jsonGOLDEURUSDGBPUSDUSDJPYNQz[C:\Users\Administrator\OneDrive\range_extension_model\src\research\live\calendar_events.csvzcalendar_events.csvgGz?i  FOMCNFPUSCPIECBBOEc                     ^ RI Hp  \        P                  ! V ! R4      4      pV\        ^R7      ,           P                  4       #   \         d9    \        P                  ! \
        P                  4      \        ^R7      ,
          p Lci ; i)zDate of the current FX session. Sessions roll at 17:00 New York
(= broker midnight): after Tue 5pm NY the session is Wednesday, and
Sunday's rollover opens Monday's session.)ZoneInfozAmerica/New_York)hours)	zoneinfor   r   now	Exceptionr   utcr   date)r   nys     QC:\Users\Administrator\OneDrive\fx_macro_intraday\src\execution\build_forecast.pysession_dater   U   sf    =%\\(#567 ##))++  =\\(,,')!*<<=s   "A A BBc                 `   ^ RI p \        R \         4       R4      pVf   / R3# \         Uu/ uF  q"\	        4       bK  	  pp \        VRRR7      ;_uu_ 4       pV P                  V4       F  p\        P                  4        Fo  w  pw  rxVP                  R4      V8X  g   K   VP                  R4      V8X  g   K8  W6,          P                  \        VP                  RR	4      4      R
,          4       Kq  	  K  	  RRR4       W13# u upi   + '       g   i     L; i  \         d    / T3u # i ; i)zESame parsing as v3; returns ({class: set(dates)}, path_used or None).Nc              3   T   "   T F  qP                  4       '       g   K  Vx  K   	  R # 5iN)exists).0ps   & r   	<genexpr>load_events.<locals>.<genexpr>d   s     @1qXXZ1s   (
(utf-8replace)encodingerrorscountryeventr    :N
   N)csvnextEVENTS_CSV_CANDIDATESEVENT_CLASSESsetopen
DictReaderitemsgetaddstrr   )	r+   pathkoutfrowclsctynames	            r   load_eventsr>   a   s    @1@$GD|4x*
+]ce8]C
+$;;q~~a((5(;(;(=$C#wwy)S0SWWW5E5MS)<%=c%BC )> ) <
 y ,;;  4xs<   DD AD!D9?D8
D D	D D-,D-c                   \        V 4      p\        V4      ^,
          V,          p\        V4      pW$,          V\        V^,           \        V4      ^,
          4      ,          W$,          ,
          W4,
          ,          ,           # )u1   Linear-interpolated quantile — identical to v3.)sortedlenintmin)vr    sr7   r9   s   &&   r   qrF   s   sY    q	A	Q!qAAA41SQA
+,qt3>>>    c                  aa8 \         P                  ! V \         P                  ^ \        4      pVe   \	        V4      R8  d   RRRR/# V Uu. uF  p\        VR,          4      NK  	  ppV Uu. uF  p\        VR,          4      NK  	  ppV Uu. uF  p\        VR	,          4      NK  	  p	pV Uu. uF  p\        VR
,          4      NK  	  p
pV Uu. uF>  p\        P                  ! VR,          \        P                  4      P                  R4      NK@  	  ppVR.,          V8X  d    VR.,          pTRr\	        V4      ^,
          pMRpV
R.,          Rr\	        V4      pVR8  d   RRRR/# VRV pVRV pV	RV pV
RV pVRV pVRV  Uu. uF=  p\        P                  ! VR,          \        P                  4      P                  4       NK?  	  pp\        P                  V\        4       Uu. uF  pS'       g   K  VS9   g   K  VNK  	  upo8V8V3R lp\        \	        V4      4       Uu. uFW  p\         P"                  ! VV,          VV,          ,          4      ^,          ^\         P"                  ! ^4      ,          ,          NKY  	  pp\$        P&                  ! VR,          4      p. p\        ^\	        V4      4       FU  pVP)                  \         P*                  ! V4      4       \,        V,          ^\,        ,
          VV,          ,          ,           pKW  	  \         P*                  ! V4      p\        ^4       Uu/ uF  pV. bK  	  pp\        ^4       Uu/ uF  pV. bK  	  ppS8 Uu/ uF  pV. bK  	  p p\        ^\	        V4      4       F  pVV^,
          ,          p!V!^ 8:  d   K  VV,          VV,          ,
          VV,          ,          V!,          \/        VV,          VV,          ,
          4      VV,          ,          V!,          3p"V! VV,          4      pV'       d   V V,          P)                  V"4       K  VVV,          ,          P)                  V"^ ,          4       VVV,          ,          P)                  V"^,          4       K  	  Rp#V'       d	   V! V4      MRp$V$'       dS   \	        V V$,          4      ^8  d<   V V$,          R/R p%T$p#V% U&U'u. uF  w  p&p'V&NK
  	  p(p&p'V% U&U'u. uF  w  p&p'V'NK
  	  p)p&p'MV$'       d   V$ R2p#VP                  V. 4      \0        ) R p(VP                  V. 4      \0        ) R p)\	        V(4      ^(8  da   \        ^4       UU*u. uF  pVV,           F  p*V*NK  	  K  	  up*pR0R p(\        ^4       UU*u. uF  pVV,           F  p*V*NK  	  K  	  up*pR0R p)V\         P*                  ! ^4      ,          ^d,          p+V\3        V(R4      ,          V\3        V(R4      ,          p-p,V\3        V)R4      ,          V\3        V)R4      ,          p/p.VR.,          p0\        ^4       Uu/ uF  pV. bK  	  p1p^ p2^ p3\5        ^\	        V4      ^,
          4      p4\        ^\	        V4      4       F  pVV^,
          ,          p!V!^ 8:  d   K  V! VV,          4      '       d   K3  VV48  dx   V1VV,          ,          \0        ) R p%\	        V%4      ^(8  dP   V!\3        V%R4      ,          p5V3^,          p3VV,          VV,          ,
          VV,          ,          V58:  d
   V2^,          p2V1VV,          ,          P)                  VV,          VV,          ,
          VV,          ,          V!,          4       K  	  V3^<8  d   R\7        RV2,          V3,          ^4      RV3/MRp6\        \5        ^ \	        V4      \8        ,
          4      \	        V4      4       Uu. uF2  pRVV,          RVV,          RVV,          RV	V,          RV
V,          /NK4  	  p7p/ RRbRV#bRV#\:        9   bR \7        V+^4      bR!\7        V,^d,          ^4      bR"\7        V-^d,          ^4      bR#\7        V.^d,          ^4      bR$\7        V/^d,          ^4      bR%V0bR&VbR'VbR(VbR)VR.,          bR*\	        V(4      bR+V6bR,R-V7/b# u upi u upi u upi u upi u upi u upi u upi u upi u upi u upi u upi u up'p&i u up'p&i u up*pi u up*pi u upi u upi )1zv3 forecast() math on COMPLETED bars, returning structured data.
`sym` is the broker symbol; `canonical` picks the event-class map;
`today` (session date, YYYY-MM-DD) selects event conditioning.Ni  okFerrorzinsufficient D1 historyr0   highlowclosetimez%Y-%m-%dprior_closez!insufficient completed D1 historyc                 >   < S F  pV SV,          9   g   K  Vu # 	  R # r    )dstrr;   classesevs   & r   	day_class#forecast_numbers.<locals>.day_class   s"    Cr#w
  rG   :      Nweekdayu!    (thin history — weekday bands)g      ?g      ?pctg      Y@ntohlcTlabelevent_conditionedann_volhl_medhl_75oc_medoc_75	ref_close
today_openanchor
anchor_srclast_completed_barpool_ncoveragecandlesD1iji)mt5copy_rates_from_posTIMEFRAME_D1PULLrA   floatr   fromtimestampr   r   strftimerY   PAIR_EVENT_CLASSESr3   DEFAULT_EVENT_CLASSESrangemathlog
statisticsfmeanappendsqrtLAMBDAabsWD_ROLLrF   maxroundCHART_BARS_D1r.   )9sym	canonical	target_wdrT   todayratesro_allh_alll_allc_allds_allri   rj   rk   endr]   r^   r_   r`   dswdr;   rU   iparkvarsigsig_nextwxr_wxo_whistrE   pairra   tclspoolabxrxorD   annf50rf75rf50of75opxxr_runinsidetotal
start_evalbandrn   
d1_candlesrS   s9   &&&f&                                                   @r   forecast_numbersr   {   s_    ##C)9)91dCE}E
S(eW&?@@',-u!U1V9uE-',-u!U1V9uE-&+,eU1U8_eE,(-.1U1W:E.-24-2 $$QvY=x
#$-2  4 bzU2Y
'
%j1n
"2Y
%j
SyeW&IJJdsAdsAdsAdsA	B#;
 a 
 
 6HLL
9
A
A
C  
  "%%i1FG$G   sG$G 3q6]$" XXadQqTk"a'AO<<" 	 $


4:
&C
C1c!f

499S>"sla&jDG33  yy~H 8$8aArE8D$ 8$8aArE8D$&'wCGwD'1c!fAJ61!!$q(#adQqTk*:QqT*AA*EF1IT"AKtAw'AKtAw'  E$9U$DDJ2%Dz$%  !DDAqaD! !DDAqaD!f=>EXXi$gXY/XXi$gXY/r7R<"1X7XtAww!!w!X7>B"1X7XtAww!!w!X7>B
TYYs^
#c
)CAb"I%x!B*'<$DAb"I%x!B*'<$D	
2B
 #1X&XaeXF&FEQA%J1c!fAJ6RU
?"Q%='+D4yB1T3<'
aD1Q4K1Q4'4/aKFr!uadQqTkQqT1A56   B; 
efnu,a0#u=   s1c%j=893u:F GA 
fQieAhU1X	eAhU1X	'F  d 	e}4 	5a=	
 	%s
A& 	tcz1% 	%s
A& 	tcz1% 	R 	j 	& 	j 	bf 	#b' 	H  	D*%! e .-,.4(
 $$ %$'$ "! 87 '.su   b-bb"/b'Ab,Ab1(
b67b6 b6&Ab;5c c"c
	ccc c!(c'8c,c                4   R\         P                  R\         P                  R\         P                  /p/ p\         F  w  rE\         P
                  ! WV,          ^ V4      pVe   \        V4      ^ 8X  d   . W4&   K>  V Uu. uF  pR\        VR,          4      R\        \        VR,          4      V4      R\        \        VR	,          4      V4      R
\        \        VR,          4      V4      R\        \        VR,          4      V4      /NK  	  upW4&   K  	  V# u upi )zrPull M15/H1/H4 candle sets for timeframe switching on the page.
Times are the raw MT5 epoch values (broker clock).H4H1M15r\   rN   r]   r0   r^   rK   r_   rL   r`   rM   )
rr   TIMEFRAME_H4TIMEFRAME_H1TIMEFRAME_M15INTRADAY_TFSrs   rA   rB   r   rv   )r   digitstf_idsr8   r=   barsrrr   s   &&      r   intraday_candlesr   	  s     C$$dC,<,<S&&(F
C"
$$S,4@:RACI 
  #ai.%ai(&1%ai(&1%ah0%aj)62	4
 
	 # J
s   =BDc                  h   \         P                  ! 4       '       g-   \        P                  ! R \         P                  ! 4        24        \        4       p V P                  4       pV P                  4       p\        4       w  r4R\        P                  ! \        P                  4      P                  R4      RTRV R RRRV'       d   \        V4      MR	R
. /p\        RV RV R R24       V'       d   \        RV 24       M\        R4       \          EF  w  rgR	pV Fc  p	\         P"                  ! V	R4      '       g   K"  \         P$                  ! V	\         P&                  ^ ^4      p
V
f   KO  \)        V
4      ^ 8  g   Ka  T	p M	  VfG   VR
,          P+                  RVRRRRRP-                  V4       R2/4       \        RVR RV R24       K  \         P.                  ! V4      pV'       d   \1        VP2                  4      M^p\5        WW#V4      pVP7                  RVRVRV/4       VP9                  R4      '       d"   VR,          P7                  \;        W4      4       VR
,          P+                  V4       VP9                  R4      '       dX   VR,          R 8X  d   R MR!p\        RVR R"VR# R$VR%,          R& R'VR(,          R& R)VR*,          R& R+VR,,           R-V R24       EK  \        RVR R.VP9                  R4       24       EK  	  \<        ;QJ d#    R/ VR
,           4       F  '       g   K   RM	  RM! R/ VR
,           4       4      VR0&   \>        PA                  \>        PB                  R1,           4      p\E        VR2R3R47      ;_uu_ 4       p\F        PH                  ! VV4       R	R	R	4       \J        PL                  ! V\>        4       \        R5\>         2VR0,          '       d   R6MR7,           4       \         PN                  ! 4        R	#   + '       g   i     Ll; i  \         PN                  ! 4        i ; i)8zMT5 initialize failed: generated_utcz%Y-%m-%d %H:%M:%Sr   session_labelz%A, %B %d, %YengineuS   vol_range_forecast v3 engine · completed-bar stats · session rolls 17:00 New York
events_csvNinstrumentsu'   VOL & RANGE FORECAST build — session z (z%A)z
  events: u=     events: no calendar_events.csv found — weekday-only bandsTdisplayrI   FrJ   zno symbol found (tried z, z  z<8z FAIL  none of z
 availablesymbolr   ro   rk   r0   zPRIOR-CLOSE anchorz OK    z<12z vol rc   z.2fz%  HL rd   /re   z  [ra   z] (z FAIL  c              3   |   "   T F2  pVP                  R 4      ;'       d    VP                  R4      R8H  x  K4  	  R# 5i)rI   rk   rO   N)r3   )r   r   s   & r   r!   main.<locals>.<genexpr>U  s8      $
' EE$K@@AEE,/=@@'s   <<anchor_pendingz.tmpr   r#   )r%   z  wrote u.     (some anchors pending — server will retry)r)   )(rr   
initializesysexit
last_errorr   	isoformatrY   r>   r   r   r   r   rx   r5   printINSTRUMENTSsymbol_selectrs   rt   rA   r   joinsymbol_inforB   r   r   updater3   r   anyOUT_JSON	with_namer=   r0   jsondumposr$   shutdown)sdr   r   rT   ev_pathr8   r   
candidatesresolvedcandtestinfor   dtagtmpr9   s                    r   mainr     s   >>*3>>+;*<=>?^JJL	!mX\\*334GHE=1 5'#g,t2	
 	7wbBqIJJwi()QR#.;GH"$$T4002249I9I1aPD'CIM#' # M"))we ''+yy'<&=Q@+ 
 72,oj\LM??8,D)-S%1F I5IAHHi(H( )uuT{{)##$4X$FG%%a(uuT{{ !,6 9f?S72,ghs^ <y\#.fQx[4EQ7C(AgJ<s3%qB C 72,gaeeGn-=>?= $/> !$ $
'$
 $
'$
 !
   !78#sW--IIc1 .


3!
#()) B/13 	4 	 .- 	sT   	C,P :(P 'P 9A-P 'B!P 	BP P ,AP P#AP P	P P1__main__)XAUUSDr   zXAUUSD.azXAUUSD.r)r   zEURUSD.azEURUSD.r)r   zGBPUSD.azGBPUSD.r)r	   zUSDJPY.azUSDJPY.r)USTECNAS100US100z
USTEC.cashzNAS100.cashz
US100.cash))r      )r   i  )r   i   )United StateszFed Interest Rate Decision)r   zPayroll Jobs Growth)r   zInflation Rate Year-over-Year)z	Euro AreazECB Interest Rate Decision)zUnited KingdomzBoE Interest Rate Decision)r   r   r   r   )r   r   r   r   )r   r   r   )%__doc__r   r|   r   r~   r   r   r   r   pathlibr   MetaTrader5rr   ImportErrorr   __file__resolveparents	BASE_PATHr   r   r   r   r-   r   ru   r   r.   ry   rz   r   r>   rF   r   r   r   __name__rQ   rG   r   <module>r      sf  2   	  
 2 2 W N""$,,Q/	++ 9:121212	    		gh%%  


<	5?	8	= --  1 	,$?K\,BJ zF 	  WHHUVWs   C C.-C.