请问各位量化均线策略为什么这个一直显示benchmark not defined
import re
import talib
import pandas as pd
import numpy as np
import quartz_futures as qf
from quartz_futures.api import *
def initialize(context):
set_benchmark('000300.XSHG')
set_option('use_real_price',True)
log.info('初始函数开始运行且全局只运行一次')
set_order_cost(OrderCost(close_tax=0.001,open_commission=0.0003,close_commission=0.0003,min_commission=5),type='stock')
run_daily(before_market_open,time='before_open',reference_security='000300.XSHG')
run_daily(market_open,time='open',reference_security='000300.XSHG')
run_daily(after_market_close,time='after_close',reference_security='000300.XSHG')
def before_market_open(context):
log.info('函数运行时间(before_market_open):'+str(context.current_dt.time()))
g.security='000333.XSHE'
def market_open(context):
log.info('函数运行时间(market_open):'+str(context.current_dt.time()))
security=g.security
close_data=get_bars(security,count=5,unit='1d',fields=['close'])
MA5=close_data['close'].mean()
current_price=close_data['close'][-1]
cash=context.portfolio.available_cash
#如果上一时间点价格高出5日平均价1%,则全仓买入
if current_price > 1.01*MA5 and cash>0:
log.info("价格高于均价1%,买入%s"%(security))
print("当前可用资金为{0},position_value={0}".format(cash,context.portfolio.positions_value))
order_value(security,cash)
elif current_price < MA5 and context.portfolio.positions[security].closeable_amount>0:
log.info("价格低于均价,卖出%s"%(security))
order_target(security,0)
def after_market_close(context):
log.info(str('函数运行时间(after_market_close):'+str(context.current_dt.time())))
trades=get_trades()
for _trade in trades.values():
log.info('成交记录:'+str(_trade))
log.info('一天结束')
报错:
Exception Traceback (most recent call last)
in ()
77 accounts=accounts, preload_data=_QUARTZ_PRELOAD_DATA,
78 display=True, return_quartz_data=True,
---> 79 threaded=quartz_createVar.get('threaded', True), need_tracking=True)
80 _QUARTZ_CACHE['start'] = start
81 _QUARTZ_CACHE['end'] = end
/home/ipython/anaconda/lib/python2.7/site-packages/mercuryq-quartz-3.61.0-1619.egg/quartz/utils/tracking_utils.pyc in _decorator(*args, **kwargs)
/home/ipython/anaconda/lib/python2.7/site-packages/mercuryq-quartz-3.61.0-1619.egg/quartz/backtest.pyc in backtest(start, end, benchmark, universe, capital_base, initialize, handle_data, post_trading_day, commission, slippage, refresh_rate, freq, security_base, security_cost, max_history_window, accounts, threaded, display, preload_data, *args, **kwargs)
/home/ipython/anaconda/lib/python2.7/site-packages/mercuryq-quartz-3.61.0-1619.egg/quartz/trading_agent.pyc in from_config(cls, clock, sim_params, data_portal, strategy, account_manager, pms_lite, broker_client, report_client, market_roller, trading_scheduler, display, context)
/home/ipython/anaconda/lib/python2.7/site-packages/mercuryq-quartz-3.61.0-1619.egg/quartz/context/context.pyc in init(self, clock, sim_params, strategy, market_service, universe_service, asset_service, calendar_service, market_roller, account_manager)
Exception: Exception in "Context.init": global name 'set_benchmark' is not defined!