feat(persistence): C-S3实走跨日状态—paper_pending_orders+positions/last_balance存取
This commit is contained in:
@@ -3,6 +3,8 @@ from sanguo_trader.persistence import (
|
||||
init_db, save_account, save_trade, save_daily_balance,
|
||||
update_account_status, update_checkpoint, load_checkpoint,
|
||||
list_trades, list_daily_balance,
|
||||
save_pending_orders, load_pending_orders,
|
||||
save_positions, load_positions, load_last_balance,
|
||||
)
|
||||
|
||||
|
||||
@@ -13,6 +15,36 @@ def test_init_and_save_account(tmp_path):
|
||||
assert aid > 0
|
||||
|
||||
|
||||
def test_pending_orders_roundtrip(tmp_path):
|
||||
"""C-S3 实走跨日 pending 订单持久化(覆盖式)。"""
|
||||
db = str(tmp_path / "p.db")
|
||||
init_db(db)
|
||||
aid = save_account(db, {"name": "t"})
|
||||
save_pending_orders(db, aid, [
|
||||
{"strategy_id": "s1", "symbol": "600000", "side": "buy", "price": 10.5,
|
||||
"volume": 100, "is_market": True, "match_session": "next_open", "listing_days": 0},
|
||||
])
|
||||
loaded = load_pending_orders(db, aid)
|
||||
assert len(loaded) == 1 and loaded[0]["symbol"] == "600000"
|
||||
assert loaded[0]["is_market"] is True and loaded[0]["volume"] == 100
|
||||
save_pending_orders(db, aid, []) # 覆盖式清空
|
||||
assert load_pending_orders(db, aid) == []
|
||||
|
||||
|
||||
def test_positions_and_last_balance_roundtrip(tmp_path):
|
||||
"""C-S3 实走 positions 快照 + 最后余额恢复。"""
|
||||
db = str(tmp_path / "p.db")
|
||||
init_db(db)
|
||||
aid = save_account(db, {"name": "t", "initial_capital": 1_000_000})
|
||||
save_positions(db, aid, "account",
|
||||
{"600000": {"volume": 200, "frozen": 0, "avg_price": 10.5}}, "2024-01-02")
|
||||
pos = load_positions(db, aid, "account")
|
||||
assert pos["600000"]["volume"] == 200 and pos["600000"]["avg_price"] == 10.5
|
||||
save_daily_balance(db, aid, "2024-01-02", 99795.0, 2100.0, 101895.0)
|
||||
bal = load_last_balance(db, aid)
|
||||
assert bal["cash"] == 99795.0 and bal["date"] == "2024-01-02"
|
||||
|
||||
|
||||
def test_save_trade_and_list(tmp_path):
|
||||
db = str(tmp_path / "p.db")
|
||||
init_db(db)
|
||||
|
||||
Reference in New Issue
Block a user