
    j                    x   d Z ddlmZ ddlZddlZddlZddlmZ ddlZ	ddl
Z ee      j                         j                  j                  Zej                   j#                  d ee             ddlmZmZmZ ddlmZmZ ddlmZ ddlmZmZmZ ddd	Z dd
Z!ddZ"ddddZ#ddZ$e%dk(  r e& e$             y)a  Research-only 4H SOPR entry-inhibition ablation.

This never changes MLP weights, promoted parameters, Pine code, or exits. It
compares the promoted Python replay with the same replay in which an entry is
permitted only when SOPR is above a discovery-calibrated low-tail cutoff.
    )annotationsN)Path)	OOS_STARTSCORE_START	WFO_FOLDS)calculate_metricsgenerate_signals)load_params)DISCOVERY_ENDVALIDATION_STARTparse_chart_timec           
     :   || n+| | d   t        j                  |      k     j                         }t        ||d      }|j	                         D ci c]<  \  }}|t        |t        t        j                  f      rt        |      n
t        |      > c}}S c c}}w )Ntimer   )score_start
min_trades)
pd	Timestampcopyr   items
isinstancefloatnpfloatingint)signalsstartendwindowresultkeyvalues          $tools/run_mlp_sopr_entry_ablation.py_metricsr#      s    WBLLQTDU1U)V)[)[)]Fv5QGF !,,.(JC 	Zr{{/CDU5\#e*T(  s   ABc                ,   | d   t        j                  |      k\  }||| d   t        j                  |      k  z  }| d   |z  }||z  }| d   j                  d      j                  | d         j	                  d      dfd} ||       ||      dS )	Nr   execute_entrycloseg      ?c                *   |    j                         }t        | j                               t        t        |            t        |      rt	        |j                         dz        nd t        |      rt	        |dkD  j                               dS d dS )Nd   r   )entriesnext_bar_observationsnext_bar_mean_return_pctnext_bar_positive_rate)dropnar   sumlenr   mean)rowsvaluesnext_returns     r"   describez$_entry_diagnostics.<locals>.describe+   s    T"))+488:%(V%5FI&kfkkmc.A(BW[DGKeVaZ,=,=,?&@	
 	
 VZ	
 	
    )baseline_entriesblocked_baseline_entries)r2   	pd.Seriesreturndict[str, float | int | None])r   r   shiftdivsub)	r   blockedr   r   mask
candidatesselectedr5   r4   s	           @r"   _entry_diagnosticsrC   #   s    6?bll511D
2<<#444)D0JG#H'"((,001ABFFsKK
 !) 4RZ[cRdeer6   c                    | | d   t        j                  t              k\  | d   t        j                  |      k  z     }|j                  rt	        d      t        |d   j                  |            S )zGReturn a low-SOPR cutoff using only rows available through history_end.r   z0no SOPR history available for cutoff calibration	sopr_norm)r   r   r   empty
ValueErrorr   quantile)datahistory_endfeature_tailhistorys       r"   calibrate_sopr_cutoffrM   7   sg    DLBLL$==$v,RTR^R^_jRkBklmG}}KLL%..|<==r6   g?)rK   c                  d|cxk  rdk  st        d       t        d      t        j                  |       }|j                  j                  j                         j                  j                         |_        d|j                  vrt        d      t        |d         |d<   |j                  d      j                  d      }t        t	        |            }|j                  d	      st        d
|       t        |t        |      }|d   |kD  j                  d      j                  t               }t#        |j%                         fi |}t#        |j%                         fd|i|}t#        |j%                         fdt'        j(                  t+        |      t               i|}	dD ]'  }
||
   j-                  |	|
         rt/        d|
        t        j0                  ||j2                         }t4        t	        t              ft	        t6              t	        t        j8                  t:              t        j<                  d      z
        ft:        d fd}|j?                         D ci c]1  \  }\  }}|tA        |||      tA        |||      tC        ||||      d3 }}}}i }tD        D ]  \  }}}t        |||      }|d   jG                  |      j                  d      }t#        |j%                         fd| j                  t               i|}|tA        |||      tA        |||      tC        ||||      d||<    t	        |       t	        |      d||g ddddd||dS c c}}}w )Nr   g      ?z&feature_tail must be between 0 and 0.5rE   zdata has no sopr_norm columnr   T)dropmlp_weights_filez winner has no mlp_weights_file: F)dtypeentry_allowed)positionr%   execute_exitz%all-true entry mask changed baseline )index   )nanoseconds)	discovery
validationoos)baselinesopr_entry_inhibitedentry_diagnostics)cutoffr[   r\   r]   zewhen flat and the unchanged MLP entry condition fires, inhibit entry if sopr_norm <= discovery cutoff)zMLP weightszall promoted parametersz
exit logicz	Pine codezYstrategy metrics include the repository's 0.5% entry and 0.5% exit commission assumptionszgeach fold recalibrates its fixed 10th-percentile cutoff using only history before that fold's OOS startzNthis is a one-policy ablation, not a threshold sweep or a promotable candidate)policyrK   sopr_cutoff	unchangedcostswfo_calibrationwarning)	data_pathwinner_path
definitionwindows	wfo_folds)$rG   r   read_csvcolumnsstrlowerstripr   sort_valuesreset_indexr
   getrM   r   fillnato_numpyboolr	   r   r   onesr0   equalsAssertionErrorSeriesrU   r   r   r   r   	Timedeltar   r#   rC   r   le)re   rf   rK   rI   paramsr^   rR   r[   gatedall_allowedcolumnr?   rh   namer   r   	summariesfoldsis_endfold_cutofffold_blocked
fold_gateds                         r"   runr   ?   s^   |!c!ABB "ABB;;y!D<<##))+//557DL$,,&788#DL1DLF#//T/:D[)*F::();K=IJJ"4EF +&/77>GGdGSM		6v6HTYY[PPPE"499;gbggc$iW[>\g`fgK?&&{6':; #H!QRR @ yydjj99G!3}#56+,c",,y2IBLLefLg2g.hi4 2G #*--/ #2D,5# 	 5#6$,UE3$?!3Hguc!R
 	

 #2   +-E's ,D&,GK(++K8??E%IIK
)5'?'?d'?'K
OU

 " 5#6$,Z$D!3HlESV!W	
c
 ( ^;'}(!^p  Ig
  /s   .6M*c                    t        j                  t              } | j                  dt        d       | j                  dt        d       | j                  dt               | j                         }t        |j                  |j                        }t        j                  |d	      }t        |       |j                  rE|j                  j                  j                  dd
       |j                  j                  |dz          y)N)descriptionz--dataT)typerequiredz--winnerz--output)r      )indent)parentsexist_ok
r   )argparseArgumentParser__doc__add_argumentr   
parse_argsr   rI   winnerjsondumpsprintoutputparentmkdir
write_text)parserargsr   texts       r"   mainr      s    $$9F
td;

=

.DDKK(F::fQ'D	$K{{   =td{+r6   __main__)N)r   pd.DataFramer   rl   r   
str | Noner:   zdict[str, float | int])
r   r   r?   r9   r   rl   r   r   r:   r;   )rI   r   rJ   zstr | pd.TimestamprK   r   r:   r   )re   r   rf   r   rK   r   r:   zdict[str, object])r:   r   )'r   
__future__r   r   r   syspathlibr   numpyr   pandasr   __file__resolver   REPOpathinsertrl   configr   r   r   strategies.strategy_mlp_scoresr   r	   tools.check_mlp_parityr
   %tools.run_mlp_extreme_bar_event_studyr   r   r   r#   rC   rM   r   r   __name__
SystemExit r6   r"   <module>r      s    #   
   H~&&-- 3t9  4 4 N . c cf(> FJ DN z
TV
 r6   