Documentation
¶
Overview ¶
Package trade provides a unified data model for aggregating market data across multiple exchanges and data feed providers.
It defines exchange-agnostic types for candles, time-and-sale transactions, order books, orders, price clusters, and market instruments. These models serve as the common schema for normalizing data from Binance, CME, NASDAQ, and other exchanges into a single queryable format.
The currency sub-package provides embedded fiat and cryptocurrency reference data with 170+ fiat currencies and 60+ crypto tokens.
Architecture ¶
Data flows from exchange-specific connectors through these shared types:
Exchange A ──┐ Exchange B ──┼── Connector → trade.TimeAndSale / trade.Candle → Storage Exchange C ──┘
This decouples storage and analysis from exchange-specific wire formats.
Index ¶
- type AggressorSide
- type AskBid
- type AskBidPrice
- type Candle
- type CandleDeltaLevels
- type DataFeedProvider
- type DateTime
- type Exchange
- type Instrument
- type InstrumentType
- type Market
- type Order
- type OrderBook
- type OrderBookEntry
- type PriceClusters
- type Sale
- type Symbol
- type SymbolType
- type Symbols
- type Temperatures
- type TimeAndSale
- type UUID
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AggressorSide ¶
type AggressorSide int
AggressorSide indicates which side initiated a trade.
const ( AggressorNone AggressorSide = iota // No side determined AggressorSell // Seller initiated AggressorBuy // Buyer initiated AggressorUnknown // Side could not be determined )
func (AggressorSide) String ¶
func (a AggressorSide) String() string
String returns a human-readable representation.
type AskBidPrice ¶
type AskBidPrice struct {
Price float64 `json:"price"` // Trade price
AskBid AskBid `json:"askBid"` // Ask/bid counts
Duration time.Time `json:"duration"` // Time delta between trades
TradesCount int32 `json:"tradesCount"` // Number of events in the deal
}
AskBidPrice represents aggregated trade details within a candlestick.
type Candle ¶
type Candle struct {
Empty bool `json:"empty,omitempty"`
// Time
TimeOpen time.Time `json:"timeOpen,omitempty"`
TimeClose time.Time `json:"timeClose,omitempty"`
// Price
Open float64 `json:"open,omitempty"`
High float64 `json:"high,omitempty"`
Low float64 `json:"low,omitempty"`
Close float64 `json:"close,omitempty"`
Ask float64 `json:"ask,omitempty"`
Bid float64 `json:"bid,omitempty"`
TradesCount float64 `json:"tradesCount,omitempty"`
PriceClusters map[string]PriceClusters `json:"priceClusters,omitempty"`
DeltaLevels *CandleDeltaLevels `json:"deltaLevels,omitempty"`
VolumeLevels *CandleDeltaLevels `json:"volumeLevels,omitempty"`
PriceSnake []float64 `json:"priceSnake,omitempty"`
}
Candle represents an OHLC candlestick with extended market microstructure data.
type CandleDeltaLevels ¶
type CandleDeltaLevels struct {
MinDeltaValue float64 `json:"minDeltaValue,omitempty"`
MinDeltaPrice float64 `json:"minDeltaPrice,omitempty"`
MaxDeltaValue float64 `json:"maxDeltaValue,omitempty"`
MaxDeltaPrice float64 `json:"maxDeltaPrice,omitempty"`
}
CandleDeltaLevels holds min/max delta values and their price levels within a candle, used for volume profile and delta analysis.
type DataFeedProvider ¶
type DataFeedProvider struct {
ID int `json:"id"`
Name string `json:"name"`
URN string `json:"urn"`
}
DataFeedProvider identifies a data feed source.
type DateTime ¶
DateTime wraps time.Time with custom JSON unmarshaling using the "2006-01-02 15:04:05" format common in exchange APIs.
func (*DateTime) UnmarshalJSON ¶
UnmarshalJSON parses a date-time string in "2006-01-02 15:04:05" format.
type Instrument ¶
type Instrument struct {
ID int `json:"id"`
Type InstrumentType `json:"type"`
Ticker string `json:"ticker"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
ExchangeID int `json:"exchangeId,omitempty"`
DataFeedProviderID int `json:"dataFeedProviderId,omitempty"`
}
Instrument represents a tradable instrument (e.g. BTCUSDT, ES futures, EUR/USD).
type InstrumentType ¶
type InstrumentType string
InstrumentType classifies a trading instrument.
const ( InstrumentSpot InstrumentType = "spot" InstrumentFuture InstrumentType = "future" InstrumentOption InstrumentType = "option" InstrumentFX InstrumentType = "fx" )
type Market ¶
type Market struct {
ID int `json:"id"`
FromSymbol string `json:"fromSymbol"` // Base (e.g. BTC)
ToSymbol string `json:"toSymbol"` // Quote (e.g. USDT)
Title string `json:"title"`
Description string `json:"description,omitempty"`
}
Market represents a trading pair with base and quote symbols.
type Order ¶
type Order struct {
OrderID uuid.UUID `mapstructure:"order_id" json:"orderId"`
CustomerID int64 `mapstructure:"customer_id" json:"customerId"`
Status string `mapstructure:"order_status" json:"status"`
ApprovedAt *time.Time `mapstructure:"order_approved_at" json:"approvedAt,omitempty"`
DeliveredToCustomerAt *time.Time `mapstructure:"order_delivered_customer_date" json:"deliveredToCustomerAt,omitempty"`
DeliveredToCarrierAt *time.Time `mapstructure:"order_delivered_carrier_date" json:"deliveredToCarrierAt,omitempty"`
DeliveryEstimatedAt *time.Time `mapstructure:"order_estimated_delivery_date" json:"deliveryEstimatedAt,omitempty"`
PurchaseAt *time.Time `mapstructure:"order_purchase_timestamp" json:"purchaseAt,omitempty"`
}
Order represents a trading order with custom JSON unmarshaling that handles string-encoded numeric fields common in exchange APIs.
func (*Order) UnmarshalJSON ¶
UnmarshalJSON handles string-to-native type conversions for exchange APIs that encode numeric and date fields as strings.
type OrderBookEntry ¶
type OrderBookEntry struct {
Ticker string `json:"ticker"`
ExchangeID int64 `json:"exchangeId"`
Time time.Time `json:"time"`
BestBid Sale `json:"bestBid"`
BestAsk Sale `json:"bestAsk"`
}
OrderBookEntry represents a single level in an order book snapshot.
type PriceClusters ¶
type PriceClusters struct {
Ask float64 `json:"ask,omitempty"` // Volume at the ask
Bid float64 `json:"bid,omitempty"` // Volume at the bid
Duration float64 `json:"durationMilli,omitempty"` // Time at this level (ms)
Trades float64 `json:"trades,omitempty"` // Number of trades
}
PriceClusters describes volume and time distribution at a price level.
type Sale ¶
type Sale struct {
Price float64 `json:"price"` // Trade price
AggressorSide AggressorSide `json:"aggressorSide"` // Buy or sell initiated
Volume int `json:"volume"` // Quantity of contracts/shares
}
Sale holds the core trade data: price, direction, and volume.
type Symbol ¶ added in v0.2.0
type Symbol struct {
ID uint `json:"id"` // Unique identifier
Type SymbolType `json:"type"` // Fiat or crypto
ParentID uint `json:"parentId,omitempty"` // Parent symbol ID (0 = root)
Code string `json:"code"` // Ticker code (e.g. "BTC", "USD")
Name string `json:"name"` // Full name (e.g. "Bitcoin")
Description string `json:"description,omitempty"` // Optional description
Website string `json:"website,omitempty"` // Project/issuer website
}
Symbol represents a market trading symbol (e.g. BTC, USDT, EUR).
Symbols form a tree structure through ParentID, enabling hierarchical relationships between base assets and their derivatives:
USD (id:1, parent:0, fiat) ├── USDT (id:2, parent:1, crypto) — stablecoin pegged to USD ├── USDC (id:3, parent:1, crypto) — stablecoin pegged to USD ├── EUR (id:4, parent:1, fiat) └── GBP (id:5, parent:1, fiat) BTC (id:14, parent:0, crypto) ├── BTCFT (id:22, parent:14, crypto) — BTC futures token └── BTCM24 (id:23, parent:14, crypto) — BTC June 2024 future
This tree allows grouping derivatives under their base asset and resolving instrument relationships across exchanges.
type SymbolType ¶ added in v0.2.0
type SymbolType int8
SymbolType classifies a trading symbol as fiat or cryptocurrency.
const ( SymbolFiat SymbolType = iota // Fiat currency (USD, EUR, GBP, ...) SymbolCrypto // Cryptocurrency (BTC, ETH, SOL, ...) )
func (SymbolType) String ¶ added in v0.2.0
func (s SymbolType) String() string
String returns a human-readable label for the symbol type.
type Symbols ¶ added in v0.2.0
type Symbols []Symbol
Symbols is a collection of Symbol values with lookup helpers.
func (Symbols) Children ¶ added in v0.2.0
Children returns all symbols whose ParentID matches the given ID.
func (Symbols) GetByCode ¶ added in v0.2.0
GetByCode returns the first symbol matching the given code, or nil.
type Temperatures ¶
type Temperatures struct {
AskBid AskBid `json:"askBid"`
TimeDurationMS int64 `json:"timeDurationMs"`
TradesCount int64 `json:"tradesCount"`
}
Temperatures aggregates ask/bid counts with timing data.
func UnmarshalTemperatures ¶
func UnmarshalTemperatures(data []byte) (Temperatures, error)
UnmarshalTemperatures parses JSON into a Temperatures value.
func (*Temperatures) Marshal ¶
func (r *Temperatures) Marshal() ([]byte, error)
Marshal serializes Temperatures to JSON.
type TimeAndSale ¶
type TimeAndSale struct {
InternalID UUID // Internal transaction ID
ID string // Exchange transaction ID / Trade ID
ExchangeID int64 // Exchange identifier
DataFeedProviderID int64 // Data feed provider identifier
TradeSequence int64 // Sequence number for same-timestamp trades
TradeOpenInterest int64 // Open interest at time of trade
Ticker string // Market trading symbol (e.g. "BTCUSDT", "ES", "NQ")
Time time.Time // Trade timestamp
Sale
}
TimeAndSale represents an atomic trade transaction captured from an exchange. This is the fundamental unit of market data — each instance records a single trade event with its price, volume, aggressor side, and timing.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package currency provides embedded fiat and cryptocurrency reference data with 170+ fiat currencies and 60+ crypto tokens, loaded from a bundled YAML file.
|
Package currency provides embedded fiat and cryptocurrency reference data with 170+ fiat currencies and 60+ crypto tokens, loaded from a bundled YAML file. |