Derived streams and gap repair

Writing a derived stream, filling a gap from its neighbours, and accepting an INCOMPLETE mark.

WriteRoute

WriteRoute picks the route Client.OverwriteCandles writes a DERIVED stream by: the one IngestCandles, IngestHistoryCandles or IngestCorrectionCandles would take.

type WriteRoute uint8

WriteRoute picks which of the three candle write routes OverwriteCandles takes: the one IngestCandles, IngestHistoryCandles or IngestCorrectionCandles would.

const (
	// RouteLive is IngestCandles' route: new bars near the present.
	RouteLive WriteRoute = iota
	// RouteHistory is IngestHistoryCandles' route: bulk bars in the past.
	RouteHistory
	// RouteCorrection is IngestCorrectionCandles' route: restating stored bars.
	RouteCorrection
)

FillGapRequest

FillGapRequest names the gap (its StartTs, exactly as ListGaps gave it), the method, and an optional cap on candles written.

type FillGapRequest struct {
	StreamID StreamID
	// GapStart is the StartTs of a gap as LIST_GAPS reported it. It must match
	// exactly: a gap's start moves when it merges with a neighbor, and a
	// request naming a start no gap has is refused rather than applied to
	// whatever hole is nearest.
	GapStart int64
	// Method is how the candles are made up. The zero value is refused.
	Method FillMethod
	// MaxCandles caps the candles one request writes; 0 means the node's own
	// cap. FillGapResponse.HasMore says whether to ask again.
	MaxCandles uint32
	// Overwrite asks to fill a DERIVED stream (Phase 8 §8.1.9), which is
	// refused without it. It travels as the frame's FlagOverwrite, not in
	// the payload, so the payload codec ignores it.
	Overwrite bool
}

FillGapRequest asks a node to auto-fill one gap.

func ParseFillGapPayload(payload []byte) (FillGapRequest, error)

ParseFillGapPayload decodes a FILL_GAP payload.

FillGapResponse

FillGapResponse says how many candles were written and whether the gap still has missing slots past the cap (call again; it continues the line).

type FillGapResponse struct {
	// Filled is how many candles were written.
	Filled uint32
	// HasMore reports missing slots left in the gap past the cap.
	HasMore bool
	// Gap is the gap as the node found it, before the fill.
	Gap GapRange
}

FillGapResponse is what one FILL_GAP did.

func ParseFillGapResponsePayload(payload []byte) (FillGapResponse, error)

ParseFillGapResponsePayload decodes a FILL_GAP_RESPONSE payload.

FillMethod

FillMethod is how the candles are made up.

type FillMethod uint8

FillMethod names how an auto-fill makes up the candles of a gap.

An auto-fill is a deliberate request from a client: nothing applies one on ingest or on read. It writes candles nobody traded into a real outage, so every method here produces candles with ZERO VOLUME. That is both the honest value (nothing traded that anyone recorded) and the only mark left on a synthetic bar once it is stored, so a consumer can still tell one apart.

A market closure (OFF_TIME) is never auto-filled, for the reason GapRange.Fillable gives: there is no data for those hours to approximate.

const (
	// FillMethodUnknown is the zero value and is refused everywhere: a request
	// that forgot to name a method must not get one chosen for it.
	FillMethodUnknown FillMethod = 0
	// FillMethodLinear draws a straight line from the close of the candle
	// before the hole to the open of the candle after it. Each made-up candle
	// opens where the previous one closed, so the series has no jumps. Needs a
	// candle on both sides.
	FillMethodLinear FillMethod = 1
	// FillMethodForward carries the close of the candle before the hole
	// forward: every made-up candle is flat (open = high = low = close) at that
	// price. Needs a candle before the hole.
	FillMethodForward FillMethod = 2
	// FillMethodBackward carries the open of the candle after the hole
	// backward: every made-up candle is flat at that price. Needs a candle after
	// the hole.
	FillMethodBackward FillMethod = 3
)
func ParseFillMethod(s string) (FillMethod, error)

ParseFillMethod reads the name String returns.

func (m FillMethod) String() string

String returns the method’s name as the API spells it.

func (m FillMethod) Validate() error

Validate refuses the zero value and anything undefined.

AcceptGapRequest

AcceptGapRequest names a derived bar’s INCOMPLETE mark (its StartTs, exactly as ListGaps gave it) and whether to accept it as it is (“known incomplete, keep it”) or put an accepted one back to OPEN. For Client.AcceptGap; GapRange.AcceptAction says which way a record may go.

type AcceptGapRequest struct {
	StreamID StreamID
	// GapStart is the StartTs of the mark as LIST_GAPS reported it. It must
	// match exactly, as FILL_GAP's does.
	GapStart int64
	// Accept true marks it ACCEPTED; false puts an accepted mark back to OPEN.
	Accept bool
}

AcceptGapRequest asks a node to accept, or put back, one INCOMPLETE mark.

func ParseAcceptGapPayload(payload []byte) (AcceptGapRequest, error)

ParseAcceptGapPayload decodes an ACCEPT_GAP payload.

AcceptGapResponse

AcceptGapResponse is the mark as it stands after the request.

type AcceptGapResponse struct {
	Gap GapRange
}

AcceptGapResponse is the mark as it stands after the request.

func ParseAcceptGapResponsePayload(payload []byte) (AcceptGapResponse, error)

ParseAcceptGapResponsePayload decodes an ACCEPT_GAP_RESPONSE payload.