python多因子选股
时间: 2023-11-20 10:56:31 浏览: 127
根据提供的引用内容,我们可以使用Python进行多因子选股。以下是一个基本的多因子选股的步骤:
1. 获取股票数据
```python
stock_df = ak.stock_zh_a_daily(symbol='sh600000', adjust="hfq")
```
2. 计算因子
```python
# 计算5日均线
stock_df['ma5'] = stock_df['close'].rolling(5).mean()
# 计算20日均线
stock_df['ma20'] = stock_df['close'].rolling(20).mean()
# 计算60日均线
stock_df['ma60'] = stock_df['close'].rolling(60).mean()
# 计算120日均线
stock_df['ma120'] = stock_df['close'].rolling(120).mean()
# 计算250日均线
stock_df['ma250'] = stock_df['close'].rolling(250).mean()
# 计算RSI指标
delta = stock_df['close'].diff()
gain = delta.where(delta > 0, 0)
loss = -delta.where(delta < 0, 0)
avg_gain = gain.rolling(14).mean()
avg_loss = loss.rolling(14).mean().abs()
rs = avg_gain / avg_loss
rsi = 100 - (100 / (1 + rs))
stock_df['rsi'] = rsi
```
3. 数据清洗
```python
# 去除缺失值
stock_df.dropna(inplace=True)
# 去除停牌日
stock_df = stock_df[stock_df['volume'] != 0]
```
4. 特征标准化
```python
# 将特征标准化
scaler = preprocessing.StandardScaler()
X = scaler.fit_transform(stock_df[['ma5', 'ma20', 'ma60', 'ma120', 'ma250', 'rsi']])
```
5. 构建模型
```python
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, stock_df['close'], test_size=0.2, random_state=42)
# 构建模型
model = Sequential()
model.add(Conv1D(filters=64, kernel_size=2, activation='relu', input_shape=(X_train.shape[1], 1)))
model.add(MaxPooling1D(pool_size=2))
model.add(Flatten())
model.add(Dense(50, activation='relu'))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')
# 训练模型
model.fit(X_train.reshape(X_train.shape[0], X_train.shape[1], 1), y_train, epochs=50, batch_size=32, verbose=0)
```
6. 模型评估
```python
# 评估模型
mse = model.evaluate(X_test.reshape(X_test.shape[0], X_test.shape[1], 1), y_test, verbose=0)
print('Mean Squared Error:', mse)
```
以上是一个基本的多因子选股的步骤,你可以根据自己的需求进行修改和优化。
阅读全文