金叉死叉策略拷贝过来更换股票后无运行结果


(oscar9609) #1

策略展示如下:https://i.bigquant.com/user/oscar9609/lab/share/%E9%87%91%E5%8F%89%E6%AD%BB%E5%8F%89%E7%AD%96%E7%95%A5.ipynb


(oscar9609) #2
克隆策略

金叉死叉策略

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

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

1. 主要参数

In [10]:
instruments = ['600651.SHA']  
start_date = '2012-05-28'  
end_date = '2017-05-07'

2. 策略回测主体

In [11]:
def initialize(context):
    context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5)) # 设置手续费
    context.short_period = 5 # 短期均线
    context.long_period = 50 # 长期均线 

def handle_data(context, data):
    
    if context.trading_day_index <  context.long_period:  # 长期均线值要有意义,需要在50根k线之后
        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() # 短期均线值
    long_mavg = data.history(sid, 'price',context.long_period, '1d').mean() # 长期均线值

    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 [12]:
m=M.backtest.v5(
    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=100000,
    )
[2017-05-18 19:14:15.236027] INFO: bigquant: backtest.v5 start ..
[2017-05-18 19:14:15.239173] INFO: bigquant: hit cache
  • 收益率0.0%
  • 年化收益率0.0%
  • 基准收益率31.46%
  • 阿尔法-0.04
  • 贝塔0.0
  • 夏普比率n/a
  • 收益波动率0.0%
  • 信息比率-0.23
  • 最大回撤0.0%
[2017-05-18 19:14:18.587760] INFO: bigquant: backtest.v5 end [3.351681s].

(小Q) #3

你可以打印该股票的价格,后复权后,价格很大,因此如果资金量很小的话,int(cash/price/100)*100为0,所以没有交易了。
我的建议是修改买入下单函数: context.order_target_percent(sid, 1) # 买入

克隆策略

金叉死叉策略

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

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

1. 主要参数

In [4]:
instruments = ['600651.SHA']  
start_date = '2012-05-28'  
end_date = '2017-05-07'

2. 策略回测主体

In [17]:
def initialize(context):
    context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5)) # 设置手续费
    context.short_period = 5 # 短期均线
    context.long_period = 50 # 长期均线 

def handle_data(context, data):
    
    if context.trading_day_index <  context.long_period:  # 长期均线值要有意义,需要在50根k线之后
        return
    
    date = data.current_dt.strftime('%Y-%m-%d')
    
    k = instruments[0] # 标的为字符串格式
    sid = context.symbol(k) # 将标的转化为equity格式
    price = data.current(sid, 'price') # 最新价格
 
    short_mavg = data.history(sid, 'price',context.short_period, '1d').mean() # 短期均线值
    long_mavg = data.history(sid, 'price',context.long_period, '1d').mean() # 长期均线值
    

    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_target_percent(sid, 1) # 买入
        
    elif short_mavg < long_mavg and cur_position > 0 and data.can_trade(sid):  
        context.order_target_percent(sid, 0) # 全部卖出
     
        

3.回测接口

In [18]:
m=M.backtest.v5(
    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',
    volume_limit=0,
    capital_base=100000,
    )
[2017-05-18 19:37:38.099548] INFO: bigquant: backtest.v5 start ..
/var/app/enabled/pandas/tseries/index.py:817: PerformanceWarning: Non-vectorized DateOffset being applied to Series or DatetimeIndex
  "or DatetimeIndex", PerformanceWarning)
/var/app/enabled/empyrical/stats.py:534: RuntimeWarning: invalid value encountered in double_scalars
  sortino = mu / dsr
[2017-05-18 19:37:55.299437] INFO: Performance: Simulated 1202 trading days out of 1202.
[2017-05-18 19:37:55.301362] INFO: Performance: first open: 2012-05-28 13:30:00+00:00
[2017-05-18 19:37:55.302784] INFO: Performance: last close: 2017-05-05 19: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.09%
  • 年化收益率5.15%
  • 基准收益率31.46%
  • 阿尔法0.0
  • 贝塔0.47
  • 夏普比率0.03
  • 收益波动率30.14%
  • 信息比率-0.02
  • 最大回撤61.22%
[2017-05-18 19:38:01.316926] INFO: bigquant: backtest.v5 end [23.217342s].
In [ ]:
 

(chenhongyi) #4

:slight_smile: