Files
crpto_predictor/scripts/generate_sample_data.py
T
crypto_predictorandClaude Sonnet 4.5 9bbdc8d42b Initial commit: BTC 1h prediction model (53.10% accuracy)
🎯 项目成果:
- 准确率: 53.10% (2026年测试集)
- 任务类型: 二分类 (预测涨/跌)
- 特征: OHLCV + RSI + MACD (9个特征)
- 训练数据: 2020-2025年BTC小时数据 (52,351序列)
- 测试数据: 2026年1-29日 (484序列)

📊 性能指标:
- 精确率: 51.79%
- 召回率: 49.36%
- F1分数: 0.5054
- 预测分布: 非常平衡 (偏差仅-11个)

🔬 实验历史:
- 尝试了5种方案 (回归/分类, 不同数据划分)
- 分类任务优于回归任务 (53.10% vs 49.5%)
- 全量训练避免了过拟合问题

📦 主要模型文件:
- models/checkpoints/dlinear_classification_2026/
  - best_model.pt (最佳模型)
  - preprocessor.pkl (预处理器)
  - test_2026_results.json (测试结果)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-29 21:27:15 +08:00

98 lines
2.6 KiB
Python

"""生成测试数据以便在无法连接Binance时使用"""
import pandas as pd
import numpy as np
from datetime import datetime, timedelta
from pathlib import Path
def generate_sample_data(
symbol: str = 'BTC',
timeframe: str = '1h',
num_days: int = 365
):
"""生成模拟的OHLCV数据用于测试
Args:
symbol: 币种符号
timeframe: 时间框架
num_days: 生成数据的天数
"""
# 计算数据点数量
if timeframe == '1h':
periods = num_days * 24
elif timeframe == '15m':
periods = num_days * 24 * 4
else:
periods = num_days * 24
# 生成时间戳
end_time = datetime.now()
start_time = end_time - timedelta(days=num_days)
if timeframe == '1h':
timestamps = pd.date_range(start=start_time, end=end_time, freq='h')
else: # 15m
timestamps = pd.date_range(start=start_time, end=end_time, freq='15min')
# 生成价格数据(随机游走)
base_price = {
'BTC': 45000,
'ETH': 2500,
'SOL': 100
}.get(symbol, 10000)
np.random.seed(42) # 固定种子以便复现
# 生成收益率
returns = np.random.normal(0.0001, 0.02, len(timestamps))
prices = base_price * np.exp(np.cumsum(returns))
# 生成OHLCV数据
data = []
for i, (ts, close) in enumerate(zip(timestamps, prices)):
# 生成高低价
volatility = close * 0.01
high = close + np.random.uniform(0, volatility)
low = close - np.random.uniform(0, volatility)
# 开盘价(前一个收盘价)
if i == 0:
open_price = close
else:
open_price = prices[i-1]
# 成交量
volume = np.random.uniform(1000, 10000)
data.append({
'timestamp': ts,
'open': open_price,
'high': high,
'low': low,
'close': close,
'volume': volume
})
df = pd.DataFrame(data)
# 保存到缓存
cache_dir = Path('./data/cache')
cache_dir.mkdir(parents=True, exist_ok=True)
cache_file = cache_dir / f"{symbol}_{timeframe}_1y_now.csv"
df.to_csv(cache_file, index=False)
print(f"✓ 已生成 {len(df)} 行模拟数据并保存到:{cache_file}")
return df
if __name__ == "__main__":
# 生成BTC、ETH、SOL的测试数据
for symbol in ['BTC', 'ETH', 'SOL']:
for timeframe in ['1h', '15m']:
print(f"\n生成 {symbol} {timeframe} 数据...")
generate_sample_data(symbol, timeframe)
print("\n✓ 所有测试数据已生成!")