Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

PyTorch

Overview

Roboquant uses the excellent PyTorch framework as a basis for the included neural-networks based strategies. It comes with many popular network architecture layers out of the box and has a Pythonic API, making it easier to get started.

Example

This section provides an example how to develop a strategy that uses PyTorch model to make predictions.

Data and Configuration

Here we define some configuration variables, the feed we’ll be using, and the timeframes for training and testing.

Model

We start with defining a LSTM model we want to use. LSTM (Long Short-Term Memory) is a type of recurrent neural network (RNN) well-suited for time-series and sequential data.

Unlike standard RNNs, LSTMs can learn long-term dependencies by using a gating mechanism that controls the flow of information, making them effective for financial time-series prediction where patterns may span many time steps.

Please note this is just an example and likely to overfit with the limited amount of data we have in this example.

Features

Now we define the input and label features we want to use. This is just a small sample of the available features and custom features can also be added.

Strategy

Finally, we create the actual strategy using the LSTM model we just defined.

Training

For fitting we use the timeframe with the first twenty years of data. There is also support for validation.

strategy.fit(
    feed,
    timeframe=train_tf,
    epochs=20,
    validation_split=0.25,
    prediction=prediction_steps)

Testing

After the model has been fitted, we can now use the strategy in a test run to see how it performs on unseen historic data.

account = rq.run(feed, strategy, timeframe=test_tf)