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

年化收益率

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')

输出:


常见基本回测指标(年化收益率,夏普率,最大回撤,β值,α值)的实现_第1张图片
601688 华泰证券.png

你可能感兴趣的:(常见基本回测指标(年化收益率,夏普率,最大回撤,β值,α值)的实现)