利用tushare获取数据,使用python根据以下要求处理数据:剔除了交易当天停牌、ST、以及涨跌停不能交易的股票;对获取的基本面数据进行了MAD(绝对中位值)去极值处理;ROE>15%;PE<=15;PEG较小的前10支股票纳入备买股票池
时间: 2024-02-22 17:55:44 浏览: 77
好的,根据你的要求,首先需要安装并引入tushare库,然后获取股票基本面数据并进行数据处理。在获取数据的过程中,我们需要注意剔除停牌、ST、涨跌停的股票,可以通过筛选条件实现。代码如下:
```python
import tushare as ts
import pandas as pd
import numpy as np
# 获取股票基本面数据
pro = ts.pro_api()
df = pro.query('daily_basic', ts_code='', trade_date='20210528',
fields='ts_code,trade_date,close,turnover_rate,pe,pb,total_share,total_mv,'
'circ_mv,roe,net_profit_ratio,eps')
# 剔除交易当天停牌、ST、以及涨跌停不能交易的股票
df = df[df['close'] != 0]
df = df[df['trade_date'] == '20210528']
df = df[df['turnover_rate'] != 0]
df = df[df['pe'] != 0]
df = df[df['pb'] != 0]
df = df[df['roe'] != 0]
df = df[df['net_profit_ratio'] != 0]
df = df[df['eps'] != 0]
df = df[(df['close'] < df['close'].rolling(window=20, min_periods=1).max() * 0.98) &
(df['close'] > df['close'].rolling(window=20, min_periods=1).min() * 1.02)]
df = df[df['name'].str.contains('ST') == False]
df = df[df['name'].str.contains('*ST') == False]
#MAD去极值处理
def mad_method(data,rate=3):
data_median = np.median(data)
mad_value = np.median(np.abs(data - data_median))
max_value = data_median + rate * mad_value
min_value = data_median - rate * mad_value
data[data > max_value] = max_value
data[data < min_value] = min_value
return data
df['roe'] = mad_method(df['roe'])
df = df[(df['roe'] > 15) & (df['pe'] <= 15)]
# 找出PEG较小的前10支股票
df['peg'] = df['pe'] / df['net_profit_ratio']
df = df.sort_values(by=['peg'], ascending=True)
stocks = df.head(10)['ts_code'].tolist()
print(stocks)
```
以上代码中,我们使用了mad_method函数进行MAD去极值处理,将ROE大于15%且PE小于等于15的股票筛选出来,并根据PEG大小排序,最后找出PEG较小的前10支股票作为备买股票池。
阅读全文