72 lines
3.2 KiB
Python
72 lines
3.2 KiB
Python
|
|
"""只用内存行情,经unit_test_guard隔离运行。"""
|
|||
|
|
import datetime as dt
|
|||
|
|
import unittest
|
|||
|
|
import numpy as np
|
|||
|
|
from calibrate import account, future_exits, stock_mask
|
|||
|
|
from prepare import chosen_batch
|
|||
|
|
|
|||
|
|
def panel():
|
|||
|
|
t,n=15,2
|
|||
|
|
p={k:np.ones((t,n))*10 for k in ['open','close','mark']}
|
|||
|
|
p.update(dates=np.array([f'2026-01-{i+1:02}' for i in range(t)]),
|
|||
|
|
codes=np.array(['000001.SZ','600000.SH']),eligible=np.ones((t,n),bool),
|
|||
|
|
can_buy=np.ones((t,n),bool),can_sell=np.ones((t,n),bool),
|
|||
|
|
asof=np.full((t,n),.8),pct=np.zeros((t,n)))
|
|||
|
|
return p
|
|||
|
|
|
|||
|
|
class ResearchTests(unittest.TestCase):
|
|||
|
|
def test_no_backfill_or_mixed_batch(self):
|
|||
|
|
rows=[{'batch_no':1,'create_time':'2026-01-01T20:00:00'},
|
|||
|
|
{'batch_no':2,'create_time':'2026-02-01T09:00:00'}]
|
|||
|
|
self.assertEqual(chosen_batch(rows,dt.datetime(2026,1,2,9,25))[0]['batch_no'],1)
|
|||
|
|
self.assertEqual(chosen_batch(rows,dt.datetime(2026,1,1,9,25)),[])
|
|||
|
|
rows.append({'batch_no':1,'create_time':'2026-02-02T09:00:00'})
|
|||
|
|
self.assertEqual(chosen_batch(rows,dt.datetime(2026,1,2,9,25)),[])
|
|||
|
|
|
|||
|
|
def test_entry_next_day_exit_t_plus_one(self):
|
|||
|
|
p=panel();m=np.zeros((15,2),bool);m[0,0]=True
|
|||
|
|
a=account(p,m,1,0,detail=True)
|
|||
|
|
self.assertEqual([(x['day'],x['side']) for x in a['journal']],[(1,'buy'),(2,'sell')])
|
|||
|
|
self.assertEqual(a['summary']['return'],0)
|
|||
|
|
|
|||
|
|
def test_unfillable_buy_keeps_allocation_cash(self):
|
|||
|
|
p=panel();m=np.zeros((15,2),bool);m[0]=True;p['can_buy'][1,1]=False
|
|||
|
|
a=account(p,m,1,0)
|
|||
|
|
self.assertAlmostEqual(a['daily'][1]['cash'],.5)
|
|||
|
|
self.assertAlmostEqual(a['daily'][1]['stock_value'],.5)
|
|||
|
|
self.assertEqual(a['summary']['skipped_buy'],1)
|
|||
|
|
|
|||
|
|
def test_delayed_exit_and_no_new_holding_clock(self):
|
|||
|
|
p=panel();m=np.zeros((15,2),bool);m[0:3,0]=True;p['can_sell'][3,0]=False
|
|||
|
|
a=account(p,m,2,0,detail=True)
|
|||
|
|
first=[x for x in a['journal'] if x['signal']==0]
|
|||
|
|
self.assertEqual([(x['day'],x['side']) for x in first],[(1,'buy'),(4,'sell')])
|
|||
|
|
self.assertGreater(a['summary']['delayed_lots'],0)
|
|||
|
|
self.assertTrue(all(r['cash']>=-1e-12 for r in a['daily']))
|
|||
|
|
|
|||
|
|
def test_net_cost_formula(self):
|
|||
|
|
p=panel();m=np.zeros((15,2),bool);m[0,0]=True
|
|||
|
|
a=account(p,m,1,.003)
|
|||
|
|
self.assertAlmostEqual(a['summary']['return'],.997/1.003-1)
|
|||
|
|
|
|||
|
|
def test_future_change_preserves_past(self):
|
|||
|
|
p=panel();m=np.zeros((15,2),bool);m[:10]=True
|
|||
|
|
a=account(p,m,3,.003,stop=7)
|
|||
|
|
for k in ['open','close','mark']:p[k][7:]*=100
|
|||
|
|
p['can_sell'][7:]=False;m[7:]=False
|
|||
|
|
b=account(p,m,3,.003,stop=7)
|
|||
|
|
self.assertEqual(a,b)
|
|||
|
|
|
|||
|
|
def test_event_labels_match_account_exit(self):
|
|||
|
|
p=panel();p['open'][3:,0]=12;p['can_sell'][2,0]=False
|
|||
|
|
exits,gross=future_exits(p,1)
|
|||
|
|
self.assertEqual(exits[0,0],3);self.assertAlmostEqual(gross[0,0],1.2)
|
|||
|
|
|
|||
|
|
def test_missing_heat_and_range_boundaries(self):
|
|||
|
|
p=panel();p['asof'][0,0]=np.nan;p['pct'][1]=[0,5]
|
|||
|
|
m=stock_mask(p,'asof',.8,0,5,2)
|
|||
|
|
self.assertFalse(m[0,0]);self.assertTrue(m[1,0]);self.assertFalse(m[1,1])
|
|||
|
|
self.assertFalse(m[3:].any())
|
|||
|
|
|
|||
|
|
if __name__=='__main__':unittest.main()
|