T09 · Insecure Skill Coding Practices
- Location
ptrade_c31_momentum.py:87- Finding
Black-Swan Protection Cannot Execute Because Only One Index Observation Is Requested
- Content
View full analysis
= 2: idx_today = idx_df["close"].iloc[-1] idx_yesterday = idx_df["close"].iloc[-2] idx_change = (idx_today - idx_yesterday) / idx_yesterday if idx_change <= BLACK_SWANG_5: logger.warning("指数跌幅{}%,清仓".format(idx_change * 100)) for stock in list(_context["positions"].keys()): sell_stock(context, data, stock) return elif idx_change <= BLACK_SWANG_3: logger.warning("指数跌幅{}%,减半仓".format(idx_change * 100)) ``` ### Technical Analysis The query uses the same previous trading date for both `start_date` and `end_date`. A daily-frequency query over a single trading day will normally return at most one observation. The subsequent `len(idx_df) >= 2` condition therefore prevents the daily index return from being calculated. As a result, both documented controls—the 3% decline position reduction and the 5% decline liquidation—are unreachable under normal API behavior. This is a fail-open design: insufficient risk data silently bypasses the protective action instead of preventing new exposure or taking a conservative fallback action. ### Attack Path 1. The strategy reaches `rebalance` on a scheduled trading day. 2. It requests CSI 300 data for only the immediately preceding trading date. 3. The API returns zero or one daily observation. 4. The `len(idx_df) >= 2` condition evaluates to false. 5. No index return is calculated, even if the market has fallen beyond the configured thresholds. 6. Existing posi ...[truncated 805 chars]- Remediation
View remediation
