Files

271 lines
7.1 KiB
Go

package main
// Tick represents a single trade event persisted into hot storage.
type Tick struct {
// Exchange identifiers
TradeID string
Seq int64
// Timing
TradeTS int64 // Bybit trade timestamp (T)
MessageTS int64
RecvTS int64 // Local receive timestamp
// Trade data
Symbol string
Side string
Price float64
Volume float64
// Exchange metadata
TickDir string // L
BlockTrade bool // BT
RPI bool // RPI
}
// FeatureBucket holds aggregated 5-second feature data.
type FeatureBucket struct {
Timestamp int64
LogReturn float64
RealizedVol float64
OFI float64
VolumeSum float64
ClosePrice float64
VWAP float64
}
// Top-level websocket message (used for topic extraction and trade parsing).
type BybitWSMessage struct {
Topic string `json:"topic"`
Type string `json:"type"`
TS int64 `json:"ts"`
Data []BybitTradeRaw `json:"data"`
}
// Raw trade from Bybit publicTrade stream.
type BybitTradeRaw struct {
T int64 `json:"T"`
S string `json:"s"`
SD string `json:"S"`
V string `json:"v"`
P string `json:"p"`
L string `json:"L"`
I string `json:"i"`
BT bool `json:"BT"`
RPI bool `json:"RPI"`
Seq int64 `json:"seq"`
}
// --- MessageHandler interface for multi-stream support ---
// MessageHandler is implemented by each stream handler (trades, ticker, klines, etc.)
type MessageHandler interface {
// Topics returns the list of Bybit WebSocket topics this handler subscribes to.
Topics() []string
// HandleMessage processes a raw WebSocket message routed by topic.
HandleMessage(data []byte)
// Close gracefully shuts down the handler, flushing any pending data.
Close()
}
// --- Ticker types ---
// BybitTickerMessage is the WebSocket response for the tickers stream.
type BybitTickerMessage struct {
Topic string `json:"topic"`
Type string `json:"type"` // "snapshot" or "delta"
TS int64 `json:"ts"`
CS int64 `json:"cs"`
Data BybitTickerRaw `json:"data"`
}
// BybitTickerRaw holds the fields from a Bybit ticker push (linear/inverse).
type BybitTickerRaw struct {
Symbol string `json:"symbol"`
LastPrice string `json:"lastPrice"`
Bid1Price string `json:"bid1Price"`
Bid1Size string `json:"bid1Size"`
Ask1Price string `json:"ask1Price"`
Ask1Size string `json:"ask1Size"`
HighPrice24h string `json:"highPrice24h"`
LowPrice24h string `json:"lowPrice24h"`
Volume24h string `json:"volume24h"`
Turnover24h string `json:"turnover24h"`
MarkPrice string `json:"markPrice"`
IndexPrice string `json:"indexPrice"`
OpenInterest string `json:"openInterest"`
FundingRate string `json:"fundingRate"`
NextFundingTime string `json:"nextFundingTime"`
Price24hPcnt string `json:"price24hPcnt"`
}
// TickerSnapshot is a parsed ticker snapshot row for hot DB storage.
type TickerSnapshot struct {
Timestamp int64
LastPrice float64
Bid1Price float64
Bid1Size float64
Ask1Price float64
Ask1Size float64
MarkPrice float64
IndexPrice float64
OpenInterest float64
FundingRate float64
Volume24h float64
Turnover24h float64
}
// TickerFeature holds derived features computed from ticker snapshots.
type TickerFeature struct {
Timestamp int64
Spread float64
SpreadBps float64
MidPrice float64
OIChange float64
FundingRate float64
MarkIndexBasis float64
BidAskImbalance float64
}
// --- Kline types ---
// BybitKlineMessage is the WebSocket response for the kline stream.
type BybitKlineMessage struct {
Topic string `json:"topic"`
Type string `json:"type"`
TS int64 `json:"ts"`
Data []BybitKlineRaw `json:"data"`
}
// BybitKlineRaw holds the fields from a single kline (candle) push.
type BybitKlineRaw struct {
Start int64 `json:"start"`
End int64 `json:"end"`
Interval string `json:"interval"`
Open string `json:"open"`
Close string `json:"close"`
High string `json:"high"`
Low string `json:"low"`
Volume string `json:"volume"`
Turnover string `json:"turnover"`
Confirm bool `json:"confirm"`
Timestamp int64 `json:"timestamp"`
}
// Kline is a parsed kline row for hot DB storage.
type Kline struct {
StartTime int64
EndTime int64
Interval string
Open float64
High float64
Low float64
Close float64
Volume float64
Turnover float64
Confirmed bool
}
// KlineFeature holds derived features computed from kline data.
type KlineFeature struct {
Timestamp int64
Interval string
BodyRatio float64
UpperWick float64
LowerWick float64
LogReturn float64
Volume float64
Turnover float64
}
// --- Orderbook types ---
// BybitOrderbookMessage is the WebSocket response for the orderbook stream.
type BybitOrderbookMessage struct {
Topic string `json:"topic"`
Type string `json:"type"` // "snapshot" or "delta"
TS int64 `json:"ts"`
Data BybitOrderbookData `json:"data"`
}
// BybitOrderbookData holds the bids/asks arrays from an orderbook push.
type BybitOrderbookData struct {
S string `json:"s"` // Symbol
B [][]string `json:"b"` // Bids: [[price, size], ...]
A [][]string `json:"a"` // Asks: [[price, size], ...]
U int64 `json:"u"` // Update ID
Seq int64 `json:"seq"`
}
// OrderbookLevel represents a single price level in the order book.
type OrderbookLevel struct {
Price float64
Size float64
}
// OrderbookFeature holds derived features computed from orderbook snapshots.
type OrderbookFeature struct {
Timestamp int64
Spread float64
MidPrice float64
BidDepth5 float64
AskDepth5 float64
BidDepth20 float64
AskDepth20 float64
DepthImbalance5 float64
DepthImbalance20 float64
WeightedMid float64
VWAP10 float64
}
// --- Liquidation types ---
// BybitLiquidationMessage is the WebSocket response for the allLiquidation stream.
type BybitLiquidationMessage struct {
Topic string `json:"topic"`
Type string `json:"type"`
TS int64 `json:"ts"`
Data BybitLiquidationData `json:"data"`
}
// BybitLiquidationData holds the fields from a single liquidation event.
type BybitLiquidationData struct {
T int64 `json:"T"` // Timestamp ms
S string `json:"s"` // Symbol
SD string `json:"S"` // Side: "Buy" (long liq) or "Sell" (short liq)
V string `json:"v"` // Quantity
P string `json:"p"` // Bankruptcy price
}
// Liquidation is a parsed liquidation event for hot DB storage.
type Liquidation struct {
Timestamp int64
Side string
Price float64
Quantity float64
Value float64 // price * quantity
}
// LiquidationFeature holds aggregated liquidation features for a 5-second bucket.
type LiquidationFeature struct {
Timestamp int64
CountTotal int
CountLong int
CountShort int
VolumeTotal float64
VolumeLong float64
VolumeShort float64
ValueTotal float64
ValueLong float64
ValueShort float64
AvgPrice float64
NetValue float64
}