almar ratio 是一种基金评价方法,它使用收益回撤比衡量基金市场表现。
from WindPy import *
import pandas as pd
import numpy as np
from datetime import datetime
import re
w.start()
#基金名单预处理
#基金存续两年以上 设置date控制
#股票型基金
e,f_1=w.wset("sectorconstituent", "date=2016-1-1;sectorId=1000002533000000", usedf=True)
#混合型基金
e,f_2=w.wset("sectorconstituent", "date=2016-1-1;sectorId=1000002535000000", usedf=True)
#QDII型基金
e,f_3=w.wset("sectorconstituent", "date=2016-1-1;sectorId=a201010900000000", usedf=True)
#LOF基金 a201010a00000000
e,f_5=w.wset("sectorconstituent", "date=2016-1-1;sectorId=a201010a00000000", usedf=True)
f_code=list(f_1['wind_code'])+list(f_2['wind_code'])+list(f_3['wind_code'])+list(f_5['wind_code'])
print(len(f_code))
data_list=f_code
new_data_list = [] # 保存筛选出来dog的列表
for data in data_list: # 遍历列表
if re.match('\d+.OF', data) != None: # 如果正则匹配出的数据不为None, 就将此数据添加到新列表中
new_data_list.append(data)
f_code=new_data_list
print(len(f_code))
f_code=list(set(f_code))
e,f=w.wss(f_code, "fund_fundscale,fund_corp_fundmanagementcompany", "", usedf=True)
#筛选规模在10亿元以上
fund_after1=f[(f['FUND_FUNDSCALE']>10**9)]
#筛选基金公司>100亿元
fund_firm=pd.read_csv('data/211228target_fund_firm.csv',encoding="gbk")
def judge_firm(x):
if x in list(fund_firm['基金公司简称']):
y=1
else:
y=0
return y
fund_after1['规模标记符']=fund_after1['FUND_CORP_FUNDMANAGEMENTCOMPANY'].apply(lambda x:judge_firm(x))
fund_after2=fund_after1[fund_after1['规模标记符']==1]
core_fund_list=list(fund_after2.index)
result_df=pd.DataFrame()
#算收益
years=['2016','2017','2018']
for year in years:
e,df=w.wsd(core_fund_list, "nav_acc", "ED-1Y", year+"-12-31", "Period=D", usedf=True)
df=df.T
def f(x):
pullback=1-(x.min()/x[0])
if pullback<0.04:
pullback=0.04
else:
pass
return pullback
#注意bug 回撤计算有个最小值,一定要第一个处理
df['interval_max_pullback']=df.apply(lambda x:f(x),axis=1)
df['interval_r']=(df[df.columns[len(df.columns)-2]])/df[df.columns[0]]-1
df['calmar_rate']=df['interval_r']/(df['interval_max_pullback'])
result_df['calmar_rate'+year]=df['calmar_rate']
result_df
bench_list=['881001.WI']
#基准
bench_df=pd.DataFrame()
#算收益
for year in years:
e,df=w.wsd(bench_list, "close", "ED-1Y", year+"-12-31", "Period=D", usedf=True)
df=df.T
def f(x):
pullback=1-(x.min()/x[0])
if pullback<0.04:
pullback=0.04
else:
pass
return pullback
#注意bug 回撤计算有个最小值,一定要第一个处理
df['interval_max_pullback']=df.apply(lambda x:f(x),axis=1)
df['interval_r']=(df[df.columns[len(df.columns)-2]])/df[df.columns[0]]-1
df['calmar_rate']=df['interval_r']/(df['interval_max_pullback'])
bench_df['calmar_rate'+year]=df['calmar_rate']
bench_df
#筛选 负值取0.8
multiplier=2
multiplier2=0.8
#result=result_df[(result_df['calmar_rate2018']>bench_df.ix[0][0]*multiplier)&(result_df['calmar_rate2019']>bench_df.ix[0][1]*multiplier)&(result_df['calmar_rate2020']>bench_df.ix[0][2]*multiplier)]
result=result_df[(result_df['calmar_rate2016']>bench_df.ix[0][0]*multiplier2)&(result_df['calmar_rate2017']>bench_df.ix[0][1]*multiplier)&(result_df['calmar_rate2018']>bench_df.ix[0][2]*multiplier2)]
result
#检测模块
e,df=w.wsd('005760.OF', "nav_acc", "ED-1Y", "2021-12-31", "Period=D", usedf=True)
df=df.T
def f(x):
pullback=(x.min()/x[0])-1
return pullback
#注意bug 回撤计算有个最小值,一定要第一个处理
df['interval_max_pullback']=df.apply(lambda x:f(x),axis=1)
df['interval_r']=(df[df.columns[len(df.columns)-2]])/df[df.columns[0]]-1
df['calmar_rate']=df['interval_r']/(-1*df['interval_max_pullback'])
df
#回测函数
bench_list=['881001.WI']
strategy_list=list(result.index)
back_test_begin="2019-01-01"
back_test_end="2019-12-31"
e,strategy_df=w.wsd(strategy_list, "nav_acc", back_test_begin,back_test_end, "Fill=Previous", usedf=True)
strategy_df=strategy_df.T
strategy_df['回测收益']=strategy_df[strategy_df.columns[len(strategy_df.columns)-1]]/strategy_df[strategy_df.columns[0]]-1
print('策略收益',strategy_df['回测收益'].mean())
e,fund_df=w.wsd(core_fund_list, "nav_acc", back_test_begin,back_test_end, "Fill=Previous", usedf=True)
fund_df=fund_df.T
fund_df['回测收益']=fund_df[fund_df.columns[len(fund_df.columns)-1]]/fund_df[fund_df.columns[0]]-1
print('公募基金平均收益',fund_df['回测收益'].mean())
e,bench_df=w.wsd(bench_list, "close", back_test_begin,back_test_end, "Period=D", usedf=True)
bench_df=bench_df.T
bench_df['回测收益']=(bench_df[bench_df.columns[len(bench_df.columns)-2]])/bench_df[bench_df.columns[0]]-1
print('基准回测收益',bench_df['回测收益'].ix[0])
想了解市场真实回测结果或获取更多量化选基知识,欢迎关注我们的微信公众号 ourwechat-271828