python写因子策略_python量化策略——Fama-French三因子模型(回归获取alpha)阿尔法α策略。…

简单的alpha策略,

选取某一时间点所有股票的相关信息ps、pb、pe等。

用三因子回归获取alpha,分别用每只股票计算。

选取排名靠前的n只股票

计算组合净值

计算结果和画图

注:代码运行需安装tushar pro 并获取TOKEN码, 这里获取token码

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

matplotlib.rcParams[‘axes.unicode_minus’]=False

plt.rcParams[‘font.sans-serif’]=[‘SimHei’]

ts.set_token(‘”输入token码”‘)

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.cn_cpi(start_m=star, end_m=end,fields=’month,nt_yoy’)

return dc

def read_GDP(self,star,end):#时间格式star=’2016Q4′

df1 = pro.cn_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=’20180226’#发布GDP需要时间,我们延迟1个月,即第一季度的GDP4月份才发布。

end_time=”20180521″

dc=readData()

dsc1=readData()

dsb1=readData()

#############计算alpha的值

def alpha_fun(code):

dsb=dsb1.read_base(code) .fillna(0)

dsc=dsc1.read_index_daily(‘000300.SH’,start_time,end_time)

dsc.set_index([‘trade_date’],inplace=True)

dsb.set_index([‘trade_date’],inplace=True)

df=pd.merge(dsc, dsb, on=’trade_date’).fillna(0)

R=np.reshape( np.array([df.close_y]) , (-1,1) )

R_f=np.reshape( np.array([ (df.change/(df.close_x.shift(-1))).fillna(0) ]) , (-1,1) )#用0 填充nan

HMI=np.reshape( np.array([ (1/df.pb).fillna(0) ]) , (-1,1) )

SMB=np.reshape( np.array([ df.total_mv]) , (-1,1) )

X=np.concatenate(( R_f-4/252, HMI,SMB ),axis=1)

y1=np.reshape(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_ ,linear.coef_,linear.score(X_test, y_test),df

######################计算篮子里的每只股票净值

def jinzhi_fun(code,start_time,end_time):#计算净值

df=pro.query(‘daily’, ts_code=str(code), start_date=start_time, end_date=end_time,fields=”)

df.index=pd.to_datetime(df.trade_date,format=”%Y-%m-%d”)

jinzhi=(df.close/df.close[-1])

return jinzhi

################选股条件

co=pro.query(‘daily_basic’, ts_code=””,trade_date=”20180226″,fields=’ts_code’)

code_list=[]

N=30#股票池

k=0

JZ=0#组合净值

cum=0

for i in co.ts_code.values[0:N]:

try:

if alpha_fun(i)[0]

Original: https://blog.csdn.net/weixin_33182922/article/details/112839064
Author: 欌月
Title: python写因子策略_python量化策略——Fama-French三因子模型(回归获取alpha)阿尔法α策略。…

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/634419/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球