baseline / Kronos /finetune /build_test_data.py
humblman's picture
Upload folder using huggingface_hub
ccd4d5a verified
Raw
History Blame Contribute Delete
2.72 kB
"""
Build test_data.pkl from 5min adjusted CSV files.
Only loads the test time range to save memory/disk.
"""
import os
import pickle
import pandas as pd
from tqdm import tqdm
# ── Config ────────────────────────────────────────────────────────────────────
DATA_DIR = "/home/hanyueju/MinModel/data/one_stock_one_csv_adjusted"
OUTPUT_DIR = "./data/processed_datasets"
OUTPUT_FILE = os.path.join(OUTPUT_DIR, "test_data.pkl")
# Match config.py test_time_range.
# Start a bit earlier to cover the lookback_window (240 bars) before backtest begins.
TEST_START = "2024-12-20"
TEST_END = "2026-04-07"
MIN_BARS = 300 # drop stocks with fewer than this many bars in the test window
# ─────────────────────────────────────────────────────────────────────────────
def build(data_dir, test_start, test_end):
files = sorted(f for f in os.listdir(data_dir) if f.endswith(".csv"))
test_data = {}
for fname in tqdm(files, desc="Loading"):
stock_code = fname.replace(".csv", "")
path = os.path.join(data_dir, fname)
try:
df = pd.read_csv(
path,
usecols=["datetime", "open", "high", "low", "close", "volume", "turnover"],
parse_dates=["datetime"],
)
except Exception as e:
print(f" skip {fname}: {e}")
continue
df = df.rename(columns={"volume": "vol", "turnover": "amt"})
df = df.set_index("datetime").sort_index()
df = df[["open", "high", "low", "close", "vol", "amt"]]
df = df.dropna()
df = df[(df.index >= test_start) & (df.index <= test_end)]
if len(df) < MIN_BARS:
continue
test_data[stock_code] = df
return test_data
if __name__ == "__main__":
os.makedirs(OUTPUT_DIR, exist_ok=True)
print(f"Building test_data.pkl")
print(f" Source : {DATA_DIR}")
print(f" Range : {TEST_START} ~ {TEST_END}")
print(f" Output : {OUTPUT_FILE}")
print()
test_data = build(DATA_DIR, TEST_START, TEST_END)
print(f"\nStocks loaded: {len(test_data)}")
sample_key = next(iter(test_data))
print(f"Sample ({sample_key}): {len(test_data[sample_key])} bars")
print(test_data[sample_key].head(3))
with open(OUTPUT_FILE, "wb") as f:
pickle.dump(test_data, f)
size_mb = os.path.getsize(OUTPUT_FILE) / 1024 / 1024
print(f"\nSaved β†’ {OUTPUT_FILE} ({size_mb:.1f} MB)")