Stock Market Prediction: A Practical Guide for choosing models
Predicting stock market trends is a challenge, but with machine learning, we can achieve valuable insights. In this article, we'll explore how various models would fare ( precision score) to predict stock market movements, and discuss how each model that could enhance your prediction accuracy.
The goal is to predict stock price movement using historical data. The RandomForestClassifier, a robust ensemble learning method, is employed to classify whether the stock price will increase or decrease.
Python code to import the stock price using Yahoo finance module for maximum period available.
import yfinance as yf
sp500 = yf.Ticker("^GSPC")
# query the historical prices
sp500 = sp500.history(period="max")
I started with a line plot to check closing prices and trend is as below
In the next step, I removed the 2 columns which were not necessary and created a new column called Tomorrow which contains yesterday closing price.
# remove the last 2 columns
del sp500["Dividends"]
del sp500["Stock Splits"]
# setup the target
sp500["Tomorrow"] = sp500["Close"].shift(-1)
Created a new column called target and check when next day opening was greater than last day closing
sp500["Target"] = (sp500["Tomorrow"] > sp500["Close"]).astype(int)
I realised that there was too much of data , so I removed the data before 1990
sp500 = sp500.loc["1990-01-01":].copy()
In this section, I called the RandomForestClassifier model. I initialized the model, fit the data into the model and started making predictions using the training dataset.
领英推荐
#training initial model using RandomForestClassifier
from sklearn.ensemble import RandomForestClassifier
model = RandomForestClassifier(n_estimators=100, min_samples_split=100, random_state=1)
#train and test test for time series data
train = sp500.iloc[:-100]
test = sp500.iloc[-100:]
#state the predictors
predictors = ["Close", "Volume", "Open", "High", "Low"]
#fit the model using predictors columns and train the Target column
model.fit(train[predictors], train["Target"])
The model is trained on features extracted from the historical stock prices and we need to improve the accuracy score.
from sklearn.metrics import precision_score
preds = model.predict(test[predictors])
import pandas as pd
#turn into a pandas series
preds = pd.Series(preds, index=test.index)
precision_score(test["Target"], preds)
#build a backtesting system
def predict(train, test, predictors, model):
model.fit(train[predictors], train["Target"])
preds = model.predict(test[predictors])
preds = pd.Series(preds,
index=test.index,
name="Predictions")
combined = pd.concat([test["Target"], preds], axis=1)
return combined
# create a backtest function
def backtest(data, model, predictors, start=2500, step=250):
all_predictions = []
for i in range(start, data.shape[0], step):
train = data.iloc[0:i].copy()
test = data.iloc[i:(i+step)].copy()
predictions = predict(train, test, predictors, model)
all_predictions.append(predictions)
return pd.concat(all_predictions)
predictions = backtest(sp500, model, predictors)
precision_score(predictions["Target"], predictions["Predictions"])
Precision score was 52% and in subsequent run, it improved upto 57%.
I ran the data using the 3 other models
Understanding the Models
The data used in our analysis consists of historical stock prices for the S&P 500. We trained each model on this data to predict whether the stock price would rise or fall the next day.
Link to the github or click here for code
Conclusion
Among the 4 models, XGBoost delivered the highest precision, but Gradient Boosting was a close second. SVM also provided valuable insights, particularly in complex feature spaces. By experimenting with these models and fine-tuning them, you can potentially achieve even better results in stock market prediction.
What other models have you used for building the stock price simulation or predictor tool ?
Leave a comment.
CXO | LinkedIn Growth Specialist | Mentoring Senior Professionals & Young Entrants to the Workplace | Speaker | Culture & Skill Mentor | India's Top 35 Mentors Niti Aayog | Content Creator | Writer
6 个月Am sure your deep insights will be instrumental for many Harsha