diff --git a/sanguo_factor/universe.py b/sanguo_factor/universe.py index 8ebd309..5493cf9 100644 --- a/sanguo_factor/universe.py +++ b/sanguo_factor/universe.py @@ -35,6 +35,16 @@ def load_universe_bars( datetime.strptime(end, "%Y-%m-%d") + timedelta(days=_FORWARD_DAYS) ).strftime("%Y-%m-%d") + # 缓存短路:含窗口/symbols/limit防串池 + import os as _os + cache_path = _os.path.join( + _os.path.dirname(_os.path.abspath(vnpy_db)), + f"universe_cache_{start}_{end}_{len(symbols) if symbols else 'all'}" + f"{'_'+str(limit) if limit else ''}.parquet" + ) + if _os.path.exists(cache_path): + return pl.read_parquet(cache_path) + if symbols is None: conn = sqlite3.connect(vnpy_db, timeout=60) try: @@ -100,6 +110,13 @@ def load_universe_bars( .with_columns(pl.int_range(pl.len()).over("vt_symbol").alias("bar_idx")) .collect() ) + + # 写缓存(失败静默,不影响主流程) + try: + df.write_parquet(cache_path) + except Exception: + pass + return df