signals

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 21, 2026 License: MIT Imports: 2 Imported by: 0

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

func BestImbalance(snap *orderbook.Snapshot) float64

BestImbalance is DepthImbalance over only the best (top) level of each side.

func DepthImbalance

func DepthImbalance(snap *orderbook.Snapshot, levels int) float64

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

func LinReg(x, y []float64) (slope, intercept, r2 float64)

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

func OFIStep(prev, cur *orderbook.Snapshot) decimal.Decimal

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 NewOFI

func NewOFI() *OFI

NewOFI returns an empty accumulator.

func (*OFI) Cumulative

func (o *OFI) Cumulative() float64

Cumulative returns the running sum of e_n since the last Reset.

func (*OFI) CumulativeExact

func (o *OFI) CumulativeExact() decimal.Decimal

CumulativeExact returns the running sum as an exact decimal.

func (*OFI) Observe

func (o *OFI) Observe(cur *orderbook.Snapshot) float64

Observe folds one snapshot into the accumulator and returns the per-step e_n. The very first snapshot only primes the previous state and returns 0.

func (*OFI) Reset

func (o *OFI) Reset()

Reset clears the previous observation and cumulative total.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL