变量 'global_max_return' 在赋值前被调用

用户头像zho****g4509
2024-09-23 发布

import talib
import datetime

全局变量用于保存最大利润率,初始值设为一个极小值

global_max_return = -float('inf')

def init(context):

‘’‘’‘’‘’

profit_rate = context.portfolio.future_account.positions[context.ins].profit_rate # 利润率
buy_realized_pnl = context.portfolio.future_account.positions[context.ins].buy_realized_pnl # 已实现的买入利润和损失
last_price = context.portfolio.future_account.positions[context.ins].last_price # 最新价格
holding_pnl = context.portfolio.future_account.positions[context.ins].holding_pnl # 持仓利润和损失

更新全局最大利润率

if profit_rate >= global_max_return:
global_max_return = profit_rate
print('最大利润率',global_max_return)

为什么还是第118行,变量 'global_max_return' 在赋值前被调用,请检查该变量的调用顺序。

评论

用户头像
2024-09-25 10:23:03

python语法问题,建议直接使用g存储全局变量

评论

需要帮助?

试试AI小助手吧