#----这里是首先要运行一次的文件
import pandas as pd
import requests
import json
def request_position_by_api_key(uname, key,notebook_id):
position_url = 'https://bigquant.com/bigwebapi/algo_info/plot_return'
r = requests.get(url=position_url, params={
'owner': uname,
'api_key': key,
'notebook_id': notebook_id,
})
return json.loads(r.text)
uname='huasan123' #这里填写你的账号用户名 比如账号用户名id
#这里填写你刚才在api中获取到的的 token_id
api_token = 'tk_29a5e47ed3a692d98d06fd1ff00f7922'
"""
api_token_id
"""
#----- 下面的 ids 填写的 是 我的交易中的策略notebook_id
# ids=['策略1的notebook_id','策略2的notebook_id','策略3的notebook_id']
ids=['fdf2fc50-5122-11ed-b228-aa86a019e7f0','385b63bc-493c-11ed-af33-c65daa70943c']
def get_strategy_metrics(notebook_id, owner, api_token):
url = f"https://bigquant.com/bigwebapi/algo_info/plot_return?owner={owner}¬ebook_id={notebook_id}&limit=-1&api_key={api_token}"
response = requests.get(url)
sharpe = response.json()["data"]["algo_info_plot_return"]["sharpe"]
return sharpe
def get_strategy_signal(api_token, ids):
url = 'https://bigquant.com/bigwebapi/algo_info/planned_orders'
headers = {'Authorization': 'Bearer {}'.format(api_token.strip().replace('',""))}
data = {
'id_list': ids
}
r = requests.post(url=url, data=data, headers=headers)
res_dict = json.loads(r.text)
return res_dict
# api_token.strip().replace('',"")
ids
sharpes = [get_strategy_metrics(ids[i], 'huasan123', api_token) for i in range(len(ids))]
res_dict =get_strategy_signal(api_token, ";".join(ids))
signals =[res_dict["data"][i]["planned_orders"] for i in range(len(ids))]
# sharpes
# res_dict
# signals
owner='huasan123'
api_token='tk_29a5e47ed3a692d98d06fd1ff00f7922'
notebook_id='fdf2fc50-5122-11ed-b228-aa86a019e7f0'
url = f"https://bigquant.com/bigwebapi/algo_info/plot_return?owner={owner}¬ebook_id={notebook_id}&limit=-1&api_key={api_token}"
response = requests.get(url)
sharpe = response.json()["data"]["algo_info_plot_return"]["sharpe"]
sharpe
# url
#这个是空的dataframe
df_empty=pd.DataFrame()
for i in ids:
#这里把自定中的 key value分离出来
result =[request_position_by_api_key('huasan123','tk_29a5e47ed3a692d98d06fd1ff00f7922',i)['data']['algo_info_plot_return']]
data = pd.DataFrame(result)
#我们把我的交易中的策略绩效数据爬取出来
del data['after_shared_cum_return_plot'],data['before_shared_cum_return_plot'],data['benchmark_cum_return_plot'],data['max_drawdown_stamp'],data['cum_return_plot']
data.reset_index(inplace=True, drop=True)
data.rename(columns={
'algo_name':'策略名',
'annual_return':'年化收益',
'cum_return':'累计收益净值',
'alpha':'alpha',
'sharpe':'夏普比率',
'today_return':'日收益',
'win_ratio':'胜率',
'max_drawdown':'最大回撤',
'week_return':'周收益',
'month_return':'月收益',
'volatility':'波动率'
}, inplace=True)
df_empty=df_empty.append(data)
df_empty
df_empty1=pd.DataFrame(df_empty,columns=['策略名','年化收益','累计收益净值','alpha','夏普比率','胜率','最大回撤','日收益','周收益','月收益','波动率'])
#data
df_empty1['年化回撤比']=df_empty1['年化收益']/df_empty1['最大回撤']
df_empty1['年化波动比']=df_empty1['年化收益']/df_empty1['波动率']
df_empty2=df_empty1.sort_values(by=['夏普比率'], ascending=0)
#df_empty2=df_empty1.sort_values(by=['年化回撤比'], ascending=0)
#df_empty2=df_empty1.sort_values(by=['年化波动比'], ascending=0)
df_empty2
stock_count=len(ids)
stock_weight=T.norm([1 / math.log(i + 2) for i in range(0, stock_count)])
stock_weight
df_empty2['资金分配到该策略的仓位比例']=stock_weight
df_empty2
# df_empty2
M3 = M.multi_strategy_analysis.v1(
input_1=m32.data,
DataSource_list=['fdf2fc50-5122-11ed-b228-aa86a019e7f0','385b63bc-493c-11ed-af33-c65daa70943c'],
ratio_list=[1/3, 2/3],
start_date='2022-10-22',
end_date='2023-02-14',
index='000300.HIX'
)
# M3