+
     =jդ              	        a  0 t $ R t^ RIHt ^ RIt^ RIt^ RIt^ RIt^ RI	t	^ RI
t^ RIt^ RIHt ^ RIt^ RIt^ RIHt ^ RIt^ RIt^ RIt]! ]4      P3                  4       P4                  P4                  t]P8                  P;                  ^ ]! ]4      4       ^ RIH t H!t!H"t"H#t#H$t$H%t%H&t&H't'H(t(H)t) ^ RI*H+t,H-t-H.t.H/t/H0t0H1t1 ^ RI2H3t3H4t4 Rt5]5s6. RMOt7]R	,          R
,          t8]R	,          R,          t9]R	,          R,          t:Rs;R]<R&   Rs=R]<R&   . RNOt>. ROOt?Rt@^tARtBR R ltCR R ltDR R ltER R ltFRPR R lltGR R ltHR R  ltIR! R" ltJR# R$ ltKR% R& ltLRQR( R) lltMR* R+ ltNR, R- ltOR. R/ ltP]Q! 4       tRR0 R1 ltSR2 R3 ltTR4 R5 ltUR6 R7 ltVR8 R9 ltWRRR: R; lltXR< R= ltYRSR> R? lltZR@ RA lt[RB RC lt\RD RE lt]RF RG lt^R^]3R'R'RRRR3	RH RI llt_RJ RK lt`]aRL8X  d
   ]`! 4        R# R# )Ta  
Deeper threshold sweep for BTC MLP presets.

This intentionally keeps MLP weights fixed and explores threshold/config
parameters around the current winners and artifact recommendations. It reuses
strategy_mlp_scores' score-to-signal block and canonical calculate_metrics so
candidate metrics stay comparable to optimizer winners.

Examples:
    .venv/bin/python tools/run_mlp_deep_sweep.py --timeframes 6H 8H 12H 1D --samples 50000
    .venv/bin/python tools/run_mlp_deep_sweep.py --timeframes 6H --samples 10000 --promote
)annotationsN)Path)Any)
	OOS_STARTSCORE_STARTSUBPERIOD_SPLIT	TRAIN_ENDTRAIN_START	WFO_FOLDSWFO_MIN_OOS_TRADESWFO_MIN_VALID_FOLDScomposite_scoreget_min_trades)FEATURE_COLS_prepare_features_score_to_signalscalculate_metricsload_mlp_artifactmlp_forward)DEFAULT_WORKER_FRACTIONresolve_worker_countCOINBASE_BTCUSDresultswinnerssweepsreportsz
str | None
_WORKER_TFzIdict[str, tuple[pd.DataFrame, np.ndarray, np.ndarray, np.ndarray]] | None_WORKER_CACHE      ?g     @U@c                    V ^8  d   QhRRRR/#    tfstrreturnr    )formats   "C/Users/jameslopez/projects/TradingBot25/tools/run_mlp_deep_sweep.py__annotate__r(   i   s     T TC TD T    c                2    \         R \         RV  R2,          # )(optimization_winner_strategy_mlp_scores__.csv)
WINNER_DIRASSETr"   s   &r'   winner_pathr1   i   s    B5'2$dSSSr)   c                    V ^8  d   QhRRRR/# )r!   r"   r#   r$   r%   )r&   s   "r'   r(   r(   m   s     B Bc Bc Br)   c                    R \          RV  R2# )z"strategies/params/mlp/mlp_weights_r,   z.json)r/   r0   s   &r'   artifact_pathr4   m   s    /wat5AAr)   c                    V ^8  d   QhRRRR/# r    r%   )r&   s   "r'   r(   r(   q   s     / /# /$ /r)   c           
         R RRRRRRRRR/pVP                  W 4      p\        R	,          R
,          \         RV R2,          pVP                  4       '       d   V# \        R	,          \         RV  R2,          # )4H2406H3608H48012H7201Ddatamlpz, r-   -)getREPOr/   exists)r"   _TF_MINUTESperiodmlp_paths   &   r'   	data_pathrI   q   sx    eT5%dSK__R$Ff}u$%6($'??H&=eWAbT...r)   c                    V ^8  d   QhRRRR/# )r!   vr   r$   boolr%   )r&   s   "r'   r(   r(   {   s      s t r)   c                    \        V \        4      '       d"   V P                  4       P                  4       R9   # \	        V 4      # )true>   1yesrN   )
isinstancer#   striplowerrL   )rK   s   &r'   as_boolrT   {   s1    !Swwy $8887Nr)   c               $    V ^8  d   QhRRRRRR/# )r!   rK   r   defaultfloatr$   r%   )r&   s   "r'   r(   r(      s!       e e r)   c                |     \         P                  ! V 4      '       d   V# \        V 4      #   \         d    Tu # i ; iN)pdisnarW   	Exception)rK   rV   s   &&r'   as_floatr]      s6    771::NQx s   + + 
+ ;;c                    V ^8  d   QhRRRR/# )r!   r"   r#   r$   dict[str, Any] | Noner%   )r&   s   "r'   r(   r(      s      C 1 r)   c                `   \        V 4      pVP                  4       '       g   R # \        \        P                  ! VP                  4       4      4      pV'       g   R # \        V^ ,          4      p\        VP                  RR4      4      VR&   \        VP                  RR4      4      VR&   ^VR&   V# )Ni_mvrv_suppress_bearFi_use_long_entry_confirmationi_use_long_exit_confirmation)	r1   rE   listcsv
DictReaderopendictrT   rC   )r"   pathrowsrows   &   r'   load_winnerrl      s    r?D;;==tyy{+,D
tAw-C")#''2H%*P"QC+2377;Z\a3b+cC'(*+C&'Jr)   c                    V ^8  d   QhRRRR/# )r!   weights_pathr#   r$   dict[str, Any]r%   )r&   s   "r'   r(   r(      s      C N r)   c                "   \        V 4      pVR ,          P                  R/ 4      P                  R/ 4      pRV RVP                  RR4      RVP                  RR4      RVP                  RR4      RVP                  RR4      R	^R
RR^ RRRRR^R^/pV# )metatrainingrecommended_thresholdsmlp_weights_file!i_long_entry_activation_threshold         i_long_exit_activation_threshold-i_long_exit_activation_confirmation_thresholdi_trailing_stop_thresholdrc   rb   Fi_regime_windowi_regime_entry_min_scorera   i_exit_score_windowi_entry_score_window     @)r   rC   )rn   artrecparamss   &   r'   recommended_from_artifactr      s    
L
)C
f+//*b
)
-
-.F
KCL+SWW5XZ]-^*CGG4VX[,\7;S:
 	$SWW-H#%N&'1"GqF  Mr)   c          
     ,    V ^8  d   QhRRRRRRRRRR/# )r!   valuerW   startstopstepr$   r%   )r&   s   "r'   r(   r(      s/     & & &e &5 & &% &r)   c                    \        \        W4      W#,
          4      p \        W,
          V,          4      p\        WV,          ,           ^4      # )   )minmaxround)r   r   r   r   ns   &&&& r'   snapr      s:    E!4;/Eu}$%AT!1%%r)   c                    V ^8  d   QhRRRR/# )r!   pro   r$   tuple[Any, ...]r%   )r&   s   "r'   r(   r(      s       O r)   c                    V R ,          \        V R,          4      \        V R,          4      \        V R,          4      \        V R,          4      \        \        V P                  RR4      4      4      3# )rt   ru   rw   rx   ry   ra   F)r]   rL   rT   rC   r   s   &r'   	param_keyr      sf    	
678567BCD./0WQUU159:; r)   c                    V ^8  d   QhRRRR/# )r!   r   ro   r$   r%   )r&   s   "r'   r(   r(      s      ~ . r)   c           "        R \        V R ,          4      R\        V R,          4      R\        V R,          4      R\        V R,          4      R\        R\        V P                  RR4      4      4      R^RRR	^ R
RR\	        V P                  RR4      4      R\        ^\        \        V P                  R^4      4      4      4      R\        ^\        \        V P                  R^4      4      4      4      /# )rt   ru   rw   rx   ry   rv   rc   rb   Frz   r{   ra   r|   r}   r~   )r#   r]   r   rC   rT   intr   s   &r'   normalise_paramr      s    C"4 56+Xa8[6\-]*HQ7Y5Z,[7=>:
 	$Shquu=XZ]7^._%`&'1"G.De(L Ms1c(1559NPQ3R*S&TUAs8AEE:PRS4T+U'V W r)   Fc               @    V ^8  d   QhRRRRRRRRRR	R
RRRRRRRRR/
# )r!   r"   r#   samplesr   seedextra_weightslist[str] | NonewiderL   entry_lofloat | Noneentry_hiexit_loexit_hir$   list[dict[str, Any]]r%   )r&   s   "r'   r(   r(      se     _ _C _# _S _IY _hl _"._AM_!-_?K_Wk_r)   c	                  a!a" \         P                  P                  V4      p	\        V 4      p
\	        V 4      pT;'       g    . p. pV
'       d   VP                  \        V
4      4       \        P                  P                  V4      '       d$   VP                  \        \        V4      4      4       V F&  pVP                  \        \        V4      4      4       K(  	  V R 8X  d_   \        R8X  dT   \	        R4      p\        P                  P                  V4      '       d$   VP                  \        \        V4      4      4       \        4       o". o!R V!V"3R llpV'       d   RMRw  ppV'       d   RMR w  ppVe   TMTpVe   TMTpVe   TMTpVe   TMTpV'       d   RMRp\         P                  ! RVR4      pV Fv  p\        V4      p\        VP!                  R	R4      4      p\#        V\         P$                  ! \         P&                  ! VV,
          4      4      ,          4      VR	&   V! VR
4       Kx  	  \)        RWe,
          R,          4      p\)        RW,
          R,          4      p\+        VR,          4      p\)        ^ VV,
          4      pV'       Ed   \)        ^V\-        V4      ,          4      p\/        V4       EFR  w  pp\1        V4       EF;  p\        V4      p\3        V	P5                  \        VR,          4      V4      WVR4      VR&   \3        V	P5                  \        VR,          4      V4      WxR4      VR&   \3        V	P5                  \        VR,          4      V4      WxR4      VR&   \#        V	P7                  \         P                  ! RVR4      4      4      VR	&   \9        V	P7                  RR.4      4      VR&   \+        V	P7                  . R!O4      4      VR&   \+        V	P7                  . R!O4      4      VR&   V! VRV 24       EK>  	  EKU  	  \;        V Uu0 uF  pVR,          kK  	  up4      p \1        V4       EFB  pR\=        V	P7                  V 4      4      R\#        V	P7                  \         P                  ! WVR4      4      4      R\#        V	P7                  \         P                  ! WxR4      4      4      R\#        V	P7                  \         P                  ! WxR4      4      4      R	\#        V	P7                  \         P                  ! RVR4      4      4      R^RRR^ RR"R\9        V	P7                  RR.4      4      R\+        V	P7                  . R!O4      4      R\+        V	P7                  . R!O4      4      /pV! VR4       EKE  	  S!# u upi )#r?   r   r=   c               $    V ^8  d   QhRRRRRR/# )r!   r   ro   sourcer#   r$   Noner%   )r&   s   "r'   r(   )generate_candidates.<locals>.__annotate__   s!      ~ s t r)   c                   < \        V 4      p\        V4      pVS9   d   R # SP                  V4       WR&   SP                  V4       R # )Nr   )r   r   addappend)r   r   ppkeyoutseens   &&  r'   r    generate_candidates.<locals>.add   s=    Qm$;8

2r)   g      Y@g      I@rv   g      @ry   r   g      @g      $@g?ru   rw   rx   FTra   r|   r}   local_rt   rc   rb   rz   r{   broad)g     g     @)     rg     r@)g     pg     p@)r   g      y@)   r   r   r!            r~   )nprandomdefault_rngrl   r4   r   r   osri   rE   r   r/   setarangerh   r]   rC   rW   argminabsr   r   len	enumerateranger   normalchoicerL   sortedr#   )#r"   r   r   r   r   r   r   r   r   rngcurrentown_artifactseedsrn   alt_12hr   _entry_lo_def_entry_hi_def_exit_lo_def_exit_hi_deftrail_hiallowed_trails	raw_traillocal_sigma_entrylocal_sigma_exitlocal_nbroad_nper_seedir,   r   weight_choicesr   r   s#   &&&&&&&&&                        @@r'   generate_candidatesr      s    ))


%C"oG $L!''RM"$E_W-.	ww~~l##_%>|%LMN%_%>|%LMN & 
Tze00  &77>>'""LL)B7)KLM!$D "C  7;? M=6:?L<#/x]H#/x]H"/w\G"/w\G.25HIIc8S1MGQUU#>DE	).}RYYrvvm^gNgGh=i/j)k
%&Av	 
 sX%8C$?@tg&73%>?'D.!G!Ww&'Guq'SZ/0e$DAq8_G9=JJx*M(NOQbc:56 9=JJx*L(MNP`ad945 FJJJx*Y(Z[]mndFAB 27szz"))CQY[^B_7`1a-.,0UDM1J,K()+.szz:O/P+Q'(,/

;P0Q,R()As|$% % %* EBEqQ122EBCN7^CJJ~$> ?/szz"))H`cBd7e1f.cjj7]aAb6c0d;U3::biiX_jnNoCp=q'szz"))CSV:W/X)Y*A+Uq&"DUDM)B$C!3szz2G'H#I"C

3H(I$J
 	Aw " J% Cs   V	c               $    V ^8  d   QhRRRRRR/# )r!   r"   r#   rn   r$   z7tuple[pd.DataFrame, np.ndarray, np.ndarray, np.ndarray]r%   )r&   s   "r'   r(   r(   4  s'     b b# bS b5l br)   c                P   \         P                  ! \        V 4      4      pVP                  P                  P                  4       P                  P                  4       Vn        \         P                  ! VR ,          RR7      P                  P                  R4      VR &   VP                  VR ,          \        8  ,          P                  4       P                  RR7      p\        V4      pVP                  R4      ;'       g    . pV Uu. uF#  qU\         9  g   K  WRP                  9  g   K!  VNK%  	  ppV'       d0   \#        R\$        P&                  P)                  V4       RV R24      h\+        Y$;'       g    R4      pV. RO,          P                  4       pR F  p	WP                  9   g   K  W),          W&   K!  	  \-        WsR,          4      VR&   WR,          VR	,          P/                  \0        P2                  4      VR
,          P/                  \0        P2                  4      3# u upi )timeT)utcN)dropfeature_colszStale artifact z8: expects features not in current FEATURE_COLS or data: z. Retrain to skip.closehighlayersactivation_score)r   r   r   )mvrv_regime)NNN	   )rZ   read_csvrI   columnsr#   rS   rR   to_datetimedttz_localizelocr	   copyreset_indexr   rC   _FEATURE_COLS
ValueErrorr   ri   basenamer   r   to_numpyr   float64)
r"   rn   fullr   art_colscmissing_from_currentXscoredcols
   &&        r'   load_scored_framer   4  s   ;;y}%D<<##))+//557DL>>$v,D9<<HHNDL88DLK/0557CCCND
L
)Cww~&,,"H'/dx!M3IAaWcWcNcAAxdbgg..|<= >--A,BBTV
 	
 	$ 0 0D1A+,113F,,)FK   "-QH!>FT7DM222::>V@U@UVXV`V`@aaa es   H#H#(H#c               (    V ^8  d   QhRRRRRRRR/# )	r!   scoresz	pd.Seriesr   ro   marginrW   r$   r   r%   )r&   s   "r'   r(   r(   K  s(     
& 
&) 
&^ 
&U 
&s 
&r)   c                   \         P                  ! \        VR ,          4      \        VR,          4      \        VR,          4      .\         P                  R7      p\         P                  ! \         P
                  ! V P                  \         P                  4      R,          VR,          ,
          4      ^R7      p\        \         P                  ! WB8  4      4      # )ru   rw   rx   )dtype)axis)r   N)Nr   )	r   arrayr]   r   r   r   r   r   sum)r   r   r   
thresholdsdists   &&&  r'   fragile_countr  K  s    V?@AV>?@VKLM	

 jjJ 66"&&4W=
7@SST[\]Drvvdm$%%r)   c               (    V ^8  d   QhRRRRRRRR/# )r!   dfzpd.DataFramescore_startr#   endr$   rW   r%   )r&   s   "r'   r(   r(   X  s.     N N NC Nc Ne Nr)   c                d   W R ,          \         P                  ! V4      8  V R ,          \         P                  ! V4      8*  ,          ,          p\        V4      ^8  d   R# \        RVR ,          P                  R,          VR ,          P                  ^ ,          ,
          P
                  R,          4      # )r         ?g     v@)rZ   	Timestampr   r   ilocdays)r
  r  r  sls   &&& r'   is_yearsr  X  sy    	Z2<<44Fr||TWGX9XY	ZB
2w{sRZ__R(2f:??1+==CCfLMMr)   c               (    V ^8  d   QhRRRRRRRR/# )r!   r"   r#   r   ro   cachezBdict[str, tuple[pd.DataFrame, np.ndarray, np.ndarray, np.ndarray]]r$   r%   )r&   s   "r'   r(   r(   b  s4     m m mn m5w m  }K mr)   c                P   VR ,          pW29  d    \        W4      W#&   W#,          \        J dx   \        V4      pVP                  / R^ bRRbR	R1bR
R1bRR1bRRbRRbRRbRRbR^ bRR1bRR1bRR1bRR1bR^cbR. bR^ bRRRR1RR1RRRRRRRRR^cR ^cR!VP                  R!R"4      /
C4       V# W#,          w  rgr\        V4      p
\        V
R#&   \        VP                  4       WW4      pWR$,          \        P                  ! \        4      8*  ,          P                  4       p\!        V\        R%7      p\#        V\        \        4      p\%        VP                  RR4      VP                  R
R4      \'        VP                  R^ 4      4      VVP                  RR4      4      p\        P                  ! \(        4      p\!        WR$,          V8*  ,          P                  4       \        R%7      p\!        WR$,          V8  ,          P                  4       \(        R%7      p. p\*         F  w  pppWR$,          \        P                  ! V4      8*  ,          P                  4       p\!        VV\,        R&7      p\'        VP                  R^ 4      4      \,        8  g   Ks  VP/                  \1        VP                  RR24      4      4       K  	  \3        V4      \4        8  d[   \1        \6        P8                  ! V4      4      p\1        \6        P:                  ! V4      4      p\'        \=        R' V 4       4      4      pMRpR2p\3        \*        4      pVR$,          P?                  4       \        P                  ! \@        4      8  dZ   \!        V\@        ^R&7      p\'        VP                  R^ 4      4      ^ 8X  d'   R3 F   pVP                  V4      R48X  g   K  RVV&   K"  	  M$\B         Uu/ uF  pV\6        PD                  bK  	  pp\6        PD                  ;p;p p!R(VPF                  9   d   WR$,          \        P                  ! \@        4      8  ,          p"\3        V"4      ^ 8  d   \3        V"4      p#\1        V"R(,          R58H  P=                  4       V#,          ^d,          4      p\1        V"R(,          ^ 8H  P=                  4       V#,          ^d,          4      p \1        V"R(,          ^8H  P=                  4       V#,          ^d,          4      p!\H         Uu/ uF  pVVV,          bK  	  ppVP                  V4       TP                  / RVbR\K        V4      bRVP                  RR44      bR)VP                  R
R44      bR*VP                  R^ 4      bRVP                  RR44      bR+VP                  R
R44      bR,VP                  R^ 4      bRVbRVbRVbR\L        PN                  ! V U$u. uF  p$\Q        V$^4      NK  	  up$4      bRVP                  R\6        PD                  4      bRVP                  R\6        PD                  4      bRVP                  R
\6        PD                  4      bRVP                  R\6        PD                  4      bRVP                  R\6        PD                  4      bRVRV RV!R\S        VR-,          VR.4      R \S        VR-,          VR/4      R!VP                  R!R04      /C4       V#   \         dD   p\        R\        P                  P                  T4       RT 2RR7       \        Y#&    Rp?ELRp?ii ; iu upi u upi u up$i )6rt   z	  [SKIP] : TflushNTotal TradesTotal P&L %rv   Sharpe RatioSortino RatioCalmar RatioP&L/DD RatioMax Drawdown %% In Market	CompositeMin_Trades_FloorSub1_CalmarSub2_Calmar	WFO_ScoreWFO_Min_FoldWFO_Neg_FoldsWFO_Fold_CalmarsOOS_Total TradesOOS_Total P&L %OOS_Sortino RatiozOOS_Calmar RatiozOOS_Max Drawdown %OOS_Bear_PctOOS_Neutral_PctOOS_Bull_PctFragile_0.05Fragile_1.0r   stale_pine_time_startr   )r  )r  
min_tradesc              3  6   "   T F  q^ 8  g   K  ^x  K  	  R# 5i)    Nr%   ).0r   s   & r'   	<genexpr>evaluate.<locals>.<genexpr>  s     ;\U!!\s   
r   Sub1_SortinoSub1_TradesSub2_SortinoSub2_Tradesr   g?r   	candidate     8     X)r  r  r        $r  )*r   r   printr   ri   r   _STALE_SENTINELrh   updaterC   r   r   r   rZ   r  r   r   r  r   r   r   r
   r   r   rW   r   r   r   meanr   r  r   r   METRIC_COLSnanr   
PARAM_COLSr   jsondumpsr   r  )%r"   r   r  weightsexcresultr   
stoch_peak	close_arrhigh_arrsignal_paramssigs
train_sigsmetricsyearscompsplitsub1sub2fold_calmarsr,   	oos_startoos_endfold_windowfold_metrics	wfo_scorewfo_minwfo_negoos_kkoos_bear_pctoos_neutral_pctoos_bull_pctoos_sigsr   r   s%   &&&                                  r'   evaluaterj  b  s
   '(G	-.r;EN ~(f 	A~q 	A- 	Anf 	A'	A1?	AIWY\	A(#	A/<c	ACNPS	A +A	A 0=f	A GTU[	A $V		A .<V		A FUVX		A
 +B	A
 1CA	A
 HYZ]+V5G,c>3HY[^&^RPR &**Xw"?	A 		B .3n+F	LM(3M$%V[[]My[D6lbll9&==>CCEJ
DGZi8ENC(OS)GKK*+M3'D LL)EZ6(:e(CDIIKYdeDZ6(:U(BCHHJXghDL!*9g F!3r||G7L!LMRRT()Xjk|237IIl&6&6~u&M NO	 "+
 <//"'',/0	|,-c;\;;<	i.F|R\\)44)J sww~q)*a/G772;%'!CG H #..+Qq"&&y+. 57FF:L:?\$VY(??@x=1HA#Xm%<%B$G$G$IA$MPS$STL#Xm%<%B$G$G$IA$MPS$STO#Xm%<%B$G$G$IA$MPS$STL4>?JqalJF?
MM'
MM	
	
u 5	
 488NE:	
 DHH_e<		

 488NA6	
 488NE:	
 DHH_e<	
 488NA6	
 	
 G	
 W	
 

+NAE!QK+N O	
  ?	
 sww}bff=	
  "&&!A	
   ?!	
" !#''*:BFF"C#	
$ LLM*5G*H&RVW=4F)GQTUfjj;7/	
6 MQ  	-Ibgg..w783%@M,EN	-| / @ ,Os)   [ \-\>\#\8\\c               $    V ^8  d   QhRRRRRR/# )r!   r"   r#   assetr$   r   r%   )r&   s   "r'   r(   r(     s!      C   r)   c                    V s / sVsR # rY   )r   r   r/   )r"   rl  s   &&r'   init_workerrn    s    JMEr)   c                    V ^8  d   QhRRRR/# )r!   r   ro   r$   r%   )r&   s   "r'   r(   r(     s     7 7N 7~ 7r)   c                b    \         e	   \        f   \        R4      h\        \         V \        4      # )Nz sweep worker was not initialized)r   r   RuntimeErrorrj  )r   s   &r'   evaluate_workerrr    s(    ]2=>>J66r)   c                    V ^8  d   QhRRRR/# )r!   rk   ro   r$   r   r%   )r&   s   "r'   r(   r(     s      N  r)   c                B   \        \        V P                  R ^ 4      4      ^ 8H  4      p\        V P                  RR4      4      p\        V P                  RR4      4      p\        \        V P                  RR4      R4      4      p\        \        V P                  RR4      4      ^ 8  4      \        \        V P                  RR4      4      ^ 8  4      ,           p\        V P                  RR4      R4      p\        V^ 8  4      \        V^ 8  4      VVVV) \        \        V P                  R^c4      4      4      ) \        V P                  R	R4      4      \        V P                  R
R4      4      V\        V P                  RR4      4      V\        V P                  RR4      4      3# )r1  r#  rv   r   r!  r%  r&  r-  r)  r(  r'  r  r  r@  rB  rA  )r   r]   rC   r   )rk   
fragile_ok	compositepnl_dddrawdownsub_okoos_sortinos   &      r'   
rank_tupler{    s]   Xcggna89Q>?Jc23Icggnc23F8CGG$4f=vFGH#''-781<=HSWWUbdiMjDknoDo@ppF377#6>FKIMFQJ			Xcggor23	44/0c*+/0,- r)   c               $    V ^8  d   QhRRRRRR/# )r!   r"   r#   rk   ro   r$   r   r%   )r&   s   "r'   r(   r(     s!     6 6S 6~ 6$ 6r)   c                    \        V 4      p\        \        ,            Uu/ uF  q3VP                  VR 4      bK  	  pp\        P
                  ! V.4      P                  VRR7       R# u upi ) FindexN)r1   rI  rG  rC   rZ   	DataFrameto_csv)r"   rk   ri   re  ordereds   &&   r'   write_winnerr    sW    r?D*4{*BC*BQ#''!R. *BGCLL'""4u"5 Ds   A&c               (    V ^8  d   QhRRRRRRRR/# )r!   bestro   r   r_   stored_winnerr$   rL   r%   )r&   s   "r'   r(   r(     s+     6 6^ 6.C 636?C6r)   c           
        Ve   V P                  R4      R8X  d   R# \        V P                  RR4      4      ^ 8:  d   R# \        V P                  R^c4      4      ^ 8w  d   R# \        V P                  RR4      4      ^ 8:  g!   \        V P                  RR4      4      ^ 8:  d   R# \        \        V P                  R	^c4      4      4      ^ 8w  d   R# \        \        VP                  RR4      4      \        T;'       g    / P                  RR4      4      4      p\        \        VP                  R
R4      4      \        T;'       g    / P                  R
R4      4      4      p\        V P                  RR4      4      V8  d   R# \        V P                  R
R4      4      V8  d   R# \        \        V P                  R4      R4      4      p\        \        \        T;'       g    / P                  R4      R4      \        VP                  R4      R4      4      4      p\        V P                  R4      R4      pV\        8  ;'       d    V\        8  ;'       d
    V\
        8  pV'       dw   \        V P                  R4      R4      p	\        \        VP                  R4      R4      \        T;'       g    / P                  R4      R4      4      p
W\        ,
          8  d   R# R# )Nr   r   Fr   rv   r1  r%  r&  r)  r  r+  r.  r-  TrB  )rC   r]   r   r   OOS_MIN_TRADESOOS_BEAR_PCT_SKIPOOS_SORTINO_DELTA)r  r   r  
ref_pnl_dd
ref_calmarbest_oos_tradesref_oos_tradesrf  oos_gate_appliesbest_oos_sortinoref_oos_sortinos   &&&        r'   
promotabler    si   $((8,	9-.!3,-2./14-Y^A_8`de8e
8DHH_b123q8 ^S12-%%2**>3?@J ^U34-%%2**>5ABJ -.;/0:= (488,>#?EFO#-%%2**+=>D/0#6 N DHH^4c:L>) 	- 	-n,	- 	-,, 
 #DHH-@$A5IW[[!45u=m))r../BCUK
 0AAAr)   c               $    V ^8  d   QhRRRRRR/# )r!   totalr   workersr$   zlist[tuple[int, int]]r%   )r&   s   "r'   r(   r(   6  s"      c C <Q r)   c                    V ^ 8:  d   . # \        \        ^V4      V 4      p\        W4      w  r4. p^ p\        V4       F.  pWc,           Wt8  d   ^M^ ,           pVP	                  Wh34       TpK0  	  V# )zIReturn balanced, contiguous ranges covering every candidate exactly once.)r   r   divmodr   r   )	r  r  shard_countbaseextrarangesr   ordinalr  s	   &&       r'   partition_candidate_rangesr  6  sq    z	c!Wou-K,KDFE%l7?a:ul# & Mr)   c               (    V ^8  d   QhRRRRRRRR/# )r!   r"   r#   
candidatesr   show_progressrL   r$   r%   )r&   s   "r'   r(   r(   E  s0      $  	r)   c           
         / p. p\        V^4       FZ  w  rVVP                  \        WV4      4       V'       g   K*  VR,          ^ 8X  g   K:  \        V  RVR R\	        V4      R 2RR7       K\  	  V# )r     : evaluated ,/Tr  )r   r   rj  rC  r   )r"   r  r  r  r   idxr   s   &&&    r'   evaluate_candidates_sequentialr  E  sp    
 QSEG Q/xE23=S4Z1_RDSG1S_Q,?@M 0 Nr)   c               $    V ^8  d   QhRRRRRR/# )r!   ri   r   payloadro   r$   r   r%   )r&   s   "r'   r(   r(   S  s!      t n  r)   c                "   V P                  V P                  R ,           4      pVP                  R4      ;_uu_ 4       p\        P                  ! W\        P
                  R7       RRR4       VP                  V 4       R#   + '       g   i     L#; i)z.tmpwb)protocolN)with_suffixsuffixrg   pickledumpHIGHEST_PROTOCOLreplace)ri   r  	temp_pathhandles   &&  r'   _write_pickle_atomicr  S  s\      v!56I			Gf.E.EF 
d 
	s   'A>>B	c               (    V ^8  d   QhRRRRRRRR/# )r!   r"   r#   
input_pathr   output_pathr$   r   r%   )r&   s   "r'   r(   r(   Z  s(      3 D t  r)   c                   VP                  R 4      ;_uu_ 4       p\        P                  ! V4      pRRR4       XP                  R4      ^8w  g   VP                  R4      V 8w  d   \	        R4      hVP                  R4      p\        V\        4      '       d8   VP                  R^ 4      VP                  R^ 4      ,
          \        V4      8w  d   \	        R4      h\        VR	,          4      s	\        WR
R7      p\        VR^R	\        RV RVR,          RVR,          RVR,          RV/4       R#   + '       g   i     EL; i)rbNversionr"   zinvalid sweep shard metadatar  r  r   z#invalid sweep shard candidate rangerl  F)r  r  r   )rg   r  loadrC   r   rQ   rd   r   r#   r/   r  r  )r"   r  r  r  r  r  r   s   &&&    r'   run_internal_shardr  Z  s   			&++f% 
{{9"gkk$&72&=788\*Jj$''7;;ua+@7;;wXYCZ+Z^abl^m+m>?? !E,R5QGqU"wy)WW%75>w	
 
		s   D--D>	c               (    V ^8  d   QhRRRRRRRR/# 	r!   r"   r#   r  r   r  r   r$   z tuple[list[dict[str, Any]], int]r%   )r&   s   "r'   r(   r(   t  s6     [# [#[#$[# [# &	[#r)   c                H   \        \        V4      V4      p\        V4      ^8:  d   \        W4      ^3# \        V  R\        V4       R2RR7       \        P
                  ! R\         RV  R2R7      ;_uu_ 4       p\        V4      p. p. p \        V4       EF
  w  pw  rVRVR	 R
2,          pVRVR	 R2,          pVRVR	 R2,          p\        VR^R\        RV RVRV	RV
RWV
 /4       VP                  R4      p\        P                  ! \        P                  \        \        \         4      P#                  4       4      R\        RRV R\        V4      R\        V4      .\$        V\        P&                  R7      pVP)                  WWV34       VP)                  WV34       EK  	  V FT  w  rpVP+                  4       pVP-                  4        V^ 8w  g   K/  VP/                  RRR7      R&R p\1        RV R V 24      h	  . p^ pT EF6  w  rrpT	T8w  g   TP;                  4       '       g   \1        R!4      hTP                  R"4      ;_uu_ 4       p\<        P>                  ! T4      pRRR4       ^\        YY3pXPA                  R4      TPA                  R4      TPA                  R4      TPA                  R4      TPA                  R4      TPA                  R4      3pTPA                  R#4      pTT8w  g-   \C        T\D        4      '       d   \        T4      Y,
          8w  d   \1        R$4      hTPG                  T4       T
pEK9  	  T\        T4      8w  g   \        T4      \        T4      8w  d   \1        R%4      hT\        T4      3uuRRR4       #   \2         de    T F\  w  rpTP5                  4       f!   TP7                  4        TP+                  4        TP8                  '       d   KL  TP-                  4        K^  	  h i ; i  + '       g   i     EL; i  + '       g   i     R# ; i)'r   z: retrying with z subprocess shardsTr  z
mlp-sweep-rB   )prefixzshard-04dz
.input.pklz.output.pklz.logr  rl  r"   r  r   r  r  r  --asset--_internal-shard--_shard-tf--_shard-input--_shard-output)cwdstdoutstderrzutf-8r  )encodingerrorsNzsweep shard failed (z): z"sweep shard coverage is incompleter  r   zinvalid sweep shard outputz5sweep shard merge did not preserve candidate coveragei`)$r  r   r  rC  tempfileTemporaryDirectoryr/   r   r   r  rg   
subprocessPopensys
executabler#   __file__resolverD   STDOUTr   waitr   	read_textrq  r\   poll	terminateclosedrE   r  r  rC   rQ   rd   extend)r"   r  r  r  temp_dir	temp_rootspecs	processesr  r   r  r  r  log_path
log_handleprocess
returncodetailr,   mergedexpected_startr  outputexpectedactualshard_resultss   &&&                       r'   evaluate_candidates_subprocessr  t  s   
 (JAF
6{a-b=q@@	RD V-?
@M		$	$jqA,F	G	G8N	>@	4	)26):%%&6'#j)II
'F73-{*KK$}D'AA$!1b!7s$js&; &]]40
$**DN2245!+%(J)K( %%,,!$ gcIJ  'x!@AI *;L 2;-X$\\^
  "?#--wy-QRWRXYD&)=j\TF'STT 2; (*38/GCa&k.@.@.B.B"#GHH!!$''6V, (5"u:H

9%

7#

4 

9%

7#

5!F #JJy1M!M4)H)HCP]L^bebmLm"#?@@MM-( N% 49& S_,Fs:0NVWWs6{"c 
H	Gb  	*3&Q<<>)%%'LLN!((($$& +4 	 ('' 
H	G	GsQ   9P
EN
)N
>*P)$PO<$DP
AO9#O99P<P	PP!	c               (    V ^8  d   QhRRRRRRRR/# r  r%   )r&   s   "r'   r(   r(     s*      C -A C Tt r)   c                   V^8X  d+   \        V  R\        V4      R R2RR7       \        W4      ^3# \        V  R\        V4      R RV R2RR7       \        ^\	        ^\        V4      \        ^V^,          4      ,          4      4      p\
        P                  ! R4      p. p \        P                  P                  VV\        V \        3R	7      ;_uu_ 4       p\        VP                  \        WR
7      ^4       FF  w  rxVP                  V4       VR,          ^ 8X  g   K&  \        V  RVR R\        V4      R 2RR7       KH  	  RRR4       WR3#   + '       g   i     YR3# ; i  \         \"        3 dg   p	\        T  RT	 R2RR7        \%        YT4      u Rp	?	#   \&         d/   p
\        T  RT
 R2RR7       \)        Y^4      u Rp
?
u Rp	?	# Rp
?
ii ; iRp	?	ii ; i)r   z: evaluating r  z candidates sequentiallyTr  z candidates with z worker processesspawn)max_workers
mp_contextinitializerinitargs)	chunksizer  r  r  Nz: worker pool unavailable ()z#: subprocess sharding unavailable (z(); falling back to sequential evaluation)rC  r   r  r   r   mpget_context
concurrentfuturesProcessPoolExecutorrn  r/   r   maprr  r   OSErrorPermissionErrorr  r\   evaluate_candidates)r"   r  r  
chunk_sizer  r   poolr  rN  rM  	shard_excs   &&&        r'   r  r    s   !|M#j/!!44LMUYZ-b=q@@	RDc*oa00A'J[
\dhiQCZC7Q;4G!GHIJ(JG:33!#%[	 4 
 

 (/:)dfghv&:?RDSG1S_Q4GHPTU  i
" #
 
"  _% :/uA6dC	:1"'JJ 	:RD;I;Fnow{|&rq999	:	:ss   "5E# AE$EE# E 	E#  E# #G4GFGG$GGGGGGGc               T    V ^8  d   QhRRRRRRRRRRR	R
RRRRRRRRRRRRRRRRRR/# )r!   r"   r#   r   r   r   top_kpromoterL   r   r   r  worker_fractionrW   force_promoter   r   r   r   r   r   r$   ro   r%   )r&   s   "r'   r(   r(     s     ~ ~s ~S ~ ~C ~$ ~*~<?~!~ ~ /3~ "	~ 5A	~
 !~
 3?~
 KY~r)   c                   \        WW%V	WWR 7	      p\        V 4      pV'       d   \        V4      MRpV'       d#   \        V4      pRVR&   VP	                  ^ V4       \        V\        V4      V4      p\        WV4      w  ppVP                  \        RR7       V^ ,          p\        R V 4       R4      pVei   Vee   \        VP                  RR[4      4      p\        VP                  RR[4      4      pV^ 8  d(   VVR,          8  d   \        R	V  R
VR RVR R2RR7       R R lpV Uu. uF  pV! V4      '       g   K  VNK  	  ppV'       d
   V^ ,          MTp\        VVV4      pV'       Eg_   V'       EdV   VEeQ   \        \        VP                  R4      R4      4      p\        \!        \        T;'       g    / P                  R4      R4      \        VP                  R4      R4      4      4      p\        VP                  R4      R4      pV\"        8  d   V\"        8  d   V\$        8  d   \        VP                  R4      R[4      p \!        \        VP                  R4      R[4      \        T;'       g    / P                  R4      R[4      4      p!V V!\&        ,
          8  d+   \        RV  RV R RV!R R\&         RV RV RVR R2RR7       T;'       d    T;'       d    VRJ p"\(        P+                  RRR7       \(        R\,         R V  R!2,          p#\.        P0                  ! VRV 4      P3                  V#R"R#7       V'       d	   V'       g	   V"'       d   \5        V V4       / R$V bR%\        V4      bR&VbR'\7        V#P9                  \:        4      4      bR(\=        T;'       d    T;'       g    T"4      bR)\=        V4      bR*V'       d   VP                  R4      M\>        P@                  bR+V'       d   VP                  R,4      M\>        P@                  bR-V'       d   VP                  R.4      M\>        P@                  bR/V'       d   VP                  R4      M\>        P@                  bR0VP                  R4      bR1VP                  R24      bR3VP                  R44      bR5VP                  R64      bR7VP                  R84      bR9VP                  R:4      bR;VP                  R<4      b/ R=VP                  R>4      bR?VP                  R@4      bRAVP                  RB4      bRCVP                  R4      bRDVP                  R,4      bREVP                  RF4      bRGVP                  RH4      bRIVP                  RJ4      bRKVP                  RL4      bRMVP                  R.4      bRNVP                  RO4      bRPVP                  R4      bRQVP                  RR4      bRSVP                  R4      bRTVP                  RU4      bRVVP                  RW4      bRXVP                  RO4      bCRYVP                  R4      RZVP                  R,4      /C# u upi )\)r   r   r   r   r   Nr   r   T)r   reversec              3  T   "   T F  qP                  R 4      R8X  g   K  Vx  K   	  R# 5i)r   r   N)rC   )r8  rs   & r'   r9  run_tf.<locals>.<genexpr>  s     H155?i+GQQs   (
(r  r  z	  [WARN] z: cur_eval Calmar=z.3fz! is <50% of stored winner Calmar=ue    — winner artifact was likely overwritten during training; using stored metrics as promotion floor.r  c                    V ^8  d   QhRRRR/# )r!   rk   ro   r$   rL   r%   )r&   s   "r'   r(   run_tf.<locals>.__annotate__  s     
 
. 
T 
r)   c                   V P                  R 4      R8g  ;'       d    \        V P                  RR4      4      ^ 8  ;'       d    \        V P                  R^c4      4      ^ 8H  ;'       dt    \        V P                  RR4      4      ^ 8  ;'       dN    \        V P                  RR4      4      ^ 8  ;'       d(    \        \        V P                  R^c4      4      4      ^ 8H  # )	r   r   r   rv   r1  r%  r&  r)  rB  )rC   r]   r   )rk   s   &r'   _gate_passesrun_tf.<locals>._gate_passes  s    GGH* A A56:A A45:A A 67!;A A 67!;	A A
 HSWW_b9:;q@	
r)   r+  rv   r.  r-  z  [OOS BLOCK] z: new OOS_Sortino=z < ref=u    − z (trades: best=z, ref=z, bear=z.0fz%)parentsexist_ok'optimization_sweep_strategy_mlp_scores_r,   r-   Fr  	timeframe	evaluatedr  
sweep_filepromotedr  current_calmarcurrent_pnl_ddr   current_wfo_minr(  current_oos_sortinobest_sourcebest_weightsrt   
best_entryru   	best_exitrw   	best_confrx   
best_trailry   best_suppress_bearra   best_tradesr  best_pnlr  best_drawdownr!  best_calmarbest_pnl_ddbest_compositer#  best_sub1_calmarr%  best_sub2_calmarr&  best_wfo_scorer'  best_wfo_minbest_wfo_negr)  r  best_oos_pnlr,  r  best_fragile_0_05r1  best_fragile_1_0r2  overall_wfo_negoverall_calmaroverall_pnl_ddrB  )!r   rl   rh   r   insertr   r   r  sortr{  nextr]   rC   rC  r  r   r   r  r  r  	SWEEP_DIRmkdirr/   rZ   r  r  r  r#   relative_torD   rL   r   rH  )$r"   r   r   r  r  r   r  r  r  r   r   r   r   r   r  r   r  resolved_workersr   actual_workersbest_overallcur_eval
stored_caleval_calr  r
  gate_passingr  can_promote
best_oos_t	ref_oos_tbear_pctbest_oos_sorref_oos_sorforce_writingr   s$   &&&&&&&&&&&&&&                      r'   run_tfrI    sd    %R$D.6-4GJ "oG &-DM$M!'*%!W%+GS_oV1"BRSG^LLZL.1:LHH$OH  9m//FG
hll>5AB>hc)99B41(3 @$S) *EFMQ
  ':w!,q/AAwL:*<?DT8];K ;<<H,@$((+=">DE
m))r../ABCHX\\"45s;
 	 DHH^4c:'I,GHWhLh#DHH-@$A5IL&9:EB---2223FGOK k,===$RD(:<:L M&s+51B0C D%%/LykRUVXZ 	 "GGlGGx47GMOOD4O0
?wat4P
PCLL%!((E(:KMR&R&S_& 	>& 	c#//$/0	&
 	D'11kCCmD& 	d;'& 	((,,~6& 	((,,~6& 	88<<7& 	Hx||,?@RTRXRX& 	txx)& 	!34& 	dhhBC& 	TXX@A& 	TXXMN&  	dhh:;!&" 	dhh'=>#&$ 	txx/%&& 	DHH]+'&( 	"23)&* 	txx/+&, 	txx/-&. 	$((;//&0 	DHH]31&2 	DHH]33&4 	$((;/5&6 	07&8 	19&: 	488$67;&< 	!23=&> 	DHH%89?&@ 	TXXn5A&B 	DHH]3C&F 	<++O<G&H 	,**>:,**>:K& &K ;s   )X?>X?c                   V ^8  d   QhRR/# )r!   r$   r   r%   )r&   s   "r'   r(   r(   q  s     7: 7:d 7:r)   c                    \         P                  ! R R7      p V P                  R\        RR7       V P                  RR\        \        R7       V P                  R\
        R	R
R7       V P                  R\
        RR7       V P                  R\
        RR7       V P                  RR. RR7       V P                  R\
        ^ RR7       V P                  R\        \        RR7       V P                  RRRR7       V P                  RRRR7       V P                  RRR R7       V P                  R!\        R"R#R7       V P                  R$\        R"R%R7       V P                  R&\        R"R'R7       V P                  R(\        R"R)R7       V P                  R*R\         P                  R7       V P                  R+R"\         P                  R7       V P                  R,R"\         P                  R7       V P                  R-R"\         P                  R7       V P                  4       pVP                  sVP                  '       d   VP                  '       d%   VP                  '       d   VP                  '       g   V P!                  R.4       \#        VP                  \%        VP                  4      \%        VP                  4      4       R"# \&        P)                  R/R/R07       . p\+        VP,                  4       F  w  r4\/        R1V R2VP0                  R3 R42R/R57       TP3                  \5        YAP0                  VP6                  VR6,          ,           VP8                  VP:                  ;'       g    VP<                  VP>                  VP@                  VPB                  VP<                  VPD                  VPF                  VPH                  VPJ                  VPL                  R77      4       K  	  \&        R8,          p\N        PP                  ! V4      PS                  VR9R:7       \/        R;VPU                  \V        4       24       \/        \N        PP                  ! V4      PY                  R9R:7      4       R"# )<zRun deeper MLP threshold sweeps)descriptionr  zAAsset identifier, e.g. COINBASE_ETHUSD (default: COINBASE_BTCUSD))rV   helpz--timeframes+)nargsrV   choicesz	--samplesiP  z$Approximate candidates per timeframe)typerV   rM  z--seedi
'5)rQ  rV   z--top-ki  z--extra-weights*z`Additional MLP artifact JSONs to include as candidate weight files for every selected timeframe.)rO  rV   rM  z	--workerszWorker processes for candidate evaluation. Use 0 for auto, currently 50%% of logical CPUs capped by candidate count; use 1 for sequential evaluation.z--worker-fractionz+Logical CPU fraction used when --workers=0.z	--promote
store_truezWrite improved winner CSVs)actionrM  z--force-promotez~Write best gate-passing candidate even when no prior winner exists (use for first-ever run on a new asset). Implies --promote.z--wideuN   Double the threshold search ranges: entry ±600, exit/conf ±750, trail 0-100.z
--entry-loNz%Override entry threshold lower bound.z
--entry-hiz%Override entry threshold upper bound.z	--exit-loz$Override exit threshold lower bound.z	--exit-hiz$Override exit threshold upper bound.r  r  r  r  z2internal shard mode requires tf, input, and outputTr  z
=== r  r  z requested samples ===r  i  )r  r   r   r   r   r   zmlp_deep_sweep_summary.csvFr  z
Summary written: )-argparseArgumentParseradd_argument_DEFAULT_ASSETTFSr   rW   r   SUPPRESS
parse_argsrl  r/   _internal_shard	_shard_tf_shard_input_shard_outputerrorr  r   
REPORT_DIRr9  r   
timeframesrC  r   r   rI  r   r  r  r  r   r  r  r   r   r   r   r   rZ   r  r  r:  rD   	to_string)parserargs	summariesoffsetr"   summary_paths         r'   mainri  q  s   $$1RSF
	>`  b
c3L
#uCij
sH=
	T:
)b  A
#q@  A +%AXJ  L
L?[\
),[  \ m  o
5$Elm
5$Elm
5$Ekl
5$Ekl
+LxGXGXY
t(:K:KL
($X=N=NO
)4h>O>OPD JJE~~~T%6%6%6d>P>P>PLLMN4>>40A0A+BDI[I[D\]TD1I0
rd"T\\!,,BC4PLL$))ftm2K $

DLL,N,ND<N<N $ 2 2DLL$BVBV.2.@.@tyy)-(,dllL 	M 1  <<LLL""<u"=	 8 8 >?
@A	",,y
!
+
+%
+
89r)   __main__)r7   r9   r;   r=   r?   )rt   ru   rw   rx   ry   rc   rb   rz   r{   ra   r|   r}   )r  r  r  r  r  r   r!  r"  r+  r,  r-  r.  )rv   )NFNNNNrY   )T)b__conditional_annotations____doc__
__future__r   rU  concurrent.futuresr  re   r   rJ  multiprocessingr  r  pathlibr   r  r  typingr   numpyr   pandasrZ   r  r  r  parentrD   ri   r5  r#   configr   r   r   r   r	   r
   r   r   r   r   strategies.strategy_mlp_scoresr   r   r   r   r   r   r   tools.worker_utilsr   r   rX  r/   rY  r.   r8  ra  r   __annotations__r   rI  rG  r  r  r  r1   r4   rI   rT   r]   rl   r   r   r   r   r   r   r  r  objectrD  rj  rn  rr  r{  r  r  r  r  r  r  r  r  rI  ri  __name__)rk  s   @r'   <module>r{     s   #   
 	          
H~&&-- 3t9     M #%I	)
9x'	I	)

J [_X _
"   TB/,&&_Db.
&N (m`7066r4[#|> .2!$;!&U$(4#'~B7:t zF r)   