基于tushare数据,用Python写一个 计算上证50指数与中证1000指数比值的RSI指标、KDJ指标、macd指标、最大回撤指标,采用机器学习的方法做一个高胜率的交易策略,并画出图形
时间: 2024-05-02 17:18:01 浏览: 199
对于上证50指数和中证1000指数,我们可以使用tushare获取它们的历史数据:
```python
import tushare as ts
# 获取上证50指数历史数据
sz50_data = ts.get_k_data('sh000016', start='2010-01-01')
# 获取中证1000指数历史数据
zz1000_data = ts.get_k_data('sz399978', start='2010-01-01')
```
接下来,我们可以分别计算它们的RSI指标、KDJ指标、macd指标和最大回撤指标:
```python
import numpy as np
import pandas as pd
import talib
# 计算RSI指标
def calc_rsi(data, n=14):
close = data['close']
rsi = talib.RSI(close, timeperiod=n)
return rsi
# 计算KDJ指标
def calc_kdj(data, n=9, m1=3, m2=3):
high = data['high']
low = data['low']
close = data['close']
kdj = talib.STOCH(high, low, close, fastk_period=n, slowk_matype=m1, slowd_period=m2, slowd_matype=m2)
return kdj
# 计算macd指标
def calc_macd(data, n1=12, n2=26, n3=9):
close = data['close']
macd, signal, hist = talib.MACD(close, fastperiod=n1, slowperiod=n2, signalperiod=n3)
return macd, signal, hist
# 计算最大回撤指标
def calc_max_drawdown(data):
close = data['close']
cummax = np.maximum.accumulate(close)
drawdown = (close - cummax) / cummax
max_drawdown = np.min(drawdown)
return max_drawdown
```
接下来,我们可以把上述指标计算出来,并将它们合并到一个DataFrame中:
```python
# 计算上证50指数的各项指标
sz50_data['rsi'] = calc_rsi(sz50_data)
sz50_data['kdj_k'], sz50_data['kdj_d'], sz50_data['kdj_j'] = calc_kdj(sz50_data)
sz50_data['macd'], sz50_data['macd_signal'], sz50_data['macd_hist'] = calc_macd(sz50_data)
sz50_data['max_drawdown'] = calc_max_drawdown(sz50_data)
# 计算中证1000指数的各项指标
zz1000_data['rsi'] = calc_rsi(zz1000_data)
zz1000_data['kdj_k'], zz1000_data['kdj_d'], zz1000_data['kdj_j'] = calc_kdj(zz1000_data)
zz1000_data['macd'], zz1000_data['macd_signal'], zz1000_data['macd_hist'] = calc_macd(zz1000_data)
zz1000_data['max_drawdown'] = calc_max_drawdown(zz1000_data)
# 合并两个DataFrame
data = pd.merge(sz50_data[['date', 'close', 'rsi', 'kdj_k', 'kdj_d', 'kdj_j', 'macd', 'macd_signal', 'macd_hist', 'max_drawdown']],
zz1000_data[['date', 'close', 'rsi', 'kdj_k', 'kdj_d', 'kdj_j', 'macd', 'macd_signal', 'macd_hist', 'max_drawdown']],
on='date',
suffixes=('_sz50', '_zz1000'))
```
接下来,我们可以使用机器学习的方法构建一个高胜率的交易策略。具体来说,我们可以将上证50指数与中证1000指数的比值作为因变量,将各项指标作为自变量,然后使用逻辑回归算法进行分类。
```python
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
# 构建特征矩阵和标签
X = data[['rsi_sz50', 'kdj_k_sz50', 'kdj_d_sz50', 'kdj_j_sz50', 'macd_sz50', 'macd_signal_sz50', 'macd_hist_sz50', 'max_drawdown_sz50',
'rsi_zz1000', 'kdj_k_zz1000', 'kdj_d_zz1000', 'kdj_j_zz1000', 'macd_zz1000', 'macd_signal_zz1000', 'macd_hist_zz1000', 'max_drawdown_zz1000']]
y = (data['close_sz50'] / data['close_zz1000']).shift(-1) > 1
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# 使用逻辑回归算法进行分类
clf = LogisticRegression(random_state=42).fit(X_train, y_train)
y_pred = clf.predict(X_test)
```
最后,我们可以画出各项指标的变化趋势图:
```python
import matplotlib.pyplot as plt
# 画出各项指标的变化趋势图
fig, axs = plt.subplots(4, 2, figsize=(15, 15))
axs[0, 0].plot(data['date'], data['close_sz50'], label='sz50')
axs[0, 0].plot(data['date'], data['close_zz1000'], label='zz1000')
axs[0, 0].legend()
axs[0, 1].plot(data['date'], data['rsi_sz50'], label='sz50')
axs[0, 1].plot(data['date'], data['rsi_zz1000'], label='zz1000')
axs[0, 1].legend()
axs[1, 0].plot(data['date'], data['kdj_k_sz50'], label='sz50')
axs[1, 0].plot(data['date'], data['kdj_k_zz1000'], label='zz1000')
axs[1, 0].legend()
axs[1, 1].plot(data['date'], data['kdj_d_sz50'], label='sz50')
axs[1, 1].plot(data['date'], data['kdj_d_zz1000'], label='zz1000')
axs[1, 1].legend()
axs[2, 0].plot(data['date'], data['kdj_j_sz50'], label='sz50')
axs[2, 0].plot(data['date'], data['kdj_j_zz1000'], label='zz1000')
axs[2, 0].legend()
axs[2, 1].plot(data['date'], data['macd_sz50'], label='sz50')
axs[2, 1].plot(data['date'], data['macd_zz1000'], label='zz1000')
axs[2, 1].legend()
axs[3, 0].plot(data['date'], data['macd_signal_sz50'], label='sz50')
axs[3, 0].plot(data['date'], data['macd_signal_zz1000'], label='zz1000')
axs[3, 0].legend()
axs[3, 1].plot(data['date'], data['max_drawdown_sz50'], label='sz50')
axs[3, 1].plot(data['date'], data['max_drawdown_zz1000'], label='zz1000')
axs[3, 1].legend()
plt.show()
```
阅读全文