Example #1
0
def startsimulate(_continueday, _end, _benchmark, _universe, _initialize,
                  _handle_data, _refresh_rate, _freq):
    bt, perf, bt_by_account = quartz.backtest(start=_continueday,
                                              end=_end,
                                              benchmark=_benchmark,
                                              universe=_universe,
                                              initialize=_initialize,
                                              handle_data=_handle_data,
                                              refresh_rate=_refresh_rate,
                                              freq=_freq,
                                              accounts=accounts)
    indexs = copy.copy(g_head_indexs)
    i = 1
    while i <= g_imaxback:
        #make the table title
        added = [x % (i) for x in g_security_return_value]
        indexs = indexs + added
        i = i + 1
    data = pd.DataFrame.from_dict(data=g_security_history, orient='index')
    if g_EMA:
        data.to_excel('龙回头模拟交易Q3%s-%s-EMA-%d.xlsx' %
                      (start, _end, g_targetprice),
                      header=indexs)
    else:
        data.to_excel('龙回头模拟交易Q3%s-%s-%d.xlsx' % (start, _end, g_targetprice),
                      header=indexs)
    cansfiltered = {}
    for k, v in g_candidates.iteritems(
    ):  #filter the candidates already bought before
        if canbuy(k, 99999999., _end, g_imaxback + 1):
            cansfiltered[k] = v
    g_security_history.clear()
    return cansfiltered
    pass
Example #2
0
def startsimulate(_continueday, _end, _benchmark, _universe, _capital_base,
                  _initialize, _handle_data, _refresh_rate, _freq):
    bt, perf = quartz.backtest(start=_continueday,
                               end=_end,
                               benchmark=_benchmark,
                               universe=_universe,
                               capital_base=_capital_base,
                               initialize=_initialize,
                               handle_data=_handle_data,
                               refresh_rate=_refresh_rate,
                               freq=_freq)
    indexs = copy.copy(g_head_indexs)
    i = 1
    while i <= 1:
        #make the table title
        added = [x % (i) for x in g_security_return_value]
        indexs = indexs + added
        i = i + 1
    indexs = indexs + ['soldcondition']
    newcol = [
        sum(v['soldpoint']) / len(v['soldpoint']) / v['cost'] - 1
        for k, v in g_vardic.iteritems()
    ]
    newcol = [sum(newcol)] + newcol
    global excel
    excel['soldcondition'] = newcol
    excel = excel[indexs]
    excel.to_excel('sold.xlsx')
    pass
Example #3
0
def initialize(context):  # 初始化策略运行环境
    pass


def handle_data(context):  # 核心策略逻辑
    account = context.get_account('fantasy_account')


bt, perf, stock = qz.backtest(start=start,
                              end=end,
                              benchmark=benchmark,
                              universe=universe,
                              capital_base=100000.0,
                              initialize=initialize,
                              handle_data=handle_data,
                              refresh_rate=1,
                              freq='d',
                              security_base={},
                              security_cost={},
                              max_history_window=(30, 241),
                              accounts=accounts)
benchmark_return_hs300 = bt[['tradeDate', 'benchmark_return']]

# benchmark数据预加载
save_dir = "alpha_ml"
tmp = pd.read_csv('%s//factors_sxgb_HS300_5.csv' % save_dir)
benchmark_return_zz500 = tmp['benchmark_return']
benchmark_return_hs300 = benchmark_return_hs300['benchmark_return']

# 行业中性组合的指标对比
Example #4
0
def startsimulate(_continueday,_end,_benchmark,_universe,_capital_base,_initialize,_handle_data,_refresh_rate,_freq):
    bt, perf,bt_by_account =  quartz.backtest(start = _continueday,end = _end,benchmark = _benchmark,universe = _universe,capital_base = _capital_base,initialize = _initialize,handle_data = _handle_data,refresh_rate = _refresh_rate,freq = _freq)
    pass
        rebalance_month=[4,8,12]
    if i==6:
        universe = ['510300.XSHG','510500.XSHG','513100.XSHG','163210.XSHE'] ## 波动最小,比较保守
        weight = [1,1,1,2]
        rebalance_month=[4,8,12]
        
   
    # 生成month
    # rebalance_month=[]
    # step=12//(i+1)
    # month_tmp=0
    # for j in range(i+1):
    #     month_tmp+=step
    #     rebalance_month.append(month_tmp)
    
    bt, acct = quartz.backtest(start = start,end = end,benchmark = benchmark,                            universe = universe,             capital_base = capital_base,             initialize = initialize,             handle_data = handle_data,             refresh_rate = refresh_rate)
    newdf = recalculate(bt)
    ((newdf['position_value']-newdf['cost'])/newdf['cost']).plot(figsize=(20,6))
    # print newdf

# newdf['cost'].plot()
# newdf['position_value'].plot()

# newdf['dingtou_cash'].plot()

# perf['benchmark_cumulative_returns'].plot()
pylab.legend(['A', 'usa','bond','Monetary fund','jijian','1:1:1:1','1:1:1:2'])


# In[ ]:
Example #6
0

# 生成策略对象
#strategy = btr.TradingStrategy(initialize, handle_data)
#bt, perf = btr.backtest(sim_params, strategy, data=data)

# 开始回测

data_dict = sim_params.to_dict()

bt, perf, account = btr.backtest(start=data_dict['start'],
                                 end=data_dict['end'],
                                 benchmark=data_dict['major_benchmark'],
                                 universe=data_dict['universe'],
                                 capital_base=data_dict['capital_base'],
                                 refresh_rate=data_dict['refresh_rate'],
                                 freq=data_dict['freq'],
                                 accounts=data_dict['accounts'],
                                 initialize=initialize,
                                 handle_data=handle_data,
                                 preload_data=data)

print('\n策略年化收益率:', format(perf['annualized_return'], '.2%'))
print('基准年化收益率:', format(perf['benchmark_annualized_return'], '.2%'))
print('策略累计收益率:', format(perf['cumulative_returns'].values[-1], '.2%'))
print('基准累计收益率:', format(perf['benchmark_cumulative_returns'].values[-1],
                         '.2%'))
#
#target_stocks = target_stocks[['date', 'industry_1', 'industry_2', 'stock_code', 'stock_name',
#                               'indicator', 'indicator_percent', 'pred_indicator_percent', 'cagr']]
#target_stocks.to_excel('target_stocks_5.xlsx')