Documentation
¶
Overview ¶
Package marketdata is Trader's public market-data model: the Bar and OHLC every consumer (storage, backtests, studies, strategies, external Go code) shares, with the validation of what a possible bar is and the exact midpoint derivation (ADR-090, ADR-091).
It depends only on the num package and the standard library, and never on storage, providers, services or the root trading engine, so anything may import it without a cycle. A test enforces that.
Example ¶
A consumer outside the package builds a bar from a two-sided quote, lets the midpoint be derived once, and reads "the price" without choosing a basis.
package main
import (
"fmt"
"time"
"github.com/rustyeddy/trader/marketdata"
"github.com/rustyeddy/trader/num"
)
func main() {
px := num.MustParsePrice
bid := marketdata.OHLC{Open: px("1.10000"), High: px("1.10100"), Low: px("1.09900"), Close: px("1.10050")}
ask := marketdata.OHLC{Open: px("1.10010"), High: px("1.10111"), Low: px("1.09911"), Close: px("1.10061")}
mid, err := marketdata.MidOf(bid, ask)
if err != nil {
panic(err)
}
open := time.Date(2024, 1, 2, 0, 0, 0, 0, time.UTC)
bar := marketdata.Bar{Time: open, AvailableAt: open.Add(time.Hour), Volume: 100, Bid: bid, Ask: ask, Mid: mid}
fmt.Println(bar.Validate())
fmt.Println(bar.OHLC().Close, bar.Bid.Close, bar.Ask.Close)
fmt.Println(bar.IsAvailable(open.Add(30*time.Minute)), bar.IsAvailable(open.Add(time.Hour)))
}
Output: <nil> 1.100555 1.1005 1.10061 false true
Example (SinglePriceSource) ¶
A single-price source writes the price to the series it is; nothing else is invented.
package main
import (
"fmt"
"time"
"github.com/rustyeddy/trader/marketdata"
"github.com/rustyeddy/trader/num"
)
func main() {
px := num.MustParsePrice
open := time.Date(2024, 1, 2, 0, 0, 0, 0, time.UTC)
bar := marketdata.Bar{Time: open, AvailableAt: open.Add(24 * time.Hour),
Trade: marketdata.OHLC{Open: px("187.10"), High: px("188.00"), Low: px("186.50"), Close: px("187.90")}}
fmt.Println(bar.Validate(), bar.OHLC().Close, bar.Bid.IsZero(), bar.Ask.IsZero())
}
Output: <nil> 187.9 true true
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidBar reports a bar whose shape is impossible: a partly filled or // zero-priced series, a high or low that does not contain the open and // close, no series at all, a negative volume, a mid that is not the midpoint // of its bid and ask, or an availability time that is zero or before the // bar opened. ErrInvalidBar = errors.New("marketdata: invalid bar") // ErrCrossed reports a bar whose ask is below its bid at some corner (open, // high, low or close): a market anomaly, not a shape error. ErrCrossed = errors.New("marketdata: crossed bid/ask") )
Errors, classifiable with errors.Is.
var ErrInvalidSeries = errors.New("marketdata: invalid series")
ErrInvalidSeries reports a Series that is not what its doc says: unnamed, a bar of another instrument, an invalid bar, or bars not strictly ascending.
var ErrInvalidSource = errors.New("marketdata: invalid source partition")
ErrInvalidSource reports a SourcePartition that does not describe a partition: a missing name or fingerprint, a month outside 1 to 12, or row numbers that are not positive and ascending.
Functions ¶
This section is empty.
Types ¶
type Bar ¶
type Bar struct {
// Instrument names what the bar is of: the symbol as spelled in the data
// path (for example "EURUSD"). Readers set it from the file's key; it is
// never written as a column of a row, so a stored file is unchanged by it.
// It lets frames, merging and orders carry the instrument without a wrapper
// type. Validate does not require it: storage and domain code may handle a
// bar before a reader has named it, and the engine checks it where it needs
// it. A file's key is the one source of truth: a bar whose Instrument is set
// and differs from its file's key is refused.
Instrument string
// Time is the bar's opening instant.
Time time.Time
// AvailableAt is the earliest time the completed bar may be consumed. The
// zero value means availability has not been established, which a stored
// bar never has. It is modeled under a named policy (canonical's
// PolicyScheduledCloseV1), not an observed provider timestamp.
AvailableAt time.Time
// Volume is the provider's activity (tick) count, not a tradable quantity.
Volume int64
Bid OHLC
Ask OHLC
// Mid is the midpoint series. When Bid and Ask are both present it is
// exactly MidOf(Bid, Ask); Validate enforces that.
Mid OHLC
// Trade is the series of traded prices, for sources that give trades. It is
// part of the API model; whether and how it is persisted is a separate
// decision (canonical-v2 stores bid, ask and mid only).
Trade OHLC
}
Bar is one observation of an instrument over one interval: up to four price series, the provider's activity count, when the interval opened and the earliest moment a consumer may use the bar. It is the one bar model shared by storage, backtests, studies, strategies and external consumers (ADR-091).
Every field is read directly. Each price series is optional: an absent series is the all-zero OHLC, and at least one must be present. A source that quotes both sides gives Bid and Ask and Mid is derived from them once, with MidOf; a source that gives a single price writes it to the series it actually is and leaves the others absent. Nothing infers a missing side: code that needs the bid or the ask reads that field and checks IsZero if absence matters.
func (Bar) IsAvailable ¶ added in v0.5.0
IsAvailable reports whether the bar may be consumed at the given time.
func (Bar) IsComplete ¶ added in v0.5.0
IsComplete reports whether availability has been established, as the owner's design for the bar defines completeness (#619).
func (Bar) OHLC ¶ added in v0.5.0
OHLC returns the preferred available price series: Mid, then Trade, then Bid, then Ask, and the zero OHLC when the bar has none. It is for code that wants "the price" and does not care which basis; a caller that needs a specific basis reads that field, and execution that needs the bid or the ask must check those fields itself, since this never substitutes one side for the other silently when a different series is present.
func (Bar) Validate ¶
Validate reports ErrInvalidBar or ErrCrossed (wrapped, with what is wrong) unless the bar is a possible observation. It does not check alignment to an interval or that AvailableAt follows a policy; those depend on the interval and the policy, and belong to the storage that applies them. The trade series has no relation to bid and ask, so only its own shape is checked.
type OHLC ¶ added in v0.5.0
OHLC is open, high, low and close of one price series: the bid, the ask, their midpoint, or trades. The all-zero OHLC means the series is not available; a series that is present has all four prices positive, and a partly filled one is invalid.
func MidOf ¶ added in v0.5.0
MidOf derives the midpoint series from bid and ask: the exact midpoint at each of open, high, low and close. Half a pip is representable, so nothing is rounded for prices with up to seven decimals. Open and close are preserved on both sides of a bar, so the midpoint is always recomputable from what is stored.
The high of the result is the midpoint of the bid high and the ask high, which are the values of the provider's two candles; it need not equal the high of a tick-by-tick mid series.
type Series ¶ added in v0.5.0
type Series struct {
// Source is the provider the bars came from (for example "oanda").
Source string
// Instrument is the symbol as spelled in the data path (for example "EURUSD").
Instrument string
// Interval is the lower-case interval token, such as "h1" or "d1".
Interval string
// Bars are in time order.
Bars []Bar
}
Series is one instrument's bars at one interval from one source, in time order: what a consumer replays. It is the shared container that readers return and the engine consumes, so no consumer needs a type of its own for it.
The bars are strictly ascending by opening Time and strictly ascending by AvailableAt, which is what delivering several instruments in availability order needs. Each carries the series' Instrument. A series may have gaps (a weekend, a month the caller allowed to be missing): nothing is filled, interpolated or carried forward.
type SourcePartition ¶ added in v0.5.0
type SourcePartition struct {
// Source names the provider, as in the path (for example "oanda").
Source string
// Instrument is the provider's symbol as spelled in the path (for example
// "EURUSD").
Instrument string
// Interval is the lower-case interval token, such as "h1" or "d1".
Interval string
Year int
Month time.Month
// Fingerprint identifies the exact raw bytes the rows were read from
// ("sha256:<hex>").
Fingerprint string
// Rows are the partition's rows in raw-file order.
Rows []SourceRow
}
SourcePartition is one provider raw partition (one month, one instrument, one interval) handed to the market-data service for conversion to canonical bars. It carries what a bare []Bar would lose: the raw row numbers, the provider's complete flag and the fingerprint of the raw bytes the rows were read from.
A provider builds it by parsing and mapping only. It sets no AvailableAt, does no validation and applies no domain rule; a row it cannot parse at all is an error from the provider, not a row here.
func (SourcePartition) Validate ¶ added in v0.5.0
func (p SourcePartition) Validate() error
Validate reports ErrInvalidSource unless p names a partition and its rows carry positive, strictly ascending row numbers and an instrument that is empty or the partition's. It looks at nothing about the prices or times: that is the market-data service's job.
type SourceRow ¶ added in v0.5.0
type SourceRow struct {
// Row is the row's position in the raw file: 1-based, counting data rows
// only (the schema comment and column header are not counted).
Row int
// Bar holds Time, Volume, Bid and Ask as the provider gave them. Its
// Instrument is the partition's, or empty.
Bar Bar
// Complete is the provider's own flag that the candle had closed.
Complete bool
}
SourceRow is one parseable row of a provider's raw partition, as the provider gave it. Bar is UNVALIDATED: its prices are exactly the provider's, Mid is not derived and AvailableAt is unset. Whether the row becomes a canonical bar or a bad record is the market-data service's decision, never the provider's, so a provider passes every row it can parse through, good or not.
Directories
¶
| Path | Synopsis |
|---|---|
|
internal
|
|
|
canonical
Package canonical is Trader's canonical bar store: the canonical-v2 file that holds one month of marketdata.Bar values, and the alignment and availability rule for the H1 and D1 intervals (ADR-090).
|
Package canonical is Trader's canonical bar store: the canonical-v2 file that holds one month of marketdata.Bar values, and the alignment and availability rule for the H1 and D1 intervals (ADR-090). |
|
Package service is the market-data domain service (ADR-091).
|
Package service is the market-data domain service (ADR-091). |