RunBarGenerator

Incremental, causal technical analysis documentation

Summary

RunBarGenerator implements a constant-threshold form of López de Prado tick run bars. Buy and sell ticks accumulate in separate totals throughout the bar; the bar closes when the dominant side reaches threshold. An intervening tick of the opposite sign does not erase the earlier side's evidence.

Update API

ind = rtta.RunBarGenerator(threshold=10)
result = ind.update(close)
result = ind.update(close, volume)  # same tick-count stopping rule
# result.bar_open, bar_close, bar_high, bar_low, bar_volume,
# result.direction, result.complete, result.bars

The one-argument overload reports the dominant tick count in bar_volume. The volume overload still stops on dominant tick count, but reports the volume accumulated by the dominant side.

Theory Of Operation

Run bars sample persistent order-flow imbalance rather than a consecutive streak. A sequence such as buy, sell, buy, buy contains three buy ticks and one sell tick; it is not reset by the sell. The adaptive formulation estimates the expected bar length and buy probability. RTTA exposes the underlying dominant side statistic with a fixed threshold, which is predictable and suitable when the caller estimates or retunes the threshold externally.

Recurrence

The tick rule is

\[b_t=\begin{cases} +1,&c_t>c_{t-1},\\ -1,&c_t<c_{t-1},\\ b_{t-1},&c_t=c_{t-1}. \end{cases}\]

Before any nonzero sign has been observed, a flat tick remains unclassified. For the active bar, maintain

\[N_t^+=\sum 1_{\{b_i=+1\}},\qquad N_t^-=\sum 1_{\{b_i=-1\}},\qquad \theta_t=\max(N_t^+,N_t^-).\]

The bar completes when

\[\theta_t\ge\max(\texttt{threshold},1).\]

On completion, direction is \(+1\) when \(N_t^+\ge N_t^-\), otherwise \(-1\); complete and bars are 1. OHLC covers every price assigned to the bar, including sign changes. Side totals and OHLC reset for the next bar, while the last price and last nonzero tick sign carry across the boundary.

For update(close, volume), \(V^+\) and \(V^-\) are also accumulated with \(v_t^+=\max(v_t,0)\), solely to report \(\max(V^+,V^-)\) as bar_volume.

Implementation Notes

The recurrence is implemented in src/rtta/indicator.cpp in class RunBarGenerator. The first observation seeds price and OHLC but has no tick direction.

Reference