fix: ensure array column order matches codes parameter

The numpy arrays extracted from target_pos and close_df must be
reindexed by the codes parameter to ensure _code_to_col mapping
is consistent regardless of DataFrame internal column order.

Fixes test_rotation_is_independent_of_close_open_symbol_order

Signed-off-by: shadowinlife <shadowinlife@gmail.com>

AI-Contributed/Feature: 0/6
AI-Contributed/UT: 0/0
This commit is contained in:
shadowinlife committed 2026-07-18 22:18:02 +08:00
1 parent c304485dd1
commit 5efdc7c0f5
1 file changed
+4 -2
+4 -2
View File
@@ -611,8 +611,10 @@ class BaseEngine(ABC):
) -> None:
"""Bar-by-bar execution with market rule enforcement."""
# Pre-extract numpy arrays for O(1) indexed access instead of DataFrame.at[]
_target_arr = target_pos.values # (n_dates, n_codes) ndarray
_close_arr = close_df.values # (n_dates, n_codes) ndarray
# Explicit column reindex ensures array column order matches codes parameter,
# regardless of DataFrame internal column ordering (which may be alphabetical).
_target_arr = target_pos[codes].values # (n_dates, n_codes) ndarray
_close_arr = close_df[codes].values # (n_dates, n_codes) ndarray
_code_to_col = {c: j for j, c in enumerate(codes)}
# Store as instance attrs for use in _calc_equity / _safe_price
self._close_arr = _close_arr