策略小测试,增加20日均线向上过滤

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

(stalkerggyy) #1
克隆策略
In [2]:
#持有3天,20日均线向上,最多10只股票,采用2010年1月至2015年1月的数据作为训练集

from matplotlib.pyplot import plot
from matplotlib.pyplot import show
import numpy as np

def check_code_date(code_name='000300.SHA',days=31):
    df=D.history_data(instruments=[code_name], start_date='2005-01-01', end_date='2017-05-27',
                   fields=['close'])
    sp=df['close'].values
    N=days
    n=np.ones(N)
    weights=n/N

    sma=np.append(np.zeros(N-1),np.convolve(weights,sp)[N-1:-N+1])
    t=np.arange(len(sp))
    #plot(t,sp,lw=1)
    #plot(t,sma,lw=2)
    #show()

    df['sma']=sma
    #print (sma)
    #print(df)
    
    date_list=[]
    for i in range(1,len(sp)):
        sma_0=df['sma'][i-1]
        sma_1=df['sma'][i]
        if sma_0>0 and sma_0<=sma_1:
            
            date_list.append(df.iloc[i].date.strftime('%Y-%m-%d'))
    return date_list


# 基础参数配置
class conf:
    start_date = '2010-01-01'
    end_date='2017-05-27'
    # split_date 之前的数据用于训练,之后的数据用作效果评估
    split_date = '2015-01-01'
    # D.instruments: https://bigquant.com/docs/data_instruments.html
    instruments = D.instruments(start_date, end_date)

    # 机器学习目标标注函数
    # 如下标注函数等价于 min(max((持有期间的收益 * 100), -20), 20) + 20 (后面的M.fast_auto_labeler会做取整操作)
    # 说明:max/min这里将标注分数限定在区间[-20, 20],+20将分数变为非负数 (StockRanker要求标注分数非负整数)
    label_expr = ['return * 100', 'where(label > {0}, {0}, where(label < -{0}, -{0}, label)) + {0}'.format(20)]
    # 持有天数,用于计算label_expr中的return值(收益)
    hold_days = 3

    # 特征 https://bigquant.com/docs/data_features.html,你可以通过表达式构造任何特征
    features = [
        'rank_market_cap_float_0',  # 流通市值排名
        'rank_market_cap_0',  # 总市值排名
        #'rank_avg_mf_net_amount_5',  # 5日平均净主动买入额排名
        #'rank_avg_mf_net_amount_10',  # 10日平均净主动买入额排名
        #'rank_avg_mf_net_amount_20',  # 20日平均净主动买入额排名
        'fs_operating_revenue_yoy_0',  # 营业收入同比增长率
        'rank_fs_operating_revenue_yoy_0',  # 营业收入同比增长率排名
        'list_days_0',  # 已经上市的天数
    ]

# 给数据做标注:给每一行数据(样本)打分,一般分数越高表示越好
m1 = M.fast_auto_labeler.v5(
    instruments=conf.instruments, start_date=conf.start_date, end_date=conf.end_date,
    label_expr=conf.label_expr, hold_days=conf.hold_days,
    benchmark='000300.SHA', sell_at='open', buy_at='open')
# 计算特征数据
m2 = M.general_feature_extractor.v5(
    instruments=conf.instruments, start_date=conf.start_date, end_date=conf.end_date,
    features=conf.features)
# 数据预处理:缺失数据处理,数据规范化,T.get_stock_ranker_default_transforms为StockRanker模型做数据预处理
m3 = M.transform.v2(
    data=m2.data, transforms=T.get_stock_ranker_default_transforms(),
    drop_null=True, astype='int32', except_columns=['date', 'instrument'],
    clip_lower=0, clip_upper=200000000)
# 合并标注和特征数据
m4 = M.join.v2(data1=m1.data, data2=m3.data, on=['date', 'instrument'], sort=True)

# 训练数据集
m5_training = M.filter.v2(data=m4.data, expr='date < "%s"' % conf.split_date)
# 评估数据集
#m5_evaluation = M.filter.v2(data=m4.data, expr='"%s" <= date' % conf.split_date)
m5_evaluation = M.filter.v2(data=m4.data, expr='"%s" <= date' % conf.start_date)
# StockRanker机器学习训练
m6 = M.stock_ranker_train.v2(training_ds=m5_training.data, features=conf.features)
# 对评估集做预测
m7 = M.stock_ranker_predict.v2(model_id=m6.model_id, data=m5_evaluation.data)


## 量化回测 https://bigquant.com/docs/strategy_backtest.html
# 回测引擎:初始化函数,只执行一次
def initialize(context):
    # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数
    context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))
    # 预测数据,通过options传入进来,使用 read_df 函数,加载到内存 (DataFrame)
    context.ranker_prediction = context.options['ranker_prediction'].read_df()
    # 设置买入的股票数量,这里买入预测股票列表排名靠前的5只
    #stock_count = 5
    stock_count = 10
    # 每只的股票的权重,如下的权重分配会使得靠前的股票分配多一点的资金,[0.339160, 0.213986, 0.169580, ..]
    context.stock_weights = T.norm([1 / math.log(i + 2) for i in range(0, stock_count)])
    # 设置每只股票占用的最大资金比例
    #context.max_cash_per_instrument = 0.2
    context.max_cash_per_instrument = 0.1

# 回测引擎:每日数据处理函数,每天执行一次
def handle_data(context, data):
    # 按日期过滤得到今日的预测数据
    ranker_prediction = context.ranker_prediction[context.ranker_prediction.date == data.current_dt.strftime('%Y-%m-%d')]

    # 1. 资金分配
    # 平均持仓时间是hold_days,每日都将买入股票,每日预期使用 1/hold_days 的资金
    # 实际操作中,会存在一定的买入误差,所以在前hold_days天,等量使用资金;之后,尽量使用剩余资金(这里设置最多用等量的1.5倍)
    is_staging = context.trading_day_index < context.options['hold_days'] # 是否在建仓期间(前 hold_days 天)
    cash_avg = context.portfolio.portfolio_value / context.options['hold_days']
    cash_for_buy = min(context.portfolio.cash, (1 if is_staging else 1.5) * cash_avg)
    cash_for_sell = cash_avg - (context.portfolio.cash - cash_for_buy)
    positions = {e.symbol: p.amount * p.last_sale_price         for e, p in context.perf_tracker.position_tracker.positions.items()}

    # 2. 生成卖出订单:hold_days天之后才开始卖出;对持仓的股票,按StockRanker预测的排序末位淘汰
    if not is_staging and cash_for_sell > 0:
        equities = {e.symbol: e for e, p in context.perf_tracker.position_tracker.positions.items()}
        instruments = list(reversed(list(ranker_prediction.instrument[ranker_prediction.instrument.apply(
                lambda x: x in equities and not context.has_unfinished_sell_order(equities[x]))])))
        # print('rank order for sell %s' % instruments)
        for instrument in instruments:
            context.order_target(context.symbol(instrument), 0)
            cash_for_sell -= positions[instrument]
            if cash_for_sell <= 0:
                break
                
    today = data.current_dt.strftime('%Y-%m-%d') # 交易日期
    #print(today)
    if today not in check_code_date('000300.SHA',20):
        print (today)
        return

    # 3. 生成买入订单:按StockRanker预测的排序,买入前面的stock_count只股票
    buy_cash_weights = context.stock_weights
    buy_instruments = list(ranker_prediction.instrument[:len(buy_cash_weights)])
    max_cash_per_instrument = context.portfolio.portfolio_value * context.max_cash_per_instrument
    for i, instrument in enumerate(buy_instruments):
        cash = cash_for_buy * buy_cash_weights[i]
        if cash > max_cash_per_instrument - positions.get(instrument, 0):
            # 确保股票持仓量不会超过每次股票最大的占用资金量
            cash = max_cash_per_instrument - positions.get(instrument, 0)
        if cash > 0:
            context.order_value(context.symbol(instrument), cash)

# 调用回测引擎
m8 = M.backtest.v5(
    instruments=m7.instruments,
    start_date=m7.start_date,
    end_date=m7.end_date,
    initialize=initialize,
    handle_data=handle_data,
    order_price_field_buy='open',       # 表示 开盘 时买入
    order_price_field_sell='close',     # 表示 收盘 前卖出
    capital_base=1000000,               # 初始资金
    benchmark='000300.SHA',             # 比较基准,不影响回测结果
    # 通过 options 参数传递预测数据和参数给回测引擎
    options={'ranker_prediction': m7.predictions, 'hold_days': conf.hold_days}
)
[2017-05-31 18:38:22.712741] INFO: bigquant: fast_auto_labeler.v5 start ..
[2017-05-31 18:38:22.716739] INFO: bigquant: hit cache
[2017-05-31 18:38:22.724168] INFO: bigquant: fast_auto_labeler.v5 end [0.011457s].
[2017-05-31 18:38:22.728938] INFO: bigquant: general_feature_extractor.v5 start ..
[2017-05-31 18:38:31.248985] INFO: general_feature_extractor: year 2010, featurerows=431567
[2017-05-31 18:38:48.780628] INFO: general_feature_extractor: year 2011, featurerows=511455
[2017-05-31 18:38:53.836591] INFO: general_feature_extractor: year 2012, featurerows=565675
[2017-05-31 18:39:17.810266] INFO: general_feature_extractor: year 2013, featurerows=564168
[2017-05-31 18:39:41.548408] INFO: general_feature_extractor: year 2014, featurerows=569948
[2017-05-31 18:40:07.678473] INFO: general_feature_extractor: year 2015, featurerows=569698
[2017-05-31 18:41:00.480046] INFO: general_feature_extractor: year 2016, featurerows=641546
[2017-05-31 18:41:21.774971] INFO: general_feature_extractor: year 2017, featurerows=280161
[2017-05-31 18:41:22.517590] INFO: general_feature_extractor: total feature rows: 4134218
[2017-05-31 18:41:22.529496] INFO: bigquant: general_feature_extractor.v5 end [179.800518s].
[2017-05-31 18:41:22.536410] INFO: bigquant: transform.v2 start ..
[2017-05-31 18:41:24.310770] INFO: transform: transformed /y_2010, 409792/431567
[2017-05-31 18:41:26.130487] INFO: transform: transformed /y_2011, 491457/511455
[2017-05-31 18:41:27.889625] INFO: transform: transformed /y_2012, 553330/565675
[2017-05-31 18:41:29.727905] INFO: transform: transformed /y_2013, 562645/564168
[2017-05-31 18:41:32.373011] INFO: transform: transformed /y_2014, 564073/569948
[2017-05-31 18:41:34.235276] INFO: transform: transformed /y_2015, 556290/569698
[2017-05-31 18:41:35.953569] INFO: transform: transformed /y_2016, 630292/641546
[2017-05-31 18:41:37.456271] INFO: transform: transformed /y_2017, 263242/280161
[2017-05-31 18:41:37.811794] INFO: transform: transformed rows: 4031121/4134218
[2017-05-31 18:41:37.826029] INFO: bigquant: transform.v2 end [15.289498s].
[2017-05-31 18:41:37.830472] INFO: bigquant: join.v2 start ..
[2017-05-31 18:41:43.628817] INFO: filter: /y_2010, rows=409274/409792, timetaken=4.207941s
[2017-05-31 18:41:48.156580] INFO: filter: /y_2011, rows=490956/491457, timetaken=4.464574s
[2017-05-31 18:41:52.622656] INFO: filter: /y_2012, rows=552286/553330, timetaken=4.383227s
[2017-05-31 18:41:57.018785] INFO: filter: /y_2013, rows=561639/562645, timetaken=4.297753s
[2017-05-31 18:42:01.917371] INFO: filter: /y_2014, rows=562594/564073, timetaken=4.794859s
[2017-05-31 18:42:06.457148] INFO: filter: /y_2015, rows=549468/556290, timetaken=4.438128s
[2017-05-31 18:42:10.653791] INFO: filter: /y_2016, rows=628890/630292, timetaken=4.097769s
[2017-05-31 18:42:13.926832] INFO: filter: /y_2017, rows=250357/263242, timetaken=3.25076s
[2017-05-31 18:42:14.397807] INFO: filter: total result rows: 4005464
[2017-05-31 18:42:14.401108] INFO: bigquant: join.v2 end [36.570563s].
[2017-05-31 18:42:14.406533] INFO: bigquant: filter.v2 start ..
[2017-05-31 18:42:14.413134] INFO: filter: filter with expr date < "2015-01-01"
[2017-05-31 18:42:15.556223] INFO: filter: filter /y_2010, 409274/409274
[2017-05-31 18:42:16.574897] INFO: filter: filter /y_2011, 490956/490956
[2017-05-31 18:42:17.506717] INFO: filter: filter /y_2012, 552286/552286
[2017-05-31 18:42:18.501907] INFO: filter: filter /y_2013, 561639/561639
[2017-05-31 18:42:19.855645] INFO: filter: filter /y_2014, 562594/562594
[2017-05-31 18:42:20.093335] INFO: filter: filter /y_2015, 0/549468
[2017-05-31 18:42:20.348086] INFO: filter: filter /y_2016, 0/628890
[2017-05-31 18:42:20.465797] INFO: filter: filter /y_2017, 0/250357
[2017-05-31 18:42:20.586656] INFO: bigquant: filter.v2 end [6.179989s].
[2017-05-31 18:42:20.591536] INFO: bigquant: filter.v2 start ..
[2017-05-31 18:42:20.597637] INFO: filter: filter with expr "2010-01-01" <= date
[2017-05-31 18:42:21.550646] INFO: filter: filter /y_2010, 409274/409274
[2017-05-31 18:42:22.560852] INFO: filter: filter /y_2011, 490956/490956
[2017-05-31 18:42:23.546236] INFO: filter: filter /y_2012, 552286/552286
[2017-05-31 18:42:24.566002] INFO: filter: filter /y_2013, 561639/561639
[2017-05-31 18:42:25.964834] INFO: filter: filter /y_2014, 562594/562594
[2017-05-31 18:42:27.000570] INFO: filter: filter /y_2015, 549468/549468
[2017-05-31 18:42:28.144550] INFO: filter: filter /y_2016, 628890/628890
[2017-05-31 18:42:28.684138] INFO: filter: filter /y_2017, 250357/250357
[2017-05-31 18:42:29.043528] INFO: bigquant: filter.v2 end [8.451938s].
[2017-05-31 18:42:29.048272] INFO: bigquant: stock_ranker_train.v2 start ..
[2017-05-31 18:42:31.135054] INFO: df2bin: prepare data: training ..
[2017-05-31 18:43:32.761907] INFO: stock_ranker_train: training: 2576749 rows
[2017-05-31 18:45:53.123956] INFO: bigquant: stock_ranker_train.v2 end [204.075622s].
[2017-05-31 18:45:53.129432] INFO: bigquant: stock_ranker_predict.v2 start ..
[2017-05-31 18:45:56.503784] INFO: df2bin: prepare data: prediction ..
[2017-05-31 18:47:31.202475] INFO: stock_ranker_predict: prediction: 4005464 rows
[2017-05-31 18:48:04.997355] INFO: bigquant: stock_ranker_predict.v2 end [131.867629s].
[2017-05-31 18:48:05.021972] INFO: bigquant: backtest.v5 start ..
2010-01-04
2010-01-05
2010-01-06
2010-01-07
2010-01-08
2010-01-11
2010-01-12
2010-01-13
2010-01-14
2010-01-22
2010-01-25
2010-01-26
2010-01-27
2010-01-28
2010-01-29
2010-02-01
2010-02-02
2010-02-03
2010-02-04
2010-02-05
2010-02-08
2010-02-09
2010-02-10
2010-02-11
2010-02-12
2010-02-22
2010-02-23
2010-02-24
2010-02-25
2010-02-26
2010-03-01
2010-03-25
2010-03-26
2010-04-19
2010-04-20
2010-04-21
2010-04-22
2010-04-23
2010-04-26
2010-04-27
2010-04-28
2010-04-29
2010-04-30
2010-05-04
2010-05-05
2010-05-06
2010-05-07
2010-05-10
2010-05-11
2010-05-12
2010-05-13
2010-05-14
2010-05-17
2010-05-18
2010-05-19
2010-05-20
2010-05-21
2010-05-24
2010-05-25
2010-05-26
2010-05-27
2010-05-28
2010-05-31
2010-06-01
2010-06-02
2010-06-03
2010-06-04
2010-06-07
2010-06-08
2010-06-09
2010-06-10
2010-06-11
2010-06-18
2010-06-23
2010-06-24
2010-06-25
2010-06-28
2010-06-29
2010-06-30
2010-07-01
2010-07-02
2010-07-05
2010-07-06
2010-07-07
2010-07-08
2010-07-09
2010-07-12
2010-07-13
2010-07-14
2010-07-15
2010-07-16
2010-07-19
2010-07-20
2010-07-21
2010-08-25
2010-08-26
2010-08-27
2010-08-30
2010-09-15
2010-09-16
2010-09-17
2010-09-20
2010-09-21
2010-11-12
2010-11-16
2010-11-17
2010-11-18
2010-11-19
2010-11-22
2010-11-23
2010-11-24
2010-11-25
2010-11-26
2010-11-29
2010-11-30
2010-12-01
2010-12-02
2010-12-03
2010-12-06
2010-12-07
2010-12-08
2010-12-09
2010-12-10
2010-12-13
2010-12-23
2010-12-24
2010-12-27
2010-12-28
2010-12-29
2010-12-30
2010-12-31
2011-01-05
2011-01-06
2011-01-10
2011-01-11
2011-01-12
2011-01-13
2011-01-14
2011-01-17
2011-01-18
2011-01-19
2011-01-20
2011-01-21
2011-01-24
2011-01-25
2011-01-26
2011-01-27
2011-01-28
2011-01-31
2011-02-01
2011-02-09
2011-02-10
2011-02-11
2011-03-15
2011-03-16
2011-03-17
2011-03-21
2011-04-06
2011-04-07
2011-04-25
2011-04-26
2011-04-27
2011-04-28
2011-04-29
2011-05-03
2011-05-04
2011-05-05
2011-05-06
2011-05-09
2011-05-10
2011-05-11
2011-05-12
2011-05-13
2011-05-16
2011-05-17
2011-05-18
2011-05-19
2011-05-20
2011-05-23
2011-05-24
2011-05-25
2011-05-26
2011-05-27
2011-05-30
2011-05-31
2011-06-01
2011-06-02
2011-06-03
2011-06-07
2011-06-08
2011-06-09
2011-06-10
2011-06-13
2011-06-14
2011-06-15
2011-06-16
2011-06-17
2011-06-20
2011-06-21
2011-06-22
2011-06-23
2011-06-29
2011-07-25
2011-07-26
2011-07-27
2011-07-28
2011-07-29
2011-08-01
2011-08-02
2011-08-03
2011-08-04
2011-08-05
2011-08-08
2011-08-09
2011-08-10
2011-08-11
2011-08-12
2011-08-15
2011-08-16
2011-08-17
2011-08-18
2011-08-19
2011-08-22
2011-08-23
2011-08-24
2011-08-25
2011-08-26
2011-08-29
2011-08-30
2011-08-31
2011-09-01
2011-09-02
2011-09-05
2011-09-06
2011-09-07
2011-09-08
2011-09-09
2011-09-13
2011-09-14
2011-09-15
2011-09-16
2011-09-19
2011-09-20
2011-09-21
2011-09-22
2011-09-23
2011-09-26
2011-09-27
2011-09-28
2011-09-29
2011-09-30
2011-10-10
2011-10-11
2011-10-12
2011-10-13
2011-10-14
2011-10-17
2011-10-18
2011-10-19
2011-10-20
2011-10-21
2011-10-24
2011-10-25
2011-10-26
2011-10-27
2011-11-22
2011-11-23
2011-11-24
2011-11-25
2011-11-28
2011-11-29
2011-11-30
2011-12-01
2011-12-02
2011-12-05
2011-12-06
2011-12-07
2011-12-08
2011-12-09
2011-12-12
2011-12-13
2011-12-14
2011-12-15
2011-12-16
2011-12-19
2011-12-20
2011-12-21
2011-12-22
2011-12-23
2011-12-26
2011-12-27
2011-12-28
2011-12-29
2011-12-30
2012-01-04
2012-01-05
2012-01-06
2012-01-09
2012-01-10
2012-01-11
2012-01-13
2012-03-21
2012-03-22
2012-03-23
2012-03-26
2012-03-27
2012-03-28
2012-03-29
2012-03-30
2012-04-05
2012-04-06
2012-04-09
2012-04-10
2012-04-11
2012-04-12
2012-04-13
2012-04-16
2012-04-17
2012-04-18
2012-04-19
2012-05-18
2012-05-21
2012-05-24
2012-05-25
2012-05-28
2012-05-30
2012-05-31
2012-06-01
2012-06-04
2012-06-05
2012-06-06
2012-06-07
2012-06-08
2012-06-11
2012-06-12
2012-06-14
2012-06-15
2012-06-18
2012-06-19
2012-06-20
2012-06-21
2012-06-25
2012-06-26
2012-06-27
2012-06-28
2012-06-29
2012-07-02
2012-07-03
2012-07-04
2012-07-05
2012-07-06
2012-07-09
2012-07-10
2012-07-11
2012-07-12
2012-07-13
2012-07-16
2012-07-17
2012-07-18
2012-07-19
2012-07-20
2012-07-23
2012-07-24
2012-07-25
2012-07-26
2012-07-27
2012-07-30
2012-07-31
2012-08-01
2012-08-02
2012-08-03
2012-08-06
2012-08-07
2012-08-08
2012-08-09
2012-08-10
2012-08-13
2012-08-14
2012-08-15
2012-08-16
2012-08-17
2012-08-20
2012-08-21
2012-08-22
2012-08-23
2012-08-24
2012-08-27
2012-08-28
2012-08-29
2012-08-30
2012-08-31
2012-09-03
2012-09-04
2012-09-05
2012-09-06
2012-09-07
2012-09-10
2012-09-11
2012-09-12
2012-09-13
2012-09-17
2012-09-18
2012-09-19
2012-09-20
2012-09-21
2012-09-24
2012-09-25
2012-09-26
2012-10-12
2012-10-15
2012-10-16
2012-10-17
2012-11-06
2012-11-07
2012-11-08
2012-11-09
2012-11-12
2012-11-13
2012-11-14
2012-11-15
2012-11-16
2012-11-19
2012-11-20
2012-11-21
2012-11-22
2012-11-23
2012-11-26
2012-11-27
2012-11-28
2012-11-29
2012-11-30
2012-12-03
2012-12-04
2012-12-05
2012-12-06
2013-02-25
2013-02-26
2013-02-27
2013-03-04
2013-03-05
2013-03-06
2013-03-07
2013-03-08
2013-03-11
2013-03-12
2013-03-13
2013-03-14
2013-03-15
2013-03-18
2013-03-19
2013-03-20
2013-03-27
2013-03-28
2013-03-29
2013-04-01
2013-04-02
2013-04-03
2013-04-08
2013-04-09
2013-04-10
2013-04-11
2013-04-12
2013-04-15
2013-04-16
2013-04-17
2013-04-18
2013-04-19
2013-04-22
2013-04-23
2013-04-24
2013-04-25
2013-04-26
2013-05-02
2013-05-03
2013-06-07
2013-06-13
2013-06-14
2013-06-17
2013-06-18
2013-06-19
2013-06-20
2013-06-21
2013-06-24
2013-06-25
2013-06-26
2013-06-27
2013-06-28
2013-07-01
2013-07-02
2013-07-03
2013-07-04
2013-07-05
2013-07-08
2013-07-09
2013-07-10
2013-07-11
2013-07-12
2013-07-15
2013-07-16
2013-07-17
2013-07-18
2013-07-19
2013-07-29
2013-07-30
2013-07-31
2013-08-08
2013-10-16
2013-10-17
2013-10-18
2013-10-21
2013-10-22
2013-10-23
2013-10-24
2013-10-25
2013-10-28
2013-10-29
2013-10-30
2013-10-31
2013-11-01
2013-11-04
2013-11-05
2013-11-06
2013-11-07
2013-11-08
2013-11-11
2013-11-12
2013-11-13
2013-11-14
2013-11-15
2013-11-18
2013-11-19
2013-12-16
2013-12-17
2013-12-18
2013-12-19
2013-12-20
2013-12-23
2013-12-24
2013-12-25
2013-12-26
2013-12-27
2013-12-30
2013-12-31
2014-01-02
2014-01-03
2014-01-06
2014-01-07
2014-01-08
2014-01-09
2014-01-10
2014-01-13
2014-01-14
2014-01-15
2014-01-16
2014-01-17
2014-01-20
2014-01-21
2014-01-22
2014-01-23
2014-01-24
2014-01-27
2014-01-28
2014-01-29
2014-01-30
2014-02-07
2014-02-25
2014-02-26
2014-02-27
2014-02-28
2014-03-03
2014-03-04
2014-03-05
2014-03-06
2014-03-07
2014-03-10
2014-03-11
2014-03-12
2014-03-13
2014-03-14
2014-03-17
2014-03-18
2014-03-19
2014-03-20
2014-03-21
2014-03-24
2014-03-28
2014-03-31
2014-04-01
2014-04-03
2014-04-28
2014-04-30
2014-05-05
2014-05-06
2014-05-07
2014-05-08
2014-05-09
2014-05-12
2014-05-13
2014-05-14
2014-05-15
2014-05-16
2014-05-19
2014-05-20
2014-05-21
2014-05-22
2014-05-23
2014-05-26
2014-05-27
2014-05-29
2014-05-30
2014-06-03
2014-06-04
2014-06-06
2014-06-10
2014-06-11
2014-06-12
2014-06-19
2014-06-23
2014-06-24
2014-06-25
2014-06-26
2014-06-27
2014-07-09
2014-07-10
2014-07-11
2014-07-14
2014-08-26
2014-08-28
2014-09-01
2014-10-23
2014-10-24
2014-10-27
2015-01-30
2015-02-02
2015-02-03
2015-02-04
2015-02-05
2015-02-06
2015-02-09
2015-02-10
2015-02-11
2015-02-12
2015-02-13
2015-02-25
2015-02-26
2015-03-02
2015-03-03
2015-05-18
2015-06-19
2015-06-23
2015-06-24
2015-06-25
2015-06-26
2015-06-29
2015-06-30
2015-07-01
2015-07-02
2015-07-03
2015-07-06
2015-07-07
2015-07-08
2015-07-09
2015-07-10
2015-07-13
2015-07-14
2015-07-15
2015-07-16
2015-07-17
2015-07-20
2015-07-21
2015-07-22
2015-07-23
2015-07-24
2015-07-27
2015-07-28
2015-07-29
2015-07-30
2015-07-31
2015-08-03
2015-08-06
2015-08-07
2015-08-10
2015-08-11
2015-08-14
2015-08-17
2015-08-18
2015-08-19
2015-08-20
2015-08-21
2015-08-24
2015-08-25
2015-08-26
2015-08-27
2015-08-28
2015-08-31
2015-09-01
2015-09-02
2015-09-07
2015-09-08
2015-09-09
2015-09-10
2015-09-11
2015-09-14
2015-09-15
2015-09-16
2015-09-17
2015-09-18
2015-09-21
2015-09-22
2015-09-23
2015-09-29
2015-09-30
2015-10-08
2015-10-09
2015-12-04
2015-12-07
2015-12-08
2015-12-09
2015-12-10
2015-12-11
2015-12-14
2015-12-15
2015-12-16
2015-12-17
2015-12-18
2015-12-31
2016-01-04
2016-01-05
2016-01-06
2016-01-07
2016-01-08
2016-01-11
2016-01-12
2016-01-13
2016-01-14
2016-01-15
2016-01-18
2016-01-19
2016-01-20
2016-01-21
2016-01-22
2016-01-25
2016-01-26
2016-01-27
2016-01-28
2016-01-29
2016-02-01
2016-02-02
2016-02-03
2016-02-04
2016-02-05
2016-02-15
2016-02-16
2016-02-17
2016-02-18
2016-02-19
2016-02-22
2016-02-23
2016-02-24
2016-02-25
2016-02-26
2016-02-29
2016-03-01
2016-04-19
2016-04-20
2016-04-21
2016-04-22
2016-04-25
2016-04-28
2016-04-29
2016-05-03
2016-05-04
2016-05-05
2016-05-06
2016-05-09
2016-05-10
2016-05-11
2016-05-12
2016-05-13
2016-05-16
2016-05-17
2016-05-18
2016-05-19
2016-05-20
2016-05-23
2016-05-24
2016-05-25
2016-05-26
2016-05-27
2016-05-30
2016-05-31
2016-06-01
2016-06-02
2016-06-13
2016-06-30
2016-07-01
2016-08-01
2016-08-02
2016-08-03
2016-08-04
2016-08-09
2016-08-10
2016-08-11
2016-09-12
2016-09-13
2016-09-14
2016-09-19
2016-09-20
2016-09-21
2016-09-22
2016-09-23
2016-09-26
2016-09-27
2016-09-28
2016-09-29
2016-09-30
2016-10-10
2016-10-11
2016-10-12
2016-10-13
2016-10-14
2016-10-17
2016-12-12
2016-12-13
2016-12-14
2016-12-15
2016-12-16
2016-12-19
2016-12-20
2016-12-21
2016-12-22
2016-12-23
2016-12-26
2016-12-27
2016-12-28
2016-12-29
2016-12-30
2017-01-03
2017-01-04
2017-01-05
2017-01-06
2017-01-09
2017-01-10
2017-01-11
2017-01-12
2017-01-13
2017-01-16
2017-01-17
2017-01-19
2017-03-20
2017-03-21
2017-03-22
2017-03-23
2017-04-17
2017-04-19
2017-04-20
2017-04-24
2017-04-25
2017-04-26
2017-04-27
2017-04-28
2017-05-02
2017-05-03
2017-05-04
2017-05-05
2017-05-08
2017-05-09
2017-05-10
2017-05-11
2017-05-12
2017-05-15
2017-05-16
2017-05-17
2017-05-18
2017-05-19
2017-05-22
[2017-05-31 19:35:48.602315] INFO: Performance: Simulated 1792 trading days out of 1792.
[2017-05-31 19:35:48.603729] INFO: Performance: first open: 2010-01-04 14:30:00+00:00
[2017-05-31 19:35:48.605061] INFO: Performance: last close: 2017-05-22 19:00:00+00:00
[注意] 有 1245 笔卖出是在多天内完成的。当日卖出股票超过了当日股票交易的2.5%会出现这种情况。
  • 收益率3542.87%
  • 年化收益率65.8%
  • 基准收益率-4.6%
  • 阿尔法0.63
  • 贝塔0.3
  • 夏普比率3.62
  • 收益波动率17.0%
  • 信息比率2.88
  • 最大回撤18.84%
[2017-05-31 19:36:02.039324] INFO: bigquant: backtest.v5 end [2877.017235s].

(copen) #2

最近由于出现漂亮50和要命3000特殊现状,市场风格大变,因此最近回撤有点大啊。不过加入大盘择时,确实躲过了15年股灾和16年初的熔断。


(小马哥) #3

在确定 均线向上的date里,我觉得楼主的代码稍微复杂了一些。我做了一个另外的版本,望多多交流!@stalkerggyy

克隆策略
In [1]:
code_name = '000300.SHA'
df=D.history_data(instruments=[code_name], start_date='2005-01-01', end_date='2017-05-27', fields=['close'])
df['ma_20'] = pd.rolling_mean(df['close'], 20)
df['ma_20_pre'] = df['ma_20'].shift(1) 
df['Up'] = df['ma_20'] > df['ma_20_pre']
date_list = list(df[df['Up'] == True].date)
date_list = [i.strftime('%Y-%m-%d') for i in date_list ]
In [4]:
date_list[:30]
Out[4]:
['2005-02-02',
 '2005-02-03',
 '2005-02-04',
 '2005-02-16',
 '2005-02-17',
 '2005-02-18',
 '2005-02-21',
 '2005-02-22',
 '2005-02-23',
 '2005-02-24',
 '2005-02-25',
 '2005-02-28',
 '2005-03-01',
 '2005-03-02',
 '2005-03-03',
 '2005-03-04',
 '2005-03-07',
 '2005-03-08',
 '2005-03-09',
 '2005-03-10',
 '2005-03-11',
 '2005-03-14',
 '2005-04-19',
 '2005-06-08',
 '2005-06-09',
 '2005-06-10',
 '2005-06-13',
 '2005-06-14',
 '2005-06-20',
 '2005-06-21']

(stalkerggyy) #4

谢谢,很久没写python了,也是刚来这个平台随手写了个策略,确实写得略繁琐,多谢指正。


(stalkerggyy) #5

嗯,不过如果太考虑近期的情况再加点条件进去,从长期回测来看未必稳定。不过这个也是随手写了个简单的策略,后续会慢慢优化,希望尽可能在较长期的样本内实现高年华收益和低最大回撤。

不过似乎这个平台虽然号称05年以来的数据都可用,但实际似乎06年下半年开始的数据才会不报错,有点奇怪。