The price of gold reached a new all-time high of $2075 in August 2020. A few days before the peak, the New York Times attributed the mania to the public bracing for inflation in response to “the easy money pouring out of central banks and government stimulus programs”. Paul Krugman disagreed on Twitter, stating that the price of gold is not rising because investors are expecting inflation but rather because bond yields are so low. In response, Peter Schiff called Krugman “clueless”, claiming that inflation explains both the fall in bond yields and the rise in gold prices.
We wanted to test the claims from these sources and uncover the underlying reason for the gold rally. So in Idea Streams episode #7, we analyzed the relationship between gold prices, expected inflation, and bond yields.
Our Process
To observe the relationship between gold prices, expected inflation, and bond yields, we gathered two data sets from the Federal Reserve Economic Data (FRED) website. For the expected inflation rate, we used the 10-year breakeven Inflation rate. For the bond yields, we used the 10-year treasury inflation-indexed bond yield data. The 10-year breakeven inflation rate is a calculation based on whether yields are equal on inflation-protected and non-inflation-protected treasuries. The latest value in the series represents what the average investor expects inflation to be 10 years into the future.
The 10-year treasury inflation-indexed bond yield data is corrected for inflation, so we can see at the moment there are negative bond yields on these 10-year treasuries.
To get these datasets from FRED into our research environment, we uploaded the data to Dropbox and created two custom data classes to read the Dropbox files. Once the data was ready to be read, we added the gold spot price and the two custom data sets to our research environment.
self.gold = self.add_cfd("XAUUSD", Resolution.DAILY, Market.OANDA).symbol
self.infl = self.add_data(ExpectedInflation, "infl", Resolution.DAILY).symbol
self.yld = self.add_data(TenYrYield, "yld", Resolution.DAILY).symbol
We then gathered 15 years of historical data for the 3 data sets.
history = self.history([self.gold, self.infl, self.yld], timedelta(365*15))
With all of the data in one place, we organized it into one large DataFrame. Since the price of gold has a high positive skew, we applied a log transformation to the values in order to make the linear regression results more reliable. After manipulating the data into place, here’s what the first 5 rows of the final DataFrame looked like:
To observe the relationship between gold and the other variables, we produced two scatter plots.
Looking at the plots above, it doesn’t seem like there is much of a relationship between inflation and gold. However, it looks like there is a strong negative correlation between bond yields and the price of gold. To see how these relationships have fared over time, we colored the scatter plots so darker dots represented more recent dates.
From these plots, we can see the relationship between the gold price and bond yields has held up well in recent times. Additionally, inflation seems to be having a somewhat positive correlation recently. If we narrow our scope and look at just the data since March 1, 2020, it appears there is a strong positive correlation between inflation and gold prices, and a strong negative correlation between bond yields and gold prices.
To assign some numbers to these relationships, we calculated the correlation coefficients and R-square values. Over the last 15 years, the correlation between gold and inflation was -0.14 (0.02 R-Square), and the correlation between gold and bond yields was -0.91 (0.84 R-Square). This tells us that over the last 15 years, 84% of gold price variability can be accounted for by bond yields and only 2% can be attributed to the variability in inflation.
When we computed the R-Square values over just the data starting March 1, 2020, we found that gold and inflation had an R-Square value as high as 0.71. However, this is only because gold, inflation, and bond yields were all highly correlated over the last few months. In conclusion, we can determine there has been a strong long-term relationship between bond yields and gold prices, but there hasn’t been a stable long-term relationship between gold prices and inflation.
With these findings in mind, we designed our trading strategy. We set the start of the backtest to January 1, 2019. We created a Scheduled Event to train a linear regression model at the beginning of each month.
self.schedule.on(self.date_rules.month_start("XAUUSD"), \
self.time_rules.at(0, 0), \
self.fit_model)
The model was trained to predict the logarithmic gold price using the latest bond yield as input. To ensure the model was trained only on recent data, we fit the model using only data from the trailing 365 days. Once trained, if the model predicted a price greater than the current gold price, we allocated 100% of our portfolio to XAUUSD. Otherwise, we liquidated our portfolio to only hold cash.
predicted_price = np.exp(self.model.predict([[bond_yield]]))
if predicted_price > gold_price:
self.set_holdings(self.gold, 1)
else:
self.set_holdings(self.gold, 0)
Results
The results of our backtest showed this strategy placed 40 trades. Through those trades, the strategy was able to produce a 2.4 Sharpe ratio and a 98% PSR. These results are impressive, but our backtest period coincided with a massive increase in the price of gold. Therefore, before deploying the strategy live, we recommend running a longer backtest to see how the strategy performs during different market regimes.
Mike smith
I tried to replicate this using the latest data up 2024 and I am getting some strange error on the research side(I have updated the 2 dropbox files with the latest data, but nothing else from the code was changed):
KeyError Traceback (most recent call last) File /opt/miniconda3/lib/python3.11/site-packages/pandas/core/indexes/base.py:3791, in Index.get_loc(self, key) 3790 try: -> 3791 return self._engine.get_loc(casted_key) 3792 except KeyError as err: File index.pyx:152, in pandas._libs.index.IndexEngine.get_loc() File index.pyx:181, in pandas._libs.index.IndexEngine.get_loc() File pandas/_libs/hashtable_class_helper.pxi:7080, in pandas._libs.hashtable.PyObjectHashTable.get_item() File pandas/_libs/hashtable_class_helper.pxi:7088, in pandas._libs.hashtable.PyObjectHashTable.get_item() KeyError: <QuantConnect.Symbol object at 0x7eff52b4b380>
Ollie Hooper
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!