build-ml-model-for-stock-price-prediction-and-trading
This repository contains a minimal example of building a machine-learning driven trading strategy. The goal is to demonstrate how historical price data can be used to train a predictive model and evaluate a simple trading rule.
- Data – Daily OHLCV data is downloaded from Yahoo Finance for a set of tickers (e.g. AAPL, MSFT).
- Features – Basic technical indicators such as SMA, RSI and MACD are computed and used as model inputs.
- Model – A small
xgboostregressor is trained to forecast the next-day return of a single stock. - Backtest – Predictions are converted to long/flat signals and the resulting equity curve is calculated.
All scripts live in the src/ directory and results (plots, csv files, trained model) are written to results/ and models/.
src/– data loading, indicator functions, model wrapper and backtesting codedata/– optional location to store fetched datasetsmodels/– persisted modelsresults/– output from backtests
Dependencies are listed in requirements.txt. The project relies on pandas, numpy, matplotlib, yfinance and xgboost. Install them with:
pip install -r requirements.txtNote: the Codex environment used for automated testing does not have internet access, so installing packages or downloading data may fail in that environment.
Once the required packages are installed, execute the main script:
python src/main.pyThe script will download price data, train the model, run a simple backtest and save:
results/equity.png– a plot of cumulative equityresults/backtest.csv– detailed daily returnsmodels/xgb_model.pkl– the fitted model