prevent_future_data_leak

This commit is contained in:
zhangkun9038@dingtalk.com 2025-08-19 01:19:15 +08:00
parent 9b5f279603
commit 9c34c92fde
3 changed files with 149 additions and 0 deletions

View File

@ -38,6 +38,8 @@ class FreqaiPrimer(IStrategy):
基于 FreqAI 的动态阈值交易策略集成动态加仓减仓和自定义 ROI 逻辑兼容最新 Freqtrade 版本
"""
INTERFACE_VERSION = 3 # 使用最新策略接口版本
# --- 🧪 固定配置参数从hyperopt优化结果获取---
TRAILING_STOP_START = 0.016
TRAILING_STOP_DISTANCE = 0.015

73
simple_strategy_check.py Normal file
View File

@ -0,0 +1,73 @@
#!/usr/bin/env python3
"""
简单策略检查 - 不依赖freqtrade库
"""
import ast
import os
def check_strategy_structure():
"""检查策略文件结构"""
strategy_file = '/Users/zhangkun/myTestFreqAI/freqtrade/templates/freqaiprimer.py'
try:
with open(strategy_file, 'r') as f:
content = f.read()
# 解析Python代码
tree = ast.parse(content)
# 检查必需元素
checks = {
'INTERFACE_VERSION': False,
'populate_indicators': False,
'populate_entry_trend': False,
'populate_exit_trend': False,
'metadata参数': False,
'FreqaiPrimer类': False
}
# 遍历AST
for node in ast.walk(tree):
if isinstance(node, ast.Assign):
# 检查INTERFACE_VERSION
for target in node.targets:
if isinstance(target, ast.Name) and target.id == 'INTERFACE_VERSION':
checks['INTERFACE_VERSION'] = True
elif isinstance(node, ast.ClassDef):
if node.name == 'FreqaiPrimer':
checks['FreqaiPrimer类'] = True
elif isinstance(node, ast.FunctionDef):
func_name = node.name
if func_name in ['populate_indicators', 'populate_entry_trend', 'populate_exit_trend']:
checks[func_name] = True
# 检查metadata参数
for arg in node.args.args:
if arg.arg == 'metadata':
checks['metadata参数'] = True
break
print("策略结构检查结果:")
print("=" * 30)
for key, value in checks.items():
status = "" if value else ""
print(f"{status} {key}: {value}")
# 检查总体状态
all_passed = all(checks.values())
print("\n" + "=" * 30)
if all_passed:
print("✅ 策略结构完整符合Freqtrade要求")
else:
print("❌ 策略结构存在问题")
return all_passed
except Exception as e:
print(f"❌ 检查失败: {e}")
return False
if __name__ == "__main__":
check_strategy_structure()

74
test_strategy_load.py Normal file
View File

@ -0,0 +1,74 @@
#!/usr/bin/env python3
"""
测试策略文件是否可以正常加载
"""
import sys
import os
# 添加freqtrade到Python路径
sys.path.insert(0, '/Users/zhangkun/myTestFreqAI')
from freqtrade.resolvers.strategy_resolver import StrategyResolver
from freqtrade.configuration import Configuration
def test_strategy_loading():
"""测试策略加载"""
try:
# 创建最小配置
config = {
'strategy': 'freqaiprimer',
'strategy_path': '/Users/zhangkun/myTestFreqAI/freqtrade/templates',
'stake_currency': 'USDT',
'stake_amount': 10,
'exchange': {
'name': 'binance',
'key': '',
'secret': '',
'ccxt_config': {},
'ccxt_async_config': {}
},
'pairlists': [{'method': 'StaticPairList'}],
'datadir': '/Users/zhangkun/myTestFreqAI/user_data/data',
'timeframe': '3m',
'freqai': {
'enabled': True,
'identifier': 'test',
'feature_parameters': {
'include_timeframes': ['3m'],
'label_period_candles': 12,
'include_shifted_candles': 3,
}
}
}
# 尝试加载策略
strategy = StrategyResolver.load_strategy(config)
print("✅ 策略加载成功!")
print(f"策略名称: {strategy.__class__.__name__}")
print(f"接口版本: {getattr(strategy, 'INTERFACE_VERSION', '未设置')}")
# 检查必需的方法
methods_to_check = [
'populate_indicators',
'populate_entry_trend',
'populate_exit_trend',
'feature_engineering_expand_all',
'set_freqai_targets'
]
for method in methods_to_check:
if hasattr(strategy, method):
print(f"✅ 找到方法: {method}")
else:
print(f"❌ 缺少方法: {method}")
return True
except Exception as e:
print(f"❌ 策略加载失败: {e}")
import traceback
traceback.print_exc()
return False
if __name__ == "__main__":
test_strategy_loading()