Welcome to the Quantiacs Python Trading Library (QNT), a comprehensive platform for quantitative finance and algorithmic trading. This library is designed for both beginners and seasoned traders, enabling the development and testing of trading algorithms.
Discover more about Quantiacs:
- Website: Quantiacs.com
- Detailed Documentation: Quantiacs Documentation
- Backtesting Engine: Test your strategies with our advanced backtesting engine.
- Market Data Access: Access a wide range of financial data, including stocks, futures, and cryptocurrencies.
- Strategy Optimization: Enhance the performance of your algorithms.
- Community and Support: Join a thriving community of quantitative traders.
Quantiacs hosts a variety of quant competitions, catering to different asset classes and investment styles:
- The Classic Quantiacs Futures Contest: A mainstay contest focusing on futures trading.
- The Crypto Bitcoin Futures Contest: Tailored for trading Bitcoin futures.
- The Crypto Top-10 Long-Only Contest: Concentrating on a long-only strategy in the top 10 cryptocurrencies.
- The Crypto Top-10 Long-Short Contest: Involves both long and short positions in the top 10 cryptocurrencies.
- The NASDAQ-100 Long-Short contest: A specialized contest focusing on long-short strategies in the NASDAQ-100, emphasizing fundamental analysis.
- The Q22 S&P500 Long-Short contest: A specialized contest focusing on long-short strategies in the S&P500.
Since 2014, Quantiacs has hosted numerous quantitative trading contests, allocating over 38 million USD to winning algorithms in futures markets. Since 2021, the platform has expanded to include contests for predicting futures, cryptocurrencies, and stocks.
The Quantiacs library (QNT) is optimized for local strategy development. We recommend using Conda for its stability and ease of managing dependencies.
-
Install Anaconda: Download and install Anaconda from Anaconda's official site.
-
Create a QNT Development Environment:
- Open your terminal and run:
conda create -n qntdev -c conda-forge -c quantiacs-source 'python>=3.10,<3.11' 'ipywidgets=8.1.3' 'nbformat>=4.2.0' ipykernel ta-lib qnt dash conda activate qntdev
- Optional: Prevent auto-activation of this environment:
conda config --set auto_activate_base false
- Alternative: Create a local environment in the project directory:
git clone https://github.com/quantiacs/toolbox.git cd toolbox conda create --prefix ./conda -c conda-forge -c quantiacs-source 'python>=3.10,<3.11' 'ipywidgets=8.1.3' 'nbformat>=4.2.0' ipykernel ta-lib qnt dash
- Open your terminal and run:
-
API Key Configuration:
-
Retrieve your API key from your Quantiacs profile.
-
Set the API key in your environment:
conda env config vars set -n qntdev API_KEY={your_api_key_here}
-
Alternatively, set the API key in your code (useful for IDE compatibility issues):
import os os.environ['API_KEY'] = "{your_api_key_here}"
-
-
Using the Environment:
- Activate the environment with:
conda activate qntdev
- Deactivate when done using:
conda deactivate
- Always reactivate when returning to development.
- Activate the environment with:
-
Strategy Development:
- Develop in your preferred IDE.
- For Jupyter notebook usage:
jupyter notebook
- To use PyCharm IDE with your created Conda environment, follow these steps:
- Open PyCharm.
- Navigate to
Settings
->Project
->Python Interpreter
. - Click on
Add Interpreter
. - Select
Conda Environment
. - Specify the path to the created Conda environment.
-
Contest Participation:
- Develop and test your strategy, then submit it to Quantiacs contests.
We recommend using Conda for its stability and ease of managing dependencies.
Installation instructions are the same as for Using QNT from Github. In step two, run the command
conda create -n qntdev -c conda-forge -c quantiacs-source 'python>=3.10,<3.11' 'ipywidgets=8.1.3' 'nbformat>=4.2.0' ipykernel ta-lib qnt dash
- Regularly update the QNT library for the latest features and fixes:
## you can remove the old one before that
# conda remove -n qntdev quantiacs-source::qnt
conda install -n qntdev quantiacs-source::qnt
You can see the library updates here.
Note: While Conda is recommended, Pip can also be used, especially if Conda is not an option.
This one-liner combines the installation of Python, creation of a virtual environment, and installation of necessary libraries.
-
One Command Setup:
- Ensure you have
pyenv
andpyenv-virtualenv
installed. - Run the following command in your terminal:
pyenv install 3.10.14 && \ pyenv virtualenv 3.10.14 name_of_environment && \ pyenv local name_of_environment && \ python -m pip install 'ipywidgets==8.1.3' 'nbformat>=4.2.0' dash ipykernel git+https://github.com/quantiacs/toolbox.git
This command will:
- Install Python 3.10.14.
- Create a virtual environment named
name_of_environment
. - Activate the environment for the current directory.
- Install the Quantiacs toolbox and other necessary Python libraries.
- Ensure you have
-
TA-Lib Installation:
- The TA-Lib library may need to be installed separately due to its specific installation requirements.
-
Setting the API Key:
- Set the Quantiacs API key in your code:
import os os.environ['API_KEY'] = "{your_api_key_here}"
- Replace
{your_api_key_here}
with the actual API key found in your Quantiacs profile.
- Set the Quantiacs API key in your code:
Use this command in your environment to install the latest version from the git repository:
python -m pip install --upgrade git+https://github.com/quantiacs/toolbox.git
If you want to use Google Colab with a hosted runtime, start with this notebook. This notebook contains the necessary commands to configure a hosted runtime.
If you use colab with a local runtime, then you can use regular conda environment. Go to the head of this page and follow the instructions for conda.
At first, setup the toolbox from github using pip:
! pip install git+https://github.com/quantiacs/toolbox.git 2>/dev/null
Then install TA-Lib (indicators library) if you need it.
!wget http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-src.tar.gz
!tar -xzvf ta-lib-0.4.0-src.tar.gz
%cd ta-lib
!./configure --prefix=/usr
! make
!make install
!pip install Ta-Lib
import talib
You can open any strategy on Quantiacs and check dependencies.
-
Launch Jupyter: Start Jupyter Notebook or Jupyter Lab.
-
Open a Strategy File: Load any
.ipynb
strategy file. -
List Dependencies: Execute
!conda list
in a notebook cell to display installed packages. -
Review Dependencies: Ensure all required dependencies for your strategy are present and up-to-date.
This quick check aids in maintaining a robust environment for your Quantiacs trading strategies.
Using the Quantiacs (QNT) library involves a few basic steps to create and run a trading strategy. Here's a guide on how to do it:
The first step in utilizing the qnt library is to define your trading strategy. An example of a simple strategy is
provided below in the strategy.py
file. This example demonstrates a basic long-short trading strategy based on the
crossing of two simple moving averages (SMAs) with lookback periods of 20 and 200 trading days.
The following Python code illustrates a straightforward implementation of a trading strategy using the qnt library:
# import os
#
# os.environ['API_KEY'] = "{your_api_key_here}"
import qnt.ta as qnta
import qnt.data as qndata
import qnt.backtester as qnbk
import xarray as xr
def load_data(period):
data = qndata.futures_load_data(tail=period)
return data
def strategy(data):
close = data.sel(field='close')
sma200 = qnta.sma(close, 200).isel(time=-1)
sma20 = qnta.sma(close, 20).isel(time=-1)
return xr.where(sma200 < sma20, 1, -1)
qnbk.backtest(
competition_type="futures",
load_data=load_data,
lookback_period=365,
test_period=2 * 365,
strategy=strategy,
check_correlation=False
)
After creating your strategy, the next step is to run it using the Python command line. To execute your strategy, you can use the following command in your Python environment:
python strategy.py
This command runs the strategy.py script, which contains the defined trading strategy and invokes the backtest function from the qnt library. It's important to ensure that the Python environment where you run this command has the qnt library installed and is properly set up to access market data.
Executing and submitting your strategy:
-
When you finish with developing your strategy, you need to upload your code in the Jupyter Notebook environment on the Quantiacs webpage. There are 2 options:
a) Copy and paste your code inside the cell of a Jupyter Notebook:
b) Upload your python file (for example, strategy.py) in your Jupyter environment root directory and type in strategy.ipynb:
import strategy
Place the installation commands for external dependencies to init.ipynb.
-
Run all cells to test your strategy in the Jupyter Notebook. Fix the errors if it is necessary. It is a good idea to run the file precheck.ipynb.
-
Send your strategy to the Contest from the Development area on your home page by clicking on the Submit button: