Trend BOSS: Classifying Market Regimes with Symbolic Fourier Approximation

Trend BOSS: Classifying Market Regimes with Symbolic Fourier Approximation
Download ALL MT5 indicators (5915)
YouTube Video Thumbnail



Similar MetaTrader Tools

Trend BOSS: Classifying Market Regimes with Symbolic Fourier Approximation

Info

The Trend BOSS: Classifying Market Regimes with Symbolic Fourier Approximation is a Indicator for MetaTrader 5 that overviewthis indicator classifies every bar on your chart as trending, ranging, or volatile using a bag-of-sfa-symbols (boss) classifier built in pure mql5. Any Expert Advisor can use this classification as context to gate its strategy on the current market regime.

Usage

This tool is typically used for trend following strategies across multiple currency pairs.

Platform

This Indicator works exclusively on MetaTrader 5 (both build 600+ and newer versions).

Setup

Place the downloaded file in MQL5/Indicators folder via File ? Open Data Folder in MetaTrader 5.


How to Install and Use Trend BOSS: Classifying Market Regimes with Symbolic Fourier Approximation

1. Installation: Place your file in the MQL/Indicators folder via "Open Data Folder" and restart your terminal.

2. Loading: Find the indicator in the Navigator, drag it onto your chart, and configure the input parameters in the popup window.

3. Customization: Press Ctrl+I to open the indicator list, select your tool, and click "Properties" to change colors, levels, or visual styles.

4. Updating: Replace the old file in the Indicators folder with the new version and restart the platform to apply changes.

Frequently Asked Questions

Q: Why is my indicator not showing? A: Verify the file is in the MQL/Indicators folder, or try right-clicking the "Indicators" tree in the Navigator and clicking "Refresh."

Q: Do custom indicators slow down the platform? A: Too many complex indicators can impact performance; remove unused ones via the "Indicator List" (Ctrl+I).

Q: Can I use MT4 indicators on MT5? A: No, MQL4 and MQL5 are distinct languages; ensure the indicator is compiled specifically for your platform version.

What this tool does

OverviewThis indicator classifies every bar on your chart as trending, ranging, or volatile using a Bag-of-SFA-Symbols (BOSS) classifier built in pure MQL5.

Typical Use Case

This Indicator excels in trend following on MetaTrader 5.

Compatible Platform & Setup

This Indicator works on MetaTrader 5. Place the file in the MQL5/Indicators folder and restart the terminal.

Description & Settings

Related: ADX Auto Range: Automated Trend Strength and Consolidation Detection - another powerful indicator for MetaTrader 5 traders.

Overview

Also recommended: Line Master: Advanced Trend Lines with Change Signals - similar indicator with strong performance on MetaTrader 5.


This indicator classifies every bar on your chart as trending, ranging, or volatile using a Bag-of-SFA-Symbols (BOSS) classifier built in pure MQL5. Any Expert Advisor can use this classification as context to gate its strategy on the current market regime.

The indicator's methodology is unique and is the reason for its existence. BOSS entirely disregards raw prices. Each window of the price series is compressed into a short word composed of a few letters. A stretch of market data is then represented as a bag of these words, and two stretches are compared based on the similarity of their vocabularies. Three differences that make raw price windows difficult to compare are each mitigated by a step in the compression process:

Z-normalization removes the price level, making the same price arc appear identical whether it occurs at 30,000 or 60,000.

A low-pass Fourier filter removes noise by retaining only the first few Fourier coefficients of the window, discarding high-frequency jitter.

Quantization into letters removes fine numerical detail, ensuring that minor differences that do not alter the shape category do not change the word.

Because the output is a bag rather than a sequence, the indicator is insensitive to the exact position of a pattern within the window. Furthermore, because the words are literally readable, the vocabulary of a market regime can be inspected, a capability not offered by traditional elastic-distance methods.

A single price window is z-normalized, low-pass filtered, and quantized into a short word of letters.

What Is Included

The package provides a complete symbolic time-series toolkit, not just the indicator. Each component is a header-only class with no external dependencies beyond ALGLIB's FFT, which is included with MetaTrader 5.

Class

Job

CDFT

The Fourier transformation front end. It returns the first few non-DC coefficients of a window, interleaved as real and imaginary parts. Lower indices correspond to lower frequencies, so retaining the first few coefficients effectively acts as the low-pass filter crucial to the method.

CMCB

Multiple Coefficient Binning, which differentiates SFA from the older SAX method. Instead of assuming a standard normal distribution and using fixed Gaussian quantiles, CMCB learns its own cut points from the data. It learns separate cut points for each coefficient slot, recognizing that low-frequency and high-frequency coefficients have different spreads.

CSFA

Symbolic Fourier Approximation. This class takes a price window as input and outputs a word.

CBossTransform

This class slides a window along a price series and places each generated word into a histogram. It applies numerosity reduction, so a run of identical consecutive words is counted only once. The resulting bag measures the number of distinct pattern occurrences within a series, rather than how long each pattern persists.

CBossClassifier

This component implements a 1-nearest-neighbor classifier using the asymmetric BOSS distance metric on word bags. It uses leave-one-out training accuracy for scoring.

CBossEnsemble

The competitive version of BOSS. It trains one member for each candidate window size, scores each member, retains the well-performing ones, and uses them for a majority vote.

CRegimeLabeler

This class provides rule-based ground truth for training. It derives trend, range, and volatile labels from the lag-1 autocorrelation and volatility of a window's log returns.

Why the Ensemble Matters

A single BOSS classifier commits to a specific sliding-window length. The optimal length is data-dependent and not known in advance. A short window captures fast, local structures but misses slow trends, while a long window does the opposite. Selecting the wrong length can render the classifier blind to the very structures that differentiate market classes. This is a known weakness of the method, and initial testing showed a single BOSS classifier achieving only 25.3% macro accuracy on real market data, which is barely better than random chance.

The ensemble approach rectifies this limitation. It trains one classifier member for each of six candidate window sizes (12, 18, 24, 32, 48, and 64 bars). Each member is scored using leave-one-out accuracy over the training bags. All members that achieve at least 92% of the best score are retained, and the final classification is determined by a majority vote among these survivors. This single enhancement improved macro accuracy from 25.3% to 62.5%. The ensemble is not a mere optional addition; for a dictionary classifier like BOSS, it is essential for the method to function effectively.

Multiple BOSS members across different window sizes classify the query, and the majority vote determines the outcome.

Indicator Visualization

The indicator trains its ensemble once upon loading and then assigns a regime label to each bar based on the trailing segment of closing prices ending at that bar. The output is displayed as a color-coded lane in a separate indicator window, with three distinct heights to prevent overlap. An optional text label can also be displayed directly on the chart to indicate the current regime.

Regime

Lane

Meaning

Range

Blue, bottom row (0)

Characterized by mean-reverting oscillations. This is typically the dominant regime for most instruments.

Trend

Green, middle row (1)

Indicates persistent directional movement, where price changes tend to continue rather than reverse.

Volatile

Red, top row (2)

Represents large, erratic price movements without a clear directional bias.

Reading from an Expert Advisor

To access the indicator's output from an Expert Advisor, first obtain a handle to the indicator using iCustom on "BOSS\BOSSRegime". Read buffer 0, which directly contains the regime code: 0 for range, 1 for trend, and 2 for volatile. EMPTY_VALUE is returned for bars lacking sufficient history. Buffer 1 provides only the color index. This simple interface requires only a single CopyBuffer call.

The color-coded regime lane displayed below a BTCUSD H1 chart: blue for range, green for trend, and red for volatile.

Important Considerations Before Attaching

1. Training Initialization: The training process occurs during OnInit and may take a moment to complete. The indicator will not load if it cannot generate a justifiable lane; it requires at least four historical segments with sufficient data for labeling.

2. Forming Bar Instability: The regime of the currently forming bar can change as the bar progresses because its trailing price segment is not yet finalized. Closed bars provide stable regime data; the rightmost bar's regime is provisional. It is recommended to base trading decisions on closed bars.

Recommended Setup

Setting

Recommended value

Notes

Symbol

Any

The indicator internally normalizes windows using Z-normalization, allowing the classifier to focus on price shape rather than absolute price levels. No per-symbol scaling is required. Development and screenshots primarily used BTCUSD.

Timeframe

H1

The indicator functions on any timeframe. The segment length is measured in bars, so a 120-bar segment represents a different duration of market activity on M5 compared to D1.

History required

At least 480 bars, ideally 20000

A minimum of four segments is strictly required for the indicator to load. The default setting requests 20000 bars to ensure the ensemble has a broad sample of all three regimes for learning.

Word length

2 coefficients (four letters)

This setting should generally not be altered unless specific measurements indicate otherwise. It represents the empirically determined robust operating point.

Why WordLen stays at 2

Retaining more Fourier coefficients (longer words) captures finer shape details, which might seem beneficial but often leads to overfitting noise. An analysis of coefficient sweeps on a controlled two-class task demonstrated that two coefficients maintain near 100% accuracy until heavy noise is introduced, and they remain well above chance even at the extremes. Three or four coefficients, however, quickly degrade towards a 50% coin flip probability as soon as any noise appears. Fewer coefficients result in a stronger low-pass filter, which is more resilient to noise.

Installation and File Structure

The header files are included using the syntax <BOSS\...>, and the indicator itself is located in an Indicators\BOSS subfolder. Maintaining this file structure is essential.

File

Description

Indicators\BOSS\BOSSRegime.mq5

The main indicator. It trains a BOSS ensemble on recent historical data upon loading and visually represents the market regime lane.

Include\BOSS\FourierTransform.mqh

CDFT: Implements the real-input Fourier front end using ALGLIB's FFT, providing low-frequency coefficients for SFA.

Include\BOSS\SFA.mqh

CMCB and CSFA: Handle learned equi-depth binning per coefficient slot and the window-to-word conversion.

Include\BOSS\BOSS.mqh

CBossHistogram, CBossTransform, CBossClassifier, and CBossEnsemble: These classes manage the bag representation, the BOSS distance calculation, and the voting ensemble.

Include\BOSS\RegimeLabeler.mqh

CRegimeLabeler: Provides rule-based classification of trend, range, and volatile regimes using autocorrelation and volatility metrics.

Input Parameters

Parameter

Default

Description

Segment

120

Defines the number of bars per classified segment. BOSS represents market data by the distribution of words generated by its sliding window; thus, the segment must be sufficiently long to produce numerous words.

WordLen

2

Specifies the number of Fourier coefficients retained, with each coefficient contributing two letters to the word. This is the empirically determined robust operating point; increasing it may lead to overfitting.

Alphabet

4

Determines the number of distinct letters available per coefficient slot. The allowed range is from 2 to 10.

TrainBars

2000

Sets the number of historical bars used to train the ensemble, considering only closed bars. Using fewer bars results in faster loading but a less comprehensive sample of market regimes.

TrendACF

0.05

The lag-1 autocorrelation threshold above which a training segment is classified as trending. This value has been lowered from the default of 0.15 to better balance the training classes, as a stricter threshold yields too few trend examples for effective learning.

VolHighMult

1.3

A multiplier for the mean segment volatility. If the segment volatility exceeds this multiple of the mean, it is classified as volatile. This is a relative measure, allowing it to adapt to different instruments.

ShowLabel

true

Enables or disables the display of a text label on the chart indicating the current market regime.

Note:
The TrendACF and VolHighMult parameters influence the generation of the training data, not the live classification. They define what the ensemble is trained to recognize as a trend or a volatile period, so altering them changes the meaning of the displayed lane. Review the training summary in the Experts log after loading to ascertain the number of segments classified for each regime; a regime with very few examples may lead to poorer classification performance regardless of ensemble quality.

Research Basis

This implementation is based on the foundational papers that introduced the BOSS methodology:

Patrick Schäfer, The BOSS is concerned with time series classification in the presence of noise, Data Mining and Knowledge Discovery, 2015. This paper details the bag-of-words representation, the BOSS distance, and the ensemble method with its 92% retention rule.

Patrick Schäfer and Mikael Högqvist, SFA: A Symbolic Fourier Approximation and Index for Similarity Search in High Dimensional Datasets, EDBT 2012. This paper covers Symbolic Fourier Approximation and Multiple Coefficient Binning.

It is important to note that the labels used by the ensemble for training are generated by a straightforward statistical rule, not by manual annotation. Consequently, the indicator learns to replicate this rule based on window shape alone. This provides a transparent, mechanical definition of market regimes, rather than relying on subjective interpretation. A more sophisticated or forward-looking labeling system could provide BOSS with a clearer target. The engine is designed to allow for straightforward replacement of the labeler.

A companion article provides a detailed, layer-by-layer explanation of the implementation, delves into the underlying mathematics, and presents a comprehensive benchmark comparison against Dynamic Time Warping: Bag-of-SFA-Symbols (BOSS) in MQL5: Symbolic Time-Series Classification for Market Regimes.

You may also like: Trend Buy/Sell Zones with Automated Lot Sizing and Risk Panel for MT5 - excellent alternative for indicator users on MetaTrader 5.

Source Code

#property copyright "RobotFX"
#property link "https://robotfx.org"
#property description "This code is educational. Feel free to use, modify and upgrade as you wish. Visit ROBOTFX.ORG for professional trading tools"
#property version     "1.00"
#property strict
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_plots   1
#property indicator_minimum -0.5
#property indicator_maximum  2.5

.......

Leave your opinion, ask a question, share some knowledge

⚠ Limitations & Risk Warning

  • This tool is provided for educational and testing purposes only.
  • Past performance does not guarantee future results.
  • Trading involves substantial risk of loss. Use on a demo account first.
  • Results may vary depending on market conditions, broker, and settings.
  • We recommend thorough backtesting and forward testing before using with real funds.
© ROBOTFX Free educational tools by RobotFX. Use entirely at your own risk; we are not liable for any financial losses incurred.