fix: refresh latest market window by default
| | |
| | | |
| | | **最新更新(v5.4):** |
| | | |
| | | - ✅ **默认增量更新** - 普通更新只合并最近行情,数据已最新时不发起网络请求 |
| | | - ✅ **默认增量更新** - 普通更新刷新并覆盖最近行情窗口,补齐上次可能缺失的数据 |
| | | - ✅ **显式全量重建** - 仅在指定 `--full` 时重新下载全部历史数据 |
| | | - ✅ **安全数据重置** - 行情、因子和选股结果先移动到时间戳备份目录,不直接永久删除 |
| | | - ✅ **OpenClaw Skill 路由** - “更新A股数据”默认调用增量入口,支持全量重建和安全重置指令 |
| | |
| | | python3 scripts/fix_data_quality_v2.py |
| | | ``` |
| | | |
| | | 增量更新会读取本地最近完整交易日,保留重叠窗口,并按“股票代码 + 日期”替换去重。若数据已经覆盖最近预期交易日,程序会直接结束;若正式数据文件不存在,则自动执行首次全量初始化。下载覆盖率低于90%时不会覆盖正式数据。 |
| | | 增量更新会读取本地最近完整交易日,保留重叠窗口,并按“股票代码 + 日期”替换去重。即使日期已经是最新,默认仍会刷新最近窗口,以覆盖并补齐上一次可能不完整的数据;只有显式添加 `--skip-if-current` 才会在日期相同时跳过网络请求。若正式数据文件不存在,则自动执行首次全量初始化。下载覆盖率低于90%时不会覆盖正式数据。 |
| | | |
| | | 安全重置数据: |
| | | |
| | |
| | | |
| | | ### v5.4 (2026-07-11) |
| | | |
| | | - ✅ 默认数据更新改为增量模式,完整交易日已覆盖时跳过网络请求 |
| | | - ✅ 默认数据更新改为增量模式,并刷新最近窗口以补齐不完整行情 |
| | | - ✅ 新增显式全量重建入口和90%下载覆盖率写入门控 |
| | | - ✅ 新增备份式安全数据重置入口 |
| | | - ✅ OpenClaw Skill 更新为默认增量路由 |
| | |
| | | --- |
| | | name: a-stock-advisor |
| | | description: 操作本地 A 股量化项目,执行行情数据更新、北交所补数、数据质量检查、因子分析、每日选股、报告读取、日志诊断、回测和模拟交易。用户提到 A 股、股票同步、AKShare、BaoStock、腾讯行情、因子 IC/IR、选股结果、每日主控、回测、模拟盘或量化报告时使用。不得执行真实券商委托或资金操作。 |
| | | metadata: {"version":"2026.7.11.1"} |
| | | metadata: {"version":"2026.7.11.2"} |
| | | --- |
| | | |
| | | # A股量化助手 |
| | |
| | | {PYTHON} scripts/update_stock_data.py |
| | | ``` |
| | | |
| | | 增量模式会读取本地最近完整交易日,保留重叠窗口,并按 `股票代码 + 日期` 替换去重。若本地数据已经覆盖最近预期交易日,则不发起网络请求。若正式数据文件不存在,会自动执行首次全量初始化。 |
| | | 增量模式会读取本地最近完整交易日,保留重叠窗口,并按 `股票代码 + 日期` 替换去重。即使日期已经是最新,默认仍会刷新并覆盖最近窗口,以补齐上一次可能缺失的数据。只有用户明确要求“数据已最新就跳过”时才添加 `--skip-if-current`。若正式数据文件不存在,会自动执行首次全量初始化。 |
| | | |
| | | 用户指定股票数量时使用 `--n-stocks`。小范围连通性测试使用: |
| | | |
| | |
| | | 正确流程的日志必须依次包含以下关键标记: |
| | | |
| | | 1. `数据更新任务开始 - 默认增量模式` |
| | | 2. `本地完整交易日` 和 `预期最新交易日` |
| | | 3. 数据已最新时出现 `增量数据更新完成(无新增)`;否则继续获取股票列表 |
| | | 2. `本地完整交易日`、`预期最新交易日` 和 `本地最新日覆盖` |
| | | 3. 即使日期已最新,默认仍出现“刷新并覆盖最近行情窗口”,随后继续获取股票列表 |
| | | 4. AKShare 成功,或者出现切换备用数据源的说明 |
| | | 5. BaoStock 沪深历史行情进度(发生降级时) |
| | | 6. 腾讯北交所历史行情进度(切换备用源且存在北交所股票时) |
| | |
| | | parser.add_argument( |
| | | "--force", |
| | | action="store_true", |
| | | help="即使数据已覆盖最近预期交易日,也强制执行增量请求", |
| | | help="与 --skip-if-current 同时使用时仍强制刷新(兼容旧命令)", |
| | | ) |
| | | parser.add_argument( |
| | | "--skip-if-current", |
| | | action="store_true", |
| | | help="本地已覆盖预期交易日时跳过网络请求;默认仍刷新最近窗口", |
| | | ) |
| | | return parser.parse_args(argv) |
| | | |
| | |
| | | |
| | | end = day - pd.Timedelta(days=1) if day.weekday() < 5 else day |
| | | return pd.bdate_range(end=end, periods=1)[0].normalize() |
| | | |
| | | |
| | | def market_date_coverage(df, date, expected_stock_count=None): |
| | | """Return stock count and coverage for one market date.""" |
| | | normalized_dates = pd.to_datetime(df["date"], errors="coerce").dt.normalize() |
| | | date = pd.Timestamp(date).normalize() |
| | | actual = int(df.loc[normalized_dates == date, "stock_code"].astype(str).nunique()) |
| | | expected = int( |
| | | expected_stock_count |
| | | if expected_stock_count is not None |
| | | else df["stock_code"].astype(str).nunique() |
| | | ) |
| | | coverage = actual / expected if expected > 0 else 0.0 |
| | | return expected, actual, coverage |
| | | |
| | | |
| | | def prepare_incremental_rows(existing, fetched, replace_start): |
| | |
| | | f"本地完整交易日: {latest_before:%Y-%m-%d}; " |
| | | f"预期最新交易日: {expected_date:%Y-%m-%d}" |
| | | ) |
| | | if latest_before >= expected_date and not args.force: |
| | | before_expected, before_actual, before_coverage = market_date_coverage( |
| | | existing, |
| | | latest_before, |
| | | ) |
| | | logger.info( |
| | | f"本地最新日覆盖: {before_actual}/{before_expected} " |
| | | f"({before_coverage:.2%})" |
| | | ) |
| | | if latest_before >= expected_date and args.skip_if_current and not args.force: |
| | | logger.info("本地行情已是最新,无需发起网络请求") |
| | | logger.info("✅ 增量数据更新完成(无新增)") |
| | | return 0 |
| | | if latest_before >= expected_date: |
| | | logger.info("日期已是最新,仍按默认策略刷新并覆盖最近行情窗口") |
| | | |
| | | stale_days = max(0, (expected_date - latest_before).days) |
| | | fetch_days = max(args.context_days, stale_days + args.overlap_days) |
| | |
| | | logger.error("合并后完整交易日倒退,拒绝写入并保留原文件") |
| | | return 1 |
| | | |
| | | latest_expected_count, latest_actual_count, latest_coverage = ( |
| | | market_date_coverage( |
| | | merged, |
| | | latest_after, |
| | | expected_stock_count=max( |
| | | expected_stock_count, |
| | | int(merged["stock_code"].astype(str).nunique()), |
| | | ), |
| | | ) |
| | | ) |
| | | |
| | | merged.attrs.update(getattr(existing, "attrs", {})) |
| | | merged.attrs.update({ |
| | | "source": f"incremental_{fetched.attrs.get('source', 'multi_source')}", |
| | |
| | | "overlap_days": int(args.overlap_days), |
| | | "previous_complete_date": latest_before.strftime("%Y-%m-%d"), |
| | | "latest_complete_date": latest_after.strftime("%Y-%m-%d"), |
| | | "latest_date_stock_count": latest_actual_count, |
| | | "latest_date_expected_stock_count": latest_expected_count, |
| | | "latest_date_coverage_ratio": latest_coverage, |
| | | "stock_count": int(merged["stock_code"].nunique()), |
| | | "total_records": int(len(merged)), |
| | | "downloaded_records": int(len(fetched)), |
| | |
| | | write_json_atomic(metadata, METADATA_FILE) |
| | | |
| | | logger.info(f"✓ 最新完整交易日: {latest_after:%Y-%m-%d}") |
| | | logger.info( |
| | | f"✓ 最新日覆盖: {latest_actual_count}/{latest_expected_count} " |
| | | f"({latest_coverage:.2%})" |
| | | ) |
| | | logger.info(f"✓ 股票数量: {merged['stock_code'].nunique()}") |
| | | logger.info(f"✓ 总记录数: {len(merged)}") |
| | | logger.info("✅ 增量数据更新完成") |
| | |
| | | |
| | | self.assertFalse(args.full) |
| | | self.assertIsNone(args.n_stocks) |
| | | self.assertFalse(args.skip_if_current) |
| | | self.assertEqual(args.context_days, 70) |
| | | self.assertEqual(args.overlap_days, 7) |
| | | |
| | |
| | | self.assertEqual(actual, 20) |
| | | self.assertEqual(coverage, 0.2) |
| | | |
| | | def test_up_to_date_file_skips_network_fetch(self): |
| | | def test_market_date_coverage_reports_incomplete_latest_day(self): |
| | | data = pd.DataFrame([ |
| | | {"date": "2026-07-10", "stock_code": "600001"}, |
| | | {"date": "2026-07-10", "stock_code": "000001"}, |
| | | {"date": "2026-07-09", "stock_code": "300001"}, |
| | | ]) |
| | | |
| | | expected, actual, coverage = incremental.market_date_coverage( |
| | | data, |
| | | pd.Timestamp("2026-07-10"), |
| | | ) |
| | | |
| | | self.assertEqual(expected, 3) |
| | | self.assertEqual(actual, 2) |
| | | self.assertAlmostEqual(coverage, 2 / 3) |
| | | |
| | | def test_skip_if_current_avoids_network_fetch(self): |
| | | data = pd.DataFrame([ |
| | | {"date": "2026-07-10", "stock_code": "600001", "close": 10.0}, |
| | | {"date": "2026-07-10", "stock_code": "000001", "close": 11.0}, |
| | | ]) |
| | | with tempfile.TemporaryDirectory() as temp_dir: |
| | | data_file = Path(temp_dir) / "stock.pkl" |
| | | with data_file.open("wb") as handle: |
| | | pickle.dump(data, handle) |
| | | |
| | | args = incremental.parse_args(["--skip-if-current"]) |
| | | with patch.object(incremental, "DATA_FILE", data_file), patch.object( |
| | | incremental, |
| | | "latest_expected_business_date", |
| | | return_value=pd.Timestamp("2026-07-10"), |
| | | ), patch.object( |
| | | incremental.data_source, |
| | | "fetch_real_stock_data_from_akshare", |
| | | ) as fetch: |
| | | result = incremental.run_incremental_update(args) |
| | | |
| | | self.assertEqual(result, 0) |
| | | fetch.assert_not_called() |
| | | |
| | | def test_current_date_is_refreshed_by_default(self): |
| | | data = pd.DataFrame([ |
| | | {"date": "2026-07-10", "stock_code": "600001", "close": 10.0}, |
| | | {"date": "2026-07-10", "stock_code": "000001", "close": 11.0}, |
| | | ]) |
| | | fetched = data.copy() |
| | | with tempfile.TemporaryDirectory() as temp_dir: |
| | | data_file = Path(temp_dir) / "stock.pkl" |
| | | with data_file.open("wb") as handle: |
| | |
| | | ), patch.object( |
| | | incremental.data_source, |
| | | "fetch_real_stock_data_from_akshare", |
| | | ) as fetch: |
| | | return_value=fetched, |
| | | ) as fetch, patch.object( |
| | | incremental, "write_pickle_atomic" |
| | | ) as write_pickle, patch.object( |
| | | incremental, "write_json_atomic" |
| | | ): |
| | | result = incremental.run_incremental_update(args) |
| | | |
| | | self.assertEqual(result, 0) |
| | | fetch.assert_not_called() |
| | | fetch.assert_called_once() |
| | | write_pickle.assert_called_once() |
| | | |
| | | |
| | | class SafeResetTests(unittest.TestCase): |