Event Filter

Calculate the events of an event filter: the dates something happens that is worth labeling, such as a run of returns in one direction.

Sampling on events rather than on every date keeps the labels of a quiet market from outnumbering those of the moves that matter, and the events are the dates get_triple_barrier_labels(events=...) labels.

  • CUSUM filter: the symmetric CUSUM filter of López de Prado, after the quality control charts of Page (1954) and the trading rule of Lam and Yam (1997). It sums the log returns since the last event of each direction and reports an event when a sum moves beyond threshold daily volatilities, after which that sum starts again from zero. A price hovering around a level triggers no events, unlike a band crossing, as it takes a full run of the threshold to set one off.
  • Z-score filter: the z-score filter: an event when the price lies at least threshold standard deviations from its average over window periods, a filter after the z-score peak detection of van Brakel (2014).

The formula is as follows, per method:

\[\text{CUSUM filter:}\;\; y_{t} = \ln\left(\frac{p_{t}}{p_{t-1}}\right),\;\; S + \text{}_{t} = \max(0,\; S + \text{}_{t-1} + y_{t}),\;\; S - \text{}_{t} = \min(0,\; S - \text{}_{t-1} + y_{t}),\;\; h_{t} = \text{threshold}\; x \sigma_{t} \;(\text{the AFML daily volatility},\; \text{an EWM standard deviation of the returns with} \;\text{span});\;\; \text{an event of} - 1 \;\text{when}\; S - \text{}_{t} < - h_{t} (S - \text{}_{t} \;\text{resets}) \;\text{or else of 1 when}\; S + \text{}_{t} > h_{t} (S + \text{}_{t} \;\text{resets})\] \[\text{Z-score filter:}\;\; z_{t} = \frac{p_{t} - \operatorname{mean}(p,\; \text{window})}{\operatorname{std}(p,\; \text{window})};\;\; \text{an event of 1 when}\; z_{t} \geq \text{threshold and of} - 1 \;\text{when}\; z_{t} \leq - \text{threshold}\]

Each value uses the prices up to its date only.

Also known as: CUSUM filter, symmetric CUSUM filter, event-based sampling, z-score filter, event filter.

Example · AAPL, MSFT · 2024 to 2025

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

from financetoolkit import Toolkit

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

events = toolkit.technicals.get_event_filter(threshold=2)

events[events.ne(0).any(axis=1)]

Which returns:

Parameters

get_event_filter accepts the following parameters.

ParameterTypeDescriptionExample
methodstrThe filter. Options are: “cusum” (the symmetric CUSUM filter), “z_score” (the z-score filter). Defaults to “cusum”."cusum"
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"
thresholdfloat | NoneThe threshold in volatilities: the multiple of the daily volatility the CUSUM sums need to pass, or the number of standard deviations of the z-score. Defaults to None, which uses 1 for “cusum” (the daily volatility itself) and 3 for “z_score”.None, which uses 1 for "cusum" (the daily volatility itself) and 3 for "z_score"
spanintThe span of the daily volatility of the CUSUM filter. Defaults to 100.100
windowintThe window of the average and standard deviation of the z-score filter. Defaults to 20.20
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 López de Prado, M. (2018). “Advances in Financial Machine Learning.” Wiley, section 2.5.2.1 (Snippet 2.4).
  • Paper Lam, K. and Yam, H.C. (1997). “CUSUM Techniques for Technical Trading in Financial Markets.” Financial Engineering and the Japanese Markets, 4(3), 257-274.
  • Paper Page, E.S. (1954). “Continuous Inspection Schemes.” Biometrika, 41(1/2), 100-115.
  • Paper van Brakel, J.P.G. (2014). “Robust peak detection algorithm using z-scores.” Stack Overflow (https://stackoverflow.com/questions/22583391).
  • Library Referenced against RiskLabAI’s symmetric_cusum_filter
Share