Stock Trend Predictor (LSTM)
AdvancedFile: project4_stock_lstm.py
Purpose: build an LSTM-based binary classifier to predict whether next day closing price goes up or down. This example uses yfinance to download historical data. If no internet, code includes fallback to synthetic data.
Note:
Internet required for yfinance. If you cannot access internet, run the fallback synthetic sample.
# project4_stock_lstm.py
import numpy as np
import pandas as pd
try:
import yfinance as yf
has_yfinance = True
except Exception:
has_yfinance = False
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import train_test_split
import tensorflow as tf
def fetch_data(ticker="AAPL", period="2y"):
if has_yfinance:
df = yf.download(ticker, period=period)
df = df[["Close"]].dropna()
else:
# fallback synthetic data
dates = pd.date_range(end=pd.Timestamp.today(), periods=500)
prices = 100 + np.cumsum(np.random.randn(len(dates)) * 0.5)
df = pd.DataFrame({"Close": prices}, index=dates)
return df
def prepare_sequences(series, lookback=10):
scaler = MinMaxScaler(feature_range=(0,1))
scaled = scaler.fit_transform(series.values.reshape(-1,1))
X, y = [], []
for i in range(len(scaled)-lookback-1):
X.append(scaled[i:(i+lookback), 0])
# Predict whether next day up (1) or down (0)
next_close = scaled[i+lookback, 0]
today_close = scaled[i+lookback-1, 0]
label = 1 if next_close > today_close else 0
y.append(label)
X = np.array(X)
y = np.array(y)
X = X.reshape((X.shape[0], X.shape[1], 1))
return X, y, scaler
def build_model(lookback=10):
model = tf.keras.Sequential([
tf.keras.layers.LSTM(64, input_shape=(lookback,1), return_sequences=False),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(32, activation="relu"),
tf.keras.layers.Dense(1, activation="sigmoid")
])
model.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])
return model
def main():
df = fetch_data("AAPL", period="2y")
print("Loaded data size:", len(df))
lookback = 20
X, y, scaler = prepare_sequences(df["Close"], lookback=lookback)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, shuffle=False)
model = build_model(lookback)
model.summary()
model.fit(X_train, y_train, epochs=10, batch_size=32, validation_split=0.1)
loss, acc = model.evaluate(X_test, y_test)
print(f"Test loss: {loss:.4f}, Test acc: {acc:.4f}")
model.save("stock_lstm.h5")
print("Saved model to stock_lstm.h5")
if __name__ == "__main__":
main()Requirements
pip install yfinance tensorflow scikit-learn pandas numpyNotes
- This is a simple up/down predictor for illustration. Real-world stock prediction is much harder and requires rigorous testing and consideration of ethics and risk.
- If yfinance cannot run (no internet), the script will create synthetic prices so you can still run and test the code.