Mirexa

Wiki Guides

Deriving bars from ticks and candles

A walkthrough of having Mirexa make candles for you — one-minute bars from trades, longer bars from shorter ones, chains, deriving existing history — and of query-time rollups for one-off bar lengths.

If you already have trades, or bars of one length, you do not need to compute longer bars yourself. Declare a stream's lineage and the node writes its candles as the source arrives; or ask for a rollup and get longer bars computed at query time. This guide walks through both. The rules themselves — silences, grace, INCOMPLETE marks, daily and weekly bucketing — are in Derived streams and lineage.

Changing a lineage uses the node's control listener from the CLI (control.enabled: true; see the Quickstart), or the ADMIN_STREAM permission from the SDKs. In a cluster, send it to the stream's primary.

One-minute bars from trades

Use one stream id for both halves: write trades to its tick half, and have the node derive its candle half.

bash
./bin/cli lineage set -control 127.0.0.1:9091 -stream binance:ETHUSDT:1m \
    -kind ticks -history from-now

With -kind ticks the source defaults to the stream itself. Now write trades:

bash
./bin/cli ingest -ticks -stream binance:ETHUSDT:1m < trades.csv

Each minute is written as a closed bar once its window and the close grace (1 s by default, -grace) have passed. A minute with no trades inside a short silence is written flat at the previous close with zero volume; a silence longer than the no-trade limit (10 s by default, -no-trade-limit) is recorded as a gap, and a bar that only partly overlaps one is written with an INCOMPLETE mark.

Set the stream's trading calendar before deriving. The deriver uses the derived stream's calendar: without one, every night and weekend of a stock is a long silence, recorded as missing data. With one, closed hours are closures and silences restart at each session open.

Read the derived bars like any candles:

bash
./bin/cli query -stream binance:ETHUSDT:1m -start 2026-10-22T09:00:00Z -end 2026-10-22T10:00:00Z

Longer bars from shorter ones

Each link is its own stream with its own lineage, of kind candles:

bash
./bin/cli lineage set -control 127.0.0.1:9091 -stream binance:ETHUSDT:5m \
    -kind candles -source binance:ETHUSDT:1m -history from-now
bash
./bin/cli lineage set -control 127.0.0.1:9091 -stream binance:ETHUSDT:1h \
    -kind candles -source binance:ETHUSDT:5m -history from-now

The rules a declaration must pass: the source is the same exchange and symbol; the derived length is a whole multiple of the source's, at least twice it; above a day, only 1d and 1w. A refused declaration says why. Corrections and fills of a source flow down the chain by themselves, and so do INCOMPLETE marks.

A derived 1d bar holds the trading date in the stream's calendar timezone and is stamped 00:00 UTC of that date; a 1w bar holds the Monday-start week and is stamped Monday 00:00 UTC.

Deriving history the source already holds

-history from-now derives only what arrives after the declaration. To derive the source's existing history as well:

bash
./bin/cli lineage set -control 127.0.0.1:9091 -stream binance:ETHUSDT:5m \
    -kind candles -source binance:ETHUSDT:1m -history rederive

The node walks the source's history in the background, a window of 2,048 target bars per pass, writing bars that are missing and rewriting only bars that differ. lineage get shows rederive_pending until the walk has caught up; a restart resumes it from the beginning. For a chain, re-derive one link at a time, starting from the bottom, and wait for each link's walk to finish before declaring the next.

bash
./bin/cli lineage get -control 127.0.0.1:9091 -stream binance:ETHUSDT:5m

From Go

go
_, err := c.SetStreamLineage(ctx, client.SetStreamLineageRequest{
	StreamID: fiveMin,
	History:  client.LineageHistoryFromNow, // or LineageHistoryRederive
	Lineage: &client.Lineage{
		Source: oneMin,
		Kind:   client.SourceCandles,
		// CloseGrace: 2 * time.Second, // zero means the default
	},
})

SetStreamLineage with History: client.LineageHistoryClear and no lineage stops derivation; bars already derived are kept. A refused declaration is a *RemoteError with code Protocol and the reason; an unknown stream answers QueryStatusStreamNotFound in the response, not an error.[1]

Writing into a derived stream

The node refuses ordinary writes to a derived stream (DerivedStream, 0x000A), so that your bars and its bars are never silently interleaved. To replace derived bars deliberately:

go
err := c.OverwriteCandles(ctx, fiveMin, bars, client.DurabilityAcked, client.RouteCorrection)

or -overwrite on mirexa-cli ingest. Your bar stands until the source changes under it, at which point the node re-derives it: the last write wins.

Resolving INCOMPLETE marks

A derived bar made over a hole in its source carries an INCOMPLETE mark. Fill the source's hole and the bar is re-derived and the mark cleared; or accept the bar as it is:

bash
./bin/cli gap accept -control 127.0.0.1:9091 -stream binance:ETHUSDT:5m \
    -start 2026-10-22T09:00:00Z

See Finding and filling gaps.

Rollups: bars at query time

For a bar length you need occasionally — or one no stream stores — ask for a rollup:

bash
./bin/cli query -stream binance:ETHUSDT:1m \
    -start 2026-10-22T00:00:00Z -end 2026-10-23T00:00:00Z -rollup 7m
bash
./bin/cli query -stream binance:ETHUSDT:1m -ticks \
    -start 2026-10-22T09:00:00Z -end 2026-10-22T10:00:00Z -rollup 1m

The first rolls the stored one-minute candles into seven-minute bars; the second builds one-minute bars straight from the trades. Nothing is stored. A rollup runs the same rules a declared derivation would, with the stream's own calendar and default grace and no-trade limit, so the two agree bar for bar; only buckets that have closed are answered. From Go, set QueryRequest.Rollup (and Ticks for trades).[2]

Rollup or lineage? A rollup recomputes on every read (on one laptop, 2026-10-22: a million trades into one-minute bars in 45 ms, a year of one-minute bars into hourly in 67 ms); a lineage computes once, stores the result, and makes it subscribable and replicated. Explore with rollups; declare what you read every day.

References

Source

Tests

Further reading


  1. The request and response types are wire.SetStreamLineageRequest and wire.StreamMetadataResponse in /internal/wire/stream_metadata.go. ↩︎

  2. TestRollup_AnswersWhatAStoredDerivationWould; timings from BenchmarkRollup_QueryTime as recorded in the repository's developer overview. ↩︎