Skip to main content

Introduction

Forecasting time series is a ubiquitous task in finance, supporting decisions in trading, risk management, and strategic planning. Despite its prevalence, predicting the future prices of financial assets remains a formidable challenge, mainly due to the inherent volatility of financial markets. For those who believe in the feasibility of forecasting these assets, or for professionals whose roles require such predictions, TimeGPT is a powerful tool that simplifies the forecasting process. In this tutorial, we will demonstrate how to use TimeGPT for financial time series forecasting, focusing on Bitcoin price prediction. We will also showcase how to use TimeGPT for uncertainty quantification, which is essential for risk management and decision-making.

Why Forecast Bitcoin Prices

Bitcoin (₿) is the first decentralized digital currency and is one of the most popular cryptocurrencies. Transactions are managed and recorded on a public ledger known as the blockchain. Bitcoins are created as a reward for mining, a process that involves solving complex cryptographic tasks to verify transactions. This digital currency can be used as payment for goods and services, traded for other currencies, or held as a store of value. Bitcoin’s high volatility and widespread trading make accurate forecasting a valuable tool for financial analysis, trading strategies, and risk management.

What You’ll Learn

  • How to load and prepare Bitcoin price data
  • How to generate short-term forecasts with TimeGPT
  • How to visualize and interpret forecast results
  • How to detect anomalies and add exogenous variables
The procedures in this tutorial apply to many financial asset forecasting scenarios, not just Bitcoin.
Quick Summary: This tutorial covers Bitcoin price prediction using TimeGPT in Python. You’ll learn to load cryptocurrency data, generate 7-day forecasts with confidence intervals, detect price anomalies, and understand forecasting limitations for volatile crypto markets.

How to Use TimeGPT to Forecast Bitcoin Prices

Open In Colab

Step 1: Load Bitcoin Price Data

Bitcoin (₿) is the first decentralized digital currency, with transactions recorded on a public ledger called the blockchain. Bitcoins are created through mining—solving cryptographic tasks—and are used for payments, trading, and long-term investment. For convenience, we’ll rename columns to match TimeGPT’s expected ds (date) and y (target) format.
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-01-01 to 2023-12-31. While Bitcoin trades continuously, “closing price” here refers to a specific daily time rather than a traditional market close.
# 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 Price Data

Bitcoin Price Data

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)
Panel view of historical data with future forecasts

Panel view of historical data with future forecasts

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.
Zoomed view showing 7-day forecast with 50%, 80%, and 90% confidence intervals

Zoomed view showing 7-day forecast with 50%, 80%, and 90% confidence intervals

Step 5: Extend Bitcoin Price Analysis with TimeGPT

Anomaly Detection

Given the volatility of the price of Bitcoin, it can be useful to try to identify anomalies in the data. TimeGPT can be used for this by calling the detect_anomalies method from the NixtlaClient class. This method evaluates each observation against its context within the series, using statistical measures to determine its likelihood of being an anomaly. By default, it identifies anomalies based on a 99 percent prediction interval. To change this, you can specify 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
)
Highlighted anomalies in Bitcoin price data

Highlighted anomalies in Bitcoin price data

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

Add Exogenous Variables

If you have additional information that you believe could help improve the forecast, consider including it as an exogenous variable. For instance, you might add data such as the price of other cryptocurrencies, proprietary information, stock market indices, or the number of transactions in the Bitcoin network. TimeGPT supports the incorporation of exogenous variables in the forecast method. However, keep in mind that you’ll need to know the future values of these variables. To learn how to incorporate exogenous variables to TimeGPT, see Numeric Features Guide.

Step 7: Understand the Model Limitations

As stated in the introduction, predicting the future prices of financial assets is a challenging task, especially for assets like Bitcoin. The predictions in this tutorial may appear accurate, mainly because they align with recent historical data and the model updates with new values at short intervals, avoiding significant deviations. However, the true challenge lies in forecasting Bitcoin’s price for the upcoming days, not just its 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.

Next Steps in Cryptocurrency Forecasting

You’ve successfully learned how to forecast Bitcoin prices using TimeGPT with Python. Apply these techniques to other cryptocurrencies like Ethereum, Cardano, or XRP. Ready to forecast at scale? Sign up for Nixtla API access and start predicting cryptocurrency prices today.

Frequently Asked Questions

How accurate is Bitcoin price prediction with TimeGPT? TimeGPT provides probabilistic forecasts with uncertainty intervals, making it suitable for risk-aware trading strategies. Accuracy depends on market volatility and forecast horizon. Can TimeGPT predict cryptocurrency crashes? TimeGPT includes anomaly detection capabilities that can identify unusual patterns in Bitcoin price data, helping detect potential market disruptions. What data do I need for Bitcoin price forecasting? You need historical Bitcoin price data with timestamps. This tutorial uses daily closing prices from 2020-2023. Does this work for other cryptocurrencies? Yes, you can apply these same techniques to forecast Ethereum, Cardano, XRP, or any other cryptocurrency with historical price data.

References and Additional Material

Financial time series often exhibit random walk behavior, especially cryptocurrencies. Evaluate your models thoroughly with statistical metrics and domain knowledge to make informed decisions.
I