Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(skills): stop every skill example relying on the pandas pct_change fill default

Haozhe Wube2c09f2a873
diff --git a/agent/src/skills/correlation-analysis/SKILL.md b/agent/src/skills/correlation-analysis/SKILL.md
index e1c4ab65a..7bb7b63e7 100644
--- a/agent/src/skills/correlation-analysis/SKILL.md
+++ b/agent/src/skills/correlation-analysis/SKILL.md
@@ -707,11 +707,11 @@ def fx_adjusted_correlation(
Raw correlation vs FX-adjusted correlation
"""
# Domestic-currency foreign return = foreign return + FX return
- foreign_ret = foreign_price.pct_change()
- fx_ret = fx_rate.pct_change()
+ foreign_ret = foreign_price.pct_change(fill_method=None)
+ fx_ret = fx_rate.pct_change(fill_method=None)
foreign_ret_cny = (1 + foreign_ret) * (1 + fx_ret) - 1
- domestic_ret = domestic_price.pct_change()
+ domestic_ret = domestic_price.pct_change(fill_method=None)
df = pd.concat([foreign_ret.rename("foreign_raw"),
foreign_ret_cny.rename("foreign_domestic"),
@@ -848,8 +848,8 @@ def generate_pair_signals(
kf = kalman_hedge_ratio(y_price, x_price)
spread = kf["spread"]
else:
- y_ret = y_price.pct_change()
- x_ret = x_price.pct_change()
+ y_ret = y_price.pct_change(fill_method=None)
+ x_ret = x_price.pct_change(fill_method=None)
res = bivariate_correlation_analysis(y_ret, x_ret, lookback)
hedge_ratio = abs(res["beta"])
spread = np.log(y_price) - hedge_ratio * np.log(x_price)
diff --git a/agent/src/skills/etf-analysis/SKILL.md b/agent/src/skills/etf-analysis/SKILL.md
index e2ac66332..35802c990 100644
--- a/agent/src/skills/etf-analysis/SKILL.md
+++ b/agent/src/skills/etf-analysis/SKILL.md
@@ -621,8 +621,8 @@ def calc_tracking_error(
aligned.columns = ['etf', 'index']
# 计算日收益率差值
- etf_ret = aligned['etf'].pct_change().dropna()
- idx_ret = aligned['index'].pct_change().dropna()
+ etf_ret = aligned['etf'].pct_change(fill_method=None).dropna()
+ idx_ret = aligned['index'].pct_change(fill_method=None).dropna()
daily_diff = etf_ret - idx_ret
# 跟踪误差 = 差值的标准差
@@ -775,7 +775,7 @@ def etf_fund_flow_analysis(
nav_df['scale'] = nav_df['unit_nav'] * nav_df['fund_share'] / 1e8
# 净值变动引起的规模变化(被动)
- nav_df['nav_return'] = nav_df['unit_nav'].pct_change()
+ nav_df['nav_return'] = nav_df['unit_nav'].pct_change(fill_method=None)
nav_df['passive_change'] = nav_df['scale'].shift(1) * nav_df['nav_return']
# 资金净流入 ≈ 规模变化 - 净值带来的被动变化
diff --git a/agent/src/skills/financial-statement/SKILL.md b/agent/src/skills/financial-statement/SKILL.md
index fb0399a5f..27a34462d 100644
--- a/agent/src/skills/financial-statement/SKILL.md
+++ b/agent/src/skills/financial-statement/SKILL.md
@@ -108,8 +108,8 @@ accrual_ratio = (net_income - cfo) / total_assets
# accrual_ratio > 10% → 应计利润占比高,盈利质量差
# 验证收入质量
-receivable_growth = accounts_receivable.pct_change()
-revenue_growth = revenue.pct_change()
+receivable_growth = accounts_receivable.pct_change(fill_method=None)
+revenue_growth = revenue.pct_change(fill_method=None)
# receivable_growth > revenue_growth → 收入质量恶化
# 验证资产负债表与现金流一致性
diff --git a/agent/src/skills/ml-strategy/SKILL.md b/agent/src/skills/ml-strategy/SKILL.md
index 5733098fb..efe62e602 100644
--- a/agent/src/skills/ml-strategy/SKILL.md
+++ b/agent/src/skills/ml-strategy/SKILL.md
@@ -63,11 +63,11 @@ def build_features(df: pd.DataFrame) -> pd.DataFrame:
"""
c = df["close"]
v = df["volume"]
- ret = c.pct_change()
+ ret = c.pct_change(fill_method=None)
features = pd.DataFrame(index=df.index)
- features["f_ret_5d"] = c.pct_change(5)
- features["f_ret_20d"] = c.pct_change(20)
+ features["f_ret_5d"] = c.pct_change(5, fill_method=None)
+ features["f_ret_20d"] = c.pct_change(20, fill_method=None)
features["f_vol_20d"] = ret.rolling(20).std()
features["f_ma_ratio"] = c / c.rolling(20).mean()
features["f_volume_ratio"] = v / v.rolling(20).mean()
@@ -230,8 +230,8 @@ The table below lists all default features. Add or remove features as needed —
| Feature Name | Formula | Meaning |
|--------|---------|------|
-| ret_5d | `close.pct_change(5)` | Past 5-day return (short-term momentum) |
-| ret_20d | `close.pct_change(20)` | Past 20-day return (medium-term momentum) |
+| ret_5d | `close.pct_change(5, fill_method=None)` | Past 5-day return (short-term momentum) |
+| ret_20d | `close.pct_change(20, fill_method=None)` | Past 20-day return (medium-term momentum) |
| vol_20d | `returns.rolling(20).std()` | 20-day volatility |
| rsi_14 | See RSI formula in code | Relative Strength Index (division-by-zero guarded) |
| ma_ratio | `close / close.rolling(20).mean()` | Degree of deviation from the 20-day moving average |
diff --git a/agent/src/skills/multi-factor/example_signal_engine.py b/agent/src/skills/multi-factor/example_signal_engine.py
index 38ad7c392..c6eba6bdf 100644
--- a/agent/src/skills/multi-factor/example_signal_engine.py
+++ b/agent/src/skills/multi-factor/example_signal_engine.py
@@ -77,7 +77,7 @@ class SignalEngine:
"""
close = df["close"]
volume = df["volume"]
- returns = close.pct_change()
+ returns = close.pct_change(fill_method=None)
factors = pd.DataFrame(index=df.index)
# 动量:过去 N 日累计收益(正向)
diff --git a/agent/src/skills/multi-factor/zoo_signal_engine.py b/agent/src/skills/multi-factor/zoo_signal_engine.py
index aaad6f17b..52fbde9f3 100644
--- a/agent/src/skills/multi-factor/zoo_signal_engine.py
+++ b/agent/src/skills/multi-factor/zoo_signal_engine.py
@@ -358,9 +358,9 @@ if __name__ == "__main__":
def compute(self, alpha_id: str, panel: dict[str, pd.DataFrame]) -> pd.DataFrame:
close = panel["close"]
if alpha_id == "fake_mom":
- return close.pct_change(3)
+ return close.pct_change(3, fill_method=None)
if alpha_id == "fake_rev":
- return -close.pct_change(1)
+ return -close.pct_change(1, fill_method=None)
raise KeyError(alpha_id)
ls_engine = ZooSignalEngine.from_zoo(
diff --git a/agent/src/skills/pine-script/SKILL.md b/agent/src/skills/pine-script/SKILL.md
index f5050771e..ff08ef1a1 100644
--- a/agent/src/skills/pine-script/SKILL.md
+++ b/agent/src/skills/pine-script/SKILL.md
@@ -128,7 +128,7 @@ alertcondition(shortCondition, title="Short Signal", message="Short entry signal
| `df['close'].rolling(n).std()` | `ta.stdev(close, n)` |
| `df['high'].rolling(n).max()` | `ta.highest(high, n)` |
| `df['low'].rolling(n).min()` | `ta.lowest(low, n)` |
-| `df['close'].pct_change()` | `(close - close[1]) / close[1]` |
+| `df['close'].pct_change(fill_method=None)` | `(close - close[1]) / close[1]` |
| `df['volume'].rolling(n).mean()` | `ta.sma(volume, n)` |
| `df['close'] > df['close'].shift(1)` | `close > close[1]` |
| Bollinger Bands | `[mid, upper, lower] = ta.bb(close, length, mult)` |
@@ -234,7 +234,7 @@ DRAWTEXT(SELL,HIGH,'S'),COLORWHITE;
| `max(a,b)` | `MAX(A,B)` |
| `min(a,b)` | `MIN(A,B)` |
| conditional | `IF(COND,A,B)` |
-| `df['close'].pct_change()` | `(CLOSE-REF(CLOSE,1))/REF(CLOSE,1)` |
+| `df['close'].pct_change(fill_method=None)` | `(CLOSE-REF(CLOSE,1))/REF(CLOSE,1)` |
| count true in N bars | `COUNT(COND,N)` |
| sum over N bars | `SUM(X,N)` |
| std over N bars | `STD(CLOSE,N)` |
diff --git a/agent/src/skills/strategy-dev-manager/examples.md b/agent/src/skills/strategy-dev-manager/examples.md
index 58dc69305..2bfcd23a8 100644
--- a/agent/src/skills/strategy-dev-manager/examples.md
+++ b/agent/src/skills/strategy-dev-manager/examples.md
@@ -90,8 +90,8 @@ Then implement `signal_engine.py` using the factor template, filling in the mome
# Inside generate():
for symbol, df in data_map.items():
# 12-month cumulative return, skip most recent month
- ret_12m = df["close"].pct_change(12).shift(1)
- ret_1m = df["close"].pct_change(1)
+ ret_12m = df["close"].pct_change(12, fill_method=None).shift(1)
+ ret_1m = df["close"].pct_change(1, fill_method=None)
mom_signal = ret_12m - ret_1m # skip the most recent month
signals[symbol] = mom_signal
```
diff --git a/agent/src/skills/strategy-dev-manager/templates/factor_signal_engine.py b/agent/src/skills/strategy-dev-manager/templates/factor_signal_engine.py
index 00e1c060a..916a6bbbe 100644
--- a/agent/src/skills/strategy-dev-manager/templates/factor_signal_engine.py
+++ b/agent/src/skills/strategy-dev-manager/templates/factor_signal_engine.py
@@ -47,7 +47,7 @@ class SignalEngine:
# TODO: Replace with actual factor formula from paper
# Example: 12-month momentum (Jegadeesh & Titman 1993)
factor_values[symbol] = (
- df["close"].pct_change(self.lookback)
+ df["close"].pct_change(self.lookback, fill_method=None)
)
# Step 2: Cross-sectional ranking
diff --git a/agent/src/skills/volatility/example_signal_engine.py b/agent/src/skills/volatility/example_signal_engine.py
index 98a840917..d69214e79 100644
--- a/agent/src/skills/volatility/example_signal_engine.py
+++ b/agent/src/skills/volatility/example_signal_engine.py
@@ -21,7 +21,7 @@ def compute_hv(close: pd.Series, window: int = 20, annualize: int = 252) -> pd.S
Returns:
年化历史波动率序列。
"""
- returns = close.pct_change()
+ returns = close.pct_change(fill_method=None)
return returns.rolling(window).std() * np.sqrt(annualize)
diff --git a/agent/tests/test_skill_pct_change_guard.py b/agent/tests/test_skill_pct_change_guard.py
new file mode 100644
index 000000000..095591a1f
--- /dev/null
+++ b/agent/tests/test_skill_pct_change_guard.py
@@ -0,0 +1,66 @@
+"""Every ``pct_change`` a skill hands the agent must state its fill policy.
+
+pandas 2.x still defaults ``Series.pct_change`` to ``fill_method='pad'``: a
+missing close is forward-filled, so the gap bar reports a **0.0% return** that
+never happened and the following bar reports the two-day move as a one-day move.
+Both are fabricated observations, not a bias with a known sign -- the rolling
+volatility built on them can land either side of the truth depending on the
+data. pandas already emits a FutureWarning for the implicit default and removes
+it in 3.0, but this repo pins ``pandas>=2.0.0,<3.0.0`` (pyproject.toml), so the
+padding is live in every supported version.
+
+Skill files are not inert documentation: the agent reads these examples and
+templates and runs what it copies, so a padded ``pct_change`` there becomes a
+padded ``pct_change`` in a user's strategy. This guard is repo-wide because the
+defect arrived one file at a time (#1397 and #1398 each fixed a single call
+site while eleven more sat untouched, two of them in files those same PRs
+edited).
+
+Equity-curve returns in ``backtest/`` are deliberately out of scope: the engine
+builds that series with a value on every bar and ``metrics.py`` documents its
+``.fillna(0.0)`` choice at the seam.
+"""
+
+from __future__ import annotations
+
+import re
+from pathlib import Path
+
+SKILLS_ROOT = Path(__file__).resolve().parents[1] / "src" / "skills"
+
+# Matches a call and captures its argument list, including the empty one.
+_CALL = re.compile(r"\.pct_change\(([^)]*)\)")
+
+
+def _offenders() -> list[str]:
+ """Return ``path:line`` for every pct_change call with no fill policy."""
+ found: list[str] = []
+ for path in sorted(SKILLS_ROOT.rglob("*")):
+ if path.suffix not in {".py", ".md"} or not path.is_file():
+ continue
+ for lineno, line in enumerate(
+ path.read_text(encoding="utf-8").splitlines(), start=1
+ ):
+ for match in _CALL.finditer(line):
+ if "fill_method" not in match.group(1):
+ rel = path.relative_to(SKILLS_ROOT.parents[2])
+ found.append(f"{rel}:{lineno}")
+ return found
+
+
+def test_no_skill_relies_on_the_pandas_pct_change_fill_default() -> None:
+ offenders = _offenders()
+ assert not offenders, (
+ "pct_change() without an explicit fill_method pads missing prices into "
+ "a fabricated 0.0% return; pass fill_method=None:\n "
+ + "\n ".join(offenders)
+ )
+
+
+def test_the_guard_can_actually_see_an_offender() -> None:
+ """The scan must fail on a padded call, not just on nothing existing."""
+ assert [m.group(1) for m in _CALL.finditer("x.pct_change()")] == [""]
+ assert [m.group(1) for m in _CALL.finditer("x.pct_change(5)")] == ["5"]
+ assert "fill_method" not in _CALL.search("x.pct_change(5)").group(1)
+ assert "fill_method" in _CALL.search("x.pct_change(fill_method=None)").group(1)
+ assert SKILLS_ROOT.is_dir() and any(SKILLS_ROOT.rglob("*.md"))