tradingSystem/research/chip_absolute_all_20261030/outcomes.py

28 lines
1.4 KiB
Python
Raw Normal View History

"""沿用首轮已验证的绝对收益和交易时序。"""
import numpy as np
def outcome(a,i,j,hold=60):
t=len(a['dates']);entry=i+1;due=entry+hold
r={'entry_index':entry,'due_index':due,'calendar_matured':bool(due<t),
'entry_filled':False,'completed':False,'entry_date':None,'exit_date':None,
'exit_delay':None,'gross':np.nan,'net30':np.nan,'net60':np.nan,
'path_complete':False,'hit15':np.nan,'up_before_down':np.nan,
'max_close_gain':np.nan,'max_close_loss':np.nan}
if entry>=t:return r
r['entry_date']=str(a['dates'][entry])
if not a['can_buy'][entry,j]:return r
r['entry_filled']=True;price=a['open'][entry,j]
if due>=t:return r
path=a['close'][entry:due,j]/price-1
r['path_complete']=bool(np.isfinite(path).all())
if r['path_complete']:
hi=np.flatnonzero(path>=.15);lo=np.flatnonzero(path<=-.10)
r.update(hit15=float(len(hi)>0),up_before_down=float(len(hi)>0 and (not len(lo) or hi[0]<lo[0])),
max_close_gain=float(path.max()),max_close_loss=float(min(0,path.min())))
exits=np.flatnonzero(a['can_sell'][due:,j])
if not len(exits):return r
end=due+int(exits[0]);gross=a['open'][end,j]/price
r.update(completed=True,exit_date=str(a['dates'][end]),exit_delay=end-due,gross=float(gross),
net30=float(gross*.997/1.003-1),net60=float(gross*.994/1.006-1))
return r