python 量化策略——Fama-French 三因⼦模型
1. 介绍:Fama-French三因⼦模型,是Fama和French 1992年对美国股票市场决定不同股票回报率差异的因素的研究发现,股票的市场的beta值不能解释不同股票回报率的差异,⽽上市公司的市值、账⾯市值⽐、市盈率可以解释股票回报率的差异。Fama and French 认为,上述超额收益是对CAPM 中β未能反映的风险因素的补偿。这三个因⼦是:市场资产组合(Rm − Rf)、市值因⼦(SMB)、账⾯市值⽐因⼦(HMI)。这个多因⼦均衡定价模型可以表⽰为:
是市场风险溢价,表⽰时间的市值市值因⼦的模拟组合收益率,为时间的账⾯市值⽐因⼦的组合收益率。是三因⼦的系数。此外真实收益 :
黔轮胎这样我们可以得到如下回归模型:
,我们选取市经率的倒数作为、总市值total_mv为和资产历史收益率作为三因⼦进⾏回归。
随便选⼀只股票查看他们的相关关系
# coding=utf-8
import math
import tushare as ts
import pandas as pd
import matplotlib
import matplotlib .pyplot as plt
import numpy as np
安全座椅立法规定import talib
import pandas as pd
from datetime import datetime , date
matplotlib .rcParams ['axes.unicode_minus']=False
plt .rcParams ['font.sans-serif']=['SimHei']
ts .set_token ('。。。。。')
pro = ts .pro_api ()
df =pro .query ('daily_basic', ts_code ='600300.SH',fields ='close,ts_code,ps,total_mv')
df .corr
()
代码运⾏需⾥
完整代码
# coding=utf-8
import math
import tushare as ts
import pandas as pd
import matplotlib
import matplotlib .pyplot as plt
import numpy as np
import talib
import pandas as pd
from datetime import datetime , date
from sklearn import datasets
from sklearn .model_selection import train_test_split
import matplotlib .pyplot as plt
from sklearn .linear_model import LinearRegression
东方之子改装
E (R )−it R =ft βE [R −i mt R ]+ft s E (SMB )+i t h E (HMI )
i i R ft R mt t R it i t E (R )−mt R ft SMB T t HMI t t β,s ,h i i i R =it E (R )−it αi
R −it R =ft α+i β(R −i mt R )+ft s SMBI +i t hiHMI +t ϵit
1/pb HMI SMB R it
from sklearn.linear_model import LinearRegression
ts.set_token('f3e00efb72token 码11477')
pro = ts.pro_api()
>>>>>###读取数据类>>>>>>>
class readData:
def read_index_daily(self,code,star,end):#指数数据
dsb = pro.index_daily(ts_code=code, start_date=star, end_date=end,fields='ts_code,trade_date,close,change')#默认读取三个数据return dsb
def read_daily(self,code,star,end):
dsc1 = pro.daily(ts_code=code, start_date=star, end_date=end,fields='ts_code,trade_date,close')
return dsc1
def read_CPI(self,star,end):#时间格式start_cpi='201609'
dc=pro_cpi(start_m=star, end_m=end,fields='month,nt_yoy')
return dc
def read_GDP(self,star,end):#时间格式star='2016Q4'
df1 = pro_gdp(start_q=star, end_q=end,fields='quarter,gdp_yoy')
return df1
def read_bond(self,code,star,end):
df=pro.cb_daily(ts_code=code,start_date=star,end_date=end)
def read_base(self,code):
df=pro.query('daily_basic', ts_code=code,fields='close,ts_code,pb,total_mv,trade_date')
return df
>>>>>>>>>>>>>####
start_time='20200110'#发布GDP需要时间,我们延迟1个⽉,即第⼀季度的GDP4⽉份才发布。
end_time="20200331"
dc=readData()
dsc1=readData()
奇瑞a3两厢论坛dsb1=readData()
def alpha_fun(code):
ad_base(code).fillna(0)
ad_index_daily('000300.SH',start_time,end_time)
dsc.set_index(['trade_date'],inplace=True)
dsb.set_index(['trade_date'],inplace=True)
(dsc, dsb, on='trade_date').fillna(0)
shape( np.array([df.close_y]),(-1,1))
R_shape( np.array([(df.change/(df.close_x.shift(-1))).fillna(0)]),(-1,1))#⽤0 填充nan
shape( np.array([(1/df.pb).fillna(0)]),(-1,1))
shape( np.array([ df.total_mv]),(-1,1))
atenate(( R_f-4/252, HMI,SMB ),axis=1)
shape(R,(1,-1)).T
X_train, X_test, y_train, y_test = train_test_split(X, y1, test_size=0.3, random_state=0)
linear = LinearRegression()
linear.fit(X_train, y_train)
alpha=linear.intercept_-4/252
return alpha,linear.intercept_ ,f_,linear.score(X_test, y_test),df
def Sy_function(df1,star,end):
df=pro.query('daily', ts_code=df1, start_date=star, end_date=end,fields='')
df=df.sort_index()
df._ade_date,format='%Y-%m-%d')#设置⽇期索引
ret=df.change/df.close.shift(-1)
dd=pd.Series(1,index=df.close.index)
cumqq=ret*dd.shift(1).fillna(0)
cum=(np.cumprod(1+ret[cumqq.index[0:]])-1)#等权重配置⼀篮⼦股票
return cum.fillna(0),ret.fillna(0)
co=pro.query('daily_basic', ts_code="",trade_date="20200203",fields='ts_code')
code_list=[]
N=300#股票池
k=0
ret=0
cum=0
for i in co.ts_code.values[0:N]:
try:
if alpha_fun(i)[0]<-10:
k+=1
ret=Sy_function(str(i),start_time,end_time)[1]+ret
except ValueError:
pass
continue
ret=ret.sort_index(axis=0,ascending=True)
cum=np.cumprod(1+ret)-1#
RET=ret/k
>>>>计算收益率函数,如沪深300>>>>>>>##
本文标签:股票 时间 收益率
def JZ_function(code,star,end):
df12 = pro.index_daily( ts_code=code, start_date=star, end_date=end)
df12=df12.sort_index()
df12._ade_date,format='%Y-%m-%d')#设置⽇期索引
ret12=df12.change/df12.close.shift(-1)
#将顺序颠倒
aq=pd.Series(1,index=df12.close.index)
SmaRet=ret12*aq.shift(1).dropna()
cum12=np.cumprod(1+ret12[SmaRet.index[0:]])-1
return cum12
>>>>>####策略的年化统计>>>>>>>###
def Tongji(RET,cum):
RET1 = RET*100-(4/252)
NH=cum[-2]*100*252/len(RET.index)
BD=np.std(RET)*100*np.sqrt(252)
拓乐行李架SR=(NH-400/252)/BD
for i in range(len(cum)):
if cum[cum.index[i]]==cum.max():
奔驰宝马降价MHC=(cum.max()-cum[cum.index[i:]].min())*100/cum.max()
print("年化收益率:{:.2f}%:,年化夏普率:{:.2f},波动率为:{:.2f}%,最⼤回撤:{:.2f}%".format( NH,SR,BD,MHC)) >>>>>>>>>>>>>>>#
if __name__=="__main__":
cum12=JZ_function('000300.SH',start_time,end_time)
Tongji(RET,cum)
plt.plot(cum12,label="沪深300",color='b')
plt.plot(cum,label="股票组合",color='r')
plt.title("alpha股+指期对冲策略")
plt.legend()
#a=alpha_fun('600300.SH')
#print("alpha:{}".format( a[0] ))
#print('f截距:{}'.format(a[1]))
#print(f'系数:{a[2]}')
#print(f'准确率:{a[3]:.4f}')
本文发布于:2024-12-27 05:29:24,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
- 上一篇: AD6.9三步搞定3D模型导入
- 下一篇: 特斯拉取消FM 5G对车载广播有何影响?
实时天气 2024-12-27 04:39:29发布
北京
-
温度:-5℃~2℃
北风3-4级
-
明天晴
2 西北风
-
后天晴
5 西南风
-
大后天晴
5 西风
发布评论