This repository uses time-series data from the S&P 500 to train a RandomForestClassifier to predict the probability of a stock price increasing or decreasing.
We will use the Yahoo Finance API to get historical data for the S&P500 (^GSPC). Yahoo Finance offers an excellent range of market data on stocks, bonds, currencies, and cryptocurrencies. It also provides news reports with various insights into different markets from around the world
$ pip install yfinance
import yfinance as yf
sp500 = yf.Ticker("^GSPC")
sp500 = sp500.history(period="max")
$ pip install matplotlib
import matplotlib.pyplot as plt
plt.plot(sp500.index, sp500["Close"])
plt.show()
sp500["Tomorrow"] = sp500["Close"].shift(-1)
sp500["Target"] = (sp500["Tomorrow"] > sp500["Close"]).astype(int)
sp500 = sp500.loc["1990-01-01":].copy()
$ pip install sklearn
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import precision_score
model = RandomForestClassifier(n_estimators=200, min_samples_split=50, random_state=1)