请 [注册] 或 [登录]  | 返回主站

量化交易吧 /  量化平台 帖子:3365835 新帖:10

计算贝塔

舞蹈家发表于:5 月 10 日 07:15回复(1)

昨天想看下某只股票的贝塔,但是又买不起wind,在社区里找了一圈也没看到,先自己写一个用下吧,贝塔的计算公式如下:
BEITA.png

这里以兆易创新相对于沪深300贝塔为例。

import statsmodels.api as sm # 最小二乘
#from statsmodels.stats.outliers_influence import summary_table # 获得汇总信息


'''
pd1=get_price('603986.XSHG', start_date='2018-01-01', end_date='2018-12-31', 
              frequency='daily', fields=['pre_close','close'], skip_paused=False, fq='pre')
pd1['change'] = pd1['close']-pd1['pre_close']
pd1['pct_change'] = (pd1['change']/pd1['pre_close'])*100
#510300.XSHG
pd2=get_price('000985.XSHG', start_date='2018-01-01', end_date='2018-12-31',
              frequency='daily', fields=['pre_close','close'], skip_paused=False, fq='pre')
pd2['change'] = pd2['close']-pd2['pre_close']


pd2['pct_change'] = (pd2['change']/pd2['pre_close'])*100

pd3=attribute_history('603986.XSHG', 100, unit='1d',
            fields=('open', 'close', 'high', 'low', 'volume', 'money'),
            skip_paused=True, df=True, fq='pre')
            
s1 = pd1['pct_change']
s2 = pd2['pct_change']
'''


pd3=attribute_history('603986.XSHG', 320, unit='1d',
            fields=('open', 'close', 'high', 'low', 'volume', 'money','pre_close'),
            skip_paused=False, df=True, fq='pre')



a = pd3[pd3['money']==0 ]
b = a.index


pd3=pd3.drop(b)
#print(pd3)

pd3['change'] = pd3['close']-pd3['pre_close']
pd3['pct_change'] = (pd3['change']/pd3['pre_close'])*100
#000985.XSHG
pd4=attribute_history('000985.XSHG', 320, unit='1d',
            fields=('open', 'close', 'high', 'low', 'volume', 'money','pre_close'),
            skip_paused=False, df=True, fq='pre')
pd4 = pd4.drop(b)
pd4['change'] = pd4['close']-pd4['pre_close']
pd4['pct_change'] = (pd4['change']/pd4['pre_close'])*100

        
s1 = pd3['pct_change']
s2 = pd4['pct_change']


print(np.cov(s1, s2))
print((np.cov(s1, s2))[0][1]/np.var(s2))
 
[[16.44448377  2.79101764]
 [ 2.79101764  1.72646479]]
1.6235467290144738
 

全部回复

0/140

量化课程

    移动端课程