接入数据库

This commit is contained in:
crypto_predictor committed 2026-02-04 18:35:10 +08:00
1 parent 1bd9d25f60
commit 59fcb11ffa
9 files changed
+1560 -103

No files matched your search

+26 -42
View File
@@ -205,12 +205,13 @@ def main():
# 还原真实价格
nbeats_pred_prices = nbeats_preprocessor.inverse_transform_price(nbeats_pred_scaled)
# 【修改】获取当前K线的open价格(未来可交易价格)用于判断方向
# 第i个样本对应的是第168+i根K线,其open价格是真正可以交易的价格
current_open_prices = df_nbeats['open'].iloc[168:168+len(nbeats_pred_prices)].values
nbeats_preds = (nbeats_pred_prices > current_open_prices).astype(int) # 1=涨, 0=跌
# 【修复】使用当前K线的close价格判断涨跌(与标签定义一致)
# 第i个样本对应的是第168+i根K线,其close价格是当前收盘价
# 标签定义: label = (future_close > current_close)
current_close_prices = df_nbeats['close'].iloc[168:168+len(nbeats_pred_prices)].values
nbeats_preds = (nbeats_pred_prices > current_close_prices).astype(int) # 1=涨, 0=跌
print(f"✓ N-BEATS预测完成 (使用当前K线open价格判断)")
print(f"✓ N-BEATS预测完成 (使用当前K线close价格判断)")
print(f" 预测上涨: {(nbeats_preds==1).sum()} ({(nbeats_preds==1).sum()/len(nbeats_preds)*100:.1f}%)")
print(f" 预测下跌: {(nbeats_preds==0).sum()} ({(nbeats_preds==0).sum()/len(nbeats_preds)*100:.1f}%)")
@@ -223,7 +224,6 @@ def main():
# 从df_2026获取价格数据
all_prices = df_2026['close'].values
all_open_prices = df_2026['open'].values
# 为每个样本创建预测
for i in range(min_samples):
@@ -235,12 +235,12 @@ def main():
if end_idx > len(all_prices):
# 如果超出范围,使用最后512个价格
context_data = all_prices[-context_length:].reshape(1, -1, 1)
# 【修改】使用对应K线的open价格作为当前可交易价格
current_open_price = all_open_prices[min(168 + i, len(all_open_prices) - 1)]
# 【修复】使用对应K线的close价格作为当前收盘价
current_close_price = all_prices[min(168 + i, len(all_prices) - 1)]
else:
context_data = all_prices[start_idx:end_idx].reshape(1, -1, 1)
# 【修改】使用第end_idx根K线的open价格(即当前K线的开盘价)
current_open_price = all_open_prices[end_idx] if end_idx < len(all_open_prices) else all_open_prices[-1]
# 【修复】使用第168+i根K线的close价格(即当前K线的收盘价)
current_close_price = all_prices[168 + i] if 168 + i < len(all_prices) else all_prices[-1]
context_tensor = torch.FloatTensor(context_data).to(device)
@@ -249,12 +249,12 @@ def main():
# 预测第一个时间步的值
pred_price = output.prediction_outputs.cpu().numpy()[0, 0, 0]
# 【修改】判断方向: 预测价格 vs 当前K线open价格
ttm_preds_list.append(1 if pred_price > current_open_price else 0)
# 【修复】判断方向: 预测价格 vs 当前K线close价格(与标签定义一致)
ttm_preds_list.append(1 if pred_price > current_close_price else 0)
ttm_preds = np.array(ttm_preds_list)
print(f"✓ TTM-R2预测完成 (使用当前K线open价格判断)")
print(f"✓ TTM-R2预测完成 (使用当前K线close价格判断)")
print(f" 预测上涨: {(ttm_preds==1).sum()} ({(ttm_preds==1).sum()/len(ttm_preds)*100:.1f}%)")
print(f" 预测下跌: {(ttm_preds==0).sum()} ({(ttm_preds==0).sum()/len(ttm_preds)*100:.1f}%)")
@@ -358,38 +358,22 @@ def main():
print(f"\n集成策略性能:")
print(f" 双模型交集 (DLinear+NBEATS): {two_model_acc*100:.2f}% (覆盖率: {two_model_valid.sum()/len(dlinear_preds)*100:.1f}%)")
print(f" 三模型多数投票: {ensemble_accuracy*100:.2f}% (覆盖率: 100%)")
if len(valid_preds) > 0:
print(f" 三模型交集 (ALL): {ensemble_accuracy*100:.2f}% (覆盖率: {valid_mask.sum()/len(dlinear_preds)*100:.1f}%)")
# 性能评估
print("\n" + "="*80)
print("🎯 策略评估")
print("="*80)
# 性能评估
print("\n" + "="*80)
print("🎯 策略评估")
print("="*80)
best_single = max(dlinear_acc, nbeats_acc, ttm_acc)
best_single = max(dlinear_acc, nbeats_acc, ttm_acc)
if ensemble_accuracy > best_single:
improvement = ensemble_accuracy - best_single
print(f"\n ✅ 三模型交集优于所有单模型!")
print(f" 准确率提升: {improvement*100:.2f}个百分点")
print(f" 代价: 弃权{(~valid_mask).sum()}次预测 ({(~valid_mask).sum()/len(dlinear_preds)*100:.1f}%)")
elif ensemble_accuracy > two_model_acc:
improvement = ensemble_accuracy - two_model_acc
print(f"\n ✅ 三模型交集优于双模型交集!")
print(f" 准确率提升: {improvement*100:.2f}个百分点")
print(f" 但覆盖率下降: {(two_model_valid.sum() - valid_mask.sum())/len(dlinear_preds)*100:.1f}%")
else:
print(f"\n ⚠️ 三模型交集未能提升准确率")
print(f" 可能原因: TTM-R2表现太差({ttm_acc*100:.2f}%),拖累了整体性能")
print(f" 建议: 使用双模型交集(DLinear+NBEATS)")
# 实际收益分析
print(f"\n💰 实际收益分析:")
print(f" 如果只在三个模型都同意时交易:")
print(f" - 交易次数: {valid_mask.sum()} ({valid_mask.sum()/len(dlinear_preds)*100:.1f}%)")
print(f" - 准确率: {ensemble_accuracy*100:.2f}%")
print(f" - 避免交易: {(~valid_mask).sum()}次 (规避不确定性)")
if ensemble_accuracy > best_single:
improvement = ensemble_accuracy - best_single
print(f"\n ✅ 三模型多数投票优于所有单模型!")
print(f" 准确率提升: {improvement*100:.2f}个百分点")
else:
print(f"\n ⚠️ 三模型多数投票未能超越最佳单模型")
print(f" 可能原因: 某个模型表现太差,拖累整体性能")
print("\n" + "="*80)
print("✓ 三模型集成预测分析完成!")