cache

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 22, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultTpnlCapacity uint64 = 4096

Variables

This section is empty.

Functions

func TpnlCacheKey

func TpnlCacheKey(accountID int, windowNs uint64) string

TpnlCacheKey builds the cache key that links a TPNL actor to its checker.

Types

type Cache

type Cache struct {
	// contains filtered or unexported fields
}

Cache maintains a self-contained data store for strategies to read. Engines write data into the cache; strategies read from it. Cache has no dependency on any engine package.

func NewCache

func NewCache() *Cache

NewCache creates a new empty Cache.

func (*Cache) ApplyBookUpdate

func (c *Cache) ApplyBookUpdate(symbolID int, bids, asks []common.PriceLevel)

ApplyBookUpdate applies incremental depth updates to the orderbook for a symbol. Unlike UpdateBook, this does NOT clear the existing book; it applies deltas. QuantityTick==0 indicates the level should be deleted (removed from the book).

func (*Cache) DeleteOrder

func (c *Cache) DeleteOrder(clientOrderID int)

DeleteOrder removes an order from the cache.

func (*Cache) GetAccountBalances

func (c *Cache) GetAccountBalances(acctID int) map[int]*common.Balance

GetAccountBalances returns all balances for an account as a map of tokenID to Balance.

func (*Cache) GetAvailable

func (c *Cache) GetAvailable(acctID int, tokenID int) float64

GetAvailable returns the available balance for a specific account and token.

func (*Cache) GetBalance

func (c *Cache) GetBalance(acctID int, tokenID int) *common.Balance

GetBalance returns the balance for a specific account and token.

func (*Cache) GetBestAsk

func (c *Cache) GetBestAsk(symbolID int) (price, qty float64, ok bool)

GetBestAsk returns the best ask price and quantity for the given symbol.

func (*Cache) GetBestBid

func (c *Cache) GetBestBid(symbolID int) (price, qty float64, ok bool)

GetBestBid returns the best bid price and quantity for the given symbol.

func (*Cache) GetBookState

func (c *Cache) GetBookState(symbolID int) (common.BookState, bool)

GetBookState returns the state of the orderbook for a symbol.

func (*Cache) GetDepth

func (c *Cache) GetDepth(symbolID int, levels int) (bids, asks []common.PriceLevel)

GetDepth returns the top N levels of bids and asks for the given symbol.

func (*Cache) GetLocked

func (c *Cache) GetLocked(acctID int, tokenID int) float64

GetLocked returns the locked balance for a specific account and token.

func (*Cache) GetMidPrice

func (c *Cache) GetMidPrice(symbolID int) (price float64, ok bool)

GetMidPrice returns the mid price for the given symbol.

func (*Cache) GetOpenOrder

func (c *Cache) GetOpenOrder(acctID int, clientOrderID int) (common.Order, bool)

GetOpenOrder returns an open order by account ID and client order ID.

func (*Cache) GetOpenOrdersByAccount

func (c *Cache) GetOpenOrdersByAccount(acctID int) []*common.Order

GetOpenOrdersByAccount returns all open orders for an account.

func (*Cache) GetOpenOrdersBySymbol

func (c *Cache) GetOpenOrdersBySymbol(acctID int, symbolID int) []*common.Order

GetOpenOrdersBySymbol returns all open orders for an account and symbol.

func (*Cache) GetOrCreateTpnlState

func (c *Cache) GetOrCreateTpnlState(key string, windowNs, capacity uint64) *TpnlState

GetOrCreateTpnlState returns the TpnlState for the given cache key, creating it with the given parameters if it doesn't exist yet.

func (*Cache) GetOrder

func (c *Cache) GetOrder(clientOrderID int) (common.Order, bool)

GetOrder returns an order by client order ID, or nil if not found.

func (*Cache) GetRiskMeta

func (c *Cache) GetRiskMeta(accountID int) (*RiskMeta, bool)

GetRiskMeta returns the risk metadata for the given account ID. Use accountID -1 for the global bucket.

func (*Cache) GetRiskNextAcceptedTime

func (c *Cache) GetRiskNextAcceptedTime(accountID int) uint64

GetRiskNextAcceptedTime returns the next accepted time (unix nanos) for the given account, or 0 if no metadata exists yet.

func (*Cache) GetSpread

func (c *Cache) GetSpread(symbolID int) (spread float64, ok bool)

GetSpread returns the bid-ask spread for the given symbol.

func (*Cache) GetTotal

func (c *Cache) GetTotal(acctID int, tokenID int) float64

GetTotal returns the total balance for a specific account and token.

func (*Cache) GetTpnlState

func (c *Cache) GetTpnlState(key string) *TpnlState

GetTpnlState returns the TpnlState for the given cache key, or nil.

func (*Cache) HasSufficientBalance

func (c *Cache) HasSufficientBalance(acctID int, tokenID int, amount float64) bool

HasSufficientBalance checks if an account has sufficient available balance.

func (*Cache) InsertOrder

func (c *Cache) InsertOrder(order *common.Order)

InsertOrder inserts a new order into the cache.

func (*Cache) IsSymbolReady

func (c *Cache) IsSymbolReady(symbolID int) bool

IsSymbolReady returns true if the orderbook for a symbol is ready.

func (*Cache) OpenOrderCount

func (c *Cache) OpenOrderCount(acctID int) int

OpenOrderCount returns the number of open orders for an account.

func (*Cache) SetAccountBalances

func (c *Cache) SetAccountBalances(acctID int, balances []common.Balance)

SetAccountBalances replaces all balances for an account.

func (*Cache) SetBalance

func (c *Cache) SetBalance(acctID int, tokenID int, available, locked, total float64)

SetBalance sets the balance for a specific account and token.

func (*Cache) SetBookState

func (c *Cache) SetBookState(symbolID int, state common.BookState)

SetBookState sets the synchronization state of the orderbook for a symbol.

func (*Cache) SetRiskNextAcceptedTime

func (c *Cache) SetRiskNextAcceptedTime(accountID int, t uint64)

SetRiskNextAcceptedTime atomically sets the next accepted time for the given account. The RiskMeta entry is lazily created if it doesn't exist.

func (*Cache) UpdateBook

func (c *Cache) UpdateBook(symbolID int, bids, asks []common.PriceLevel)

UpdateBook updates the orderbook depth for a symbol. bids and asks should be sorted (best first). QuantityTick==0 indicates the level should be deleted (removed from the book).

func (*Cache) UpdateOrder

func (c *Cache) UpdateOrder(order *common.Order)

UpdateOrder updates an existing order and maintains the open-orders index.

type OrderBook

type OrderBook struct {
	// contains filtered or unexported fields
}

OrderBook holds the orderbook state for a single symbol. Uses PriceTick as btree key; QuantityTick==0 indicates a deleted level.

type OrderCache

type OrderCache struct {
	// contains filtered or unexported fields
}

func NewOrderCache

func NewOrderCache() *OrderCache

func (*OrderCache) DeleteOrder

func (oc *OrderCache) DeleteOrder(clientOrderID int)

func (*OrderCache) GetOpenOrders

func (oc *OrderCache) GetOpenOrders(accountID int) []*common.Order

func (*OrderCache) GetOrder

func (oc *OrderCache) GetOrder(clientOrderID int) (common.Order, bool)

func (*OrderCache) InsertOrder

func (oc *OrderCache) InsertOrder(order *common.Order)

func (*OrderCache) UpdateOrder

func (oc *OrderCache) UpdateOrder(order *common.Order)

type RiskMeta

type RiskMeta struct {
	NextAcceptedTime atomic.Uint64 // unix nanoseconds
}

RiskMeta holds risk-related metadata for an account (or global key -1).

type TpnlState

type TpnlState struct {
	// contains filtered or unexported fields
}

TpnlState holds the shared mutable state for a TPNL (account, window) pair. The TPNL actor adds trades and purges on executions; the checker purges on risk checks and reads exposures. All access is from the single dispatch thread.

func NewTpnlState

func NewTpnlState(windowNs, capacity uint64) *TpnlState

NewTpnlState creates a new TpnlState with the given window and ring buffer capacity.

func (*TpnlState) AddTrade

func (s *TpnlState) AddTrade(rec TpnlTradeRecord) bool

AddTrade appends a trade to the ring buffer and applies it to exposure. Returns true on success. If the buffer is full, the oldest trade is force-evicted and the method returns false.

func (*TpnlState) AdvanceClock

func (s *TpnlState) AdvanceClock(ts uint64)

AdvanceClock moves the internal clock forward to ts if ts > current.

func (*TpnlState) Capacity

func (s *TpnlState) Capacity() uint64

func (*TpnlState) Exposures

func (s *TpnlState) Exposures() []TpnlSymbolExposure

Exposures returns a snapshot of all non-zero per-symbol exposures.

func (*TpnlState) GetExposure

func (s *TpnlState) GetExposure(symbolID int) (baseQty, quoteChange float64)

GetExposure returns the exposure for a specific symbol.

func (*TpnlState) NowNs

func (s *TpnlState) NowNs() uint64

func (*TpnlState) Purge

func (s *TpnlState) Purge() int

Purge removes expired trades from the buffer head and reverts their effect on exposure. Returns the number of trades purged.

func (*TpnlState) SetTokens

func (s *TpnlState) SetTokens(symbolID, baseTokenID, quoteTokenID int)

SetTokens registers the base/quote token IDs for a symbol (idempotent).

func (*TpnlState) TradeCount

func (s *TpnlState) TradeCount() uint64

type TpnlSymbolExposure

type TpnlSymbolExposure struct {
	SymbolID    int
	BaseQty     float64
	QuoteChange float64
}

TpnlSymbolExposure holds the running base and quote exposure for a single symbol within a TPNL window.

type TpnlTokenPair

type TpnlTokenPair struct {
	BaseTokenID  int
	QuoteTokenID int
}

TpnlTokenPair caches the base/quote token IDs for a symbol.

type TpnlTradeRecord

type TpnlTradeRecord struct {
	SymbolID int
	Side     common.Side
	Qty      float64
	Price    float64
	FeeCcyID int
	FeeQty   float64
	FilledAt uint64
}

TpnlTradeRecord stores a single fill in the sliding window.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL