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

量化交易吧 /  量化平台 帖子:3364737 新帖:1

多因子选股模型

汇市风云榜发表于:8 月 6 日 11:25回复(1)

1、因子有效性检验代码

from jqfactor import get_factor_values
import pandas as pd

# 获取沪深300所有股票代码和因子
indexs = get_index_stocks('000300.XSHG')
factors = ['VOL5','fixed_asset_ratio','cfo_to_ev','debt_to_asset_ratio','roe_ttm','roa_ttm',
           'operating_revenue_growth_rate','net_asset_growth_rate','net_profit_growth_rate',
           'EBITDA','ROC12','VOSC','retained_earnings_per_share','eps_ttm','momentum']
# return一个 dict: key 是因子名称, value 是 pandas.dataframe
factor_data = get_factor_values(securities=indexs, factors=factors, start_date='2014-01-01',
                                end_date='2014-02-01')
# 以5日换手率因子为例,去除nan的列,转置
df=factor_data['VOL5'].dropna(axis=1,how='any').T
# 取每一行平均值,添加到dataframe最后一列
df['average']=df.apply(lambda x :x.mean(),axis=1)
#从小到大排序
new_df = df.sort_values(by='average')
# 取索引,以列表的形式存储股票代码
code = new_df.index.values.tolist()
# 将列表中的数据分成组合1-5,
group1 = code[0:50]
group2 = code[50:100]
group3 = code[100:150]
group4 = code[150:200]
group5 = code[200:250]
#print(group1)
#print(group5)



# 获取每组股票的月初第一个交易日流通市值,以列表形式
q1 = query(valuation).filter(valuation.code.in_(group1))
market_cap = get_fundamentals(q1, date='2014-01-01')['market_cap'].tolist()
#print(market_cap)
#计算所有股票市值只和
market_cap_sum = 0
for i in range(0,len(market_cap)):
    market_cap_sum += market_cap[i]
#print(market_cap_sum)

# 获取组合中所有股票和沪深300月初价格p0,月末价格p1
p0=get_price(group1,end_date='2014-01-01',frequency='1d',count=1,skip_paused=False,
             fq=None)['close']
p1=get_price(group1,end_date='2014-02-01',frequency='1d',count=1,skip_paused=False,
             fq=None)['close']
p3=get_price('000300.XSHG',end_date='2014-01-01',frequency='1d',count=1,skip_paused=False,
             fq=None)['close'][0]
p4=get_price('000300.XSHG',end_date='2014-02-01',frequency='1d',count=1,skip_paused=False,
             fq=None)['close'][0]
# 将两个dataframe转置、合并
step1 = pd.concat([p0,p1],ignore_index=False).T
#print(step1.T)
#print(p3)
#print(p4)



# 计算沪深300有效年利率
apr = p4/p3-1
eff = (1+apr/12)**12-1
#print(eff)

# 计算并向组合添加apr、market_cap、weight_apr、eff、超额收益ar
step1['apr'] = step1.apply(lambda x: (x[0] / x[1])-1, axis=1)
step1['market_cap']=market_cap
step1['weight_apr']=step1.apply(lambda x: x[3]/market_cap_sum*x[2],axis=1)
step1['eff'] = step1.apply(lambda x: ((1+x[4]/12)**12)-1,axis=1)
step1['ar'] = step1.apply(lambda x: x[5]-eff,axis=1)
#print(step1.T)



# 最终的当月该因子加权年化收益率和超额收益率
print(step1['eff'].sum()/50)
print(step1['ar'].sum()/50)
0.0013246173477273727
0.05472584726587093
/opt/conda/lib/python3.6/site-packages/jqresearch/api.py:109: FutureWarning: 
Panel is deprecated and will be removed in a future version.
The recommended way to represent these types of 3-dimensional data are with a MultiIndex on a DataFrame, via the Panel.to_frame() method
Alternatively, you can use the xarray package http://xarray.pydata.org/en/stable/.
Pandas provides a `.to_xarray()` method to help automate this conversion.

  pre_factor_ref_date=_get_today())
 

全部回复

0/140

量化课程

    移动端课程