如何查看中间变量?

策略分享
标签: #<Tag:0x00007fcf66a7e378>

(小Q) #1
克隆策略

金叉死叉策略

当短期均线上穿长期均线,出现金叉,买入

当短期均线下穿长期均线,出现死叉,卖出

1. 主要参数

In [3]:
# 股票选择
instruments = ['600519.SHA']  
# 开始时间
start_date = '2017-05-29'  
# 结束时间
end_date = '2017-11-08'

2. 策略回测主体

In [4]:
# 初始化账户
def initialize(context):
    context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5)) # 设置手续费,买入成本为万分之三,卖出为千分之1.3
    context.short_period = 5 # 短期均线
    context.long_period = 50 # 长期均线 

def handle_data(context, data):
    
    # 长期均线值要有意义,需要在50根k线之后
    if context.trading_day_index <  context.long_period:   
        return
    
    k = instruments[0] # 标的为字符串格式
    sid = context.symbol(k) # 将标的转化为equity格式
    price = data.current(sid, 'price') # 最新价格
 
    short_mavg = data.history(sid, 'price',context.short_period, '1d').mean() # 短期均线值
    global long_mavg
    long_mavg = data.history(sid, 'price',context.long_period, '1d').mean() # 长期均线值
    print('long_mavg: ',long_mavg)
    record(long_mavg=long_mavg)
    cash = context.portfolio.cash  # 现金
    cur_position = context.portfolio.positions[sid].amount # 持仓
    
    # 交易逻辑
    # 如果短期均线大于长期均线形成金叉,并且没有持仓,并且该股票可以交易
    if short_mavg > long_mavg and cur_position == 0 and data.can_trade(sid):  
        context.order(sid, int(cash/price/100)*100) # 买入
    # 如果短期均线小于长期均线形成死叉,并且有持仓,并且该股票可以交易
    elif short_mavg < long_mavg and cur_position > 0 and data.can_trade(sid):  
        context.order_target_percent(sid, 0) # 全部卖出
        
    
     

3.回测接口

In [5]:
m=M.trade.v3(
    instruments=instruments,
    start_date=start_date,
    end_date=end_date,
    initialize=initialize,
    handle_data=handle_data,
    order_price_field_buy='open', # 以开盘价买入
    order_price_field_sell='open', # 以开盘价卖出
    capital_base=1000000, # 本金
    )
[2018-04-16 13:36:15.124215] INFO: bigquant: backtest.v7 开始运行..
[2018-04-16 13:36:15.352994] INFO: algo: set price type:backward_adjusted
/var/app/enabled/empyrical/stats.py:534: RuntimeWarning: invalid value encountered in double_scalars
  sortino = mu / dsr
/var/app/enabled/empyrical/stats.py:534: RuntimeWarning: divide by zero encountered in double_scalars
  sortino = mu / dsr
long_mavg:  3297.50748
long_mavg:  3304.48184
long_mavg:  3310.6556200000005
long_mavg:  3319.4784400000003
long_mavg:  3327.1344799999997
long_mavg:  3333.38642
long_mavg:  3338.4058600000003
long_mavg:  3342.7925800000003
long_mavg:  3346.19038
long_mavg:  3349.208800000001
long_mavg:  3353.0037400000006
long_mavg:  3358.0416400000004
long_mavg:  3363.3391
long_mavg:  3367.445100000001
long_mavg:  3371.72352
long_mavg:  3375.6059400000004
long_mavg:  3379.2239
long_mavg:  3383.41782
long_mavg:  3385.5450199999996
long_mavg:  3387.4078799999993
long_mavg:  3389.79112
long_mavg:  3391.967559999999
long_mavg:  3393.325219999999
long_mavg:  3396.8529799999997
long_mavg:  3402.5017799999996
long_mavg:  3407.9560599999995
long_mavg:  3413.86092
long_mavg:  3420.1
long_mavg:  3426.4286799999995
long_mavg:  3431.2384799999995
long_mavg:  3436.7465599999996
long_mavg:  3442.80644
long_mavg:  3448.99572
long_mavg:  3457.1262799999995
long_mavg:  3465.19568
long_mavg:  3472.498539999999
long_mavg:  3479.892419999999
long_mavg:  3486.2125599999995
long_mavg:  3493.319159999999
long_mavg:  3502.1508399999984
long_mavg:  3512.662119999999
long_mavg:  3522.648619999999
long_mavg:  3532.9209799999994
long_mavg:  3544.2229799999996
long_mavg:  3555.1011799999997
long_mavg:  3566.1770599999995
long_mavg:  3581.1638999999996
long_mavg:  3595.48374
long_mavg:  3608.5463799999998
long_mavg:  3620.5992800000004
long_mavg:  3631.2399600000003
long_mavg:  3647.3759200000004
long_mavg:  3670.9228200000007
long_mavg:  3688.308880000001
long_mavg:  3705.667920000001
long_mavg:  3723.66124
long_mavg:  3742.75104
long_mavg:  3764.01538
long_mavg:  3787.24372
long_mavg:  3809.26606
long_mavg:  3831.5045599999994
[2018-04-16 13:36:17.066558] INFO: Performance: Simulated 111 trading days out of 111.
[2018-04-16 13:36:17.067653] INFO: Performance: first open: 2017-05-31 01:30:00+00:00
[2018-04-16 13:36:17.068583] INFO: Performance: last close: 2017-11-08 07:00:00+00:00
/var/app/enabled/pandas/core/generic.py:1138: PerformanceWarning: 
your performance may suffer as PyTables will pickle object types that it cannot
map directly to c-types [inferred_type->mixed,key->block4_values] [items->['LOG', 'POS_FAC', 'TRA_FAC', 'orders', 'period_label', 'positions', 'transactions']]

  return pytables.to_hdf(path_or_buf, key, self, **kwargs)
/var/app/enabled/pandas/core/indexing.py:141: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  self._setitem_with_indexer(indexer, value)
  • 收益率22.63%
  • 年化收益率58.91%
  • 基准收益率16.31%
  • 阿尔法0.44
  • 贝塔0.28
  • 夏普比率3.49
  • 胜率--
  • 盈亏比--
  • 收益波动率15.63%
  • 信息比率1.08
  • 最大回撤3.67%
[2018-04-16 13:36:17.709418] INFO: bigquant: backtest.v7 运行完成[2.585208s].
In [7]:
print(long_mavg)
3831.5045599999994
In [6]:
m.raw_perf.read_df()['long_mavg']
Out[6]:
2017-05-31 07:00:00+00:00           NaN
2017-06-01 07:00:00+00:00           NaN
2017-06-02 07:00:00+00:00           NaN
2017-06-05 07:00:00+00:00           NaN
2017-06-06 07:00:00+00:00           NaN
2017-06-07 07:00:00+00:00           NaN
2017-06-08 07:00:00+00:00           NaN
2017-06-09 07:00:00+00:00           NaN
2017-06-12 07:00:00+00:00           NaN
2017-06-13 07:00:00+00:00           NaN
2017-06-14 07:00:00+00:00           NaN
2017-06-15 07:00:00+00:00           NaN
2017-06-16 07:00:00+00:00           NaN
2017-06-19 07:00:00+00:00           NaN
2017-06-20 07:00:00+00:00           NaN
2017-06-21 07:00:00+00:00           NaN
2017-06-22 07:00:00+00:00           NaN
2017-06-23 07:00:00+00:00           NaN
2017-06-26 07:00:00+00:00           NaN
2017-06-27 07:00:00+00:00           NaN
2017-06-28 07:00:00+00:00           NaN
2017-06-29 07:00:00+00:00           NaN
2017-06-30 07:00:00+00:00           NaN
2017-07-03 07:00:00+00:00           NaN
2017-07-04 07:00:00+00:00           NaN
2017-07-05 07:00:00+00:00           NaN
2017-07-06 07:00:00+00:00           NaN
2017-07-07 07:00:00+00:00           NaN
2017-07-10 07:00:00+00:00           NaN
2017-07-11 07:00:00+00:00           NaN
                                ...    
2017-09-21 07:00:00+00:00    3442.80644
2017-09-22 07:00:00+00:00    3448.99572
2017-09-25 07:00:00+00:00    3457.12628
2017-09-26 07:00:00+00:00    3465.19568
2017-09-27 07:00:00+00:00    3472.49854
2017-09-28 07:00:00+00:00    3479.89242
2017-09-29 07:00:00+00:00    3486.21256
2017-10-09 07:00:00+00:00    3493.31916
2017-10-10 07:00:00+00:00    3502.15084
2017-10-11 07:00:00+00:00    3512.66212
2017-10-12 07:00:00+00:00    3522.64862
2017-10-13 07:00:00+00:00    3532.92098
2017-10-16 07:00:00+00:00    3544.22298
2017-10-17 07:00:00+00:00    3555.10118
2017-10-18 07:00:00+00:00    3566.17706
2017-10-19 07:00:00+00:00    3581.16390
2017-10-20 07:00:00+00:00    3595.48374
2017-10-23 07:00:00+00:00    3608.54638
2017-10-24 07:00:00+00:00    3620.59928
2017-10-25 07:00:00+00:00    3631.23996
2017-10-26 07:00:00+00:00    3647.37592
2017-10-27 07:00:00+00:00    3670.92282
2017-10-30 07:00:00+00:00    3688.30888
2017-10-31 07:00:00+00:00    3705.66792
2017-11-01 07:00:00+00:00    3723.66124
2017-11-02 07:00:00+00:00    3742.75104
2017-11-03 07:00:00+00:00    3764.01538
2017-11-06 07:00:00+00:00    3787.24372
2017-11-07 07:00:00+00:00    3809.26606
2017-11-08 07:00:00+00:00    3831.50456
Name: long_mavg, dtype: float64