Skip to main content

Back-testing and machine learning pipelines trades labeling utility

Project description

WARNING:

This is a work in progress DO NOT USE IT FOR PRODUCTION PURPOSES

Overview

Triple Barrier is a trade labeler that can be used for algorithmic trading back-testing or machine learning training and validating pipelines. It records for each trade when, at what price level and why a position was closed.

It includes features to plot the triple for a particular trade and the closing event:

Other features:

  • Built upon pandas, numpy, matplotlib mplfinance.
  • It can be used to label single trades or semi vectorized, meaning that can be used with a pandas apply function.

Why?

This project emerges from a repeated trading strategy back-testing process where I was caught again and again copying and pasting from previous pipelines the code to perform a vectorized (semi-vectorized to be more accurate) labeling of trades. To avoid this DRY (Do not Repeat Yourself) routine, that is why I decided to move this code to a library.

The name and core idea were inspired by an algorithm found in the book: Advances in Financial Machine Learning, by Marcos Lopez de Prado.

Before moving further into the library details a little bit of context.

Trading Strategies

A trading strategy describes the logic for opening, managing and closing positions.

Depending on the trading strategy, once a position is opened, four events can determine how the position ends:

  1. Stop loss hit: The price hit the stop loss, which is the maximum tolerable loss (Limit Order)
  2. Take profit hit: The price hit the take profit, which is the estimated maximum profit the position can reach (Limit Order)
  3. Expiration time reached: The position has reached a specific expiration time (Good Til Time or GTT orders)
  4. A custom condition: Any custom condition that can trigger the position closing. These conditions depend on price action while the position is opened

To determine if a strategy is potentially profitable before live trading, it is necessary to collect a large sample of trades to analyze the effectiveness of the strategy in terms of profits, mean profits, profits distributions, drawdowns or any metric you prefer.

Doing this analysis process manually is not recommended at all, considering the amount of data you need to analyze, the volume of trades required to determine if the strategy is significantly profitable and the human error, to name a few reasons.

Algorithmic Trading and Triple Barrier

In simple terms, algorithmic trading is using software to automate all the processes described above: Open, manage and Close trading positions.

But before running live a trading algorithm that implements a trading model you need to perform some research and analyze the algorithm behavior on historic data to understand whether the algorithm is able to generalize well and behave similarly on unseen or future data, this process is called back-testing.

Is in back-testing where you need to identify: when positions were opened, when and why they were closed, so you can calculate all required performance metrics and that is why Triple Barrier was built for.

How?

How to install

pip install triple-barrier==0.4.3rc0

Triple barrier test

Or to this Jupyter Notebook that combines triple barrier calculation with plotting.

Triple Barrier Jupyter Notebook

How to install

As of now, the latest version (Release candidate) is 0.4.1rc and can be installed as follows:

pip install triple-barrier==0.4.2rc0

Examples

This is a work in progress, but you can find some examples here:

You can see use case examples in this folder

TODO

This project is its final stages of testing, documentation and CLEANing.

Besides that, there are some identified tasks that need to be done before the first release.

  • Add string representations for some classes
  • Provide an out-of-the-box function that can be easily used by pandas apply . Currently, you need to build one.
  • Refactor list of barriers hits (OrderBoxHits.barriers) as dictionary, currently is a list which is not much actionable.
  • Plotting: Add possibility to plot oscillators in a panel below
  • Add trailing stops

Other Documentation

Uml models

References

Why numpy is fast

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

triple-barrier-0.4.3rc0.tar.gz (11.7 kB view details)

Uploaded Source

Built Distribution

triple_barrier-0.4.3rc0-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file triple-barrier-0.4.3rc0.tar.gz.

File metadata

  • Download URL: triple-barrier-0.4.3rc0.tar.gz
  • Upload date:
  • Size: 11.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.6

File hashes

Hashes for triple-barrier-0.4.3rc0.tar.gz
Algorithm Hash digest
SHA256 6048a9e22f96df6087f7004c6dc9cf00baf098f4824001d0f1040927452160d8
MD5 03eeb3f1cc393219054fd0af2f3b6dfb
BLAKE2b-256 3919ab779119c51d5a2ac45bec5b27489edb5e6ed1cde6b33c6b0f4c78fc1858

See more details on using hashes here.

File details

Details for the file triple_barrier-0.4.3rc0-py3-none-any.whl.

File metadata

File hashes

Hashes for triple_barrier-0.4.3rc0-py3-none-any.whl
Algorithm Hash digest
SHA256 81d7fa0addf9b9ef3fb637d9a04894639f0f601ad773fcbcbd7e876958868772
MD5 af9e6aa4b52acf63bb4783b020d81a47
BLAKE2b-256 e0a7060dfaf2bc0af33419567f20f1e268a0b587aab178736acd871a2ee91595

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page