Regime Detection

All functions · nseries package

DFAAlpha

func (s Series) DFAAlpha(n int) Series

DFAAlpha computes the DFA (DFA1 / linear detrending) scaling exponent alpha over a rolling window of length n.

By default, this implementation applies DFA to first differences (returns) of the input series. This aligns the typical trading interpretation where alpha ≈ 0.5 corresponds to a random walk in returns, >0.5 trending persistence, and <0.5 mean reversion.

Implementation notes: - Scales considered are integers s in [4, n/4]. If fewer than two scales are available, the alpha for that bar is 0. - For each scale s, the profile is split into floor(n/s) non-overlapping segments from the start and again from the end (2*m segments total). Linear least squares detrending is applied within each segment and the total mean squared residual across all segments is used to compute F(s). - Alpha is estimated as the slope of the linear regression of log(F(s)) versus log(s) across the chosen scales.

Bars 0 to n-2 are exactly 0: the first valid index is n-1 and the minimum length is n. n must be at least 20, which gives the two scales (4 and 5) a fit needs; a smaller n is invalid and gives all zeros, as it did before. alpha = 0 reads as strongly mean-reverting, well below 0.5, so mask (SetN(n-1, math.NaN())) the bars before n-1.

RegimeDFA

func (s Series) RegimeDFA(n int, args ...float64) Series

RegimeDFA maps DFA (Detrended Fluctuation Analysis) alpha into trading regimes using conventional thresholds. Good default thresholds for setting DFA-based regime classification in trading are: * Trending regime: alpha > 0.55 * Mean-reverting regime: alpha < 0.45 * Random walk/neutral: alpha ≈ 0.5

These thresholds create a buffer around the “random walk” mid-value of 0.5, reducing false positives from noise or estimation error. Using 0.55 and 0.45 as cutoffs is a widely-used, empirically validated starting point, but you may fine-tune these based on the specific instrument or the characteristics of your time series and trading strategy.

Returns a series with values: +1 for trending (alpha > trendThresh), 0 for random/neutral, -1 for mean-reverting (alpha < meanRevThresh).

trendThresh and meanRevThresh are values of the DFA scaling exponent alpha (dimensionless, where 0.5 is a random walk), not percentages, and default to 0.55 and 0.45. The comparisons are strict: alpha > trendThresh gives +1, alpha < meanRevThresh gives -1, and anything else gives 0. Only zero or two optional arguments are accepted. n < 20 (fewer than the two DFA scales a fit needs), one optional argument, a NaN threshold or meanRevThresh above trendThresh is invalid and gives all zeros; equal thresholds (no neutral band) and infinite thresholds are valid. The warm-up reads 0, not NaN: bars 0 to n-2 are 0, the neutral reading, and the first valid index is n-1. Releases before this change read -1 (mean-reverting) on those bars, and on every bar for n < 20, because DFAAlpha’s warm-up 0 is below the default 0.45. For n from 20 to 23 alpha is a two-point fit (scales 4 and 5).