SpreadRegimeDetector

Incremental, causal technical analysis documentation

Summary

SpreadRegimeDetector is RTTA's streaming implementation of: Stateful quoted-spread regime detector using relative bid/ask spread.

Update API

result = rtta.SpreadRegimeDetector().update(bid_price, ask_price)

The update(...) call consumes one observation using bid_price, ask_price. advance(...) uses the same inputs when the caller wants to update state without materializing a Python return value.

Theory Of Operation

SpreadRegimeDetector first constructs a scalar market-state metric from the current observation and compact streaming state, then passes that metric through explicit entry/exit hysteresis. The metric is named in the recurrence below; the hysteresis keeps the output stable until the metric crosses the opposite exit band.

Recurrence

Let \(z_t = (bid_price_t, ask_price_t)\) denote the observation consumed by one update(...) call and let \(\theta\) denote constructor parameters such as window lengths, thresholds, and smoothing constants.

\[mid_t=\frac{bid_t+ask_t}{2}, \qquad q_t=\frac{\max(ask_t-bid_t,0)}{\max(|mid_t|,\epsilon)}\]
\[r_t = \begin{cases} 1, & r_{t-1} \le 0 \text{ and } q_t \ge u_e \\ 0, & r_{t-1} = 1 \text{ and } q_t \le u_x \\ -1, & r_{t-1} \ge 0 \text{ and } q_t \le \ell_e \\ 0, & r_{t-1} = -1 \text{ and } q_t \ge \ell_x \\ r_{t-1}, & \text{otherwise} \end{cases}\]

The entry/exit constants satisfy \(\ell_e < \ell_x \le u_x < u_e\).

The return value is the current scalar indicator value.

Implementation Notes

The recurrence is implemented in src/rtta/indicator.cpp in class SpreadRegimeDetector.

Reference