Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

拉取美股数据 #199

Open
QYiYang opened this issue Dec 14, 2024 · 1 comment
Open

拉取美股数据 #199

QYiYang opened this issue Dec 14, 2024 · 1 comment

Comments

@QYiYang
Copy link

QYiYang commented Dec 14, 2024

No description provided.

@QYiYang
Copy link
Author

QYiYang commented Dec 14, 2024

pip install yfinance scikit-learn xgboost matplotlib
import yfinance as yf
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_squared_error
import matplotlib.pyplot as plt

  1. 获取美股数据(以Apple为例)
    ticker = 'AAPL' # 可以替换为任何美股代码,如 'TSLA','GOOG' 等
    data = yf.download(ticker, start='2010-01-01', end='2024-01-01')

  2. 数据预处理:计算技术指标,如移动平均线
    data['MA5'] = data['Close'].rolling(window=5).mean() # 5日均线
    data['MA20'] = data['Close'].rolling(window=20).mean() # 20日均线
    data['RSI'] = 100 - (100 / (1 + (data['Close'].diff().clip(lower=0).rolling(window=14).mean() /
    data['Close'].diff().clip(upper=0).rolling(window=14).mean()).fillna(0))) # RSI
    删除缺失值
    data = data.dropna()

  3. 特征和标签:使用收盘价和技术指标预测下一天的收盘价
    X = data[['Close', 'MA5', 'MA20', 'RSI']]
    y = data['Close'].shift(-1) # 预测下一天的收盘价
    X = X[:-1]
    y = y[:-1]

  4. 数据标准化
    scaler = StandardScaler()
    X_scaled = scaler.fit_transform(X)

  5. 划分训练集和测试集
    X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=0.2, random_state=42)

  6. 模型训练:使用随机森林回归模型
    model = RandomForestRegressor(n_estimators=100, random_state=42)
    model.fit(X_train, y_train)

  7. 预测
    y_pred = model.predict(X_test)

  8. 评估模型:计算均方误差(MSE)和R²分数
    mse = mean_squared_error(y_test, y_pred)
    r2_score = model.score(X_test, y_test)
    print(f'Mean Squared Error: {mse}')
    print(f'R^2 Score: {r2_score}')

  9. 可视化:预测结果与真实值对比
    plt.figure(figsize=(10, 6))
    plt.plot(y_test.index, y_test, label='Real Price', color='blue')
    plt.plot(y_test.index, y_pred, label='Predicted Price', color='red')
    plt.title(f'{ticker} Stock Price Prediction')
    plt.xlabel('Date')
    plt.ylabel('Price')
    plt.legend()
    plt.show()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant