Untitled
unknown
plain_text
9 months ago
1.1 kB
3
Indexable
import pandas as pd import numpy as np from alpha_vantage.timeseries import TimeSeries from sklearn.ensemble import RandomForestRegressor from sklearn.model_selection import train_test_split # Alpha Vantage API credentials api_key = "YOUR_API_KEY" ts = TimeSeries(key=api_key, output_format="pandas") # Fetch stock data stock_data, meta_data = ts.get_daily(symbol="MSFT", outputsize="full") # Calculate technical indicators stock_data["SMA_50"] = stock_data["Close"].rolling(window=50).mean() stock_data["EMA_20"] = stock_data["Close"].ewm(span=20, adjust=False).mean() # Prepare data for ML model X = stock_data.drop(["Close"], axis=1) y = stock_data["Close"] # Split data into training and testing sets X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # Train a simple Random Forest Regressor model model = RandomForestRegressor(n_estimators=100, random_state=42) model.fit(X_train, y_train) # Make predictions on the test set y_pred = model.predict(X_test) # Evaluate the model mse = np.mean((y_pred - y_test) ** 2) print(f"Mean Squared Error: {mse:.2f}")
Editor is loading...
Leave a Comment