【代码报错】AttributeError: 'StrategyContext' object has no attribute 'perf_tracker
由liyajuan57创建,最终由small_q 被浏览 12 用户
1.0策略升级3.0报错
\n**==AttributeError: 'StrategyContext' object has no attribute 'perf_tracker'==**
Output is truncated. View as a open in a text editor. Adjust cell output settings...
K线处理代码为:
回测引擎:每日数据处理函数,每天执行一次
def bigquant_run(context, data):
#获取当日日期
today = data.current_dt.strftime('%Y-%m-%d')
stock_hold_now = [equity.symbol for equity in context.portfolio.positions ]
#大盘风控模块,读取风控数据
current_day_data2 = context.data2[context.data2["date"] == today]
benckmark_risk = current_day_data2['market_risk_indicator'].iloc[0]
benckmark_risk=context.benckmark_risk[today]
context.symbol
#当risk为1时,市场有风险,全部平仓,不再执行其它操作
if benckmark_risk > 0:
for instrument in stock_hold_now:
context.order_target(symbol(instrument), 0)
print(today,'大盘风控止损触发,全仓卖出')
return
#------------------------------------------止损模块START--------------------------------------------
equities = {e.symbol: p for e, p in context.portfolio.positions.items() if p.amount>0}
# 新建当日止损股票列表是为了handle_data 策略逻辑部分不再对该股票进行判断
stoploss_stock = []
if len(equities) > 0:
for i in equities.keys():
stock_market_price = data.current(context.symbol(i), 'price') # 最新市场价格
last_sale_date = equities[i].last_sale_date # 上次交易日期
delta_days = data.current_dt - last_sale_date
hold_days = delta_days.days # 持仓天数
# 建仓以来的最高价
highest_price_since_buy = data.history(context.symbol(i), 'high', hold_days, '1d').max()
# 确定止损位置
stoploss_line = highest_price_since_buy - highest_price_since_buy * 0.05
#record('止损位置', stoploss_line)
# 如果价格下穿止损位置
if stock_market_price < stoploss_line:
context.order_target_percent(context.symbol(i), 0)
stoploss_stock.append(i)
if len(stoploss_stock)>0:
print('日期:', today, '股票:', stoploss_stock, '出现跟踪止损状况')
#-------------------------------------------止损模块END---------------------------------------------
# 按日期过滤得到今日的预测数据
ranker_prediction = context.ranker_prediction[
context.ranker_prediction.date == data.current_dt.strftime('%Y-%m-%d')]
ranker_prediction =context.data[context.data["date"] == data.current_dt.strftime("%Y-%m-%d")]
# 1. 资金分配
# 平均持仓时间是hold_days,每日都将买入股票,每日预期使用 1/hold_days 的资金
# 实际操作中,会存在一定的买入误差,所以在前hold_days天,等量使用资金;之后,尽量使用剩余资金(这里设置最多用等量的1.5倍)
is_staging = context.trading_day_index < context.options['hold_days'] # 是否在建仓期间(前 hold_days 天)
cash_avg = context.portfolio.portfolio_value / context.options['hold_days']
cash_for_buy = min(context.portfolio.cash, (1 if is_staging else 1.5) * cash_avg)
cash_for_sell = cash_avg - (context.portfolio.cash - cash_for_buy)
positions = {e.symbol: p.amount * p.last_sale_price
for e, p in context.perf_tracker.position_tracker.positions.items()}
sell_stock = []
# 2. 生成卖出订单:hold_days天之后才开始卖出;对持仓的股票,按机器学习算法预测的排序末位淘汰
if not is_staging and cash_for_sell > 0:
==equities = {e.symbol: e for e, p in context.perf_tracker.position_tracker.positions.items()}==
instruments = stock_hold_now
# print('rank order for sell %s' % instruments)
for instrument in instruments:
# 如果已经移动止损卖出过则不再轮仓卖出,以防止出现空头持仓
if instrument in stoploss_stock:
continue
context.order_target(context.symbol(instrument), 0)
cash_for_sell -= positions[instrument]
if cash_for_sell <= 0:
break
\