ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

常见基本回测指标(年化收益率,夏普率,最大回撤,β值,α值)的实现

2019-07-16 14:00:47  阅读:739  来源: 互联网

标签:code end start 年化 夏普率 fund df 回测 close


年化收益率

import pandas as pd
import tushare as ts
 
def get_annual_profit(maotai,geli,start_date,end_date):
    df_maotai=ts.get_hist_data(maotai,start=start_date,end=end_date)
    df_geli=ts.get_hist_data(geli,start=start_date,end=end_date)
    maotai_annual_profit=(1+(df_maotai.head(1)['close'].values[0]/df_maotai.tail(1)['close'].values[0]-1))**(250/df_maotai.shape[0])-1
    geli_annual_profit=(1+(df_geli.head(1)['close'].values[0]/df_geli.tail(1)['close'].values[0]-1))**(250/df_geli.shape[0])-1
    print u'茅台年化收益: ',maotai_annual_profit,u' 格力电器年化收益: ',geli_annual_profit
 
get_annual_profit('600519','000651','2017-06-01','2017-11-17')

输出:
茅台年化收益: 1.3600202949 格力电器年化收益: 0.82970020908

Sharp率

import pandas as pd
import pymongo,datetime
import tushare as ts
import matplotlib as mpl
import matplotlib.pyplot as plt
 
conn = pymongo.MongoClient()
 
def get_sharp(fund_code):
    c=conn.stks.fund_codes.find_one({'code':fund_code})
    if c==None:
        return
    fund={
        'name':c['name'],
        'fund_id':c['_id'],
        'code':fund_code
    }
    df_fund=pd.DataFrame(list(conn.stks.fund_daily_values.find({
        'fund_id':fund['fund_id'],
        'date':{
            '$gte':datetime.datetime.strptime('2017-01-01','%Y-%m-%d'),
            '$lte':datetime.datetime.now()
        }
    })))
    df_fund.sort_values('date',ascending=True,inplace=True)
    df_fund['change']=df_fund['net_asset_value'].pct_change()
    
    annual_return=(df_fund['net_asset_value'].tail(1).values[0]/df_fund['net_asset_value'].head(1).values[0])**(250/df_fund.shape[0])-1
    lost_free_return=0.04
    sharp=(annual_return-lost_free_return)/df_fund['change'].describe().std()
    print fund['name']+' Sharp=',round(sharp*100,2),'%'
 
funds=['519195','110022','003095','001617','001195','502010','217027']
for f in funds:
    get_sharp(f)

输出

万家品质 Sharp= 0.34 %
易方达消费行业 Sharp= 0.78 %
中欧医疗健康混合A Sharp= 0.33 %
天弘中证电子指数A Sharp= 0.29 %
工银农业产业股票 Sharp= 0.13 %
易方达证券公司分级 Sharp= -0.09 %
招商央视财经50指数A Sharp= 0.54 %

最大回撤

import pandas as pd
import tushare as ts
 
def calculate_max_drawdown(code,start='2017-01-01',end='2017-11-21'):
    df=ts.get_hist_data(code,start=start,end=end)
    highest_close=df['close'].max()
    df['dropdown']=(1-df['close']/highest_close)
    max_dropdown=df['dropdown'].max()
    print 'max dropdown of %s is %.2f%s' % (code,max_dropdown*100,'%')
 
calculate_max_drawdown('002049')

输出:
max dropdown of 002049 is 47.63%

α、β值 与 定价曲线CAPM

import pandas as pd
import datetime,pymongo
import tushare as ts
import matplotlib as mpl
import matplotlib.pyplot as plt
 
def calculate_beta(df_stock,df_hs300,code,start='2017-01-01',end='2017-11-20'):
    df=pd.DataFrame({code:df_stock['close'].pct_change(),'hs300':df_hs300['close'].pct_change()},index=df_stock.index)
    cov=df.corr().iloc[0,1]
    df_hs300['change']=df_hs300['close'].pct_change()*100
    var=df_hs300['change'].var()
    beta=cov/var
    return beta
 
# 定价曲线
def make_capm(code,start='2017-01-01',end='2017-11-20'):
    df_stock=ts.get_hist_data(code,start=start,end=end)
    df_hs300=ts.get_hist_data('hs300',start=start,end=end)
    df_stock.sort_index(ascending=True,inplace=True)
    df_hs300.sort_index(ascending=True,inplace=True)
    beta=calculate_beta(df_stock,df_hs300,code,start=start,end=end)
    loss_free_return=0.04
    df=pd.DataFrame({code:df_stock['close']/df_stock['close'].values[1]-1,
                     'hs300':df_hs300['close']/df_hs300['close'].values[1]-1,
                     'days':xrange(1,df_stock.shape[0]+1)},index=df_stock.index)
    df['beta']=df['days']*loss_free_return/250 + beta*(df['hs300']-df['days']*loss_free_return/250)
    df['alpha']=df[code]-df['beta']
    df[[code,'hs300','beta','alpha']].plot(figsize=(960/72,480/72))
    
 
# make_capm('601318')
 
# make_capm('600030')
# make_capm('600030',start='2017-10-10')
# make_capm('600036')
make_capm('601688')
输出:
19883-aa01a9ec094fc078.png
601688 华泰证券.png

推荐阅读:

1.市面上经典的量化交易策略都在这里了!

2.期货/股票数据大全查询(历史/实时/Tick/财务等)

3.量化交易领域最重要的10本参考书推荐!

4.最科学的仓位管理利器-凯利公式,从方法上胜过99%散户

标签:code,end,start,年化,夏普率,fund,df,回测,close
来源: https://blog.csdn.net/weixin_42219751/article/details/96131093

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有