Matrix Flag Labels

Calculate the matrix flag labels: how closely the prices of the last window periods fit a bull (or bear) flag, a sharp move followed by a consolidation that chartists read as the pause before the move continues.

The window’s prices are laid out as a 10 x 10 grid of their price deciles (rows) over ten consecutive parts of the window (columns), and a template of weights rewards the cells the flag passes through and penalises the others. The score is the sum of the weighted grid; with a threshold the label is 1 from that score.

The templates are those of Leigh, Modani, Purvis and Roberts (2002), “leigh_bull”, and of Cervelló-Royo, Guijarro and Michniuk (2015), “cervello_royo_bull”, who proposed a flag whose weights are concentrated in the consolidation. The bear templates are the bull templates upside down, the highest decile becoming the lowest.

The formula is as follows:

\[D\left[d,\; j\right] = \text{the share of the prices of part}\; j \;\text{of the window in price decile}\; d \;\text{of the whole window},\;\; \text{the highest decile in the top row}\] \[\text{Score} = \sum(W \cdot D),\;\; W \text{ the template}\] \[\text{Label} = 1 \;\text{when Score} \geq \text{threshold},\;\; \text{else 0}\]

Unlike the other labels, the score looks back: it uses the prices of the window up to and including the date, so a flag is known at the close of the date.

Also known as: matrix flags, bull flag, bear flag, flag pattern, template matching.

Example · AAPL, MSFT · 2024 to 2025

Install the Finance Toolkit with pip install financetoolkit -U, then call get_matrix_flag_labels:

from financetoolkit import Toolkit

toolkit = Toolkit(
    ["AAPL", "MSFT"],
    api_key="FINANCIAL_MODELING_PREP_KEY",
    start_date="2024-01-01",
    end_date="2025-12-31",
)

flags = toolkit.technicals.get_matrix_flag_labels(threshold=2.5)

flags.xs("MSFT", level=1, axis="columns")

Or, with the Finance Toolkit MCP set up, ask an AI assistant such as Claude or ChatGPT:

Did Microsoft's price form a bull flag at any point in 2025, with a matrix flag score of at least 2.5?

Which returns:

Parameters

get_matrix_flag_labels accepts the following parameters.

ParameterTypeDescriptionExample
periodstrThe time period to consider for historical data. Options are: “intraday” (intraday prices), “daily” (daily prices), “weekly” (weekly prices), “monthly” (monthly prices), “quarterly” (quarterly prices), “yearly” (yearly prices). Defaults to “daily”."weekly"
close_columnstrThe column name for closing prices in the historical data. Defaults to “Adj Close”."Close"
windowintThe number of prices of the window, at least 10. Defaults to 40.20
templatestrThe template of weights. Options are: “leigh_bull” (the bull flag of Leigh, Modani, Purvis and Roberts, 2002), “leigh_bear” (the Leigh bull flag upside down), “cervello_royo_bull” (the bull flag of Cervelló-Royo, Guijarro and Michniuk, 2015), “cervello_royo_bear” (the Cervelló-Royo bull flag upside down). Defaults to “leigh_bull”."leigh_bull"
thresholdfloat | NoneThe score from which the label is 1. Defaults to None, which returns the score only.None, which returns the score only
roundingint | NoneThe number of decimals to round the results to. Defaults to None, which uses the rounding of the Toolkit (4 unless set otherwise).2
show_columnsstr | list[str] | NoneThe columns to keep, a name or a list of names, matched on every column level, or the rows when no column has those names (such as the line items of a financial statement). Names that do not exist are logged, and when none exist the result is returned in full. Defaults to None, which keeps every column.None, which keeps every column

References

  • Paper Leigh, W., Modani, N., Purvis, R. and Roberts, T. (2002). “Stock Market Trading Rule Discovery Using Technical Charting Heuristics.” Expert Systems with Applications, 23(2), 155-159.
  • Paper Cervelló-Royo, R., Guijarro, F. and Michniuk, K. (2015). “Stock Market Trading Rule Based on Pattern Recognition and Technical Analysis: Forecasting the DJIA Index with Intraday Data.” Expert Systems with Applications, 42(14), 5963-5975.
Share