"""估值标记与原始可成交bar分开;缺bar不是已确认正式停牌。""" from decimal import Decimal as D,InvalidOperation import json def clean(bar): if bar is None:return None,'no_raw_bar' try: opening,close,volume=[D(str(bar[k])) for k in ['open','close','volume']] except (InvalidOperation,ValueError,KeyError): return None,'invalid_bar' if any(not x.is_finite() for x in [opening,close,volume]) or min(opening,close)<=0 or volume<0: return None,'invalid_bar' return (opening,close,volume),'zero_volume' if volume==0 else 'executable' def run(bars,forge=False,initial_quantity=0): cash,quantity,fees,last,age=D(1000) if not initial_quantity else D(599),initial_quantity,0,None,None rows=[] for i,raw in enumerate(bars): observed,status=clean(raw);orders=[];request='buy' if i==0 and not quantity else 'sell' if i>=1 and quantity else None # 错误控制故意把估值前填造为bar;正式路径始终不执行此分支。 if forge and observed is None and last is not None: observed=(last,last,D(100));status='forged_bar' executable=observed is not None and observed[2]>0 if request and executable: price=observed[0] if request=='buy':cash-=40*price+1;quantity=40 else:cash+=quantity*price-1;quantity=0 fees+=1;orders.append([request,40,str(price),1]) rejected=request is not None and not executable if observed is not None:last=observed[1];age=0 elif last is not None:age+=1 equity=cash+quantity*last if last is not None else cash if not quantity else None rows.append({'i':i,'raw_present':raw is not None,'bar_status':status,'request':request, 'orders':orders,'rejected':rejected,'quote_age_sessions':age, 'valuation_mark':str(last) if last is not None else None, 'quantity':quantity,'cash':str(cash),'fees':fees, 'equity':str(equity) if equity is not None else None}) return rows bars=[{'open':10,'close':10,'volume':100},None,None,{'open':8,'close':'8.5','volume':100}] rows=run(bars) assert rows[0]['orders']==[['buy',40,'10',1]] and rows[0]['cash']=='599' for i in [1,2]: assert rows[i]['orders']==[] and rows[i]['rejected'] and not rows[i]['raw_present'] assert rows[i]['quote_age_sessions']==i and rows[i]['valuation_mark']=='10' assert rows[i]['cash']=='599' and rows[i]['quantity']==40 and rows[i]['fees']==1 and rows[i]['equity']=='999' assert rows[3]['orders']==[['sell',40,'8',1]] and rows[3]['valuation_mark']=='8.5' assert D(rows[3]['cash'])==D(rows[3]['equity'])==918 and rows[3]['quantity']==0 and rows[3]['fees']==2 assert D(rows[2]['equity'])-D(rows[3]['equity'])==40*(D(10)-D(8))+1==81 fake=run(bars,True) assert fake[1]['orders']==[['sell',40,'10',1]] and fake[-1]['cash']=='998' assert run(bars[:3])==rows[:3] changed=list(bars);changed[3]={'open':100,'close':100,'volume':100} assert run(changed)[:3]==rows[:3] unknown=run([None,None],initial_quantity=40) assert unknown[1]['equity'] is None and unknown[1]['quantity']==40 and unknown[1]['valuation_mark'] is None zero=run([bars[0],{'open':10,'close':10,'volume':0}]) assert zero[1]['quote_age_sessions']==0 and zero[1]['rejected'] and zero[1]['quantity']==40 for bad in [None,float('nan'),float('inf'),float('-inf')]: check=run([bars[0],{'open':bad,'close':10,'volume':100}]) assert check[1]['bar_status']=='invalid_bar' and check[1]['orders']==[] and check[1]['quantity']==40 print(json.dumps({'raw_rows':bars,'ledger':rows,'forged_control':fake[1], 'forged_final_cash':fake[-1]['cash'],'unknown_initial_mark':unknown[1], 'zero_volume_control':zero[1], 'scope':'synthetic missing-bar execution; no official halt reason or data-feed validation'}))