在策略回测中,如何查看中间变量的值?

策略分享
回测引擎
record
标签: #<Tag:0x00007f73e135d1b0> #<Tag:0x00007f73e135d070> #<Tag:0x00007f73e135cf30>

(iQuant) #1

之所以需要查看中间变量的数值是因为我们有时在编写策略、策略调试中需要检查中间变量的数值是否正确,具体方法见下:

克隆策略

方法一:通过print查询

In [9]:
def initialize(context):
    pass
def handle_data(context, data):
    stock_1 = '000002.SZA'
    stock_2 =  '600519.SHA'
    wanke_price = data.current(context.symbol(stock_1), 'price')
    gzmt_price = data.current(context.symbol(stock_2) , 'price')
    print('万科价格:', wanke_price)
    print('贵州茅台价格:', gzmt_price)
m6 = M.trade.v2(
    instruments=['000002.SZA', '600519.SHA'],
    start_date='2017-07-15',
    end_date='2017-08-01',
    initialize=initialize,
    handle_data=handle_data,
    plot_charts=False,     
)   
[2017-08-14 21:19:55.246594] INFO: bigquant: backtest.v7 start ..
万科价格: nan
贵州茅台价格: 3200.39
万科价格: 3359.39648438
贵州茅台价格: 3218.02
万科价格: 3338.15136719
贵州茅台价格: 3248.67
万科价格: 3366.03564453
贵州茅台价格: 3317.0
万科价格: 3290.34960938
贵州茅台价格: 3364.86
万科价格: 3160.22290039
贵州茅台价格: 3397.57
万科价格: 3088.52026367
贵州茅台价格: 3398.71
万科价格: 3109.76538086
贵州茅台价格: 3353.7
万科价格: 3053.99682617
贵州茅台价格: 3369.62
万科价格: 3137.64990234
贵州茅台价格: 3441.09
万科价格: 3103.12646484
贵州茅台价格: 3421.89
万科价格: 3109.76538086
贵州茅台价格: 3414.92
[2017-08-14 21:19:55.638006] INFO: Performance: Simulated 12 trading days out of 12.
[2017-08-14 21:19:55.639962] INFO: Performance: first open: 2017-07-17 13:30:00+00:00
[2017-08-14 21:19:55.641097] INFO: Performance: last close: 2017-08-01 19:00:00+00:00
[2017-08-14 21:19:55.678226] INFO: bigquant: backtest.v7 end [0.431627s].

方法二:通过record函数记录下来,回测完成后在回测结果中查询

In [10]:
def initialize(context):
    pass
def handle_data(context, data):
    stock_1 = '000002.SZA'
    stock_2 =  '600519.SHA'
    # 通过record函数记录下来,以便回测结束以后可以查询
    record(wanke=data.current(context.symbol(stock_1), 'price'), gzmt=data.current(context.symbol(stock_2) , 'price'))

m6 = M.trade.v2(
    instruments=['000002.SZA', '600519.SHA'],
    start_date='2017-07-15',
    end_date='2017-08-01',
    initialize=initialize,
    handle_data=handle_data,
    plot_charts=False,     
) 
# 回测结果中进行查询
m6.raw_perf.read_df()[['wanke', 'gzmt']]
[2017-08-14 21:20:20.428582] INFO: bigquant: backtest.v7 start ..
[2017-08-14 21:20:20.704574] INFO: Performance: Simulated 12 trading days out of 12.
[2017-08-14 21:20:20.705708] INFO: Performance: first open: 2017-07-17 13:30:00+00:00
[2017-08-14 21:20:20.706577] INFO: Performance: last close: 2017-08-01 19:00:00+00:00
[2017-08-14 21:20:20.743551] INFO: bigquant: backtest.v7 end [0.314969s].
Out[10]:
wanke gzmt
2017-07-17 19:00:00+00:00 NaN 3200.385742
2017-07-18 19:00:00+00:00 3359.396484 3218.020508
2017-07-19 19:00:00+00:00 3338.151367 3248.668457
2017-07-20 19:00:00+00:00 3366.035645 3317.003906
2017-07-21 19:00:00+00:00 3290.349609 3364.859863
2017-07-24 19:00:00+00:00 3160.222900 3397.569824
2017-07-25 19:00:00+00:00 3088.520264 3398.707520
2017-07-26 19:00:00+00:00 3109.765381 3353.695801
2017-07-27 19:00:00+00:00 3053.996826 3369.624268
2017-07-28 19:00:00+00:00 3137.649902 3441.088379
2017-07-31 19:00:00+00:00 3103.126465 3421.888916
2017-08-01 19:00:00+00:00 3109.765381 3414.920410