Documentation
¶
Index ¶
- Constants
- func TpnlCacheKey(accountID int, windowNs uint64) string
- type Cache
- func (c *Cache) ApplyBookUpdate(symbolID int, bids, asks []common.PriceLevel)
- func (c *Cache) DeleteOrder(clientOrderID int)
- func (c *Cache) GetAccountBalances(acctID int) map[int]*common.Balance
- func (c *Cache) GetAvailable(acctID int, tokenID int) float64
- func (c *Cache) GetBalance(acctID int, tokenID int) *common.Balance
- func (c *Cache) GetBestAsk(symbolID int) (price, qty float64, ok bool)
- func (c *Cache) GetBestBid(symbolID int) (price, qty float64, ok bool)
- func (c *Cache) GetBookState(symbolID int) (common.BookState, bool)
- func (c *Cache) GetDepth(symbolID int, levels int) (bids, asks []common.PriceLevel)
- func (c *Cache) GetLocked(acctID int, tokenID int) float64
- func (c *Cache) GetMidPrice(symbolID int) (price float64, ok bool)
- func (c *Cache) GetOpenOrder(acctID int, clientOrderID int) (common.Order, bool)
- func (c *Cache) GetOpenOrdersByAccount(acctID int) []*common.Order
- func (c *Cache) GetOpenOrdersBySymbol(acctID int, symbolID int) []*common.Order
- func (c *Cache) GetOrCreateTpnlState(key string, windowNs, capacity uint64) *TpnlState
- func (c *Cache) GetOrder(clientOrderID int) (common.Order, bool)
- func (c *Cache) GetRiskMeta(accountID int) (*RiskMeta, bool)
- func (c *Cache) GetRiskNextAcceptedTime(accountID int) uint64
- func (c *Cache) GetSpread(symbolID int) (spread float64, ok bool)
- func (c *Cache) GetTotal(acctID int, tokenID int) float64
- func (c *Cache) GetTpnlState(key string) *TpnlState
- func (c *Cache) HasSufficientBalance(acctID int, tokenID int, amount float64) bool
- func (c *Cache) InsertOrder(order *common.Order)
- func (c *Cache) IsSymbolReady(symbolID int) bool
- func (c *Cache) OpenOrderCount(acctID int) int
- func (c *Cache) SetAccountBalances(acctID int, balances []common.Balance)
- func (c *Cache) SetBalance(acctID int, tokenID int, available, locked, total float64)
- func (c *Cache) SetBookState(symbolID int, state common.BookState)
- func (c *Cache) SetRiskNextAcceptedTime(accountID int, t uint64)
- func (c *Cache) UpdateBook(symbolID int, bids, asks []common.PriceLevel)
- func (c *Cache) UpdateOrder(order *common.Order)
- type OrderBook
- type OrderCache
- func (oc *OrderCache) DeleteOrder(clientOrderID int)
- func (oc *OrderCache) GetOpenOrders(accountID int) []*common.Order
- func (oc *OrderCache) GetOrder(clientOrderID int) (common.Order, bool)
- func (oc *OrderCache) InsertOrder(order *common.Order)
- func (oc *OrderCache) UpdateOrder(order *common.Order)
- type RiskMeta
- type TpnlState
- func (s *TpnlState) AddTrade(rec TpnlTradeRecord) bool
- func (s *TpnlState) AdvanceClock(ts uint64)
- func (s *TpnlState) Capacity() uint64
- func (s *TpnlState) Exposures() []TpnlSymbolExposure
- func (s *TpnlState) GetExposure(symbolID int) (baseQty, quoteChange float64)
- func (s *TpnlState) NowNs() uint64
- func (s *TpnlState) Purge() int
- func (s *TpnlState) SetTokens(symbolID, baseTokenID, quoteTokenID int)
- func (s *TpnlState) TradeCount() uint64
- type TpnlSymbolExposure
- type TpnlTokenPair
- type TpnlTradeRecord
Constants ¶
const DefaultTpnlCapacity uint64 = 4096
Variables ¶
This section is empty.
Functions ¶
func TpnlCacheKey ¶
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 (*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 ¶
DeleteOrder removes an order from the cache.
func (*Cache) GetAccountBalances ¶
GetAccountBalances returns all balances for an account as a map of tokenID to Balance.
func (*Cache) GetAvailable ¶
GetAvailable returns the available balance for a specific account and token.
func (*Cache) GetBalance ¶
GetBalance returns the balance for a specific account and token.
func (*Cache) GetBestAsk ¶
GetBestAsk returns the best ask price and quantity for the given symbol.
func (*Cache) GetBestBid ¶
GetBestBid returns the best bid price and quantity for the given symbol.
func (*Cache) GetBookState ¶
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) GetMidPrice ¶
GetMidPrice returns the mid price for the given symbol.
func (*Cache) GetOpenOrder ¶
GetOpenOrder returns an open order by account ID and client order ID.
func (*Cache) GetOpenOrdersByAccount ¶
GetOpenOrdersByAccount returns all open orders for an account.
func (*Cache) GetOpenOrdersBySymbol ¶
GetOpenOrdersBySymbol returns all open orders for an account and symbol.
func (*Cache) GetOrCreateTpnlState ¶
GetOrCreateTpnlState returns the TpnlState for the given cache key, creating it with the given parameters if it doesn't exist yet.
func (*Cache) GetRiskMeta ¶
GetRiskMeta returns the risk metadata for the given account ID. Use accountID -1 for the global bucket.
func (*Cache) GetRiskNextAcceptedTime ¶
GetRiskNextAcceptedTime returns the next accepted time (unix nanos) for the given account, or 0 if no metadata exists yet.
func (*Cache) GetTpnlState ¶
GetTpnlState returns the TpnlState for the given cache key, or nil.
func (*Cache) HasSufficientBalance ¶
HasSufficientBalance checks if an account has sufficient available balance.
func (*Cache) InsertOrder ¶
InsertOrder inserts a new order into the cache.
func (*Cache) IsSymbolReady ¶
IsSymbolReady returns true if the orderbook for a symbol is ready.
func (*Cache) OpenOrderCount ¶
OpenOrderCount returns the number of open orders for an account.
func (*Cache) SetAccountBalances ¶
SetAccountBalances replaces all balances for an account.
func (*Cache) SetBalance ¶
SetBalance sets the balance for a specific account and token.
func (*Cache) SetBookState ¶
SetBookState sets the synchronization state of the orderbook for a symbol.
func (*Cache) SetRiskNextAcceptedTime ¶
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 ¶
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 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 ¶
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 ¶
AdvanceClock moves the internal clock forward to ts if ts > current.
func (*TpnlState) Exposures ¶
func (s *TpnlState) Exposures() []TpnlSymbolExposure
Exposures returns a snapshot of all non-zero per-symbol exposures.
func (*TpnlState) GetExposure ¶
GetExposure returns the exposure for a specific symbol.
func (*TpnlState) Purge ¶
Purge removes expired trades from the buffer head and reverts their effect on exposure. Returns the number of trades purged.
func (*TpnlState) SetTokens ¶
SetTokens registers the base/quote token IDs for a symbol (idempotent).
func (*TpnlState) TradeCount ¶
type TpnlSymbolExposure ¶
TpnlSymbolExposure holds the running base and quote exposure for a single symbol within a TPNL window.
type TpnlTokenPair ¶
TpnlTokenPair caches the base/quote token IDs for a symbol.