Geek Culture
Published in

Geek Culture

Using locally the Python Quantiacs Toolbox in your own environment

In this article we describe how to install the most recent version of the Python Quantiacs Toolbox on your machine and how to setup your own environment for local algorithm development.

The Quantiacs Toolbox is an open-source engine for developing and testing trading algorithms written in Python. The Toolbox can be easily integrated with all Python libraries for data science, machine learning and statistical data visualization.

The Toolbox can be used online after registering to Quantiacs or downloaded offline for use on your local machine. Getting started is easier on our cloud, however after a while it is more useful to work offline for different reasons:

  • Quantiacs provides online support for Jupyter Notebooks and JupyterLab. Working offline, you can use your preferred IDE.
  • Working on your machine you can use your local memory, processing and storage resources.
  • You can modify the source code and send us suggestions if you think the Toolbox can be improved.
  • You can download datasets on your own machine and analyze them as you like.

The source code can be inspected at our GitHub repository, where you can find the source code and many examples for getting started with algorithmic trading and using more advanced methods.

The Quantiacs Toolbox is written in Python, it operates on Linux, Mac and Windows operating systems and it supports trading systems written in Python 3.7. Its structure is highly modular and the engine is very fast, as it relies on xarray and pandas for data handling and on numba for high-performance computing.

Installing the Toolbox

The best solution for installing the Quantiacs Toolbox is to use conda as it allows for a stable and flexible handling of packages and it provides the option to easily define environments for your projects. Moreover, as a package manager, conda helps you find and install packages and it can install and manage the thousand packages at repo.anaconda.com.

For using conda you can simply follow the installation instructions available for example at the Anaconda page.

After the conda installation you can create an isolated environment for developing strategies and install the Quantiacs Toolbox as follows:

conda create -n qntdev quantiacs-source::qnt conda-forge::ta-lib conda-forge::pandas==1.2.5 conda-forge::dash==1.18 python==3.7

The command will create an environment called qntdev which can de activated using:

conda activate qntdev

and deactivated, for example when you want to revert to the conda base environment, using:

conda deactivate qntdev

Setting your API key

Next, you need to set your API key. Your API key is available in your personal account. Click on “Show” and copy the key in your clipboard.

Then, save your API key in our conda environment using:

conda env config vars set -n qntdev API_KEY=your_key

If you have issues, which can arise using for example PyCharm, you can add the following lines to the head of your strategy, to make sure that the API key is correctly set:

import os
os.environ['API_KEY'] = "your_key"

Running locally a system

Now you can run locally a strategy: activate your environment, code the system in a strategy.py file, and run it typing:

python strategy.py

A simple starting example trading cryptocurrencies can be for example a crossover system:

import xarray as xrimport qnt.ta as qnta
import qnt.backtester as qnbt
import qnt.data as qndata
def load_data(period):
return qndata.cryptodaily_load_data(tail=period)
def strategy(data):
close = data.sel(field=”close”)
is_liquid = data.sel(field=”is_liquid”)
sma_slow = qnta.sma(close, 200).isel(time=-1)
sma_fast = qnta.sma(close, 20).isel(time=-1)
weights = xr.where(sma_slow < sma_fast, 1, 0)
weights = weights * is_liquid
weights = weights / 10.0
return weights
weights = qnbt.backtest(
competition_type = “crypto_daily_long”,
load_data = load_data,
lookback_period = 365,
start_date = “2014–01–01”,
strategy= strategy)

After running the system, a data visualization app based on dash will show up. There you can inspect the position values for your assets:

You can check the values of the most important statistical indicators:

In addition, you can visualize the evolution of Profit and Losses, the Underwater Chart and the rolling Sharpe Ratio:

More information can be found on the Quantiacs documentation page.

Your code can be uploaded to our contest by translating it to a Jupyter Notebook with name strategy.ipynb and submitting it.

Adding packages

Installing new packages is easy, simply use conda, for example:

! conda install -y scikit-learn

can be used for installing the scikit-learn package. Dependencies on the cloud can be resolved editing the init.py file you can find in your root directory.

Do you have questions or suggestions? Contact us on our Forum page!

Are you using Google Colab?

  • If you want to use a hosted runtime, start with this notebook. It contains the necessary commands to configure a hosted runtime.
  • If you use colab with a local runtime, then you can use a regular conda environment.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store