问答交流

求助:'NoneType' object has no attribute 'get_value'

由bqwby4wg创建,最终由small_q 被浏览 42 用户

可视化策略{w:100}

代码列表:

{w:100}回测 HFTrade:

初始化函数:

# 交易引擎:初始化函数,只执行一次
def bigquant_run(context):
    # 加载预测数据
    print("initialize")  
    context.ins = context.instruments[0]#从传入参数中获取需要交易的合约
    context.order_num = 100#下单手数
    context.set_universe(context.ins)#设置需要处理的合约
    context.last_grid = 0 # 储存前一个网格所处区间,用来和最新网格所处区间作比较
    context.set_stock_t1(0)
    context.set_commission(PerOrder(buy_cost=0.0008, sell_cost=0.0008, min_cost=0))
    context.first = 0
   
   

盘前处理函数:

# 盘前处理函数:每个天开盘前调用一次
def bigquant_run(context, data):
    context.subscribe(context.ins) #注册合约
    # 记录上一次交易时网格范围的变化情况(例如从4区到5区,记为4,5)
    context.grid_change_last = [0,0]
    # 以前一日的收盘价为中枢价格
    context.center = data.history(context.ins,["close"],1,"1d").iat[0,0]

k线处理函数:

#K线处理函数:每个bar调用一次,该策略1分钟为1bar
def bigquant_run(context, data):
    cur_date =  data.current_dt
    cur_hm = cur_date.strftime('%H:%M') #time  
    
    # 分别获取持仓
    position = context.get_position(context.ins)
    # 获取当前价格
    price = data.current(context.ins, "close")
    
    #以50%仓位开仓
    if context.first == 0:
         rv = context.order(context.ins, context.order_num*5, price, order_type=OrderType.MARKET)
         print('开仓50%')
         context.first = 1 
         return
    cash = context.portfolio.cash
    cash_num = context.order_num * price    
    # 设置网格和当前价格所处的网格区域
    band = np.arange(0.9, 1.011, 0.02)* context.center
    grid = pd.cut([price], band, labels=np.arange(1,len(band)))[0]
    # 如果新的价格所处网格区间和前一个价格所处的网格区间不同,说明触碰到了网格线,需要进行交易
    # 如果新网格大于前一天的网格,做空或平多
    if context.last_grid < grid:
        # 记录新旧格子范围(按照大小排序)
        grid_change_new = [context.last_grid,grid]
        # 几种例外:
        # 当last_grid = 0 时是初始阶段,不构成信号
        if context.last_grid == 0:
            context.last_grid = grid
            return
        if context.last_grid != 0:
            # 如果前一次开仓是4-5,这一次是5-4,算是没有突破,不成交
            if grid_change_new != context.grid_change_last:
                # 更新前一次的数据
                context.last_grid = grid
                context.grid_change_last = grid_change_new
                # 如果有仓位,卖出1份
                if position.current_qty != 0:
                    rv = context.order(context.ins,  context.order_num*(-1), order_type=OrderType.MARKET) 
    # 如果新网格小于前一天的网格,开仓
    if context.last_grid > grid:
        # 记录新旧格子范围(按照大小排序)
        grid_change_new = [grid,context.last_grid]
        # 几种例外:
        # 当last_grid = 0 时是初始阶段,不构成信号
        if context.last_grid == 0:
            context.last_grid = grid
            return
        if context.last_grid != 0:
            # 如果前一次开仓是4-5,这一次是5-4,算是没有突破,不成交
            if grid_change_new != context.grid_change_last:
                # 更新前一次的数据
                context.last_grid = grid
                context.grid_change_last = grid_change_new
                if cash > cash_num+1000:
                    rv = context.order(context.ins, context.order_num, order_type=OrderType.MARKET)
    # 设计一个条件:当持仓量达到10手,卖出部分
    if context.get_position(context.ins).current_qty >= 10*context.order_num:
        rv = context.order(context.ins, context.order_num*(-3), order_type=OrderType.MARKET)
        return
    # 设计一个条件:当持仓量为0,买入部分    
    if(context.get_position(context.ins).current_qty == 0):
        rv = context.order(context.ins, context.order_num*(3), order_type=OrderType.MARKET)

该策略在上个月还可以正常运行,过了一个月再打开就报错了。没能看出报错的原因。

报错内容:

initialize 2023-01-31 22:11:20.591563 strategy strategy exception:Traceback (most recent call last): File "bigtrader/strategy/engine.py", line 713, in bigtrader2.bigtrader.strategy.engine.StrategyEngine._call_strategy_func File "bigtrader/strategy/strategy_base.py", line 2281, in bigtrader2.bigtrader.strategy.strategy_base.StrategyBase.call_handle_data File "<ipython-input-1-252a29e0fbb3>", line 40, in m2_handle_data_bigquant_run price = data.current(context.ins, "close") File "/var/app/enabled/bigtrader2/bigtrader/protocol.py", line 285, in current return self.__data_engine.get_current_value(asset, curr_dt, fields) File "bigtrader/mdata/engine.py", line 700, in bigtrader2.bigtrader.mdata.engine.DataEngine._get_minute_value_from_df AttributeError: 'NoneType' object has no attribute 'get_value'


AttributeError Traceback (most recent call last) <ipython-input-1-252a29e0fbb3> in <module> 123 ) 124 --> 125 m2 = M.hftrade.v2( 126 instruments=m3.data, 127 start_date='',

<ipython-input-1-252a29e0fbb3> in m2_handle_data_bigquant_run(context, data) 38 position = context.get_position(context.ins) 39 # 获取当前价格 ---> 40 price = data.current(context.ins, "close") 41 42 #以50%仓位开仓

AttributeError: 'NoneType' object has no attribute 'get_value'


问题可能出在data.current 这个函数,求助工程师大佬如何解决

评论
  • 策略代码分享看看
  • 策略代码分享看看
  • [https://bigquant.com/experimentshare/6333e26fed414893ba58e38352567e72](https://bigquant.com/experimentshare/6333e26fed414893ba58e38352567e72) \
  • 分钟回测需要在盘前处理函数注册一下,参照: ![{w:100}](/wiki/api/attachments.redirect?id=d8be10fb-d1f4-4238-a889-e4f52aab21c5) [https://bigquant.com/experimentshare/7fcdc387e5ba4035ab902824d9068736](https://bigquant.com/experimentshare/7fcdc387e5ba4035ab902824d9068736) \