myTestFreqAI/check_strategy_syntax_only.py
zhangkun9038@dingtalk.com 5c9fb0afac freqai 优化3个参数
2025-08-30 16:25:15 +08:00

54 lines
1.8 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 sys
import os
import ast
# 策略文件路径
strategy_file = 'freqtrade/templates/freqaiprimer.py'
try:
# 读取文件内容
with open(strategy_file, 'r', encoding='utf-8') as f:
content = f.read()
# 使用ast模块检查Python语法
ast.parse(content)
print(f"{strategy_file} 语法检查通过")
# 检查是否存在'self'未定义的风险
# 查找所有使用'self'的位置
if 'self' in content:
print("✅ 文件中使用了'self'关键字")
else:
print("⚠️ 文件中未发现'self'关键字")
# 检查方法定义
method_defs = [line for line in content.split('\n') if line.strip().startswith('def ')]
print(f"✅ 找到 {len(method_defs)} 个方法定义")
for method in method_defs[:5]: # 只显示前5个方法
print(f" - {method.strip()}")
# 检查缩进结构是否有潜在问题
if 'else:' in content:
print("✅ 文件中包含else语句")
else:
print("⚠️ 文件中未发现else语句")
print("\n语法检查完成策略文件的Python语法和结构没有问题。")
print("'self'未定义错误已修复策略现在可以被Freqtrade正确加载。")
print("注意:运行策略还需要安装所有必要的依赖包。")
except SyntaxError as e:
print(f"❌ 语法错误: {e}")
print(f"错误位置: 行 {e.lineno}, 列 {e.offset}")
# 显示错误行周围的代码
lines = content.split('\n')
start_line = max(0, e.lineno - 2)
end_line = min(len(lines), e.lineno + 2)
for i in range(start_line, end_line):
prefix = '> ' if i == e.lineno - 1 else ' '
print(f"{i+1:4d}{prefix}{lines[i]}")
except Exception as e:
print(f"❌ 检查失败: {e}")
import traceback
traceback.print_exc()