Documentation
¶
Overview ¶
Package signals computes market-microstructure signals from order-book snapshots: book imbalance and order-flow imbalance (OFI) to start.
These are the first entries in the research agenda (docs/research-roadmap.md). A standing caveat travels with them: the well-known strong relationship between OFI and price change is *contemporaneous* (it explains the move over the same interval), not a proven next-tick forecast. This package computes the signals; the experiments that test their predictive value live in the research and backtest layers.
Signals are dimensionless and returned as float64, which is the natural type for feeding regressions and plots — distinct from the exact decimals used for money in the core library.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BestImbalance ¶
BestImbalance is DepthImbalance over only the best (top) level of each side.
func DepthImbalance ¶
DepthImbalance returns the depth-weighted order-book imbalance over the top `levels` levels of each side:
(ΣbidQty − ΣaskQty) / (ΣbidQty + ΣaskQty) ∈ [−1, 1]
Positive means more resting bid size than ask size (buy pressure); negative the reverse. Returns 0 only for an empty book (no size on either side) or non-positive levels; a fully one-sided book yields ±1 — all pressure on one side, which is a meaningful extreme rather than "no signal".
func LinReg ¶
LinReg fits y = slope·x + intercept by ordinary least squares and returns the slope, intercept, and R² (coefficient of determination, the fraction of the variance in y explained by x). It returns zeros when there are fewer than two points, the lengths differ, or x has no variance.
R² here is the square of the Pearson correlation — the quantity the OFI literature reports. Note it measures variance explained, not directional hit-rate, and says nothing on its own about whether the relationship is contemporaneous or predictive.
func OFIStep ¶
OFIStep computes the best-level order-flow-imbalance contribution e_n between two consecutive book snapshots, following Cont, Kukanov & Stoikov (2014):
e_n = ΔV^bid − ΔV^ask
where, comparing previous (P, Q) to current (P', Q') at the best level:
ΔV^bid = Q' if P' > P (bid stepped up: fresh buy size)
Q' − Q if P' = P (same level: net change)
−Q if P' < P (bid stepped down: size pulled)
ΔV^ask = −Q if P' > P (ask stepped up: size pulled)
Q' − Q if P' = P
Q' if P' < P (ask stepped down: fresh sell size)
Positive e_n reflects net buy pressure at the touch, negative net sell pressure. A side missing from either snapshot contributes 0.
Types ¶
type OFI ¶
type OFI struct {
// contains filtered or unexported fields
}
OFI accumulates order-flow imbalance across a stream of snapshots. Feed it successive snapshots with Observe; it maintains the previous observation and a running cumulative OFI (the windowed quantity that the CKS regression uses against price change). Not safe for concurrent use.
func (*OFI) Cumulative ¶
Cumulative returns the running sum of e_n since the last Reset.
func (*OFI) CumulativeExact ¶
CumulativeExact returns the running sum as an exact decimal.