#!/usr/bin/env python3
"""[第2步收尾] 物理移除四池日线的 qfq_*/hfq_* 落盘列, 只存 raw*4 + adj_factor + 技术字段。
qfq/hfq 改由 grid_or._derive_split_prices 内存派生(展示/判定时算) → qfq 不再持久化。
删除安全: raw+factor 在不丢, 随时可派生回; 代码层已全部适配派生。
DRY=1 只统计不写盘。
"""
import os, glob
import pandas as pd

BASE = os.path.expanduser('~/zt_app/backtest_zt_full')
DRY = os.environ.get('DRY') == '1'

STD = ['ts_code', 'trade_date',
       'raw_open', 'raw_high', 'raw_low', 'raw_close', 'adj_factor',
       'pre_close', 'change', 'pct_chg', 'vol', 'amount']
DROP = ['qfq_open', 'qfq_high', 'qfq_low', 'qfq_close',
        'hfq_open', 'hfq_high', 'hfq_low', 'hfq_close']

files = sorted(glob.glob(f'{BASE}/daily_*/*.csv'))
dropped = 0
for f in files:
    d = pd.read_csv(f, dtype={'trade_date': str})
    if not {'qfq_close', 'hfq_close'} & set(d.columns):
        continue  # 已是纯 raw+factor
    if not DRY:
        d = d.drop(columns=[c for c in DROP if c in d.columns], errors='ignore')
        keep = [c for c in STD if c in d.columns] + [c for c in d.columns if c not in STD]
        d = d[keep].to_csv(f, index=False)
    dropped += 1
print(f'移除落盘 qfq_/hfq_ 列: {dropped} 只' + (' (DRY=统计)' if DRY else ''))

# 验证样例
if not DRY:
    import glob as g2
    sample = g2.glob(f'{BASE}/daily_*.csv' )
    for f in files[:2]:
        cols = pd.read_csv(f, nrows=1).columns.tolist()
        print(f'  {os.path.basename(f)}: {len(cols)}列 = {cols}')