请高手帮我看下策略出了什么问题?(cache_set, not supported type: <class 'NoneType'>)

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

(chenjianjia) #1
克隆策略

技术分析指标策略: 基于MACD指标

1. 策略参数

In [ ]:
import talib

def prepare(context):
    context.instruments = ['000651.SZA'] #以格力电器为例
    start_date = context.start_date# 起始时间    
    end_date = context.end_date # 结束时间
    context.short = 12
    context.long = 26
    context.smoothperiod = 9
    context.observation = 100

2. 策略主体

In [ ]:
def initialize(context):
   
    context.set_commission(PerDollar(0.0015)) # 手续费设置
    # 需要设置计算MACD的相关参数参数
  
    
    
def handle_data(context, data):
    
 
    if context.trading_day_index < 100: # 在100个交易日以后才开始真正运行 
        return
    
    sid = context.symbol(context.instruments[0])
    #sid = context.instruments
    # 获取价格数据
    prices = data.history(sid, 'price', context.observation, '1d')
    # 用Talib计算MACD取值,得到三个时间序列数组,分别为macd, signal 和 hist
    macd, signal, hist = talib.MACD(np.array(prices), context.short,
                                    context.long, context.smoothperiod)
 
    # 计算现在portfolio中股票的仓位
    cur_position = context.portfolio.positions[sid].amount
    
    # 策略逻辑
    # 卖出逻辑 macd下穿signal
    if macd[-1] - signal[-1] < 0 and macd[-2] - signal[-2] > 0:
        # 进行清仓
        if cur_position > 0 and data.can_trade(sid):
            context.order_target_value(sid, 0)

    # 买入逻辑  macd上穿signal
    if macd[-1] - signal[-1] > 0 and macd[-2] - signal[-2] < 0:
        # 买入股票
        if cur_position == 0 and data.can_trade(sid):
            context.order_target_percent(sid, 1)

3. 回测接口

In [33]:
m=M.trade.v3( 
    #instruments=context.instruments,
    start_date='2017-01-01',
    end_date='2017-12-31',
    prepare=prepare,
    initialize=initialize,
    handle_data=handle_data,
    order_price_field_buy='open',
    order_price_field_sell='open',
    capital_base=float("1.0e6"),
    benchmark='000300.INDX',
)
[2018-02-07 11:14:37.054636] INFO: bigquant: backtest.v7 开始运行..
[2018-02-07 11:14:37.166931] INFO: algo: set price type:backward_adjusted
[2018-02-07 11:14:41.363958] INFO: Performance: Simulated 244 trading days out of 244.
[2018-02-07 11:14:41.365795] INFO: Performance: first open: 2017-01-03 01:30:00+00:00
[2018-02-07 11:14:41.367022] INFO: Performance: last close: 2017-12-29 07:00:00+00:00
  • 收益率27.02%
  • 年化收益率28.02%
  • 基准收益率21.78%
  • 阿尔法0.09
  • 贝塔0.81
  • 夏普比率1.28
  • 胜率0.5
  • 盈亏比4.846
  • 收益波动率18.49%
  • 信息比率0.33
  • 最大回撤9.78%
[2018-02-07 11:14:42.434834] INFO: bigquant: backtest.v7 运行完成[5.380181s].

(小Q) #2

M.trade.v3中传入的instruments参数必须是list格式的。

克隆策略

技术分析指标策略: 基于MACD指标

In [5]:
import talib

1. 策略参数

In [6]:
def prepare(context):
    start_date = context.start_date# 起始时间    
    end_date = context.end_date # 结束时间
    context.short = 12
    context.long = 26
    context.smoothperiod = 9
    context.observation = 100

2. 策略主体

In [7]:
def initialize(context):
   
    context.set_commission(PerDollar(0.0015)) # 手续费设置
    # 需要设置计算MACD的相关参数参数
  
    
    
def handle_data(context, data):
    
 
    if context.trading_day_index < 100: # 在100个交易日以后才开始真正运行 
        return
    
    sid = context.symbol(context.instruments[0])
    #sid = context.instruments
    # 获取价格数据
    prices = data.history(sid, 'price', context.observation, '1d')
    # 用Talib计算MACD取值,得到三个时间序列数组,分别为macd, signal 和 hist
    macd, signal, hist = talib.MACD(np.array(prices), context.short,
                                    context.long, context.smoothperiod)
 
    # 计算现在portfolio中股票的仓位
    cur_position = context.portfolio.positions[sid].amount
    
    # 策略逻辑
    # 卖出逻辑 macd下穿signal
    if macd[-1] - signal[-1] < 0 and macd[-2] - signal[-2] > 0:
        # 进行清仓
        if cur_position > 0 and data.can_trade(sid):
            context.order_target_value(sid, 0)

    # 买入逻辑  macd上穿signal
    if macd[-1] - signal[-1] > 0 and macd[-2] - signal[-2] < 0:
        # 买入股票
        if cur_position == 0 and data.can_trade(sid):
            context.order_target_percent(sid, 1)

3. 回测接口

In [8]:
m=M.trade.v3( 
    instruments= ['000651.SZA'],
    start_date='2017-01-01',
    end_date='2017-12-31',
    prepare=prepare,
    initialize=initialize,
    handle_data=handle_data,
    order_price_field_buy='open',
    order_price_field_sell='open',
    capital_base=float("1.0e6"),
    benchmark='000300.INDX',
)
[2018-02-07 09:37:47.521266] INFO: bigquant: backtest.v7 开始运行..
[2018-02-07 09:37:47.634425] 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
[2018-02-07 09:37:49.866505] INFO: Performance: Simulated 244 trading days out of 244.
[2018-02-07 09:37:49.867810] INFO: Performance: first open: 2017-01-03 01:30:00+00:00
[2018-02-07 09:37:49.868802] INFO: Performance: last close: 2017-12-29 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)
  • 收益率27.02%
  • 年化收益率28.02%
  • 基准收益率21.78%
  • 阿尔法0.09
  • 贝塔0.81
  • 夏普比率1.28
  • 胜率0.5
  • 盈亏比4.846
  • 收益波动率18.49%
  • 信息比率0.33
  • 最大回撤9.78%
[2018-02-07 09:37:50.918527] INFO: bigquant: backtest.v7 运行完成[3.397273s].

(chenjianjia) #3

instruments好像没传入context,handle_data函数里的context.instruments数据是从哪来的呀?从哪个地方赋值的呀?


(小Q) #4

image

这个地方(M.trade.v3)传入instruments参数以后,context.instruments 就有了。