Skip to main content

Introduction

Time series forecasting is essential in finance for trading, risk management, and strategic planning. However, predicting financial asset prices remains challenging due to market volatility. Whether you believe financial forecasting is possible or your role requires it, TimeGPT simplifies the process. This tutorial demonstrates how to use TimeGPT for Bitcoin price prediction and uncertainty quantification for risk management.

Why Forecast Bitcoin Prices

Bitcoin (₿), the first decentralized cryptocurrency, records transactions on a blockchain. Bitcoins are mined by solving cryptographic tasks and can be used for payments, trading, or investment. Bitcoin’s volatility and popularity make price forecasting valuable for trading strategies and risk management.

What You’ll Learn

The procedures in this tutorial apply to many financial asset forecasting scenarios, not just Bitcoin.

How to Forecast Bitcoin Prices with TimeGPT

Open In Colab

Step 1: Load Bitcoin Price Data

Start by loading the Bitcoin price data:
import pandas as pd

# Load Bitcoin historical price data from 2020-2023
df = pd.read_csv(
    'https://raw.githubusercontent.com/Nixtla/transfer-learning-time-series/main/datasets/bitcoin_price_usd.csv',
    sep=','
)

df.head()
DateClose
02020-01-017200.174316
12020-01-026985.470215
22020-01-037344.884277
32020-01-047410.656738
42020-01-057411.317383
This dataset includes daily Bitcoin closing prices (in USD) from 2020 to 2023. “Closing price” refers to the price at a specific daily time, not a traditional market close. Next, rename the columns to match TimeGPT’s expected ds (date) and y (target) format.
# Rename columns to TimeGPT's expected format (ds=date, y=target value)
df.rename(columns={'Date': 'ds', 'Close': 'y'}, inplace=True)

Step 2: Get Started with TimeGPT

Initialize the NixtlaClient with your Nixtla API key. To learn more about how to set up your API key, see Setting up your API key.
from nixtla import NixtlaClient

# Initialize TimeGPT client with your API key
nixtla_client = NixtlaClient(
    api_key='my_api_key_provided_by_nixtla'
)

Step 3: Visualize the Data

Before attempting any forecasting, it is good practice to visualize the data we want to predict. The NixtlaClient class includes a plot method for this purpose. The plot method has an engine argument that allows you to choose between different plotting libraries. Default is matplotlib, but you can also use plotly for interactive plots.
# Visualize Bitcoin price history
nixtla_client.plot(df)
Bitcoin historical price data from 2020-2023 showing upward trends and significant volatility patterns

Bitcoin historical price data from 2020-2023 showing price trends and volatility

If you did not rename the columns, specify them explicitly:
nixtla_client.plot(
    df,
    time_col='Date Column',
    target_col='Close Column'
)

Step 4: Forecast with TimeGPT

Now we are ready to generate predictions with TimeGPT. To do this, we will use the forecast method from the NixtlaClient class. The forecast method requires the following arguments:
  • df: The DataFrame containing the time series data
  • h: (int) The forecast horizon. In this case, we will forecast the next 7 days.
  • level: (list) The confidence level for the prediction intervals. Given the inherent volatility of Bitcoin, we will use multiple confidence levels.
# Generate 7-day forecast with 50%, 80%, and 90% prediction intervals
level = [50, 80, 90]

fcst = nixtla_client.forecast(
    df,
    h=7,  # Forecast horizon: 7 days
    level=level  # Confidence intervals for uncertainty quantification
)

fcst.head()
dsTimeGPTTimeGPT-lo-90TimeGPT-lo-80TimeGPT-lo-50TimeGPT-hi-50TimeGPT-hi-80TimeGPT-hi-90
02024-01-0142269.46093839567.20902040429.95363641380.65464643158.26722944108.96823944971.712855
12024-01-0242469.91796939697.94166940578.19704941466.51136143473.32457644361.63888845241.894268
22024-01-0342864.07812540538.87124341586.25250742284.31667443443.83957644141.90374345189.285007
32024-01-0442881.62109440603.11744841216.10649342058.53939243704.70279544547.13569445160.124739
42024-01-0542773.45703140213.69976040665.38478041489.81243144057.10163244881.52928245333.214302
We can pass the forecasts we just generated to the plot method to visualize the predictions with the historical data.
# Plot historical data with forecast and confidence intervals
nixtla_client.plot(df, fcst, level=level)
Bitcoin price forecast showing 7-day ahead predictions with 50%, 80%, and 90% confidence intervals using TimeGPT

Bitcoin price forecast with 7-day predictions and multiple confidence intervals

To get a closer look at the predictions, we can zoom in on the plot or specify the maximum number of in-sample observations to be plotted using the max_insample_length argument. Note that setting max_insample_length=60, for instance, will display the last 60 historical values along with the complete forecast.
Detailed zoom showing Bitcoin 7-day price forecast with 50%, 80%, and 90% prediction intervals for uncertainty quantification

Detailed view of Bitcoin 7-day forecast with multiple prediction intervals

Step 5: Extend Bitcoin Price Analysis with TimeGPT

Anomaly Detection

Given Bitcoin’s volatility, identifying anomalies can be valuable. Use TimeGPT’s detect_anomalies method to evaluate each observation statistically within its series context. By default, it identifies anomalies using a 99% prediction interval, which you can adjust with the level argument.
# Detect anomalies in Bitcoin price data
anomalies_df = nixtla_client.detect_anomalies(df)

# Visualize anomalies highlighted on the price chart
nixtla_client.plot(
  df,
  anomalies_df,
  plot_anomalies=True
)
Bitcoin price anomaly detection showing highlighted unusual price movements and volatility spikes identified by TimeGPT

Bitcoin price anomaly detection highlighting unusual market movements

To learn how to incorporate exogenous variables to TimeGPT, see Real-time Anomaly Detection.

Add Exogenous Variables

To improve forecasts, include relevant data as exogenous variables, such as other cryptocurrency prices, stock market indices, or Bitcoin network transaction volumes. To learn how to incorporate exogenous variables to TimeGPT, see Numeric Features Guide.

Understand the Model Limitations

As stated in the introduction, predicting Bitcoin prices is challenging. The predictions here may appear accurate because they use recent data and update frequently, but the real test is forecasting future prices, not historical performance. For those who need or want to try to forecast these assets, TimeGPT can be an option that simplifies the forecasting process. With just a couple of lines of code, TimeGPT can help you:
  • Produce point forecasts
  • Quantify the uncertainty of your predictions
  • Produce in-sample forecasts
  • Detect anomalies
  • Incorporate exogenous variables
To learn more about TimeGPT capabilities, see the TimeGPT Introduction.

Conclusion

TimeGPT simplifies Bitcoin price forecasting by providing:
  • Accurate short-term predictions with quantified uncertainty
  • Automated anomaly detection for risk management
  • Support for exogenous variables to improve forecast accuracy
This approach applies to various cryptocurrency and financial asset forecasting scenarios, helping traders and analysts make data-driven decisions despite market volatility.

Next Steps

References and Additional Material