Data model
Streams, candles, ticks and query points.
StreamID
StreamID identifies a stream: exchange, symbol, timeframe.
func NewStreamID(exchange, symbol, timeframe string) StreamID
type StreamID struct {
Exchange string
Symbol string
Timeframe string
}
StreamID uniquely identifies a stream by exchange, symbol, and timeframe.
func NewStreamID(exchange, symbol, timeframe string) StreamID
NewStreamID creates a new StreamID with the given components.
func ParseStreamID(s string) (StreamID, error)
ParseStreamID parses a string produced by String() back into a StreamID. It expects the format “symbol:timeframe:exchange”.
func (s StreamID) Hash() uint64
Hash computes a deterministic hash of the full stream coordinates (symbol, timeframe, exchange).
func (s StreamID) Interval() (int64, error)
Interval returns the candle interval in nanoseconds derived from the timeframe string, using the grammar ParseTimeframe documents.
It returns an error wrapping ErrInvalidTimeframe rather than guessing. An earlier version defaulted to 1 minute, which meant a stream named with a typo ingested happily and bucketed every candle at the wrong width: five hourly candles under the timeframe “banana” queried back as 241 points, 236 of them gap markers for data that was never missing. The wire boundary (network.validateStreamID) and config validation now both reject such a timeframe before it reaches here; this signature is what keeps any future caller from reintroducing the guess. See TestStreamID_Interval_RejectsUnparseableTimeframe and
func (s StreamID) MarshalBinary() ([]byte, error)
MarshalBinary implements encoding.BinaryMarshaler.
func (s StreamID) MarshalJSON() ([]byte, error)
MarshalJSON implements json.Marshaler.
func (s StreamID) PartitionKey() uint64
PartitionKey computes a deterministic hash of the partition routing key (symbol + exchange). All timeframes for the same symbol and exchange share the same partition key so they are colocated in the same partition.
func (s StreamID) StreamDir() string
StreamDir returns the filesystem directory name for this stream. Format: <symbol>_<timeframe>.
func (s StreamID) String() string
String returns a string representation for debugging.
func (s *StreamID) UnmarshalBinary(data []byte) error
UnmarshalBinary implements encoding.BinaryUnmarshaler.
func (s *StreamID) UnmarshalJSON(data []byte) error
UnmarshalJSON implements json.Unmarshaler.
Candle
Candle is one OHLCV record. Timestamp is Unix nanoseconds.
type Candle struct {
Timestamp int64
Open float64
High float64
Low float64
Close float64
Volume float64
}
Candle represents a single OHLCV candle.
func FillRun(method FillMethod, prev, next *Candle, start, interval int64, k, n int) ([]Candle, error)
FillRun makes up the first n candles of a run of k consecutive missing slots, the first at start, each interval apart.
prev is the last real candle before the run and next the first one after it; either may be nil when there is none, and a method that needs a missing one returns ErrNoAnchor. n may be less than k: a fill capped partway through a run produces exactly the candles a whole-run fill would have produced first, so a later call that finishes the run continues the same line. (For a linear fill, that later call starts from the last candle this one made, which lies on the line, so the slope it computes is the same one.)
func NewCandle(timestamp int64, open, high, low, closePrice, volume float64) Candle
NewCandle creates a new Candle with the given values.
func (c Candle) MarshalBinary() ([]byte, error)
MarshalBinary implements encoding.BinaryMarshaler. Format: 8 bytes timestamp + 5x8 bytes for OHLCV = 48 bytes total.
func (c *Candle) UnmarshalBinary(data []byte) error
UnmarshalBinary implements encoding.BinaryUnmarshaler.
func (c *Candle) Validate() error
Validate reports whether the candle may be stored: every field storable, volume non-negative, and OHLC mutually consistent.
The RECEIVER IS A POINTER and the accept path is one inlinable expression, for speed rather than style. This runs once per candle inside wire.ParseCandleBatchPayload’s decode loop — the path payload.go documents tuning from 4,247ns to 878ns per 500 records — and the first draft of this method reintroduced the exact mistake that tuning removed: a value receiver copies the 48-byte struct on every call, and splitting the checks across helper methods copies it again per helper.
Measured 2026-09-07, quiet machine, GOMAXPROCS=1 taskset -c 3, interleaved A/B min-of-5 on BenchmarkParseCandleBatchPayload/batch_500 (spread under 1.5%, so these are real):
no validation 7.03 ns/record
value receiver 15.82 ns/record +8.79 2.25x
pointer receiver 11.78 ns/record +4.75 1.68x
Identical checks in the last two rows; the 4.04ns between them is the struct copy alone. The tick twin costs +1.80ns (3.75 -> 5.54). Both allocate nothing on the accept path (BenchmarkCandleValidate).
So validation nearly doubles this micro-benchmark and is still the right trade: the loop it doubles is a few nanoseconds against an end-to-end ingest path measured in hundreds of nanoseconds per candle, and what it buys is refusing a value that would otherwise be acked and then silently dropped. Re-derive the end-to-end figure before treating any of this as a budget.
Message formatting lives in explain(), which the inliner may leave alone because only a candle already being rejected reaches it.
Order matters inside explain(): storability is diagnosed before range, because the range comparisons cannot detect a NaN — every comparison against NaN is false, so a NaN High silently satisfies “High >= Low” and would be reported, if at all, as some unrelated violation.
Tick
Tick is one trade record. Timestamp is Unix nanoseconds.
type Tick struct {
Timestamp int64
Price float64
Volume float64
}
Tick represents a single market tick.
func NewTick(timestamp int64, price, volume float64) Tick
NewTick creates a new Tick with the given values.
func (t Tick) MarshalBinary() ([]byte, error)
MarshalBinary implements encoding.BinaryMarshaler. Format: 8 bytes timestamp + 8 bytes price + 8 bytes volume = 24 bytes total.
func (t *Tick) UnmarshalBinary(data []byte) error
UnmarshalBinary implements encoding.BinaryUnmarshaler.
func (t *Tick) Validate() error
Validate reports whether the tick may be stored: price and volume storable, volume non-negative. It runs once per tick on the ingest decode path (wire.ParseTickBatchPayload); see Candle.Validate for the fast-path shape.
A NEGATIVE OR ZERO PRICE IS VALID and must stay valid. Until 2026-09-07 this method opened with if t.Price <= 0 { return ... "must be positive" }, which was safe only because nothing called it. Wiring it to the ingest path unchanged would have made the engine reject real market data: WTI crude settled at -$37.63 on 2020-04-20, and negative clearing prices are routine in European power markets and in spread and basis instruments. Zero is likewise a real print. The engine’s only actual constraint on a price is that it be storable, and inventing a tighter one silently discards the days a trader most needs recorded.
This is worth stating plainly because this repo’s usual pattern points the other way: where a dead implementation and a live one disagree, the dead one has been correct four times out of four. Here the dead one was correct about NaN and infinities and WRONG about sign, so it was adopted in part rather than wholesale. TestTickBatchPayload_AcceptsNegativeAndZeroPrice is the guard against someone restoring the stricter rule on the strength of that pattern.
CandlePoint
CandlePoint is one item of a query result: either a candle, or a gap marker classifying why a slot has none. A candle of a derived stream may carry a mark of its own, GapReasonIncomplete (CandlePoint.Incomplete): its source had a hole under its window. The gap classification is the point of this engine — a market closure (GapReasonOffTime, GapStatusPermanent) is not data loss, while an open gap is missing data a backfill can still repair.
The classification comes from the stream’s trading calendar, which this package does not set (it is stream metadata on the node’s gRPC control listener; see mirexa-cli calendar). A calendar change re-files recorded gaps at once, so a gap’s classification can differ between two queries.
type CandlePoint struct {
// Candle holds the OHLCV data. Zero-valued when IsGap is true.
Candle Candle
// IsGap indicates the time slot has no candle data.
IsGap bool
// Status and Reason classify a gap: whether it is still open (and so
// backfillable) and what caused it. They are meaningful when IsGap is
// true, or on a BAR whose Reason is GapReasonIncomplete (see Incomplete),
// and only when the slot falls inside a range the gap index has
// already classified -- a hole the classifier has not reached yet reports
// the zero value, GapStatusOpen/GapReasonUnknown, which is the honest
// answer rather than a guess.
//
// These exist because a market closure is not data loss. Without them a
// weekend and a dropped feed are the same single bit on the wire, which
// makes the distinction this whole engine is built around unobservable to
// a caller. gapOverlayIterator is what fills them in; see
// internal/storage/gap_overlay.go.
Status GapStatus
Reason GapReason
}
CandlePoint is one item of a query result: either a candle, or a marker for a slot that has none.
It lives here rather than in internal/storage, where it was defined until 2026-09-06, because it needs nothing from the storage engine — a types.Candle, a bool, and the two gap enums above. Its old home forced every consumer of a query result to import internal/storage, which is what kept the wire codec (internal/network/payload.go) transitively bound to the whole database. That binding is the blocking fact of the Phase 4 client SDK: a public package cannot drag storage and wal into an external caller’s binary. internal/storage keeps a type alias, so it is the same type by any name.
func (p CandlePoint) Incomplete() bool
Incomplete reports whether the point is a bar carrying an INCOMPLETE mark: a derived bar whose source had a hole under its window. The bar’s data is real and is in Candle; the mark says it was made from less than its whole window. ACCEPTED is still a mark — it says an operator knows. A gap marker is never incomplete: there is no bar.