Forecasting SPY using TimeGPT

Let's try forecasting next week's SPY closing prices using TimeGPT and see how it performs.

Forecasting SPY using TimeGPT
Photo by Chris Liverani / Unsplash

TimeGPT is a production-ready generative pretrained transformer for time series. It’s capable of accurately predicting various domains such as retail, electricity, finance, and IoT. Let's try forecasting next week's SPY closing prices using TimeGPT and see how it performs.

The data

We will use last year's SPY daily adjusted closing prices for this exercise, a total of 252 data points.

Last year's SPY daily adjusted closing prices

Forecast using TimeGPT

We will try to forecast the next 7 days' SPY closing prices using TimeGPT.

First we need to install nixtla by running pip install nixtla.

Then we initialize the NitxlaClient which requires an API key to access TimeGPT. You can get your API key here.

from nixtla import NixtlaClient

nixtla_client = NixtlaClient(
    # defaults to os.environ.get("NIXTLA_API_KEY")
    api_key = 'YOUR_NIXTLA_API_KEY'
)

Then we can forecast SPY closing prices for the next 7 days.

forecast = nixtla_client.forecast(df, h=7, freq='B', time_col='date', target_col='SPY')

Here are the results:

date TimeGPT
2024-07-11 562.443
2024-07-12 562.611
2024-07-15 563.493
2024-07-16 564.101
2024-07-17 563.833
2024-07-18 563.227
2024-07-19 564.2

What about uncertainty?

The above forecast results are point predictions. Usually the reality does not exactly match forecast. If we know the range of the prediction, we can make better informed decisions. We can use multiple confidence levels (50%, 80% and 90%) with TimeGPT.

forecast = nixtla_client.forecast(df, h=7, freq='B', time_col='date', target_col='SPY', level=[50, 80, 90])

Here are the results:

date TimeGPT TimeGPT-lo-90 TimeGPT-lo-80 TimeGPT-lo-50 TimeGPT-hi-50 TimeGPT-hi-80 TimeGPT-hi-90
2024-07-11 562.443 556.664 557.151 558.216 566.669 567.735 568.221
2024-07-12 562.611 552.142 553.118 557.728 567.495 572.104 573.081
2024-07-15 563.493 552.572 554.28 559.204 567.783 572.707 574.415
2024-07-16 564.101 553.351 555.058 558.475 569.726 573.144 574.85
2024-07-17 563.833 549.934 552.478 557.478 570.188 575.188 577.732
2024-07-18 563.227 549.812 552.444 553.863 572.592 574.01 576.643
2024-07-19 564.2 550.439 553.206 556.606 571.794 575.194 577.961

Performance

How do we know how good the predictions are? Let's look at the predictions on historical data.

The trend of predictions largely track the historical prices, and historical prices mostly fall within 50% confidence interval. That's a good sign. In the next post, we will evaluate TimeGPT's performance using more sophisticated cross validation technique.

Summary

In this post, we use TimeGPT to predict the next 7 days' adjusted closing prices of SPY, and qualitatively evaluated its performance - historical prices largely fall within 50% confidence interval, which is promising. Stay tuned for more experiments with TimeGPT.