Local development of trading strategies

You can use the Quantiacs library (QNT) for developing locally trading strategies on your computer.

You can follow these easy steps and create an isolated environment on your machine using conda for managing dependencies and avoiding conflicts:

  1. Install Anaconda from https://www.anaconda.com/products/individual or https://repo.anaconda.com/archive/.

  2. Create an isolated environment for developing strategies and install the QNT library together with needed dependencies:

    conda create -n qntdev quantiacs-source::qnt conda-forge::ta-lib conda-forge::dash=1.18 python=3.7
    
  3. Set your API key. You can find it in your profile on your personal Quantiacs area.

    key

    conda env config vars set -n qntdev API_KEY={your_api_key_here}
    
  4. Activate your environment if not yet activated:

    conda activate qntdev
    

    For leaving the environment:

    conda deactivate
    

    Each time you want to use the QNT library, reactivate the environment.

  5. Develop your strategy using the IDE you like. For example, you can develop strategies using Jupyter notebook (specify your {USER_NAME}):

     conda install notebook python=3.7 &&
     conda deactivate && 
     conda activate qntdev && 
     /home/{USER_NAME}/anaconda3/envs/qntdev/bin/jupyter notebook
    

A good starting point is represented by the following strategy.py file, where a simple long-short trading strategy based on the crossing of two simple moving averages with lookback periods of 20 and 200 trading days is implemented:

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
)

Here we have used the function qnt.backtester.backtest whose details can be found in your private area in the /qnt/backtester.py file.

The function requires the following input:

  • competition type: “futures” for the futures contest or “cryptofutures” for the Bitcoin futures contest;

  • load data: the pre-defined load_data function. The period passed to load_data is given by test_period + lookback_period;

  • lookback_period: the maximal lookback period in calendar days used for building indicators. In this case, as we use 200 trading days for defining the largets simple moving average, 1 year is fine;

  • test_period, in calendar days, is the In-Sample period used for the simulation. Here we use 2 years of data;

  • strategy: the pre-defined strategy function which should return allocation weights for all assets at a fixed point in time (note that in strategy we select the last index, isel(time=-1)).

This strategy uses a lookback period of 365 calendar days for computing indicators and, after a warmup of 365 calendar days, performs a simulation for 2 years.

  1. Use this command to run your strategy (replace python with python3 if your default python version is 2):

     python strategy.py
    
  2. When you finish with developing your strategy, you need to upload your code 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:

    nb

    b) Upload your python file (for example, strategy.py) in your Jupyter environment root directory and type in ** strategy.ipynb**:

     import strategy
    
  3. 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.

  4. Send your strategy to the Contest from the Development area on your home page by clicking on the Submit button:

    submit

  5. Wait for your strategy to pass contest filters and take part in the Contest.