问答交流

频内负现金bug复现

由bqnhvhmb创建,最终由small_q 被浏览 21 用户

# 回测引擎:初始化函数,只执行一次
def m2_initialize_bigquant_run(context):
    context.myIns = ['000897.SZA', '600208.SHA', '600533.SHA', '000926.SZA', '601668.SHA', '600854.SHA', '600228.SHA', '600383.SHA', '300988.SZA', '603858.SHA', '300939.SZA']
# 交易引擎:每日盘前触发一次。
def m2_before_trading_start_bigquant_run(context, data):
    context.subscribe(context.myIns)
# 交易引擎:bar数据处理函数,每个时间单位执行一次
def m2_handle_data_bigquant_run(context, data):
    today = data.current_dt.strftime('%Y-%m-%d')
    if today == '2022-06-01':
        myAcc = context.get_trading_account()
        for buy in context.myIns:
            copen = data.current(buy, 'open')
            tmpCash = myAcc.available
            frzCash = myAcc.frozen_cash
            print('{}买{}之前已冻结{}元,剩余现金{}元'.format(today, buy, frzCash, tmpCash))
            if tmpCash > 200 * copen + 5:
                context.order(
                    buy, 
                    200, 
                    limit_price=copen,
                    order_type=OrderType.LIMIT
                )
m1 = M.instruments.v2(
    start_date='2022-06-01',
    end_date='2022-06-06',
    market='CN_STOCK_A',
    instrument_list='',
    max_count=0
)
m2 = M.hftrade.v2(
    start_date='', end_date='', instruments=m1.data,
    initialize=m2_initialize_bigquant_run,
    before_trading_start=m2_before_trading_start_bigquant_run,
    handle_tick=None,
    handle_data=m2_handle_data_bigquant_run,
    handle_trade=None,
    handle_order=None,
    after_trading=None,
    options_data=None,
    history_ds=None,
    benchmark_ds=None,
    capital_base=10000,
    frequency='daily',
    price_type='真实价格',
    product_type='股票',
    before_start_days='0',
    volume_limit=1,
    order_price_field_buy='open',
    order_price_field_sell='open',
    benchmark='000300.HIX',
    plot_charts=True,
    disable_cache=True,
    replay_bdb=False,
    show_debug_info=False,
    backtest_only=True
)

:::warning 这样运行后就可以看到:在m2_handle_data_bigquant_run()回调函数的内部,context.get_trading_account()的StockTradingAccount内部属性值 是不会更新了,只有到了新的bar才会更新……

:::

:::tip 但是正常来说,下单之后不就应该 即时冻结现金嘛?

:::

:::info Output of m2

HFTrade backtest_only mode! 2022-06-01买000897.SZA之前已冻结0.0元,剩余现金10000元 2022-06-01买600208.SHA之前已冻结0.0元,剩余现金10000元 2022-06-01买600533.SHA之前已冻结0.0元,剩余现金10000元 2022-06-01买000926.SZA之前已冻结0.0元,剩余现金10000元 2022-06-01买601668.SHA之前已冻结0.0元,剩余现金10000元 2022-06-01买600854.SHA之前已冻结0.0元,剩余现金10000元 2022-06-01买600228.SHA之前已冻结0.0元,剩余现金10000元 2022-06-01买600383.SHA之前已冻结0.0元,剩余现金10000元 2022-06-01买300988.SZA之前已冻结0.0元,剩余现金10000元 2022-06-01买603858.SHA之前已冻结0.0元,剩余现金10000元 2022-06-01买300939.SZA之前已冻结0.0元,剩余现金10000元

:::

\

标签

量化交易策略算法交易数据分析金融工程
评论
  • 当前bar的数据都是静态的,在frequency='daily'改成tick级别的可以尝试一下
{link}