Featured Photo by Pixabay.
- Yesterday @Barchart shared the Gold’s performance chart against the S&P 500 after Yield Curve Inversions. Is the uptrend for safe-havens set to continue?
- According to Florian Kössler, you would expect Gold to be at 1000$
- Thorsten Polleit also believes that it is time to buy Gold
- The objective of this post is to predict Gold price using Machine Learning (ML) algorithms in Python.
- Following the step-by-step guide, we will create a Machine Learning linear regression model that takes information from the past Gold ETF (GLD) prices and returns a Gold price prediction the next day.
- Recall that GLD is the largest ETF to invest directly in physical gold.
Let’s set the working directory GOLD
import os
os.chdir(‘GOLD’)
os. getcwd()
and import the following libraries
from sklearn.linear_model import LinearRegression
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
plt.style.use(‘seaborn-darkgrid’)
import yfinance as yf
Let’s read the data
Df = yf.download(‘GLD’, ‘2022-01-01’, ‘2023-03-25’, auto_adjust=True)
Df = Df[[‘Close’]]
Df = Df.dropna()
Let’s plot the closing price of GLD
Df.Close.plot(figsize=(10, 7),color=’r’)
plt.ylabel(“Gold ETF Prices”)
plt.title(“Gold ETF Price Series”)
plt.savefig(‘goldetfprice.png’)

Let’s define the explanatory variables
Df[‘S_3’] = Df[‘Close’].rolling(window=3).mean()
Df[‘S_9’] = Df[‘Close’].rolling(window=9).mean()
Df[‘next_day_price’] = Df[‘Close’].shift(-1)
Df = Df.dropna()
X = Df[[‘S_3’, ‘S_9’]]
and the target variable
y = Df[‘next_day_price’]
Let’s split the data into the train and test datasets
t = .8
t = int(t*len(Df))
X_train = X[:t]
y_train = y[:t]
X_test = X[t:]
y_test = y[t:]
Let’s create the following linear regression model
from sklearn import linear_model
clf = linear_model.BayesianRidge()
linear = clf.fit(X_train, y_train)
Predicting the Gold ETF prices
predicted_price = linear.predict(X_test)
predicted_price = pd.DataFrame(
predicted_price, index=y_test.index, columns=[‘price’])
predicted_price.plot(figsize=(10, 7))
y_test.plot()
plt.legend([‘predicted_price’, ‘actual_price’])
plt.ylabel(“Gold ETF Price”)
plt.savefig(‘goldetfpricepredicted.png’)

The R2-score is
r2_score = linear.score(X[t:], y[t:])*100
float(“{0:.2f}”.format(r2_score))
79.81
Let’s plot the Cumulative Returns
gold = pd.DataFrame()
gold[‘price’] = Df[t:][‘Close’]
gold[‘predicted_price_next_day’] = predicted_price
gold[‘actual_price_next_day’] = y_test
gold[‘gold_returns’] = gold[‘price’].pct_change().shift(-1)
gold[‘signal’] = np.where(gold.predicted_price_next_day.shift(1) < gold.predicted_price_next_day,1,0)
gold[‘strategy_returns’] = gold.signal * gold[‘gold_returns’]
((gold[‘strategy_returns’]+1).cumprod()).plot(figsize=(10,7),color=’g’)
plt.ylabel(‘Cumulative Returns’)
plt.savefig(‘goldetfpricecumreturn.png’)

Let’s calculate the Sharpe ratio
sharpe = gold[‘strategy_returns’].mean()/gold[‘strategy_returns’].std()(252*0.5)
‘Sharpe Ratio %.2f’ % (sharpe)
'Sharpe Ratio 2.33'
Let’s get the forecast
import datetime as dt
current_date = dt.datetime.now()
data = yf.download(‘GLD’, ‘2022-01-01’, current_date, auto_adjust=True)
data[‘S_3’] = data[‘Close’].rolling(window=3).mean()
data[‘S_9’] = data[‘Close’].rolling(window=9).mean()
data = data.dropna()
data[‘predicted_gold_price’] = linear.predict(data[[‘S_3’, ‘S_9’]])
data[‘signal’] = np.where(data.predicted_gold_price.shift(1) < data.predicted_gold_price,”Buy”,”No Position”)
data.tail(1)[[‘signal’,’predicted_gold_price’]].T

Summary
- The SPDR Gold Trust Shares quote is equal to $185.220 at 2023-03-24, whereas our next-day forecast is $185.136.
- With the investment starting at 2022-12-15, the revenue is expected to be around +8%.
- The R2-score of our prediction model is about 80%.
- The Sharpe ratio of 2.33 is considered very good. The higher a fund’s Sharpe ratio, the better its returns have been relative to the amount of investment risk taken.
- Results fully support the Barchart opinion – 100% BUY Overall Average Signal calculated from all 13 indicators.
Explore More
Towards Max(ROI/Risk) Trading in Q1 2023
SARIMAX X-Validation of EIA Crude Oil Prices Forecast in 2023 – 2. Brent
SARIMAX X-Validation of EIA Crude Oil Prices Forecast in 2023 – 1. WTI
Stock Market ’22 Round Up & ’23 Outlook: Zacks Strategy vs Seeking Alpha Tactics
XOM SMA-EMA-RSI Golden Crosses ’22
BTC-USD Freefall vs FB/Meta Prophet 2022-23 Predictions
BTC-USD Price Prediction with LSTM Keras
Towards min(Risk/Reward) – SeekingAlpha August Bear Market Update
ML/AI Regression for Stock Prediction – AAPL Use Case
Make a one-time donation
Make a monthly donation
Make a yearly donation
Choose an amount
Or enter a custom amount
Your contribution is appreciated.
Your contribution is appreciated.
Your contribution is appreciated.
DonateDonate monthlyDonate yearly