Directional Change
The Directional Change module contains functions and classes related to the detection of general price movement and feature generation based on direction split. It is planned to later build a Regime Change module on top of Directional Change tools.
- These include:
-
signal
total_price_movement
time_for_completion
time_adjusted_return
Note
Underlying Literature
-
Detecting Regime Change in Computational Finance by Jun Chen & Edward P K Tsang
-
Directional Changes: A New Way to Look at Price Dynamics by Edward P K Tsang
Is Directional Change
Directional Change (DC) is a metric which samples data points at their peaks and troughs in their movement. Price movements are defined by two types of events: Directional Change (DC) Event and Overshoot (OS) Event. With a pre-defined Threshold, every price curve can be dissected by these two components.
The value of the threshold needs to be pre-defined by the observer. It represents how big of a price change the observer considers as significant.
A DC Event will be confirmed when the price change reaches the threshold. A DC Event is not usually immediately followed by an opposite DC Event, but by an OS Event. An OS Event records the price movement from one DC Event to the next.
Price movements are partitioned into uptrend and downtrend. When the price changes from Point A to Point B, the price change reaches a threshold \(\theta\), then a DC Event is confirmed. Point A is then considered as an Extreme point (EXT). And Point B is considered as a Directional Change Confirmation (DCC) point. Similarly, the next DC Event is confirmed at Point D. The price movement between two DC Events is considered an OS Event (from Point B to C).
Implementation
This is an implementation of the is_directional_change function over a time series.
- class DirectionalChange(time_series: Series, threshold: float = 0.05)
-
The Directional Change module contains functions and classes related to the detection of general price movement and feature generation based on direction split.
The implementation is based on the book Detecting Regime Change in Computational Finance by Jun Chen & Edward P K Tsang
- __init__(time_series: Series, threshold: float = 0.05) None
-
Init of Directional Change.
- Parameters:
-
-
ts – (pd.Series) Financial time series of prices.
-
threshold – (float) Threshold. (0.05 by default)
-
- __weakref__
-
list of weak references to the object (if defined)
- signal()
-
Function to implement directional change test over a financial time series of prices.
- Returns:
-
(pd.Series[int]) Series of directional change signals.
- time_adjusted_return() Series
-
Detecting Regime Change in Computational Finance, Snippet 2.4, page 13
Time adjusted return for directional change indicator measures the absolute return (R) in a trend. It is calculated by dividing the absolute TMV by the time interval T. It measures the percentage of price change per time unit.
- Returns:
-
(pd.Series[float]) Time adjusted return.
- time_for_completion() Series
-
Detecting Regime Change in Computational Finance, Snippet 2.3, page 13
Time for completion of a trend indicator measures the amount of physical time (T) that it takes to complete a TMV trend.
- Returns:
-
(pd.Series[timedelta]) Time completion for a trend.
- total_price_movement() Series
-
Detecting Regime Change in Computational Finance, Snippet 2.2, page 12
Total price movement (TMV) is used to measure the percentage change from point A to point C, normalized by the threshold. It usually measures the total price change of a DC event and an OS event.
- Returns:
-
(pd.Series[float]) Total price movement.
Example
# Importing tools
from mlfinlab.features import DirectionalChange
# Getting directional change of time series
dc = DirectionalChange(time_series_prices, threshold)
dc_signal = dc.signal()
Total Price Movement
This indicator measures the absolute percentage of the price change in a trend. As shown in Figure 1, Total Price Movement (TMV) is used to measure the percentage change from Point A to Point C, normalized by the threshold. It usually measures the total price change of a DC event and an OS event. It is defined as:
Implementation
- DirectionalChange.total_price_movement() Series
-
Detecting Regime Change in Computational Finance, Snippet 2.2, page 12
Total price movement (TMV) is used to measure the percentage change from point A to point C, normalized by the threshold. It usually measures the total price change of a DC event and an OS event.
- Returns:
-
(pd.Series[float]) Total price movement.
Example
# Importing tools
from mlfinlab.features import DirectionalChange
# Getting the total price movement of time series
dc = DirectionalChange(time_series_prices, threshold)
dc_price_movement = dc.total_price_movement()
Time for Completion
This indicator measures the amount of the physical time (T) that it takes to complete a TMV trend. As shown in Figure 1, it measures the time from Time 3 to Time 9. It is defined as:
where \(t_{EXT}(n)\) represents the time at \(n-th\) extreme point.
Implementation
- DirectionalChange.time_for_completion() Series
-
Detecting Regime Change in Computational Finance, Snippet 2.3, page 13
Time for completion of a trend indicator measures the amount of physical time (T) that it takes to complete a TMV trend.
- Returns:
-
(pd.Series[timedelta]) Time completion for a trend.
Example
# Importing tools
from mlfinlab.features import DirectionalChange
# Getting time for completion of time series
dc = DirectionalChange(time_series_prices, threshold)
dc_completion_time = dc.time_for_completion()
Time Adjusted Return
This indicator measures the absolute return (R) in a trend. It is calculated by dividing the absolute TMV by the time interval T. It measures the percentage of price change per time unit:
where \(R(n)\) represents the value of the time-adjusted return of DC at the \(n-th\) extreme point.
Implementation
- DirectionalChange.time_adjusted_return() Series
-
Detecting Regime Change in Computational Finance, Snippet 2.4, page 13
Time adjusted return for directional change indicator measures the absolute return (R) in a trend. It is calculated by dividing the absolute TMV by the time interval T. It measures the percentage of price change per time unit.
- Returns:
-
(pd.Series[float]) Time adjusted return.
Example
# Importing tools
from mlfinlab.features import DirectionalChange
# Getting time adjusted return of time series
dc = DirectionalChange(time_series_prices, threshold)
dc_completion_time = dc.time_adjusted_return()