"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(china-a): size STAR Market and Beijing buys on their own order grid
DrDLyuan27359bc4ff1d
diff --git a/agent/backtest/engines/china_a.py b/agent/backtest/engines/china_a.pyindex d1963a247..c5b23c37b 100644--- a/agent/backtest/engines/china_a.py+++ b/agent/backtest/engines/china_a.py@@ -5,7 +5,9 @@ Market rules:- No short selling for retail investors- Ordinary price limits: ±10% main board, ±20% ChiNext/STAR, ±30% Beijing(ST status and listing-day exemptions cannot be inferred from code alone)- - Minimum lot: 100 shares (odd lots can only be sold, not bought)+ - Order size: 100-share lots on the main board and ChiNext; at least 200+ shares on the STAR Market and 100 on the Beijing exchange, then 1-share+ steps (odd lots can only be sold, not bought)- Commission: ¥5 minimum, 0.025% bilateral- Stamp tax: 0.05% sell-side only- Transfer fee: 0.001% bilateral@@ -52,8 +54,10 @@ class ChinaAEngine(BaseEngine):return china_a_can_execute(self, self, symbol, direction, bar)def round_size(self, raw_size: float, price: float) -> float:- """Round down to 100-share lots."""- return max(int(raw_size / 100) * 100, 0)+ """Round down to the order-size grid of the active symbol's board."""+ minimum, step = _order_grid(self._active_symbol)+ size = int(raw_size / step) * step+ return size if size >= minimum else 0def calc_commission(self, size: float, price: float, _direction: int, is_open: bool) -> float:"""A-share fee structure: commission + stamp tax (sell) + transfer fee.@@ -176,6 +180,26 @@ def _price_limit(symbol: str) -> float:return 0.10+def _order_grid(symbol: str) -> tuple[int, int]:+ """Smallest buy order and its step, in shares, on the symbol's board.++ Args:+ symbol: Stock code (e.g. 600000.SH, 688001.SH, 830799.BJ).++ Returns:+ ``(minimum, step)``. The STAR Market (688xxx, 689xxx CDRs) takes at+ least 200 shares and the Beijing exchange at least 100, each then in+ 1-share steps; the main board and ChiNext trade 100-share lots.+ """+ code = symbol.split(".")[0] if "." in symbol else symbol+ if code.startswith(("688", "689")):+ return 200, 1+ # Same Beijing code ranges as _price_limit.+ if len(code) == 6 and code.startswith(("4", "8", "920")):+ return 100, 1+ return 100, 100++def china_a_can_execute(state, rules, symbol: str, direction: int, bar: pd.Series) -> bool:"""A-share execution rules read against ``state`` with params from ``rules``.diff --git a/agent/tests/test_china_a_board_order_grid.py b/agent/tests/test_china_a_board_order_grid.pynew file mode 100644index 000000000..314ef114f--- /dev/null+++ b/agent/tests/test_china_a_board_order_grid.py@@ -0,0 +1,78 @@+"""A-share buy orders follow each board's order-size grid.++The STAR Market (688xxx, 689xxx CDRs) accepts a buy of at least 200 shares and+the Beijing exchange at least 100, each then in 1-share steps. The main board+and ChiNext trade 100-share lots.+"""++from __future__ import annotations++import pandas as pd+import pytest++from backtest.engines.china_a import ChinaAEngine+from backtest.engines.composite import CompositeEngine+++def _rounded(symbol: str, raw: float) -> float:+ engine = ChinaAEngine({"initial_cash": 1_000_000})+ engine._active_symbol = symbol+ return engine.round_size(raw, 10.0)+++ ("symbol", "raw", "expected"),+ [+ ("688256.SH", 199.9, 0), # STAR Market: below the 200-share minimum+ ("688256.SH", 200.0, 200),+ ("688256.SH", 253.7, 253), # then 1-share steps+ ("689009.SH", 253.7, 253), # STAR Market CDR+ ("920002.BJ", 99.9, 0), # Beijing: below the 100-share minimum+ ("920002.BJ", 153.7, 153),+ ("830799.BJ", 153.7, 153), # Beijing legacy 8xxxxx code+ ("600519.SH", 253.7, 200), # main board: 100-share lots+ ("300750.SZ", 253.7, 200), # ChiNext: 100-share lots+ ],+)+def test_round_size_follows_the_board_grid(+ symbol: str, raw: float, expected: int+) -> None:+ assert _rounded(symbol, raw) == expected+++def test_negative_size_clamps_to_zero_on_one_share_boards() -> None:+ assert _rounded("688256.SH", -250.0) == 0+ assert _rounded("920002.BJ", -150.0) == 0+++def _opened(symbol: str, price: float, weight: float, initial_cash: float) -> float:+ """Shares opened through the engine's own sizing path."""+ engine = ChinaAEngine({"initial_cash": initial_cash})+ dates = pd.date_range("2026-01-05", periods=2, freq="B")+ close = pd.DataFrame({symbol: [price, price]}, index=dates)+ data_map = {symbol: pd.DataFrame({"open": close[symbol], "close": close[symbol]})}+ weights = pd.DataFrame({symbol: [0.0, weight]}, index=dates)+ engine._execute_bars(dates, data_map, close, weights, [symbol])+ opens = [fill for fill in engine.fill_records if fill.action == "open"]+ return sum(abs(fill.signed_quantity) for fill in opens)+++def test_star_buy_below_200_shares_is_not_filled() -> None:+ # 200,000 at 1,300 plus slippage buys about 153 shares: a legal 100-share+ # lot on the main board, but below the STAR Market's 200-share minimum.+ assert _opened("688256.SH", 1_300.0, 1.0, 200_000) == 0+++def test_star_buy_fills_in_one_share_steps() -> None:+ # 100,000 at 80 plus 0.1% slippage is 1,248.75 shares.+ assert _opened("688256.SH", 80.0, 0.1, 1_000_000) == 1_248+ # The same order on the main board rounds down to a 100-share lot.+ assert _opened("600519.SH", 80.0, 0.1, 1_000_000) == 1_200+++def test_composite_sizes_star_symbols_on_the_star_grid() -> None:+ engine = CompositeEngine({}, ["688256.SH", "600519.SH"])+ engine._active_symbol = "688256.SH"+ assert engine.round_size(253.7, 10.0) == 253+ engine._active_symbol = "600519.SH"+ assert engine.round_size(253.7, 10.0) == 200