"""真实给定NYSE会话片段的月末篮子;日历与缺行情分别处理。""" from datetime import date,timedelta from decimal import Decimal as D import json sessions=['2026-06-29','2026-06-30','2026-07-01','2026-07-02', '2026-07-06','2026-07-07','2026-07-08'] closed={'2026-07-03'} def schedule(calendar,confirmed_month_end='2026-06-30'): parsed=[date.fromisoformat(s) for s in calendar] if len(set(calendar))!=len(calendar) or parsed!=sorted(parsed): raise ValueError('calendar_order') if any(s in closed for s in calendar): raise ValueError('official_closed_session') if confirmed_month_end not in calendar: raise ValueError('missing_month_end') end=date.fromisoformat(confirmed_month_end) if any(s[:7]==confirmed_month_end[:7] and s>confirmed_month_end for s in calendar): raise ValueError('not_last_provided_month_session') year,month=(end.year+1,1) if end.month==12 else (end.year,end.month+1) next_key=f'{year:04d}-{month:02d}' next_month=[s for s in calendar if s[:7]==next_key] if len(next_month)<4: raise ValueError('incomplete_next_month_sessions') return {'entry':confirmed_month_end,'exit':next_month[3]} def run(missing_exit_bar=False): plan=schedule(sessions);cash,holdings,fees,ledger=D(1000),{},0,[] for day in sessions: orders,rejected=[],[] if day==plan['entry']: for symbol,quantity,price in [('A',40,D(10)),('B',20,D(20))]: cash-=quantity*price+1;holdings[symbol]=quantity;fees+=1 orders.append([symbol,'buy',quantity,str(price),1]) if day>=plan['exit']: for symbol in sorted(list(holdings)): if symbol=='B' and day==plan['exit'] and missing_exit_bar: rejected.append([symbol,'missing_executable_bar']);continue price=D('10.5') if symbol=='A' else D('18.5') if day=='2026-07-08' else D(19) quantity=holdings.pop(symbol);cash+=quantity*price-1;fees+=1 orders.append([symbol,'sell',quantity,str(price),1]) ledger.append({'day':day,'orders':orders,'rejected':rejected, 'cash':str(cash),'holdings':dict(holdings),'fees':fees}) return ledger plan=schedule(sessions) assert plan=={'entry':'2026-06-30','exit':'2026-07-07'} rows=run() assert rows[1]['orders']==[['A','buy',40,'10',1],['B','buy',20,'20',1]] assert rows[1]['cash']=='198' and rows[1]['fees']==2 assert rows[5]['orders']==[['A','sell',40,'10.5',1],['B','sell',20,'19',1]] assert D(rows[-1]['cash'])==996 and rows[-1]['fees']==4 and not rows[-1]['holdings'] late=run(True) assert late[5]['cash']=='617.0' and late[5]['holdings']=={'B':20} assert late[5]['rejected']==[['B','missing_executable_bar']] assert late[6]['orders']==[['B','sell',20,'18.5',1]] assert D(late[-1]['cash'])==986 and late[-1]['fees']==4 and not late[-1]['holdings'] weekday_dates=[date(2026,7,1)+timedelta(days=i) for i in range(10)] wrong=[d.isoformat() for d in weekday_dates if d.weekday()<5][3] assert wrong=='2026-07-06' and wrong!=plan['exit'] for calendar in [sessions[:5],sorted(sessions+['2026-07-03']),sessions[:5]+['2026-08-03']]: try: schedule(calendar) except ValueError: pass else: raise AssertionError('缺日历或正式休市不可伪造成有效会话') assert schedule(sessions)==plan # 日历函数没有任何价格输入 print(json.dumps({'provided_sessions':sessions,'plan':plan,'entry':rows[1], 'exit':rows[5],'normal_final':rows[-1],'missing_bar_exit':late[5], 'late_final':late[-1],'weekday_shortcut_exit':wrong, 'scope':'frozen real session dates around one month end; synthetic stock prices, not full-year results'}))