test strategy

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

(zengliang1995) #1
克隆策略

This is my first testing strategy.

In [176]:
import talib
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
In [177]:
#test RB fuatures in SHF
class conf:
    ins = ['RB1709.SHF']
    start = '2016-09-20'
    end = '2017-06-30'
    fields = fields=['open_0','high_0','low_0','close_0']
    capital_base = 1000000
In [178]:
df = D.features(instruments=conf.ins,start_date=conf.start, end_date=conf.end,fields=conf.fields)
print(df.shape)
print(df.head())
print(df.tail())
(188, 6)
   instrument       date  open_0  close_0   low_0  high_0
0  RB1709.SHF 2016-09-20  2267.0   2298.0  2267.0  2313.0
1  RB1709.SHF 2016-09-21  2291.0   2290.0  2285.0  2306.0
2  RB1709.SHF 2016-09-22  2301.0   2337.0  2301.0  2380.0
3  RB1709.SHF 2016-09-23  2364.0   2350.0  2335.0  2375.0
4  RB1709.SHF 2016-09-26  2365.0   2368.0  2358.0  2385.0
     instrument       date  open_0  close_0   low_0  high_0
183  RB1709.SHF 2017-06-26  3208.0   3252.0  3186.0  3255.0
184  RB1709.SHF 2017-06-27  3249.0   3308.0  3232.0  3309.0
185  RB1709.SHF 2017-06-28  3308.0   3352.0  3308.0  3360.0
186  RB1709.SHF 2017-06-29  3360.0   3377.0  3335.0  3399.0
187  RB1709.SHF 2017-06-30  3377.0   3412.0  3347.0  3419.0
In [179]:
def initialize(context):
    # set commission,buy 0.0001,sell 0.0001
    context.set_commission(PerOrder(buy_cost=0.0001, sell_cost=0.0001))
    context.short_period = 15
    context.long_period = 35
In [180]:
def handle_data(context, data):
    if context.trading_day_index < context.long_period:
        return
    for instrument in conf.ins:
        instrument = context.symbol(instrument)
        price = data.current(instrument, 'price')
        short_mavg = data.history(instrument, 'price', context.short_period, '1d').mean()
        long_mavg = data.history(instrument, 'price', context.long_period, '1d').mean()
        
        cash = context.portfolio.cash  / len(conf.ins)
        cur_position = context.portfolio.positions[instrument].amount  
        
        if cur_position > 0:
            positions = {e.symbol: p.cost_basis  for e, p in context.portfolio.positions.items()}
            for i in positions.keys():
                stock_cost = positions[i] 
                stock_market_price = data.current(context.symbol(i), 'price') 
                if stock_market_price - stock_cost  <= -0.01:   
                    context.order_target_percent(context.symbol(i),0) 
        elif short_mavg > long_mavg and cur_position == 0  and data.can_trade(instrument):  
            context.order(instrument, int(cash/price/100)*100) 
        elif short_mavg < long_mavg  and cur_position > 0 and  data.can_trade(instrument):  
            context.order_target_percent(instrument, 0)
In [181]:
m = M.trade.v2(
    instruments = conf.ins,
    start_date = conf.start,
    end_date = conf.end,
    initialize = initialize,
    handle_data = handle_data,
    # order price field buy with open price
    order_price_field_buy = 'open',
    # order price field sell with sell price
    order_price_field_sell = 'open',
    capital_base = conf.capital_base,
)
[2017-12-26 18:21:25.154710] INFO: bigquant: backtest.v7 开始运行..
[2017-12-26 18:21:25.283483] INFO: algo: set price type:backward_adjusted
[2017-12-26 18:21:27.455357] INFO: Performance: Simulated 188 trading days out of 188.
[2017-12-26 18:21:27.457030] INFO: Performance: first open: 2016-09-20 01:30:00+00:00
[2017-12-26 18:21:27.458197] INFO: Performance: last close: 2017-06-30 07:00:00+00:00
  • 收益率8.94%
  • 年化收益率12.16%
  • 基准收益率12.37%
  • 阿尔法0.08
  • 贝塔-0.03
  • 夏普比率0.66
  • 收益波动率11.68%
  • 信息比率-0.31
  • 最大回撤8.25%
[2017-12-26 18:21:28.314110] INFO: bigquant: backtest.v7 运行完成[3.159397s].
In [ ]:
 

(zhuhaoyu) #2

虽然看不太懂,但是觉得好牛逼