This is the codebase for the captioned project. We aim to improve the performance of the TFT model mentioned in the paper Trading with the Momentum Transformer: An Intelligent and Interpretable Architecture. The codes are based on Github of the reference paper. At the END of each python file in this Github repository, we documented the parts that were edited by us.
-
Create a virtual environment:
python -m venv momentum-transformer
-
Install Python version 3.7
-
Install packages:
pip install -r requirements.txt
- If you want to download Quandl data, create a Nasdaq Data Link account to access the free Quandl dataset. This dataset provides continuous contracts for 600+ futures, built on top of raw data from CME, ICE, LIFFE etc.
- Change the list
ALL_QUANDL_CODES
for Quandl, orTICKERS
for cryptocurrency data for Coingecko. For Quandl, we use the 100 futures tickers which have i) the longest history ii) more than 90% of trading days have data iii) data up until at least Dec 2021. For Coingecko, we use 100 tickers with the highest market cap as of 1st Jan 2018. - Download the Quandl data with:
python -m data.download_quandl_data <<API_KEY>>
, or download the Coingecko data with:python -m data.download_coingecko_data
.
- Run the changepoint detection module:
python -m examples.concurent_cpd <<CPD_WINDOW_LENGTH>>
, for examplepython -m examples.concurent_cpd 21
andpython -m examples.concurent_cpd 126
. For cryptocurrency, use 30 and 183 instead. - If
concurent_cpd
does not work, trypython -m examples.sequential_cpd <<CPD_WINDOW_LENGTH>>
.
- Create Momentum Transformer input features with:
python -m examples.create_features
. The default settings would prepare a csv file with normalized returns and MACD. The following arguments can be added in the command:
--rsi
: whether to add RSI as additional input features--kd
: whether to add stochastic oscillator as additional input features--volume
: whether to add volume and VWAP as additional input features (only available for cryptocurrency)--categorical
: whether to convert MACD, RSI, stochastic oscillator values into signal labels--crypto
: whether your raw data is cryptocurrency data
- Create Momentum Transformer input features including CPD module features with:
python -m examples.create_features 21
after the changepoint detection module has completed. (or 30 for cryptocurrency data) - To create a features file with multiple changepoint detection lookback windows:
python -m examples.create_features 126 21
after the 126 (183 for crypto) day LBW changepoint detection module has completed and a features file for the 21 (30 for crypto) day LBW exists. - Additional flags can be added when calling the python module, same as those described in the previous section.
- Run one of the experiments with
python -m examples.run_dmn_experiment <<EXPERIMENT_NAME>>
. Optional arguments include:-
--rsi
: whether to add RSI as additional input features--kd
: whether to add stochastic oscillator as additional input features--volume
: whether to add volume and VWAP as additional input features (only available for cryptocurrency)--categorical
: whether to convert MACD, RSI, stochastic oscillator values into signal labels--crypto
: whether your raw data is cryptocurrency data--GLU_Variant
which GLU variant to use (Bilinear / ReGLU / GEGLU / SwiGLU), default is GLU. e.g.--GLU_Variant "Bilinear"
--train_start
: indicate the starting year of training data, e.g.--train_start 2018
--test_start
: indicate the starting year of testing data, e.g.--test_start 2021
--test_end
: indicate the ending year of testing data, e.g.--test_end 2023
--num_repeats
: indicate the number of repeats that the experiment would be conducted, e.g.--num_repeats 5
This script is for generating the results of Long only and TSMOM for benchmarking.
- Create features file for the reference experiment (See section Create input featues)
- Change necessary variables in
run_classical_strategies.py
and execute the code withpython -m examples.run_classical_strategies
.
This script is for generating variable importance for the input features of a model. This script can only be run after the model has been built and stored under the folder results
.
- Change the necessary parameters in
variable_importance.py
. - Execute the code with
python -m examples.variable_importance
. An csv file will be generated in for each experiment in theresults
folder.
This script is for simulating the change of fund balance given an initial fund of $100. Multiple experiments can be plotted on the same graph. This script can only be run after the models have been built and stored under the folder results
.
- Change the necessary parameters in
plot_returns_graph.py
. - Execute the code with
python -m examples.plot_returns_graph
. A graph will be displayed.
This script is for visualizing the difference between activation functions.
- Execute the code with
python -m examples.plot_activation_functions
. A graph will be displayed.
The results and models of each trial of each experiment in our report has been uploaded under the folder results
.