Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

Tradingene: A Package For Backtesting Trading Algorithms

image

See full documentation here

The Tradingene package turns your computer into a tool for developing and backtesting trading strategies you write in the Python programming language. Having been developed and backtested, these ones can be then easily adapted for live trading at the Tradingene Platform.

Installation

Tradingne can be installed via pip for python3:

pip3 install tradingene

Getting Started

An algorithm performs a trading logic that is implemented in a user defined function. If we want to test profitability of this logic we have to perform a backtest which consists of a series of consecutive calls of this (user defined) function.


Suppose we came up with the following trading logic:

  • open a long position if the closing price of a bar is greater than the open price;
  • open a short position if the closing price of a bar is less than the open price;
  • do not make any changes to the position otherwise.

To start coding we need to define the name and the regime of the algorithm as well as the start_date and the end_date of the backtesting period:

Setting parameters

from datetime import datetime
from tradingene.algorithm_backtest.tng import TNG
from tradingene.backtest_statistics import backtest_statistics as bs
name = "Cornucopia"
regime = "SP"
start_date = datetime(2018, 9, 1)
end_date = datetime(2018, 10, 1)

After that we are ready to create an instance of the TNG class. The instance named alg will contain all the methods required for backtesting.

alg = TNG(name, regime, start_date, end_date)

See more on initialization.

Next we are able to specify an instrument and timeframe (measured in minutes) that we will use in our backtest:

alg.addInstrument("btcusd")
alg.addTimeframe("btcusd", 1440)

See more on adding instruments and timeframes.

Implementing trading logic

In the next step we will code the onBar() function that will implement our trading logic:

def onBar(instrument):
  if instrument.open[1] > instrument.close[1]:
    # If the price moved down we take a short position
    alg.sell()
  elif instrument.open[1] < instrument.close[1]:
    # If the price moved up we take a long position
    alg.buy()
  else:
    # If the price did not change then do nothing
    pass

The instrument variable contains price values as well as the values of specified technical indicators.

See more on onBar function.

Now we are ready to run a backtest:

alg.run_backtest(onBar)

Results of backtest

After the backtest is complete we may retrieve the statistics to estimate the performance of our algorithm:

stats = bs.BacktestStatistics(alg)
stats.backtest_results(plot=True, filename="backtest_stats")

With these lines of code we make the backtest statistics formatted into an html page named backtest_stats.html. This page also shows us a cumulative profit plot, just like the one presented below:

image

See more on backtest statistics.

Machine learning and loading data

A powerful feature of the Tradingene package is the ability to load, recalculate and convert data into a form suitable to train machine learning models with. That's why an algotrader can easily create and backtest "learning" trading robots.

With a series of our mini-lessons you'll learn how to use neural networks for solving classification and regression problems (with respect to the challenges of trading) as well as how to engage an SVM etc.

See more on loading data.

Metadata

Release files for tradingene 0.0.dev35

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tradingene 0.0.dev35
File Size Uploaded
tradingene-0.0.dev35.tar.gz 52.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for tradingene 0.0.dev35
File Interpreter ABI Platform
tradingene-0.0.dev35-py3-none-any.whl Python 3 none any Details

Total release size: 122.3 kB

Release files / tradingene-0.0.dev35.tar.gz

Download URL tradingene-0.0.dev35.tar.gz
Size 52.6 kB
Tags Source
SHA-256 checksum
How to use checksums
2251472a59581da5650d0a240db61ba1b6d9bc11b6233e9be4a68100ca31dc2a
BLAKE2b-256 checksum
How to use checksums
7c4b7c96088c31e4c4e8ebeba13b8c2df1740c78cb67173997325974e45efc51
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.9.1 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.5.2

Release files / tradingene-0.0.dev35-py3-none-any.whl

Download URL tradingene-0.0.dev35-py3-none-any.whl
Size 69.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
35d54826f0effde638749a91389bffb32aac48346d6ab68f2fc14fb15eda771c
BLAKE2b-256 checksum
How to use checksums
63c51026a06719a979f010d3dff4645ebbdbb8a71054796eb244db38918add38
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.9.1 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.5.2

Release history Release notifications | RSS feed

This release

0.0.dev35 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page