Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(journal): treat NaN securities codes as empty

santhreala43c519fbb98
diff --git a/agent/src/tools/trade_journal_parsers.py b/agent/src/tools/trade_journal_parsers.py
index 62614abbd..1b62c91af 100644
--- a/agent/src/tools/trade_journal_parsers.py
+++ b/agent/src/tools/trade_journal_parsers.py
@@ -162,12 +162,23 @@ def _normalize_side(raw: Any) -> str:
raise ValueError(f"Unsupported trade side: {raw!r}")
+def _is_empty_code(raw: Any) -> bool:
+ """True for None/NaN/blank securities codes from CSV/Excel cells."""
+ if raw is None:
+ return True
+ try:
+ if pd.isna(raw):
+ return True
+ except (TypeError, ValueError):
+ pass
+ return not str(raw).strip()
+
+
def _qualify_a_share(code: str) -> str:
"""Append .SH/.SZ/.BJ suffix to a bare A-share ticker."""
- code = str(code).strip()
- if not code:
+ if _is_empty_code(code):
raise ValueError("empty securities code")
- code = code.zfill(6)
+ code = str(code).strip().zfill(6)
if "." in code:
return code.upper()
first = code[0]
@@ -196,8 +207,8 @@ def parse_tonghuashun(df: pd.DataFrame) -> list[TradeRecord]:
"""
records: list[TradeRecord] = []
for _, row in df.iterrows():
- raw_code = str(row.get("证券代码", "")).strip()
- if not raw_code:
+ raw_code = row.get("证券代码", "")
+ if _is_empty_code(raw_code):
continue
qty = _to_float(row.get("成交数量"))
price = _to_float(row.get("成交价格"))
@@ -225,8 +236,8 @@ def parse_eastmoney(df: pd.DataFrame) -> list[TradeRecord]:
"""
records: list[TradeRecord] = []
for _, row in df.iterrows():
- raw_code = str(row.get("股票代码", "")).strip()
- if not raw_code:
+ raw_code = row.get("股票代码", "")
+ if _is_empty_code(raw_code):
continue
raw_date = str(row.get("成交日期", "")).strip()
raw_time = str(row.get("成交时间", "")).strip()
diff --git a/agent/tests/test_trade_journal.py b/agent/tests/test_trade_journal.py
index f07f2ad75..8032de6d9 100644
--- a/agent/tests/test_trade_journal.py
+++ b/agent/tests/test_trade_journal.py
@@ -20,6 +20,7 @@ from src.tools.trade_journal_parsers import (
_normalize_side,
_qualify_a_share,
parse_tonghuashun,
+ parse_eastmoney,
detect_format,
parse_file,
records_to_dataframe,
@@ -529,3 +530,38 @@ def test_parse_tonghuashun_skips_blank_code_rows() -> None:
rec = parse_tonghuashun(df)
assert len(rec) == 1
assert rec[0].symbol == "600519.SH"
+
+
+def test_qualify_a_share_rejects_nan() -> None:
+ with pytest.raises(ValueError, match="empty"):
+ _qualify_a_share(float("nan"))
+
+
+def test_parse_tonghuashun_skips_nan_code_rows() -> None:
+ df = pd.DataFrame([{
+ "成交时间": "2024-01-01 10:00:00", "证券代码": float("nan"), "证券名称": "",
+ "操作": "买入", "成交数量": 100, "成交价格": 10.0, "成交金额": 1000,
+ "手续费": 0, "印花税": 0, "过户费": 0,
+ }, {
+ "成交时间": "2024-01-01 10:01:00", "证券代码": "600519", "证券名称": "茅台",
+ "操作": "买入", "成交数量": 100, "成交价格": 10.0, "成交金额": 1000,
+ "手续费": 0, "印花税": 0, "过户费": 0,
+ }])
+ rec = parse_tonghuashun(df)
+ assert len(rec) == 1
+ assert rec[0].symbol == "600519.SH"
+
+
+def test_parse_eastmoney_skips_nan_code_rows() -> None:
+ df = pd.DataFrame([{
+ "成交日期": "20240101", "成交时间": "10:00:00", "股票代码": float("nan"),
+ "股票名称": "", "买卖标志": "B", "成交数量": 100, "成交均价": 10.0,
+ "成交金额": 1000, "佣金": 0, "印花税": 0,
+ }, {
+ "成交日期": "20240101", "成交时间": "10:01:00", "股票代码": "000001",
+ "股票名称": "平安", "买卖标志": "B", "成交数量": 100, "成交均价": 10.0,
+ "成交金额": 1000, "佣金": 0, "印花税": 0,
+ }])
+ rec = parse_eastmoney(df)
+ assert len(rec) == 1
+ assert rec[0].symbol == "000001.SZ"