利用tushare的pro版本进行股票数据的获取并给出一套alpha因子选取的python代码
时间: 2024-05-05 08:17:03 浏览: 189
借助Tushare获取股票数据,构建多因子模型框架.zip
5星 · 资源好评率100%
以下是一个示例python代码,用于利用tushare的pro版本获取股票数据并执行alpha因子选取:
```python
import tushare as ts
import pandas as pd
import numpy as np
# 设置tushare的token
ts.set_token('your_token_here')
# 初始化tushare的pro api
pro = ts.pro_api()
# 获取股票列表
stock_list = pro.stock_basic(exchange='', list_status='L', fields='ts_code,symbol,name,industry')
# 准备alpha因子选取所需的数据
start_date = '20200101'
end_date = '20211231'
benchmark = '000300.SH'
factor_list = ['pe', 'pb', 'roe']
# 获取股票因子数据
def get_stock_factors(code):
# 获取股票的日线数据
daily_data = pro.daily(ts_code=code, start_date=start_date, end_date=end_date)
# 获取股票的财务数据
financial_data = pro.fina_indicator(ts_code=code, start_date=start_date, end_date=end_date)
# 合并数据
merged_data = pd.merge(daily_data, financial_data, on='trade_date', how='inner')
# 计算因子值
pe = merged_data['close'] / merged_data['eps_basic']
pb = merged_data['close'] / merged_data['bvps']
roe = merged_data['roe']
# 返回因子值
return pd.DataFrame({'pe': pe, 'pb': pb, 'roe': roe})
# 计算alpha因子
def calc_alpha(stock_factors):
# 计算每个因子的z-score
pe_z = (stock_factors['pe'] - stock_factors['pe'].mean()) / stock_factors['pe'].std()
pb_z = (stock_factors['pb'] - stock_factors['pb'].mean()) / stock_factors['pb'].std()
roe_z = (stock_factors['roe'] - stock_factors['roe'].mean()) / stock_factors['roe'].std()
# 计算alpha因子
alpha = pe_z * 0.3 + pb_z * 0.3 + roe_z * 0.4
return alpha
# 获取股票因子数据和alpha因子
def get_stock_alpha(code):
# 获取股票因子数据
stock_factors = get_stock_factors(code)
# 计算alpha因子
alpha = calc_alpha(stock_factors)
# 返回因子数据和alpha因子
return stock_factors, alpha
# 获取股票alpha因子数据
stock_alpha_data = {}
for code in stock_list['ts_code']:
try:
stock_factors, alpha = get_stock_alpha(code)
stock_alpha_data[code] = {'factors': stock_factors, 'alpha': alpha}
except:
pass
# 将alpha因子转换为DataFrame
alpha_data = pd.DataFrame({code: data['alpha'] for code, data in stock_alpha_data.items()})
# 计算每支股票的alpha因子得分
alpha_score = (alpha_data - alpha_data.mean()) / alpha_data.std()
# 获取市场指数数据
index_data = pro.index_daily(ts_code=benchmark, start_date=start_date, end_date=end_date)
index_data = index_data.set_index('trade_date').sort_index()
# 计算市场指数收益率
index_return = index_data['close'] / index_data['close'].shift(1) - 1
# 计算每支股票的超额收益率
excess_return = alpha_score * index_return
# 计算每日的投资组合收益率
portfolio_return = excess_return.mean(axis=1)
# 计算投资组合的夏普比率
sharpe_ratio = np.sqrt(252) * portfolio_return.mean() / portfolio_return.std()
# 输出结果
print('Sharpe Ratio:', sharpe_ratio)
```
该代码的基本思路是首先获取股票列表,然后循环遍历每支股票,获取其日线数据和财务数据,计算出其对应的pe、pb和roe因子值,然后根据这些因子值计算出该股票的alpha因子。接下来,将每支股票的alpha因子转换为DataFrame,并计算每支股票的alpha因子得分。然后获取市场指数数据,计算出市场指数收益率,再根据每支股票的alpha因子得分和市场指数收益率计算出每支股票的超额收益率,最后计算投资组合的夏普比率。
阅读全文