"""A股单批entry_date可卖检查;给定2026国庆跨休市会话,不做全批次冻结。""" from decimal import Decimal as D import json sessions=['2026-09-30','2026-10-08','2026-10-09'] class Account: def __init__(self): self.cash,self.quantity,self.entry,self.fees,self.realized=D(5000),0,None,0,D(0) def buy(self,day,quantity,price): if day not in sessions: raise ValueError('not_a_session') if self.quantity: raise ValueError('additional_batch_unsupported') price=D(str(price)) if type(quantity) is not int or quantity<=0 or not price.is_finite() or price<=0: raise ValueError('invalid_order') cost=quantity*price+1 if cost>self.cash: raise ValueError('cash') self.cash-=cost;self.quantity=quantity;self.entry=day;self.entry_price=price;self.fees+=1 def sell(self,day,price): if day not in sessions: return {'status':'not_a_session'} if not self.quantity: return {'status':'no_position'} if sessions.index(day)<=sessions.index(self.entry): return {'status':'T_plus_1_blocked'} price=D(str(price)) if not price.is_finite() or price<=0: raise ValueError('invalid_price') quantity=self.quantity self.cash+=quantity*price-1;self.realized+=quantity*(price-self.entry_price) self.quantity=0;self.fees+=1 return {'status':'filled','day':day,'quantity':quantity,'price':str(price),'fee':1} a=Account();a.buy('2026-09-30',100,10) assert a.cash==3999 and a.quantity==100 and a.fees==1 and a.realized==0 buy_record={'day':a.entry,'side':'buy','quantity':a.quantity,'price':str(a.entry_price), 'cash_after':str(a.cash),'fees':a.fees} blocked=a.sell('2026-09-30',10.5) assert blocked=={'status':'T_plus_1_blocked'} assert a.cash==3999 and a.quantity==100 and a.fees==1 and a.realized==0 blocked_state={'cash':str(a.cash),'quantity':a.quantity,'fees':a.fees,'realized_price_pnl':str(a.realized)} holiday=a.sell('2026-10-01',10.5) assert holiday=={'status':'not_a_session'} and a.cash==3999 and a.quantity==100 try: a.buy('2026-10-08',100,9.5) except ValueError as e: assert str(e)=='additional_batch_unsupported' else: raise AssertionError('单entry_date模型不能冒充逐批次持仓系统') filled=a.sell('2026-10-08',9.5) assert filled=={'status':'filled','day':'2026-10-08','quantity':100,'price':'9.5','fee':1} assert a.cash==4948 and a.quantity==0 and a.fees==2 and a.realized==-50 assert a.cash-D(5000)==a.realized-a.fees==D(-52) assert (D('10.5')-D(10))*100-D(2)==48 # 被禁止同日卖出的虚假净利,不能入账 print(json.dumps({'provided_sessions':sessions,'buy':buy_record,'same_day_attempt':blocked, 'blocked_state':blocked_state,'holiday_attempt':holiday, 'next_session_fill':filled,'ending_cash':str(a.cash),'quantity':a.quantity, 'fees':a.fees,'realized_price_pnl':str(a.realized),'net_cash_pnl':str(a.cash-D(5000)), 'scope':'ordinary A-share synthetic single-entry cash model, no per-lot freeze system'}))