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()