/nautilus_trader

A high-performance algorithmic trading platform and event-driven backtester

Primary LanguagePythonGNU Lesser General Public License v3.0LGPL-3.0

Nautech Systems


NautilusTrader

build Documentation Status codacy-quality codecov lines pypi-pythons pypi-version Downloads pypi-format code-style: black

The project is temporarily out of space on PyPI. Please install from source for the latest version.

Introduction

NautilusTrader is an open-source, high-performance, production-grade algorithmic trading platform, providing quantitative traders with the ability to backtest portfolios of automated trading strategies on historical data with an event-driven engine, and also deploy those same strategies live.

NautilusTrader is AI/ML first, designed to deploy models for algorithmic trading strategies developed using the Python ecosystem - within a highly performant and robust Python native environment.

The platform aims to be universal, with any REST/FIX/WebSocket API able to be integrated via modular adapters. Thus the platform can handle high-frequency trading operations for any asset classes including FX, Equities, Futures, Options, CFDs and Crypto - across multiple venues simultaneously.

Features

  • Fast: C-level speed and type safety provided through Cython. Asynchronous networking utilizing uvloop.
  • Reliable: Redis backed performant state persistence for live implementations.
  • Flexible: Any FIX, REST or WebSocket API can be integrated into the platform.
  • Backtesting: Multiple instruments and strategies simultaneously with historical quote tick, trade tick, bar and order book data.
  • Multi-venue: Multiple venue capabilities facilitate market making and statistical arbitrage strategies.
  • AI Agent Training: Backtest engine fast enough to be used to train AI trading agents (RL/ES).

Why NautilusTrader?

One of the key value propositions of NautilusTrader is that it addresses the challenge of keeping the research/backtest environment consistent with the production live trading environment.

Normally research and backtesting may be conducted in Python (or other suitable language), with trading strategies traditionally then needing to be reimplemented in C++/C#/Java or other statically typed language(s). The reasoning here is to enjoy the performance a compiled language can offer, along with the tooling and support which has made these languages historically more suitable for large enterprise systems.

The value of NautilusTrader here is that this re-implementation step is circumvented, as the platform was designed from the ground up to hold its own in terms of performance and quality.

Python has simply caught up in performance (via Cython offering C-level speed) and general tooling, making it a suitable language for implementing a large system such as this. The benefit being that a Python native environment can be offered, suitable for professional quantitative traders and hedge funds.

Why Python?

Python was originally created decades ago as a simple scripting language with a clean straight forward syntax. It has since evolved into a fully fledged general purpose object-oriented programming language. Not only that, Python has become the de facto lingua franca of data science, machine learning, and artificial intelligence.

The language out of the box is not without its drawbacks however, especially in the context of implementing large systems. Cython has addressed a lot of these issues, offering all the advantages of a statically typed language, embedded into Pythons rich ecosystem of software libraries and developer/user communities.

What is Cython?

Cython is a compiled programming language that aims to be a superset of the Python programming language, designed to give C-like performance with code that is written mostly in Python with optional additional C-inspired syntax.

The project heavily utilizes Cython to provide static type safety and increased performance for Python through C extension modules. The vast majority of the production Python code is actually written in Cython, however the libraries can be accessed from both pure Python and Cython.

Values

  • Reliability
  • Performance
  • Testability
  • Modularity
  • Maintainability
  • Scalability

Documentation

The documentation for the latest version of the package is available at readthedocs.

https://nautilus-trader.readthedocs.io

Architecture

Installation

The master branch will always reflect the code of the latest release version. Also, the documentation is always current for the latest version.

The package is tested against Python 3.7 - 3.9 on both Linux and MacOS.

We recommend users setup a virtual environment to isolate the dependencies, and run the platform with the latest stable version of Python.

Installation for Unix-like systems can be achieved through one of the following options;

From PyPI

To install the latest binary wheel (or sdist package) from PyPI, run:

pip install -U nautilus_trader

From GitHub Release

To install a binary wheel from GitHub, first navigate to the latest release.

https://github.com/nautechsystems/nautilus_trader/releases/latest/

Download the appropriate .whl for your operating system and Python version, then run:

pip install <file-name>.whl

From Source

Installation from source requires Cython to compile the Python C extensions.

  1. To install Cython, run:

     pip install -U Cython==3.0a6
    
  2. Then to install NautilusTrader using pip, run:

     pip install -U git+https://github.com/nautechsystems/nautilus_trader
    

    Or clone the source with git, and install from the projects root directory by running:

     git clone https://github.com/nautechsystems/nautilus_trader
     cd nautilus_trader
     pip install .
    

Data Types

The following data types can be requested, and also subscribed to as streams.

  • Instrument
  • OrderBook (L1, L2 and L3 if available. Streaming or interval snapshots)
  • QuoteTick
  • TradeTick
  • Bar

The following PriceType options can be used for bar aggregations;

  • BID
  • ASK
  • MID
  • LAST

The following BarAggregation options are possible;

  • SECOND
  • MINUTE
  • HOUR
  • DAY
  • TICK
  • VOLUME
  • VALUE (a.k.a Dollar bars)
  • TICK_IMBALANCE (TBA)
  • TICK_RUNS (TBA)
  • VOLUME_IMBALANCE (TBA)
  • VOLUME_RUNS (TBA)
  • VALUE_IMBALANCE (TBA)
  • VALUE_RUNS (TBA)

The price types and bar aggregations can be combined with step sizes >= 1 in any way through BarSpecification objects. This enables maximum flexibility and now allows alternative bars to be produced for live trading.

# BarSpecification examples
tick_bars   = BarSpecification(100, BarAggregation.TICK, PriceType.LAST)
time_bars   = BarSpecification(1, BarAggregation.MINUTE, PriceType.BID)
volume_bars = BarSpecification(100, BarAggregation.VOLUME, PriceType.MID)
value_bars  = BarSpecification(1_000_000, BarAggregation.VALUE, PriceType.MID)

Bars can be either internally or externally aggregated (alternative bar types are only available by internal aggregation). External aggregation is normally for standard bar periods as available from the provider through the adapter integration.

Custom data types can also be requested through a users custom handler, and fed back to the strategies on_data method.

Order Types

The following order types are available (when possible on an exchange);

  • Market
  • Limit
  • StopMarket

More will be added in due course including StopLimit, MarketIfTouched, LimitIfTouched and icebergs. Users are invited to open discussion issues to request specific order types or features.

Integrations

An integration adapter for CCXT Pro is currently under active development. The adapter requires the ccxtpro package, which in turn requires a license.

See https://ccxt.pro for more information.

Currently there are beta versions of integrations for Binance and BitMEX available for early testing. These include advanced order options such as post_only, hidden reduce_only, and all the TimeInForce options. These integrations will be incrementally added to.

The other exchanges will be available through CCXTs unified API with a more limited feature set. The intent here is to specify other data clients for arbitrage or market making strategies. Execution clients will be possible if a user only requires simple vanilla MARKET and LIMIT orders for trading on those exchanges.

Development

For development we recommend using the PyCharm Professional edition IDE, as it interprets Cython syntax. Alternatively, you could use Visual Studio Code with a Cython extension.

poetry is the preferred tool for handling all Python package and dev dependencies.

https://python-poetry.org/

pre-commit is used to automatically run various checks, auto-formatters and linting tools at commit.

https://pre-commit.com/

Environment Setup

The following steps are for Unix-like systems, and only need to be completed once.

  1. Install the pre-commit package by running:

     pip install pre-commit
    
  2. Install the Cython package by running:

     pip install -U Cython==3.0a6
    
  3. Install poetry by running:

     curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -
    
  4. Then install all Python package dependencies, and compile the C extensions by running:

     poetry install
    
  5. Setup the pre-commit hook which will then run automatically at commit by running:

     pre-commit run --all-files
    

Builds

Following any changes to .pyx or .pxd files, you can re-compile by running:

python build.py

Refer to the Developer Guide for further information.

Contributing

Involvement from the trading community is a goal for this project. All help is welcome! Developers can open issues on GitHub to discuss proposed enhancements/changes, or to make bug reports.

Please make all pull requests to the develop branch.

Refer to the CONTRIBUTING.md for further information.

License

NautilusTrader is licensed under the LGPL v3.0 as found in the LICENSE file.

Contributors are also required to sign a standard Contributor License Agreement (CLA), which is administered automatically through CLAassistant.


Copyright (C) 2015-2021 Nautech Systems Pty Ltd. All rights reserved.

https://nautechsystems.io

cython