Mirexa

Wiki Concepts

Gaps and trading calendars

How Mirexa records missing bars, tells a closed market apart from lost data using each stream's trading calendar, what each gap status and reason means, and how gaps reach a client.

A candle stream has a slot for every interval, so Mirexa can say not only what data it holds but which bars are missing. It records each run of missing bars as a gap and classifies it against the stream's trading calendar: a hole during trading hours is lost data that can still be recovered; a hole over a weekend, a holiday or a night session is a market closure, where there was never anything to record. Keeping the two apart is what lets "how complete is this stream?" have a meaningful answer for a market that trades 32 hours a week.

What a gap is

A gap is a half-open time range [StartTs, EndTs) of a stream's grid with no bar in it, plus a status and a reason.[1] Gaps are found when the hot cache is flushed: the flusher looks at the slots it is evicting, takes each run of empty slots, classifies it, and writes the records into the stream's gap index (gap.idx) together with the chunk.

Three boundaries are worth knowing:

  • A stream begins at its first bar. Nothing before it is a gap.
  • The end of a stream is not a gap. A run of empty slots is filed only once it has ended — once a later bar arrives. A feed that has simply stopped is detected by staleness instead: mirexa_stream_trading_seconds_since_data measures trading time since the newest bar closed, so a closed market adds nothing to it.[2]
  • A pause across a restart is recorded too. When a stream resumes after an absence — across a restart, or longer than its hot cache — the span it skipped is kept and filed with the next chunk.[3]

A gap still in the hot cache, or a run that has not ended, is reported by queries as unclassified (OPEN / UNKNOWN) — an honest "not known yet" rather than a guess — and is not yet in the gap index.[4]

Status and reason

Status Meaning
OPEN (0) Missing data that can still be recovered.
FILLING (1) A fill is in progress. The node itself moves a gap from OPEN to CLOSED in one write, so a stored record shows OPEN or CLOSED, not FILLING.
CLOSED (2) Was missing, has since been filled. Kept, not deleted: the record that a range was back-filled is provenance.
PERMANENT (3) Not recoverable. Market closures are filed this way.
ACCEPTED (4) An INCOMPLETE mark an operator decided to keep as it is. Valid only on INCOMPLETE records.
Reason Meaning Filed by
UNKNOWN (0) Missing; cause not recorded. The flusher, for every hole during trading time
OFF_TIME (8) The market was closed. Not data loss. The flusher, from the calendar
INCOMPLETE (9) Not a hole: a derived bar exists here, made while its source had a hole under its window. The bar is stored and returned, carrying the mark. The deriver (Derived streams)
CORRUPTION, NETWORK, CRASH, MAINTENANCE, RATE_LIMIT, EXCHANGE, MANUAL (1–7) Named causes of a real loss. Defined in the protocol; nothing in the node files them at this commit.

What the flusher files is exactly two shapes: missing data as OPEN / UNKNOWN, and a closure as PERMANENT / OFF_TIME.[5]

Fillability

Whether a gap may be filled with supplied bars has one definition, GapRange.Fillable(), which every client and the node itself use:[6]

Gap Fillable? Reason given
OPEN, any reason but OFF_TIME yes —
reason OFF_TIME (any status) no the market was closed, so there is no data to recover
PERMANENT no marked permanent, not considered recoverable
CLOSED no already filled
FILLING no another fill is in progress
ACCEPTED no an incomplete bar was accepted as it is
unknown status (from a newer writer) no treated as permanent: fail safe

A closure is never fillable, and the node enforces it. Anything written into hours the exchange was shut is fabricated by definition, and once written, a "filled" weekend is indistinguishable from one that traded. A write — a correction, or a plain write that restates history — with any timestamp inside a filed OFF_TIME gap is refused whole, before anything is stored, naming the closure and the first offending timestamp.[7]

What is not refused: new bars arriving for hours the calendar calls closed. Real data arriving then is evidence that the calendar is wrong (an extended session, a moved holiday), and discarding market data because a calendar disagrees with the exchange would be the worse error. Those bars land, and the calendar should be fixed.

A fill closes a gap only when it covers every slot of it. A partial fill leaves the gap OPEN with its original range — overstating what is missing, which is the safe direction. See Finding and filling gaps.

Trading calendars

A stream's calendar is part of its metadata:[8]

  • Working hours — for each weekday, zero or more windows HH:MM-HH:MM in one IANA timezone (America/New_York, Asia/Tokyo, UTC). Windows are [open, close) in local time, open must be before close, and each weekday appears once. Daylight-saving changes follow the timezone.
  • Closures — specific ranges [from, to) the market was or will be closed outside its weekly hours: a holiday, an early close, a halt, an exchange outage declared after the fact, or a hole you know was expected. Each may carry a note (up to 256 bytes); a stream may hold up to 10,000. Overlapping or touching closures are merged.
  • GapThresholdHigh — for a stream with no working hours (a 24/7 market), a gap at least this long is treated as a closure (a halt, a delisting).

How a hole is classified

The rule, applied to each piece of a hole:[9]

  1. A piece is missing data only if it lies inside the working hours (or the stream has none) and outside every closure. Otherwise it is a closure.
  2. A hole that spans a boundary — Friday 15:00 to Monday 10:00 on a 09:30–16:00 market — is split at the boundaries: one hour missing on Friday, the weekend a closure, thirty minutes missing on Monday.
  3. The schedule decides first. Any part of a hole that overlaps trading time is missing data, however long the hole is; GapThresholdHigh applies only to streams without working hours.
  4. A stream with no calendar at all files every hole as missing data.

A calendar change re-files gaps at once

Changing a calendar — new hours, a closure added or removed, a new threshold — takes effect immediately on every gap the calendar decided: the node re-classifies the stream's recorded OPEN / UNKNOWN and PERMANENT / OFF_TIME records under the new calendar in one atomic write before the call returns, and gaps not yet flushed are classified under the new calendar when they are.[10] Declaring a holiday today turns last week's "missing" hour into a closure; removing it turns it back. A gap since filled holds real data and is never touched, nor is a record filed for another reason.

So do not cache a gap's classification as permanent: read it again after a calendar change. The update returns how many nanoseconds of gap changed class.

The change is not atomic across a crash (the catalog is written before the re-file), but it is idempotent: re-sending the same update re-files.

Setting a calendar

Interface Calls Needs
mirexa-cli calendar get / set / add-closure / remove-closure the node's control listener (gRPC) control.enabled: true on the node
Go SDK GetStreamMetadata, SetStreamMetadata, AddStreamClosure, RemoveStreamClosure the data port ADMIN_STREAM permission to change, READ_CANDLES to read
Python SDK set_stream_metadata, add_stream_closure, … the data port as the Go SDK
bash
./bin/cli calendar set -control 127.0.0.1:9091 -stream nyse:AAPL:1m \
    -timezone America/New_York -hours mon-fri=09:30-16:00
bash
./bin/cli calendar add-closure -control 127.0.0.1:9091 -stream nyse:AAPL:1m \
    -from 2026-11-26T09:30:00-05:00 -to 2026-11-26T16:00:00-05:00 -note Thanksgiving

calendar set reads the current record and writes it back with only the flags you gave changed, and registers a stream the node does not know yet. Several windows in one day are comma-separated (mon-fri=09:00-11:30,12:30-15:00); day ranges wrap (sun-thu). -no-hours removes the weekly hours. In a cluster, send calendar changes to the stream's primary; copies take their primary's calendar with replication.

Limits of the calendar model, as built: a window cannot run to 24:00 (the latest close is 23:59, and windows are [open, close), so 00:00-23:59 leaves the day's last minute outside the schedule); a session crossing midnight is written as two windows on two days; there is no recurring closure (a weekly early close is a working-hours window, a yearly holiday is one closure per year); and there is no built-in exchange holiday data. GapThresholdLow is stored but has no effect (see Streams).

How gaps reach a client

Gaps surface in two ways:

As markers in a query. With IncludeGaps (-gaps on the CLI), a candle query returns a point for each empty slot in the range, with IsGap set and the slot's status and reason. A query answers per interval, so a weekend on a one-minute stream is 2,880 markers. Tick queries carry no gap markers.[11]

As ranges in a listing. ListGaps returns a stream's recorded gaps as ranges, ordered by start, filtered (OpenOnly, ExcludeOffTime) before paging (default page 200, at most 1,000; larger is refused, not clamped), with a summary of the whole window:[12]

Summary field Meaning
MissingDuration Time genuinely absent: excludes closures, filled (CLOSED) gaps and INCOMPLETE marks. The number to alert on.
OffTimeDuration Scheduled closure time.
Count, OpenCount, OffTimeCount, FillableCount Counts over the window; subsets, not a partition.

A gap overlapping the window is returned whole (a hole is not two holes because a window cut it), while the summary's durations are clipped to the window. A listing reads the gap index, so it shows flushed gaps only; a hole still in the hot cache appears in queries but not yet in listings.

mirexa-cli has no gap-listing command; use the SDKs (ListGaps; per-stream gap counts and missing time through ListStreams with statistics) or the web dashboard.

References

Source

Tests

Further reading

External

  • IANA Time Zone Database — https://www.iana.org/time-zones

  1. types.GapRange; 18 bytes on disk (MarshalBinary). ↩︎

  2. mirexa_stream_trading_seconds_since_data in /docs/metrics/catalog.txt. ↩︎

  3. TestIngest_AResumedStreamFilesTheAbsenceBeforeIt. ↩︎

  4. StreamCache.PendingGaps; TestQuery_APauseStillGoingOnIsReportedWhole. ↩︎

  5. CompactResult.GapRanges; GapIndex.CloseFilled writes CLOSED directly after validating both transitions (/internal/storage/gap_index.go). The named loss reasons (1–7) have no producer outside the type definitions and the protocol codecs, checked by search at this commit. ↩︎

  6. GapRange.Fillable in /internal/types/gap.go; TestGapRange_Fillable. ↩︎

  7. LiveStreamWriter.refuseClosureFill; TestCorrection_RefusesToFillAMarketClosure, TestIngest_APlainWriteFillsAStoredOutageButNeverAMarketClosure. ↩︎

  8. /internal/types/metadata.go, /internal/types/closure.go (MaxClosures, MaxClosureNoteBytes, WorkingHours.validate, ParseTimeWindow). ↩︎

  9. GapClassifier.Classify, ClassifyAndSplit; TestClassify_TradingHoursOutageIsGap, TestClassify_FullSessionOutageIsGap, TestFlusher_ClassifiesGapsWithMetadata. ↩︎

  10. /internal/ingestion/stream_calendar.go, GapIndex.Refile; TestCalendar_AChangeRefilesEveryGapAtOnce, TestCLI_CalendarChangesRefileAStreamsGaps. ↩︎

  11. TestQueryCandles_GapClassificationSurvivesToTheResult. ↩︎

  12. wire.ListGapsRequest, GapSummary, DefaultListGapsPage, MaxListGapsPage; TestServer_ListGapsReportsAWeekendAndAnOutageDifferently, TestSummarizeGaps_AgreesWithTheGapIndex. ↩︎