Algorithmic TradingTrading

Trade Labeler Latest Release: triple-barrier 0.5.6rc0

To reduce efforts in my trading models research I have built a python library to label trades, that can be used from backtesting or machine learning pipelines. This library allows to calculate trades attributes: open time, close time open price, close price, profit. The close price and time can be calculated as a hit of a stop-loss, take-profit, time limit or other custom condition.

Since I trade only FOREX instruments, the library is prepared for FOREX instruments, you will find concept like pips to measure profits.

The library is heavily based on pandas and in this candidate release I have added a very powerful feature which is a wrapper for the pandas apply function reducing drastically the code required to do the labeling.

The following is an example extracted from a Jupyter Notebook available in the repository

Apply Scenarios.ipynb

You can find the repository here

https://github.com/mchiuminatto/triple_barrier

https://github.com/mchiuminatto/triple_barrier/blob/main/README.md

And the pypi package:

https://pypi.org/project/triple-barrier/#history

Example

Trading Strategy

In this section is described how to implement a simple trading strategy that generates the inputs for triple_barrier.

– The calculations are performed on a OHLC time series.
– The price used is FOREX and for now the triple barrier is tested only on forex price.


The strategy is as follows:

1. Two moving averages are calculated: 10 periods ( called FAST ), the fast one, and 20 periods one (called SLOW) the slow one
2. A long signal is recorded for a bar after the closing if the following condition is met: FAST crosses above SLOW. More precisely:

3. A short signal is recorded for a bar after the closing if the following condition is met FAST crosses below SLOW. More precisely:

Notes

The entry period, when the position is opened, is the period right after the signal, the position open price is the open price for this bar.

For closing there will be a few cases that will be setup using the triple barrier and will be described in the cases next. One of the closing cases is closing when the trading condition is not valid anymore and is described next.

4. Long closing: FAST < SLOW
5. Short closing: FAST > SLOW

For the sake of simplicity, strategy calculations will be omitted so you just need to know that a pandas dataframe contains the columns with the entry signal in the column: long-entry

The image above shows that the entry period (blue) is plotted with its corresponding entry signal (green), but only the entry signal is relevant for the labeler and that is the column long-entry

The parameters for orders are the following:

Stop-loss width: 10 Take-profit width: 20 Trade periods: 2

STOP_LOSS_WIDTH = 10
TAKE_PROFIT_WIDTH = 20
TRADE_PERIODS = 2

With all that data in place, to label the trades is as simple as the following instruction:

from triple_barrier.trading import DataSetLabeler
from triple_barrier.trade_labeling import TradeSide
from triple_barrier.trading import TradingParameters

trade_params = TradingParameters(
    open_price=price.open,
    high_price=price.high,
    low_price=price.low,
    close_price=price.close,
    entry_mark=price["long-entry"],
    stop_loss_width=STOP_LOSS_WIDTH,
    take_profit_width=TAKE_PROFIT_WIDTH,
    trade_side=TradeSide.BUY,
    pip_decimal_position=PIP_DECIMAL_POSITION,
    time_barrier_periods=TRADE_PERIODS,
    dynamic_exit=None
)

dataset_labeler = DataSetLabeler(trade_params)
trades: pd.DataFrame = dataset_labeler.compute()

trades.head()
trades_examples.png

Profit in pips (FOREX)

trades_examples_profit_plot.png

Eventually, you may want to check one single trade to verify results. To do so you can plot it as follows:

from triple_barrier.plots import PlotTripleBarrier
from triple_barrier.trade_labeling import Labeler
from triple_barrier.orders import Orders

box_setup = Orders()

box_setup.open_time = "2023-01-02 14:40:00"  # first trade of the list
box_setup.open_price = price.loc[box_setup.open_time]["open"]
box_setup.take_profit_width = TAKE_PROFIT_WIDTH
box_setup.stop_loss_width = STOP_LOSS_WIDTH
box_setup.time_limit = price[box_setup.open_time:].index[TRADE_PERIODS]
box_setup.trade_side = TradeSide.BUY
box_setup.pip_decimal_position = PIP_DECIMAL_POSITION

print(box_setup)

trade_labeler = Labeler(open_price=price.open,
                               high_price=price.high,
                               low_price=price.low,
                               close_price=price.close,
                               box_setup=box_setup)
orders_hit = trade_labeler.compute()
print(orders_hit)

plot_tb = PlotTripleBarrier(price.open,
                           price.high,
                           price.low,
                           price.close,
                           4,
                           periods_to_plot=30,
                           overlay_features=[ price["mva-10"], price["mva-20"] ]
                           )

plot_tb.plot_multi_barrier(trade_labeler)
plot_one_trade.png

Collaboration Required

If you want to collaborate in this project, please don’t hesitate to contact me. I mostly require test cases coding.

Hi, I’m Marcello