剔除了交易当天停牌、ST、以及涨跌停不能交易的股票;对获取的基本面数据进行了MAD(绝对中位值)去极值处理;PEG较小的前10支股票纳入备买股票池
时间: 2024-02-21 20:02:15 浏览: 210
好的,下面是根据您的要求,使用 akshare 获取股票数据并进行去极值处理和 PEG 值筛选的 Python 代码示例:
```python
import akshare as ak
import pandas as pd
import numpy as np
# 获取 A 股股票数据
stock_zh_a_daily_df = ak.stock_zh_a_daily()
# 获取股票基本面数据
stock_zh_a_profit_df = ak.stock_zh_a_profit()
# 合并数据
stock_data_df = pd.merge(stock_zh_a_daily_df, stock_zh_a_profit_df, on='代码')
# 剔除停牌、ST、涨跌停不能交易的股票
selected_stock_df = stock_data_df[(stock_data_df['开盘价'] != 0)
& (stock_data_df['收盘价'] != 0)
& (stock_data_df['涨跌幅'] != 0)
& (stock_data_df['ST'] == '否')
& (stock_data_df['是否涨停'] == '否')
& (stock_data_df['是否跌停'] == '否')]
# 计算 PEG 值
selected_stock_df['PEG'] = selected_stock_df['PE_TTM'] / selected_stock_df['每股收益同比增长率']
# MAD(绝对中位值)去极值处理
def mad_based_outlier(points, thresh=3.5):
if len(points.shape) == 1:
points = points[:,None]
median = np.median(points, axis=0)
difference = np.sum((points - median)**2, axis=-1)
difference = np.sqrt(difference)
median_difference = np.median(difference)
modified_z_score = 0.6745 * difference / median_difference
return modified_z_score > thresh
selected_stock_df = selected_stock_df[~mad_based_outlier(selected_stock_df['PEG
阅读全文