复制链接
克隆策略
In [1]:
#----这里是首先要运行一次的文件
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']
In [2]:
def get_strategy_metrics(notebook_id, owner, api_token):
    url = f"https://bigquant.com/bigwebapi/algo_info/plot_return?owner={owner}&notebook_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
    
In [3]:
# api_token.strip().replace('',"")
ids
Out[3]:
['fdf2fc50-5122-11ed-b228-aa86a019e7f0',
 '385b63bc-493c-11ed-af33-c65daa70943c']
In [6]:
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))]
In [7]:
# sharpes
Out[7]:
[2.8657216264727268, 1.0748496943608166]
In [5]:
# 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}&notebook_id={notebook_id}&limit=-1&api_key={api_token}"
response = requests.get(url)
sharpe = response.json()["data"]["algo_info_plot_return"]["sharpe"]
sharpe
Out[5]:
2.8657216264727268
In [8]:
# url
In [11]:
#这个是空的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
Out[11]:
策略名 年化收益 benchmark_symbol capital_base count 累计收益净值 description error_data first_date grade ... subscriber_active_status subscribe_by_price follow_status want_subscribe_status benchmark_name alpha beta 波动率 夏普比率 ir
0 股票双均线策略 0.316065 000300.HIX 1000000.0 0.088728 2022-10-21 00:00:00 99 ... 0 0 沪深300 0.167502 0.313563 0.086872 2.865722 -0.022762
0 可视化策略-小市值选股1011 0.374509 000300.HIX 1000000.0 0.107655 2022-10-14 00:00:00 113 ... 0 0 沪深300 0.336715 0.206371 0.310459 1.074850 0.022925

2 rows × 63 columns

In [12]:
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
Out[12]:
策略名 年化收益 累计收益净值 alpha 夏普比率 胜率 最大回撤 日收益 周收益 月收益 波动率 年化回撤比 年化波动比
0 股票双均线策略 0.316065 0.088728 0.167502 2.865722 0.615885 0.031589 0.004336 0.022823 0.028436 0.086872 10.005600 3.638302
0 可视化策略-小市值选股1011 0.374509 0.107655 0.336715 1.074850 0.333333 0.128525 -0.004188 0.029122 0.172586 0.310459 2.913901 1.206307
In [17]:
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
Out[17]:
策略名 年化收益 累计收益净值 alpha 夏普比率 胜率 最大回撤 日收益 周收益 月收益 波动率 年化回撤比 年化波动比 资金分配到该策略的仓位比例
0 股票双均线策略 0.316065 0.088728 0.167502 2.865722 0.615885 0.031589 0.004336 0.022823 0.028436 0.086872 10.005600 3.638302 0.613147
0 可视化策略-小市值选股1011 0.374509 0.107655 0.336715 1.074850 0.333333 0.128525 -0.004188 0.029122 0.172586 0.310459 2.913901 1.206307 0.386853
In [20]:
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 
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-20-6aeba62465a8> in <module>
      1 M3 = M.multi_strategy_analysis.v1(
----> 2     input_1=m32.data,
      3     DataSource_list=['fdf2fc50-5122-11ed-b228-aa86a019e7f0','385b63bc-493c-11ed-af33-c65daa70943c'],
      4     ratio_list=[1/3, 2/3],
      5     start_date='2022-10-22',

NameError: name 'm32' is not defined
In [ ]:
 
In [ ]: