Managing Positions with Triple Barriers and Trailing Stops
Summary
This document explains how a position executor can manage entries and exits in spot and perpetual markets using predefined conditions. The triple barrier method sets a take-profit level, a stop-loss level, and a time limit. Whichever condition is reached first closes the trade, so a position that remains between its price thresholds still exits after a specified duration. A trailing stop adds a dynamic control that follows favorable price movement and can help protect accrued gains.
The executor monitors market prices and acts when a configured barrier is reached. The document says spot use requires holding the asset beforehand, while perpetual markets can manage long or short positions. It describes configuration parameters and a simplified execution flow, but gives no parameter values, empirical results, or guidance for choosing thresholds. Actual outcomes will depend on market behavior and execution; the framework defines exit rules rather than demonstrating that a strategy is profitable. It also does not explain fees, slippage, or how gaps through a barrier are handled.
Key ideas
- The triple barrier method defines take-profit, stop-loss, and time-based exit conditions for each position.
- A time limit closes a position that has reached neither price threshold within the configured duration.
- A trailing stop follows favorable price movement after its activation condition is met.
- The executor supports spot and perpetual markets, with spot use requiring prior ownership of the asset.
- The document describes an execution framework but does not provide evidence that particular settings are profitable.
Tags
Full text
# Configure the parameters for the position
**PositionExecutor:** Manages opening and closing positions of equal amounts, ensuring the portfolio remains balanced ± the position's profit or loss. It's applicable in both perpetual and spot markets, requiring pre-ownership of the asset for spot markets.
The **PositionExecutor** uses a configuration object, **PositionExecutorConfig**, to manage an order after it is placed, following the [Triple Barrier Method](#the-triple-barrier-method). This configuration sets pre-defined stop loss, take profit, time limit, and trailing stop parameters.
```python
class TripleBarrierConf(BaseModel):
# Configure the parameters for the position
stop_loss: Optional[Decimal]
take_profit: Optional[Decimal]
time_limit: Optional[int]
trailing_stop_activation_price_delta: Optional[Decimal]
trailing_stop_trailing_delta: Optional[Decimal]
# Configure the parameters for the order
open_order_type: OrderType = OrderType.LIMIT
take_profit_order_type: OrderType = OrderType.MARKET
stop_loss_order_type: OrderType = OrderType.MARKET
time_limit_order_type: OrderType = OrderType.MARKET
```
Key Configs:
- `stop_loss`: Determines the stop-loss percentage
- `take_profit`: Sets the take-profit percentage.
- `time_limit`: Establishes a time limit for the trade.
- `trailing_stop_activation_price_delta`: Specifies the delta for activating a trailing stop.
- `trailing_stop_trailing_delta`: Sets the trailing delta for the trailing stop.
Example:

### The Triple Barrier Method
The triple barrier method is a structured approach to position management. Every
position is opened with its exit conditions already decided, expressed as three
"barriers". Whichever one the price reaches first ends the trade:
* **Take profit** — an upper barrier at a target price. Reaching it first closes the position at a profit.
* **Stop loss** — a lower barrier at a maximum acceptable loss. Reaching it first closes the position at a loss.
* **Time limit** — a vertical barrier. If neither price barrier is touched within the configured duration, the position is closed anyway.
The two price barriers are horizontal lines on a price chart and the time limit
is a vertical one, which is where the name comes from. The point of the third
barrier is that a position which is neither winning nor losing still has capital
and risk committed to it; the time limit forces a decision instead of letting it
sit open indefinitely.
The method was popularized in *Advances in Financial Machine Learning* by Marcos
López de Prado (Wiley, 2018, ISBN 978-1-119-48208-6), where it is used to label
training data for machine-learning models. Hummingbot uses the same structure
for live execution: the barriers are exit conditions rather than labels.
Hummingbot adds a fourth control on top of the three barriers — a **trailing
stop**, which moves the stop-loss level up behind a favorable price rather than
leaving it fixed. It is described [below](#trailing-stop).
The [PositionExecutor](https://github.com/hummingbot/hummingbot/blob/master/hummingbot/strategy_v2/executors/position_executor/position_executor.py) class is the implementation.
### Spot vs Perpetual Behavior
The `PositionExecutor` class is designed to work on both spot and perpetual exchanges, allowing you to write strategies that be used on either type:
* On perpetual exchanges, they apply the take-profit and stop-loss levels described below to manage a long or short position after it has been created.
* On spot exchanges, they place take-profit and stop-loss orders to manage an order after it has been filled. This is similar to [Hanging Orders](../../v1-strategies/strategy-configs/hanging-orders.md) but on an individual order level.
### Configuration
The `PositionExecutor` engages with the market by executing orders based on the `PositionConfig`. It applies the triple barrier method as follows:
```python
triple_barrier_confs = TripleBarrierConf(
stop_loss=stop_loss,
take_profit=take_profit,
time_limit=time_limit,
trailing_stop_activation_price_delta=trailing_stop_activation_price_delta,
trailing_stop_trailing_delta=trailing_stop_trailing_delta,
)
```
#### Stop Loss
Activated when the price moves against the position beyond a specified threshold.

#### Take Profit
Triggered when the price reaches a pre-set level that represents a desired profit.

#### Time Limit
When the time limit is reached, the position will be closed or an opposing trade will be executed.

#### Trailing Stop
The trailing stop evaluates the position after a certain time has passed and may close it to avoid market shifts or decay.
- `trailing_stop_activation_price_delta`: The price movement required to activate a trailing stop.
- `trailing_stop_trailing_delta`: The distance maintained behind the price as a trailing stop, which adjusts as the price moves favorably.

### Execution Flow
Here's a simplified flow of how the `PositionExecutor` operates in conjunction with the triple barrier method:
1. The `PositionExecutor` initiates a position based on signals from the strategy, which interprets market data
2. It continuously monitors market prices and compares them against the defined barriers.
3. If the price hits the take profit or stop loss levels, the `PositionExecutor` executes a trade to close the position accordingly.
4. The trailing stop is adjusted as the price moves favorably, providing a dynamic risk management tool.
5. The time limit barrier ensures that positions do not remain open indefinitely, addressing the risk of market conditions changing over time.
### Conclusion
The `PositionExecutor` is a powerful tool within Hummingbot for implementing strategies that require precise entry and exit conditions. By leveraging the triple barrier method, it provides a structured and disciplined approach to trade management, vital for both market making and directional trading strategies.
---Shown in full with attribution under the source's licence. Licence: Apache-2.0
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.