问答交流

【代码报错】TypeError: Argument 'bar_count' has incorrect type (expected int, got float)

由liyajuan57创建,最终由small_q 被浏览 10 用户

71 # 确定止损位置

 72 stoploss_line = highest_price_since_buy - highest_price_since_buy \* 0.07

File /var/app/enabled/bigtrader2/bigtrader/protocol.py:328, in BarDatas.history(self, assets, fields, bar_count, frequency, expect_ndarray)

325 else:

326     curr_dt = self.__current_dt

--> 328 return self.__data_engine.history_data(assets, fields, bar_count, frequency, curr_dt,

329                                        expect_ndarray=expect_ndarray)

==TypeError: Argument 'bar_count' has incorrect type (expected int, got float)==


出错部分代码:

     #----------------------------------------止盈模块START----------------------------------------#
    # 获取当前持有的所有股票
    current_hold_instruments = set(context.get_account_positions().keys())
    print(current_hold_instruments)
    # 新建当日止损股票列表是为了handle_data 策略逻辑部分不再对该股票进行判断
    stoploss_stock = [] 
    if len(current_hold_instruments) > 0:
        for i in current_hold_instruments:
#             stock_market_price = data.current(context.symbol(i), 'price')  # 最新市场价格
            stock_market_price = context.get_position(i).last_price
#             last_sale_date = current_hold_instruments[i].last_sale_date   # 上次交易日期
            last_sale_date = context.get_position(i).last_sale_date
            delta_days = data.current_dt - last_sale_date  
            hold_days = delta_days.days # 持仓天数
            
            print(last_sale_date)
            print(data.current_dt)
            print(hold_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.07
            #record('止损位置', stoploss_line)
            # 如果价格下穿止损位置
            if stock_market_price < stoploss_line:
                context.order_target_percent(context.symbol(i), 0)     
                stoploss_stock.append(i)
                # 开盘卖出后所得资金可用来买入股票,更新当日可用现金
#               cash_for_buy += current_hold_instruments[i].amount * current_hold_instruments[i].last_sale_price
                cash_for_buy += context.get_position(i).amount * context.get_position(i).last_sale_price
                print('买入资金:',cash_for_buy)
        if len(stoploss_stock)>0:
            print('日期:', current_date, '股票:', stoploss_stock, '出现跟踪止损状况')

打印结果发现:print(last_sale_date)中有空值==NaT==,哪位大神帮解决一下?

  • ==2024-10-16 00:00:00==
  • ==2024-10-21 15:00:00==
  • ==5==
  • 买入资金: 62792.18000000033
  • 日期: 2024-10-21 股票: ['603016.SH'] 出现跟踪止损状况
  • {'002583.SZ', '603038.SH', '605086.SH', '600622.SH', '600336.SH', '002456.SZ'}
  • 2024-10-10 00:00:00
  • 2024-10-22 15:00:00
  • 12
  • ==NaT==
  • ==2024-10-22 15:00:00==
  • ==nan==
  • ERROR:STRAT:strategy strategy exception:Traceback (most recent call last):

\

评论
  • 发现3.0 BigTrade 的一个BUG,没有考虑取消无法成交的订单,出错的地方均在第二天无法成交的一字板。老版本有,3.0版没有此选项
  • 能分享一下具体代码吗,这个文字不好复现
{link}