Files
etf/core/datasource/cache.py
aszerW 6454e6823f fix(datasource): 修正混合数据源导入路径错误
- 修正 strategies.rotation.engine 中 hybrid_source 模块导入路径错误
- 新增 core.datasource 目录下多个数据源实现模块
- 增加 Akshare 数据源支持 A股指数数据拉取
- 实现数据缓存管理机制,支持本地数据缓存读写
- 新增 YFinance 数据源,支持通过 SSH 隧道访问美股和港股数据
- 实现混合数据源支持 A股/Tushare、港美股/YFinance、加密货币/CCXT 的统一访问
- 集成 SSH 隧道管理,支持 SOCKS5 转 HTTP 代理转发
- 新增 socks2http.py 代理转发工具,解决 CCXT 仅支持 HTTP 代理问题
- 修改 rotation.yaml 加密货币注释,明确使用 OKX 现货和 SSH->HTTP 代理访问
- 删除.gitignore中无用的 data/ 忽略规则,保留 test/ 文件夹忽略规则
2026-03-25 01:32:33 +08:00

55 lines
1.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
数据缓存管理模块
"""
import os
import pandas as pd
from pathlib import Path
from typing import Optional
class DataCache:
"""CSV文件缓存管理器"""
def __init__(self, cache_dir: str = "data_cache"):
self.cache_dir = Path(cache_dir)
self.cache_dir.mkdir(exist_ok=True)
def _get_cache_path(self, code: str, start_date: str, end_date: str) -> Path:
"""生成缓存文件路径"""
# 统一日期格式为 YYYYMMDD
sd = start_date.replace("-", "")
ed = end_date.replace("-", "")
safe_code = code.replace(".", "_")
return self.cache_dir / f"{safe_code}_{sd}_{ed}.csv"
def get(self, code: str, start_date: str, end_date: str) -> Optional[pd.DataFrame]:
"""
从缓存读取数据
Returns:
DataFrame or None缓存不存在
"""
cache_path = self._get_cache_path(code, start_date, end_date)
if cache_path.exists():
df = pd.read_csv(cache_path)
df["date"] = pd.to_datetime(df["date"])
return df
return None
def set(self, code: str, start_date: str, end_date: str, df: pd.DataFrame) -> None:
"""保存数据到缓存"""
cache_path = self._get_cache_path(code, start_date, end_date)
df.to_csv(cache_path, index=False)
def clear(self, code: str = None) -> None:
"""清除缓存"""
if code:
# 清除指定代码的缓存
for f in self.cache_dir.glob(f"{code.replace('.', '_')}*.csv"):
f.unlink()
else:
# 清除所有缓存
for f in self.cache_dir.glob("*.csv"):
f.unlink()