为了进一步打通研究模块与回测模块,我们增加了在研究中调用回测的新功能。
主要功能如下:
- 可以在研究中创建回测
- 设定初始持仓
- 设定全局变量值
- 获取回测状态
- 获得回测参数
- 获得收益曲线
- 获得持仓详情
- 获得交易详情
- 获得所有 record 记录
- 获得总的风险指标
- 获得分月计算的风险指标
具体功能参看 API - 研究中创建回测函数
具体用法见下面的研究内容。
研究模块调用回测功能
首先打开策略编译页面,找到algorithmId, 如下所示(上方红色隐去的部分):
测试所使用的代码如下所示:
def initialize(context): # 定义一个全局变量, 保存要操作的股票 # 000001(股票:平安银行) g.security = '000001.XSHE' # 初始化此策略 # 设置我们要操作的股票池, 这里我们只操作一支股票 set_universe([g.security]) # 设置均线长度 g.ma_long = 5 g.m = 1.01 # 每个单位时间(如果按天回测,则每天调用一次,如果按分钟,则每分钟调用一次)调用一次 def handle_data(context, data): security = g.security # 取得过去五天的平均价格 average_price = data[security].mavg(g.ma_long, 'close') # 取得上一时间点价格 current_price = data[security].close # 取得当前的现金 cash = context.portfolio.cash # 如果上一时间点价格高出五天平均价1%, 则全仓买入 if current_price > g.m*average_price: # 用所有 cash 买入股票 order_value(security, cash) # 记录这次买入 log.info("Buying %s" % (security)) # 如果上一时间点价格低于五天平均价, 则空仓卖出 elif current_price < average_price and context.portfolio.positions[security].sellable_amount > 0: # 卖出所有股票,使这只股票的最终持有量为0 order_target(security, 0) # 记录这次卖出 log.info("Selling %s" % (security)) # 画出上一时间点价格 record(stock_price=current_price)
接下来设定初始化仓位,设定g.全局变量的值,当然也可以选择不设置:
# 设定初始仓位 InitialPositions = [ { 'security': '000001.XSHE', 'amount': '1000', # 100股
'avg_cost': '10.0' # 持仓均价10元 } ] # 设定g.全局变量的值 extra_vars = {'ma_long': 5, 'm': 1.01} 收起代码 ↑
传入create_backtest函数的algorithm_id切记要改成自己的algorithmId,否则会提示错误:
Exception: can not find algorithm
获取 algorithmId 文章初始所示。
# 记得此处传入你自己的 algorithmId out_algorithm_id = create_backtest('bfdde744e43517e4de41d8bc0ffd59d7','2015-01-01', '2016-06-01', \ frequency="day",initial_cash=100000, initial_positions=InitialPositions, extras=extra_vars) out_algorithm_id
u'849c256bca210cd95e2d2bc9219239b3'
out_algorithm_id = '456bbf8cd483f21218528c45335cb229'
gt = get_backtest(out_algorithm_id)
gt.get_status() # 获取回测状态
u'running'
gt.get_params() # 获取回测参数
{u'algorithm_id': u'bfdde744e43517e4de41d8bc0ffd59d7', u'end_date': u'2016-06-01 23:59:59', u'extras': {u'm': 1.01, u'ma_long': 5}, u'frequency': u'day', u'initial_cash': u'100000',展开输出 ↓
gt.get_results() # 获取收益曲线
[{u'benchmark_returns': 0.030516412660366532, u'returns': 0.10932029999999981, u'time': u'2015-01-05'}, {u'benchmark_returns': 0.03038001191384132, u'returns': 0.0919802999999999,展开输出 ↓
gt.get_positions() # 获取所有持仓列表
[{u'amount': 10200, u'avg_cost': 10.775686274509804, u'closeable_amount': 10200, u'price': 10.87, u'security': u'000001.XSHE',展开输出 ↓
gt.get_orders() # 获取交易列表
[{u'action': u'open', u'amount': 9200, u'commission': 29.97, u'filled': 9200, u'price': 10.86,展开输出 ↓
gt.get_records() # 获取所有record()记录
[{u'stock_price': 10.74, u'time': u'2015-01-05'}, {u'stock_price': 10.87, u'time': u'2015-01-06'}, {u'stock_price': 10.7, u'time': u'2015-01-07'}, {u'stock_price': 10.5, u'time': u'2015-01-08'}, {u'stock_price': 10.15, u'time': u'2015-01-09'},展开输出 ↓
gt.get_risk() # 获取总的风险指标
{u'algorithm_return': -0.03993160000000073, u'algorithm_volatility': 0.26412380390523005, u'alpha': -0.03924882545523693, u'annual_algo_return': -0.029097790469759577, u'annual_bm_return': -0.07768720537202145,展开输出 ↓
k = gt.get_risk() k['max_drawdown_period'] = [(u'2015-04-16', u'2015-10-28')] import pandas as pd pd.DataFrame(k)
algorithm_return | algorithm_volatility | alpha | annual_algo_return | annual_bm_return | benchmark_return | benchmark_volatility | beta | excess_return | information | max_drawdown | max_drawdown_period | max_leverage | period_label | sharpe | sortino | trading_days | treasury_return | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | -0.039932 | 0.264124 | -0.039249 | -0.029098 | -0.077687 | -0.1056 | 0.370343 | 0.25363 | -0.096151 | 0.044267 | 0.25707 | (2015-04-16, 2015-10-28) | 0 | 2016-06 | -0.261611 | -0.357912 | 345 | 0.056219 |
gt.get_period_risks() # 获取分月计算的风险指标
{'algorithm_return': one_month three_month six_month twelve_month 2015-01 -0.078339 NaN NaN NaN 2015-02 -0.013155 NaN NaN NaN 2015-03 0.058672 -0.037099 NaN NaN 2015-04 0.195223 0.248704 NaN NaN展开输出 ↓
gt.get_period_risks()['alpha'] # 获取分月计算的风险指标中的alpha
one_month | three_month | six_month | twelve_month | |
---|---|---|---|---|
2015-01 | -0.340114 | NaN | NaN | NaN |
2015-02 | -0.285462 | NaN | NaN | NaN |
2015-03 | -0.705164 | -0.771542 | NaN | NaN |
2015-04 | 2.222741 | -0.120284 | NaN | NaN |
2015-05 | -0.683038 | -0.435415 | NaN | NaN |
2015-06 | 0.761667 | 0.769600 | 0.005202 | NaN |
2015-07 | -0.475122 | -0.254308 | 0.148066 | NaN |
2015-08 | -0.378048 | -0.229071 | 0.173228 | NaN |
2015-09 | -0.210559 | -0.392439 | 0.042345 | NaN |
2015-10 | -1.756624 | -0.329792 | -0.290167 | NaN |
2015-11 | 0.696051 | -0.158699 | -0.135661 | NaN |
2015-12 | -0.356621 | -0.268037 | -0.241502 | -0.077842 |
2016-01 | -0.040000 | 0.167151 | -0.099187 | 0.050571 |
2016-02 | -0.026688 | -0.058275 | -0.033985 | 0.073793 |
2016-03 | -0.488207 | 0.087901 | 0.033778 | 0.037806 |
2016-04 | -0.242546 | -0.120085 | 0.056010 | -0.137978 |
2016-05 | -0.040000 | -0.141177 | -0.050841 | -0.093103 |
2016-06 | -0.649478 | -0.185857 | -0.033635 | -0.149710 |
祝大家使用愉快~~~