Predicting Gold (GLD) ETF Prices Using Machine Learning
This project builds a Random Forest Regression model to predict the price of the SPDR Gold Shares (GLD) ETF using historical financial market data. The model leverages correlated financial instruments โ including the S&P 500 index, crude oil (USO), silver (SLV), and the EUR/USD exchange rate โ as predictive features to estimate gold price movements.
The complete analysis pipeline โ from data loading and exploratory analysis through model training and evaluation โ is implemented in a single, reproducible Jupyter Notebook designed for Google Colab.
- ๐ Exploratory Data Analysis โ Statistical summaries, null-value checks, distribution plots, and correlation heatmaps to deeply understand the dataset before modeling.
- ๐ค Random Forest Regressor โ Ensemble learning model with 100 decision trees for robust, high-accuracy price predictions.
- ๐ Visual Model Evaluation โ Side-by-side comparison of actual vs. predicted gold prices using line plots for intuitive performance assessment.
- ๐ Quantitative Metrics โ Model accuracy measured using R-squared error and Mean Absolute Error (MAE).
- โ๏ธ Cloud-Ready โ Pre-configured for seamless execution on Google Colab with zero local setup required.
Gold_Price_Prediction-main/
โ
โโโ Gold_Price_Prediction.ipynb # Main Jupyter Notebook (full pipeline)
โโโ gold_price_data.csv # Dataset (2,290 trading days)
โโโ Gold Price Prediction.docx # Project documentation
โโโ README.md # This file
โโโ LICENSE # MIT License
The dataset contains 2,290 daily observations spanning from January 2, 2008 to May 16, 2018, with no missing values.
| Column | Description | Type |
|---|---|---|
Date |
Trading date | Object |
SPX |
S&P 500 Index closing price | Float64 |
GLD |
SPDR Gold Shares ETF price (Target) | Float64 |
USO |
United States Oil Fund price | Float64 |
SLV |
iShares Silver Trust price | Float64 |
EUR/USD |
EUR to USD exchange rate | Float64 |
| Metric | SPX | GLD | USO | SLV | EUR/USD |
|---|---|---|---|---|---|
| Mean | 1,654.32 | 122.73 | 31.84 | 20.08 | 1.2837 |
| Std | 519.11 | 23.28 | 19.52 | 7.09 | 0.1315 |
| Min | 676.53 | 70.00 | 7.96 | 8.85 | 1.0390 |
| Max | 2,872.87 | 184.59 | 117.48 | 47.26 | 1.5988 |
- Load the CSV dataset into a Pandas DataFrame.
- Inspect for missing values, data types, and basic statistical measures.
- Distribution Analysis โ Visualize the GLD price distribution using Seaborn's
displot. - Correlation Analysis โ Compute and visualize feature correlations using a heatmap to identify relationships between financial instruments.
- Features (X):
SPX,USO,SLV,EUR/USD - Target (Y):
GLD - Drop the
Datecolumn (non-numeric) and the target variable from the feature set.
- 80/20 split with
random_state=2for reproducibility. - Training set: 1,832 samples
- Test set: 458 samples
- Algorithm:
RandomForestRegressorfrom Scikit-learn - Hyperparameters:
n_estimators=100(100 decision trees in the ensemble)
- Predictions generated on the held-out test set.
- Performance assessed using:
- R-squared (Rยฒ) โ Measures the proportion of variance explained by the model.
- Mean Absolute Error (MAE) โ Average absolute difference between predicted and actual values.
- Visualization: Actual vs. predicted prices plotted for visual comparison.
| Category | Libraries |
|---|---|
| Data Manipulation | NumPy, Pandas |
| Visualization | Matplotlib, Seaborn |
| Machine Learning | Scikit-learn (RandomForestRegressor, metrics) |
| Environment | Jupyter Notebook, Google Colab |
The quickest way to run this project โ no local installation needed:
- Click the "Open in Colab" badge at the top of this README.
- Upload
gold_price_data.csvto the Colab runtime when prompted (or modify the path). - Run all cells sequentially (
Runtime โ Run all).
- Python 3.9 or higher
- pip (Python package manager)
# 1. Clone the repository
git clone https://github.com/MYoussef885/Gold_Price_Prediction.git
cd Gold_Price_Prediction
# 2. Create and activate a virtual environment
python -m venv venv
# Windows
venv\Scripts\activate
# macOS / Linux
source venv/bin/activate
# 3. Install dependencies
pip install numpy pandas matplotlib seaborn scikit-learn jupyter
# 4. Launch Jupyter Notebook
jupyter notebook Gold_Price_Prediction.ipynbThe Random Forest Regressor demonstrates strong predictive performance on the test set:
- โ High Rยฒ score โ The model explains a significant proportion of variance in gold prices.
- โ Low MAE โ Predicted values closely track actual GLD prices.
- โ Visual alignment โ The actual vs. predicted price plot shows tight correspondence, particularly for mid-range gold prices.
Note: The model performs best when market conditions follow historical patterns observed in the 2008โ2018 training period. Performance may vary for out-of-distribution data.
- Add time-series features (moving averages, RSI, MACD)
- Experiment with additional models (XGBoost, LSTM, Gradient Boosting)
- Implement hyperparameter tuning via GridSearchCV or RandomizedSearchCV
- Incorporate additional macroeconomic indicators (interest rates, inflation)
- Build an interactive dashboard for real-time predictions
- Extend the dataset to include more recent trading data
Contributions, issues, and feature requests are welcome! Feel free to:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License โ see the LICENSE file for details.
- MYoussef885 โ Original project author
- Siddhardhan โ Project mentor and tutorial guidance
- The open-source community behind NumPy, Pandas, Scikit-learn, Matplotlib, and Seaborn