- Published on
Labeling Financial Data
Labeling Financial Data
This document discusses the critical process of labeling financial data for supervised machine learning. While supervised algorithms require labels (outcomes) to learn from features, financial data is notoriously noisy, making proper labeling essential.
Common Approaches: Fixed-Time Horizon
The most common but flawed method for labeling is the fixed-time horizon approach. This method assigns a label based on a return () crossing a static threshold () after a fixed number of bars ().
Labeling Equation:
Where is the return over the horizon .
Key Flaws:
- Ignores Volatility: The static threshold is arbitrary. It's too easy to hit during high-volatility periods and too hard to hit during low-volatility periods.
- Ignores Path: The method is path-independent. It only looks at the price at time . It completely ignores the fact that a real trading strategy would have been stopped out if the price had hit a stop-loss barrier before reaching the profit target.
Dynamic Approaches: The Triple-Barrier Method
A superior, path-dependent method is the Triple-Barrier Approach. This method labels an observation based on which of three barriers is touched first:
- Upper Barrier: A profit-take limit, which is set dynamically based on volatility (e.g.,
target_return * (1 + pt_factor)). - Lower Barrier: A stop-loss limit, also set dynamically based on volatility (e.g.,
target_return * (1 - sl_factor)). - Vertical Barrier: A time limit (expiration) based on the number of bars elapsed.
- Labeling Logic:
- Label 1: The upper barrier (profit-take) is hit first.
- Label -1: The lower barrier (stop-loss) is hit first.
- Label 0 (or sign of return): The vertical barrier (time expiration) is hit first.
This method correctly simulates a real trading strategy by acknowledging that a position can be closed early by a stop-loss, even if it might have become profitable later.
Meta-Labeling
Meta-labeling is an advanced, two-step technique used to improve a model's F1-Score (the harmonic mean of precision and recall) by filtering out false positives.
It is particularly useful for bet sizing and creating "quantamental" models that combine human intuition or simpler models with machine learning.
Process:
- Primary Model (High Recall): A first model (which can be an ML model, an economic theory, or a human portfolio manager's "call") is used to determine the side of the bet (i.e., buy or sell). This model is optimized for high recall, meaning it finds most of the true opportunities but also produces many false positives.
- Secondary Model (Meta-Model): A second ML model is trained only on the positive signals from the primary model. Its goal is to act as a filter.
- Input: Original features + predictions from the primary model.
- Labels: Binary (0 or 1). A "1" means the primary model's signal was correct (it hit the profit target first). A "0" means the signal was a false positive (it hit the stop-loss or timed out).
Final Outcome: The primary model decides the side (buy/sell), and the secondary meta-model decides the size (e.g., the probability of label "1" can be used to size the bet) or whether to take the bet at all (a binary "bet / no-bet" decision).
Advantages:
- Improves the F1-Score by increasing precision.
- Reduces overfitting, as the ML model is not deciding the direction, only the execution.
- Allows for "quantamental" strategies, where an ML model can filter the discretionary calls of a human trader.
- Separates the side prediction from the size prediction, which is critical for risk management.
Other Concepts
- Stacking vs. Meta-Labeling: Stacking uses the predictions of base models as new features for a final model. Meta-labeling uses the secondary model to filter the primary model's predictions, often on a binary (bet/no-bet) basis.
- Dropping Unnecessary Labels: A utility function to improve classifiers that struggle with imbalanced classes. It recursively removes observations belonging to the rarest classes until a minimum percentage threshold (
minPct) is met.
Implementation: CUSUM, Triple-Barrier, and Meta-Labeling
We implement the core labeling techniques from Chapter 3 in the data.labeling.labeling module.
Event Sampling: CUSUM Filters
To sample events based on market volatility rather than fixed time intervals, we provide the symmetric CUSUM filter. We implement both a fixed-threshold version and a dynamic-threshold version where the barrier changes over time (e.g., based on daily volatility).
The Triple-Barrier Method
The core of our labeling methodology is the Triple-Barrier Method. We implement this in a multi-threaded, parallelized fashion.
- Volatility Estimation: First, we need a dynamic threshold for the barriers. We provide a function to calculate the EWMA of daily log returns.
- Vertical Barrier: We set the vertical barrier (maximum holding period).
- Barrier Touching: The
meta_eventsfunction is the main entry point. It takes the event start times (time_events), profit-take/stop-loss multipliers (ptsl), the target volatility (target), and side (if any), and runs the triple-barrier search in parallel to find the first barrier touch time for each event. This function utilizes a helper,triple_barrier, which processes a subset (a "molecule") of the events.
Meta-Labeling
Once we have the first-touch times from meta_events, we use the meta_labeling function to determine the outcome. This function calculates the return and assigns a binary label (1 for profit, 0 for loss/no profit) based on the primary model's side.
This returns a DataFrame containing the return, the final label, and the time the trade ended.
API reference
RiskLabAI implements these in Python and Julia (signatures auto-generated from the package source):
| Python | Julia |
|---|---|
| |
| |
| |
| |
| |
| |
| |
| |