感谢 写均线优化的那个大神
import matplotlib.pyplot as plt
import pandas as pd
from IPython.display import display
from ipywidgets import *
import seaborn as sns
import datetime
import time
def f(roc):
name = 'Roc'+roc
print risk_all[name]
fig = plt.figure(figsize=(14,4))
ax = fig.add_subplot(111)
ax.plot(results_all[name].index, results_all[name].benchmark_returns)
ax.plot(results_all[name].index, results_all[name].returns)
ax.legend(['benchmark_returns','returns'], loc='best')
ax.set_ylabel('returns',fontsize=16)
ax.set_yticklabels([str(x*100)+'% 'for x in ax.get_yticks()])
ax.set_title("Strategy's performances of %s" % name, fontsize=21)
#--------------------------------------
# 设置要调试的参数
#--------------------------------------
roc = range(30,250,10)
#stockCount = range(1,10,1)
#period = [5,10,15,20]
#scorepb = range(1,20,1)
#scorecmc = range(1,20,1)
#scoreroc = range(1,20,1)
col ={}
#for a in period:
#for b in stockCount:
for c in roc:
# for d in scorepb:
# for e in scorecmc:
# for f in scoreroc:
# col['Roc'+str(c)+'Scorepb'+str(d)+'Scorecmc'+str(e)+'Scoreroc'+str(f)] \
# = {'roc':str(c)+'d','scorepb':d,'scorecmc':e,'scoreroc':f}
col['Roc'+str(c)]={'roc':str(c)+'d',}
df = pd.DataFrame(col).T
print df
roc Roc100 100d Roc110 110d Roc120 120d Roc130 130d Roc140 140d Roc150 150d Roc160 160d Roc170 170d Roc180 180d Roc190 190d Roc200 200d Roc210 210d Roc220 220d Roc230 230d Roc240 240d Roc30 30d Roc40 40d Roc50 50d Roc60 60d Roc70 70d Roc80 80d Roc90 90d
#每十个回测调试一次
#single_ids保存本10次的id 防止kernel中断 可以在中断后根据singleid和finish继续跑,需要人工置顶
#finsh保存回测id
single_ids = {}
single_gt={}
finish={}
for x in arange(0,df.shape[0]+1,10):
if x==df.shape[0]//10*10:
print '{} 至 {}'.format(x,df.shape[0])
for i in arange(x,df.shape[0]):
var = df.ix[i].to_dict()
single_id = create_backtest(algorithm_id='96566e851d99c4e4a0016223c8df8e65', # id 需要自己拷贝
start_date='2006-01-01',
end_date='2016-09-27',
frequency="minute",
initial_cash=100000,
initial_positions=None,
extras=var)
single_ids[df.ix[i].name] = single_id
else:
print '{} 至 {}'.format(x,x+9)
for i in arange(x,x+10):
var = df.ix[i].to_dict()
single_id = create_backtest(algorithm_id='96566e851d99c4e4a0016223c8df8e65', # id 需要自己拷贝
start_date='2006-01-01',
end_date='2016-09-27',
frequency="minute",
initial_cash=100000,
initial_positions=None,
extras=var)
single_ids[df.ix[i].name] = single_id
print single_ids
for key in single_ids.keys():
gt = get_backtest(single_ids[key])
single_gt[key]=gt
while len(single_ids)<>0:
for key in single_ids.keys():
gt=single_gt[key]
if gt.get_status() == 'done':
single_ids.pop(key)
finish[key]=gt
print ',',
time.sleep(10)
print finish
0 至 9 {'Roc110': u'da9d68814b2ae33dd91c20cb53abf6ed', 'Roc120': u'949435af2057046381ba60ca0cc75642', 'Roc130': u'f8c37e317f6fb4110b7fa7f4d75b0594', 'Roc190': u'adf42d95d4d612e442db8aea2616e2a7', 'Roc160': u'60d2bd4677253045acc8c5823bf8806d', 'Roc170': u'fb3d947f72f37afdf44a235fb16fa16d', 'Roc100': u'864ff111a1319278f6edbee46e3fbe5d', 'Roc140': u'b92dd5d6adec82106cdc2561a2d70e11', 'Roc150': u'fb68bd759ec57ce7be211aaf9dd8003a', 'Roc180': u'fe5fd65e57495cf453f9de9907d5c2f3'} , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , {'Roc110': Backtest(id="da9d68814b2ae33dd91c20cb53abf6ed"), 'Roc120': Backtest(id="949435af2057046381ba60ca0cc75642"), 'Roc130': Backtest(id="f8c37e317f6fb4110b7fa7f4d75b0594"), 'Roc190': Backtest(id="adf42d95d4d612e442db8aea2616e2a7"), 'Roc160': Backtest(id="60d2bd4677253045acc8c5823bf8806d"), 'Roc170': Backtest(id="fb3d947f72f37afdf44a235fb16fa16d"), 'Roc100': Backtest(id="864ff111a1319278f6edbee46e3fbe5d"), 'Roc140': Backtest(id="b92dd5d6adec82106cdc2561a2d70e11"), 'Roc150': Backtest(id="fb68bd759ec57ce7be211aaf9dd8003a"), 'Roc180': Backtest(id="fe5fd65e57495cf453f9de9907d5c2f3")} 10 至 19 {'Roc40': u'52d6a6717c5b2fca4943a0f26d88498e', 'Roc240': u'1038711083000276b240e01bb054d7a0', 'Roc50': u'4203b0b14201d89ed4813ea3af28c7c9', 'Roc230': u'edf51e40698aa0da7e01166235f0ea29', 'Roc220': u'86d0af2bae17ad2a605afde50a188f14', 'Roc30': u'75a466cf71ec0b44339513007cfa0d3c', 'Roc70': u'b33c624d111a88c89f349de26725b3f0', 'Roc210': u'98f2b3738448c0e0356621831b8cc678', 'Roc200': u'38d3f3ac62fec974147ef2190b32deae', 'Roc60': u'9b441296742b02960f322a042b5113a4'} , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , {'Roc60': Backtest(id="9b441296742b02960f322a042b5113a4"), 'Roc210': Backtest(id="98f2b3738448c0e0356621831b8cc678"), 'Roc240': Backtest(id="1038711083000276b240e01bb054d7a0"), 'Roc40': Backtest(id="52d6a6717c5b2fca4943a0f26d88498e"), 'Roc110': Backtest(id="da9d68814b2ae33dd91c20cb53abf6ed"), 'Roc200': Backtest(id="38d3f3ac62fec974147ef2190b32deae"), 'Roc50': Backtest(id="4203b0b14201d89ed4813ea3af28c7c9"), 'Roc120': Backtest(id="949435af2057046381ba60ca0cc75642"), 'Roc130': Backtest(id="f8c37e317f6fb4110b7fa7f4d75b0594"), 'Roc220': Backtest(id="86d0af2bae17ad2a605afde50a188f14"), 'Roc190': Backtest(id="adf42d95d4d612e442db8aea2616e2a7"), 'Roc160': Backtest(id="60d2bd4677253045acc8c5823bf8806d"), 'Roc170': Backtest(id="fb3d947f72f37afdf44a235fb16fa16d"), 'Roc30': Backtest(id="75a466cf71ec0b44339513007cfa0d3c"), 'Roc100': Backtest(id="864ff111a1319278f6edbee46e3fbe5d"), 'Roc70': Backtest(id="b33c624d111a88c89f349de26725b3f0"), 'Roc230': Backtest(id="edf51e40698aa0da7e01166235f0ea29"), 'Roc140': Backtest(id="b92dd5d6adec82106cdc2561a2d70e11"), 'Roc150': Backtest(id="fb68bd759ec57ce7be211aaf9dd8003a"), 'Roc180': Backtest(id="fe5fd65e57495cf453f9de9907d5c2f3")} 20 至 22 {'Roc80': u'4557a94b7d2c65ab28b47fad7969a9e8', 'Roc90': u'65826767e46c1113aee869f32e7d12db'} , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , {'Roc110': Backtest(id="da9d68814b2ae33dd91c20cb53abf6ed"), 'Roc130': Backtest(id="f8c37e317f6fb4110b7fa7f4d75b0594"), 'Roc190': Backtest(id="adf42d95d4d612e442db8aea2616e2a7"), 'Roc40': Backtest(id="52d6a6717c5b2fca4943a0f26d88498e"), 'Roc60': Backtest(id="9b441296742b02960f322a042b5113a4"), 'Roc160': Backtest(id="60d2bd4677253045acc8c5823bf8806d"), 'Roc220': Backtest(id="86d0af2bae17ad2a605afde50a188f14"), 'Roc80': Backtest(id="4557a94b7d2c65ab28b47fad7969a9e8"), 'Roc100': Backtest(id="864ff111a1319278f6edbee46e3fbe5d"), 'Roc140': Backtest(id="b92dd5d6adec82106cdc2561a2d70e11"), 'Roc200': Backtest(id="38d3f3ac62fec974147ef2190b32deae"), 'Roc240': Backtest(id="1038711083000276b240e01bb054d7a0"), 'Roc120': Backtest(id="949435af2057046381ba60ca0cc75642"), 'Roc50': Backtest(id="4203b0b14201d89ed4813ea3af28c7c9"), 'Roc30': Backtest(id="75a466cf71ec0b44339513007cfa0d3c"), 'Roc70': Backtest(id="b33c624d111a88c89f349de26725b3f0"), 'Roc180': Backtest(id="fe5fd65e57495cf453f9de9907d5c2f3"), 'Roc230': Backtest(id="edf51e40698aa0da7e01166235f0ea29"), 'Roc170': Backtest(id="fb3d947f72f37afdf44a235fb16fa16d"), 'Roc90': Backtest(id="65826767e46c1113aee869f32e7d12db"), 'Roc210': Backtest(id="98f2b3738448c0e0356621831b8cc678"), 'Roc150': Backtest(id="fb68bd759ec57ce7be211aaf9dd8003a")}
#生产收益分析字典
results_all={}
risk_all={}
for i in finish.keys():
ret_tmp = pd.DataFrame(finish[i].get_results()).set_index('time')
ret_tmp.index = pd.to_datetime(ret_tmp.index)
risk_tmp = pd.DataFrame(finish[i].get_risk())
risk_tmp = risk_tmp[['algorithm_return','annual_algo_return','benchmark_return','alpha','beta','sharpe','sortino','information','max_drawdown']].drop_duplicates()
risk_tmp.columns = ['策略收益','策略年化收益','基准收益','Alpha','Beta','Sharpe','Sortino','Information Ratio','最大回撤']
results_all[i] = ret_tmp
risk_all[i] = risk_tmp
#--------------------------------------
# 将各回测的表现情况作图比较
#--------------------------------------
fig = plt.figure(figsize=(28,14))
ax = fig.add_subplot(111)
for i in results_all.keys():
ax.plot(results_all[i].index, results_all[i]['returns'], label=i)
ax.legend(loc='best')
ax.set_ylabel('returns',fontsize=16)
ax.set_yticklabels([str(x*100)+'% 'for x in ax.get_yticks()])
ax.set_title("Strategy's performances of different levels of roc", fontsize=21)
<matplotlib.text.Text at 0x7f74fcee4ed0>
#单个指标看图
container = widgets.HBox()
Select_Roc = widgets.Dropdown(description='Select_Roc:', options=['30','40','50','60','70','80','90','100','110','120','130','140','150','160','170','180','190','200','210','220','230','240'],border_radius=4,margin=20)
container.children = (Select_Roc,)
i = interactive(f, roc=Select_Roc)
display(container)
策略收益 策略年化收益 基准收益 Alpha Beta Sharpe Sortino \ 0 2169.88038 1.087378 2.509395 0.964556 0.943496 2.654752 3.212907 Information Ratio 最大回撤 0 2.361608 0.583131
本社区仅针对特定人员开放
查看需注册登录并通过风险意识测评
5秒后跳转登录页面...