问答交流

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

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

TypeError Traceback (most recent call last)

Cell In[1], line 466

454 m9 = M.extract_data_dai.v17(

455     sql=m8.data,

456     start_date="""2021-01-01""",

(...)

462     m_name="""m9"""

463 )

465 # @module(position="51.193634033203125,299", comment="""""", comment_collapsed=True)

--> 466 m6 = M.bigtrader.v33(

467     data=m5.predictions,

468     options_data=m9.data,

469     start_date="""""",

470     end_date="""""",

471     initialize=m6_initialize_bigquant_run,

472     before_trading_start=m6_before_trading_start_bigquant_run,

473     handle_tick=m6_handle_tick_bigquant_run,

474     handle_data=m6_handle_data_bigquant_run,

475     handle_trade=m6_handle_trade_bigquant_run,

476     handle_order=m6_handle_order_bigquant_run,

477     after_trading=m6_after_trading_bigquant_run,

478     capital_base=100000,

479     frequency="""daily""",

480     product_type="""股票""",

481     rebalance_period_type="""交易日""",

482     rebalance_period_days="""10""",

483     rebalance_period_roll_forward=True,

484     backtest_engine_mode="""标准模式""",

485     before_start_days=0,

486     volume_limit=0,

487     order_price_field_buy="""open""",

488     order_price_field_sell="""close""",

489     benchmark="""沪深300指数""",

490     plot_charts=True,

491     debug=False,

492     backtest_only=False,

493     m_name="""m6"""

494 )

495 # </aistudiograph>

File /opt/pyenv/versions/3.11.8/lib/python3.11/site-packages/bigmodule/modules.py:28, in call(self, **kwargs)

File /opt/pyenv/versions/3.11.8/lib/python3.11/site-packages/bigmodule/moduleinvoker.py:203, in module_invoke(name, version, kwargs)

File /opt/pyenv/versions/3.11.8/lib/python3.11/site-packages/bigmodule/moduleinvoker.py:169, in _module_invoke(name, version, kwargs)

File /opt/pyenv/versions/3.11.8/lib/python3.11/site-packages/bigmodule/moduleinvoker.py:41, in _module_run(module, kwargs)

File dist/build/bigtrader/v33/init.py:389, in v33.run()

File dist/build/bigtrader/v33/init.py:193, in v33._run()

File dist/build/bigtrader/v33/init.py:181, in v33._run_backtest()

File dist/build/bigtrader/v33/core/pybacktest/init.py:446, in v33.core.pybacktest.BigQuantModule.run()

File dist/build/bigtrader/v33/core/pybacktest/init.py:389, in v33.core.pybacktest.BigQuantModule.run_algo()

File /var/app/enabled/bigtrader2/bigtrader/run_trading.py:634, in run_backtest(start_date, end_date, strategy, strategy_setting, capital_base, frequency, market, product_type, instruments, options_data, **kwargs)

620     capital_base = kwargs.pop("capital")

622 rt = RunTrading(RunMode.BACKTEST,

623                 strategy=strategy,

624                 strategy_setting=strategy_setting,

(...)

632                 options_data=options_data,

633                 \*\*kwargs)

--> 634 return rt.run(**kwargs)

File /var/app/enabled/bigtrader2/bigtrader/run_trading.py:394, in RunTrading.run(self, **kwargs)

391         return

393 if self.trading_env.run_mode == RunMode.BACKTEST:

--> 394 return self._run_backtest(**kwargs)

395 else:

396     if not self.trading_env.account_id:

File /var/app/enabled/bigtrader2/bigtrader/run_trading.py:421, in RunTrading._run_backtest(self, **kwargs)

419     debug_print("run_backtest: running...")

420 t0 = time.time()

--> 421 bkt_engine.run()

422 cost_time = round(time.time() - t0, 3)

423 if show_debug_info:

File bigtrader/strategy/backtest_engine.py:259, in bigtrader2.bigtrader.strategy.backtest_engine.BacktestEngine.run()

File bigtrader/strategy/backtest_engine.py:512, in bigtrader2.bigtrader.strategy.backtest_engine.BacktestEngine.transform()

File bigtrader/strategy/backtest_engine.py:474, in bigtrader2.bigtrader.strategy.backtest_engine.BacktestEngine.transform.replay_bars_dt()

...

 85 # 确定止损位置

 86 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)==

Output is truncated. View as a open in a text editor. Adjust cell output settings...


出错部分代码:

#----------------------------------------止盈模块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 # 持仓天数
        # 建仓以来的最高价
        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, '出现跟踪止损状况')

\

评论
  • highest_price_since_buy = data.history(context.symbol(i), 'high', hold_days, '1d').max()中history函数部分出错:
  • TypeError: Argument 'bar_count' has incorrect type (expected int, got float)
  • hold_days传入一个int
  • 试一下hold_days.astype(‘int’)
  • 打印了一下,时间有空值NaT。
  • {'000761.SZ', '600696.SH', '002583.SZ'}
  • 2024-09-27 00:00:00
  • 2024-09-27 15:00:00
  • 0
  • NaT
  • 2024-09-27 15:00:00
  • nan
{link}