[UQT] C18: Temporal Fusion Transformer (TFT) based trading strategy

Supriya Devidutta
3 min readDec 25, 2022

--

Temporal fusion transformer (TFT) is a type of deep learning model that is designed for modeling long sequences of data. It is a variant of the transformer architecture, which is a type of neural network that uses self-attention mechanisms to process sequences of data.

The TFT model has several key features that make it well-suited for modeling long sequences of data:

  1. Multi-scale processing: The TFT model uses multiple levels of self-attention to process the input data at different scales, which allows it to capture both short- and long-range dependencies in the data.
  2. Fusion mechanism: The TFT model uses a fusion mechanism to combine the outputs from the different levels of self-attention, which allows it to effectively integrate information from different scales.
  3. Temporal encoding: The TFT model uses a temporal encoding scheme to represent the input data, which allows it to efficiently model long sequences of data.

Overall, the TFT model is a powerful and flexible tool for modeling long sequences of data, and it has been shown to achieve state-of-the-art results on a wide range of tasks, including natural language processing, machine translation, and time series prediction.

Application to Stock data: One study published in 2021 used a TFT model to predict stock prices for a large number of companies listed on the NASDAQ stock exchange. The authors found that the TFT model was able to outperform several baseline models, including a long short-term memory (LSTM) model and a convolutional neural network (CNN) model, on this task.

Another study published in 2020 applied a TFT model to predict stock prices for a single company, and found that the model was able to outperform a LSTM model on this task.

Overall, these studies suggest that the TFT model can be a effective tool for predicting stock prices.

Steps to write a python example:

  1. Collects Apple stock price data using the yfinance library.
  2. Preprocesses the data by removing missing values and normalizing it.
  3. Tokenizes the data using the TFTokenizer from the transformers library.
  4. Builds a TFT model using the TFFusionModel from the transformers library.
  5. Splits the data into train and test sets.
  6. Trains the TFT model.
  7. Makes predictions on the test set.
  8. Evaluates the model using mean absolute error.
import yfinance as yf
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

from transformers import TFTokenizer, TFFusionModel
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_absolute_error

# Collect Apple stock price data
apple = yf.Ticker("AAPL")
data = apple.history(period="max")

# Preprocess the data
data.dropna(inplace=True)
data = data[['Close']]
scaler = MinMaxScaler(feature_range=(0, 1))
data = scaler.fit_transform(data)

# Tokenize the data using the TFTokenizer
tokenizer = TFTokenizer.from_pretrained('t5-small')
input_ids = tokenizer.encode(data.tolist(), max_length=1024)
input_ids = np.expand_dims(input_ids, axis=0)

# Build the TFT model
model = TFFusionModel.from_pretrained('t5-small')

# Split the data into train and test sets
train_size = int(len(input_ids) * 0.8)
train, test = input_ids[:train_size], input_ids[train_size:]

# Train the TFT model
model.train_model(train)

# Make predictions on the test set
predictions = model.predict(test)

# Inverse transform the predictions and the test data
predictions = scaler.inverse_transform(predictions[0])
test = scaler.inverse_transform(test[0])

# Plot the predictions and the test data
plt.plot(predictions, label='Predictions')
plt.plot(test, label='Test data')
plt.legend()
plt.show()

# Generate a trading signal based on the predictions
signal = np.zeros(len(predictions))
for i in range(1, len(predictions)):
if predictions[i] > test[i-1]:
signal[i] = 1
else:
signal[i] = -1

# Plot the trading signal
plt.plot(signal, label='Trading signal')
plt.legend()
plt.show()

Disclaimer: The content herein is for education purpose only. I do not endorse or recommend any particular investment or financial product, nor do i make any representation. It is the responsibility of the reader to conduct their own research and due diligence, and consult with a qualified financial advisor before making any investment or trading decisions.

--

--

Supriya Devidutta

Sr Engineering Manager, traveller, foodie , AI / Quant enthusiast, Educator