- SARIMA模型:
import pandas as pd
import numpy as np
from statsmodels.tsa.statespace.sarimax import SARIMAX
# 将日期转换为datetime格式
data['date'] = pd.to_datetime(data['date'], format='%Y%m%d')
# 创建SARIMA模型
model = SARIMAX(data['close'], order=(1, 0, 0), seasonal_order=(1, 1, 1, 12))
# 拟合模型
model_fit = model.fit()
# 预测未来3天的价格
future_dates = pd.date_range(start=data['date'].iloc[-1], periods=3, freq='D')
forecast = model_fit.get_forecast(steps=3)
predicted_values = forecast.predicted_mean
# 输出预测结果
print(predicted_values)
- 简单移动平均线(SMA)模型:
# 计算简单移动平均线
data['SMA'] = data['close'].rolling(window=7).mean()
# 预测未来3天的价格(使用最近7天的平均值作为预测值)
last_7_days = data['close'].tail(7)
predicted_values = np.mean(last_7_days)
# 输出预测结果
print(predicted_values)
- 指数加权移动平均线(EMA)模型:
# 计算指数加权移动平均线
data['EMA'] = data['close'].ewm(span=7, adjust=False).mean()
# 预测未来3天的价格(使用最近一个指数加权移动平均值作为预测值)
last_EMA = data['EMA'].iloc[-1]
predicted_values = last_EMA
# 输出预测结果
print(predicted_values)
- Bollinger带模型:
# 计算布林带指标
data['MA'] = data['close'].rolling(window=20).mean()
data['std'] = data['close'].rolling(window=20).std()
data['upper_band'] = data['MA'] + 2 * data['std']
data['lower_band'] = data['MA'] - 2 * data['std']
# 预测未来3天的价格(使用最近一个布林带的上轨值作为预测值)
last_upper_band = data['upper_band'].iloc[-1]
predicted_values = last_upper_band
# 输出预测结果
print(predicted_values)
- 相对强弱指标(RSI)模型:
# 计算相对强弱指标
data['delta'] = data['close'].diff()
data['gain'] = np.where(data['delta'] >= 0, data['delta'], 0)
data['loss'] = np.where(data['delta'] < 0, -data['delta'], 0)
data['avg_gain'] = data['gain'].rolling(window=14).mean()
data['avg_loss'] = data['loss'].rolling(window=14).mean()
# 计算相对强弱指标
data['RS'] = data['avg_gain'] / data['avg_loss']
data['RSI'] = 100 - (100 / (1 + data['RS']))
# 预测未来3天的价格(使用最近一个相对强弱指标值作为预测值)
last_RSI = data['RSI'].iloc[-1]
predicted_values = last_RSI
# 输出预测结果
print(predicted_values)
- 随机指标(KD指标)模型:
# 计算随机指标(KD指标)
data['lowest_low'] = data['low'].rolling(window=9).min()
data['highest_high'] = data['high'].rolling(window=9).max()
data['%K'] = (data['close'] - data['lowest_low']) / (data['highest_high'] - data['lowest_low']) * 100
data['%D'] = data['%K'].rolling(window=3).mean()
# 预测未来3天的价格(使用最近一个随机指标值作为预测值)
last_%K = data['%K'].iloc[-1]
predicted_values = last_%K
# 输出预测结果
print(predicted_values)
- 线性回归模型:
from sklearn.linear_model import LinearRegression
# 创建线性回归模型
model = LinearRegression()
# 准备训练数据
X = data['date'].values.reshape(-1, 1)
y = data['close']
# 拟合模型
model.fit(X, y)
# 预测未来3天的价格
future_dates = pd.date_range(start=data['date'].iloc[-1], periods=3, freq='D')
X_future = future_dates.values.reshape(-1, 1)
predicted_values = model.predict(X_future)
# 输出预测结果
print(predicted_values)
- 随机森林回归模型:
from sklearn.ensemble import RandomForestRegressor
# 创建随机森林回归模型
model = RandomForestRegressor()
# 准备训练数据
X = data['date'].values.reshape(-1, 1)
y = data['close']
# 拟合模型
model.fit(X, y)
# 预测未来3天的价格
future_dates = pd.date_range(start=data['date'].iloc[-1], periods=3, freq='D')
X_future = future_dates.values.reshape(-1, 1)
predicted_values = model.predict(X_future)
# 输出预测结果
print(predicted_values)
- 支持向量回归(SVR)模型:
from sklearn.svm import SVR
from sklearn.preprocessing import StandardScaler
# 创建支持向量回归模型
model = SVR()
# 准备训练数据
X = data['date'].values.reshape(-1, 1)
y = data['close']
# 特征缩放
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
# 拟合模型
model.fit(X_scaled, y)
# 预测未来3天的价格
future_dates = pd.date_range(start=data['date'].iloc[-1], periods=3, freq='D')
X_future = future_dates.values.reshape(-1, 1)
X_future_scaled = scaler.transform(X_future)
predicted_values = model.predict(X_future_scaled)
# 输出预测结果
print(predicted_values)
civilpy:Python数据分析及可视化实例目录926 赞同 · 36 评论文章编辑