Documentation ¶
Index ¶
- Constants
- type BuyPriceLevel
- type Comparator
- type LevelIndex
- type LevelIter
- type MatchEng
- type MergedPriceLevel
- type OrderBookInterface
- type OrderBookOnBTree
- func (ob *OrderBookOnBTree) GetOverlappedRange(overlapped *[]OverLappedLevel, buyLevels *[]PriceLevel, ...) int
- func (ob *OrderBookOnBTree) InsertOrder(id string, side int8, time int64, price int64, qty int64) (*PriceLevel, error)
- func (ob *OrderBookOnBTree) RemoveOrder(id string, side int8, price int64) (OrderPart, error)
- type OrderBookOnULList
- func (ob *OrderBookOnULList) Clear()
- func (ob *OrderBookOnULList) GetAllLevels() ([]PriceLevel, []PriceLevel)
- func (ob *OrderBookOnULList) GetOrder(id string, side int8, price int64) (OrderPart, error)
- func (ob *OrderBookOnULList) GetOverlappedRange(overlapped *[]OverLappedLevel, buyBuf *[]PriceLevel, sellBuf *[]PriceLevel) int
- func (ob *OrderBookOnULList) GetPriceLevel(price int64, side int8) *PriceLevel
- func (ob *OrderBookOnULList) InsertOrder(id string, side int8, time int64, price int64, qty int64) (*PriceLevel, error)
- func (ob *OrderBookOnULList) InsertPriceLevel(pl *PriceLevel, side int8) error
- func (ob *OrderBookOnULList) RemoveOrder(id string, side int8, price int64) (OrderPart, error)
- func (ob *OrderBookOnULList) RemoveOrders(beforeTime int64, side int8, cb func(OrderPart)) error
- func (ob *OrderBookOnULList) RemoveOrdersBasedOnPriceLevel(expireTime int64, forceExpireTime int64, priceLevelsToReserve int, side int8, ...) error
- func (ob *OrderBookOnULList) RemovePriceLevel(price int64, side int8) int
- func (ob *OrderBookOnULList) ShowDepth(maxLevels int, iterBuy LevelIter, iterSell LevelIter)
- func (ob *OrderBookOnULList) String() string
- func (ob *OrderBookOnULList) UpdateForEachPriceLevel(side int8, updater LevelIter)
- type OrderPart
- type OverLappedLevel
- type PriceLevel
- type PriceLevelInterface
- type SellPriceLevel
- type SurplusIndex
- type TakerSideOrders
- type Trade
- type ULList
- func (ull *ULList) AddPriceLevel(p *PriceLevel) bool
- func (ull *ULList) Clear()
- func (ull *ULList) DeletePriceLevel(price int64) bool
- func (ull *ULList) GetPriceLevel(p int64) *PriceLevel
- func (ull *ULList) GetPriceRange(p1 int64, p2 int64, buffer *[]PriceLevel) []PriceLevel
- func (ull *ULList) GetTop() *PriceLevel
- func (ull *ULList) Iterate(levelNum int, iter LevelIter)
- func (ull *ULList) String() string
- func (ull *ULList) UpdateForEach(updater LevelIter)
Constants ¶
const ( UNKNOWN int8 = 0 BUYSIDE int8 = 1 SELLSIDE int8 = 2 )
const ( Unknown = iota SellTaker BuyTaker BuySurplus SellSurplus Neutral )
Trade status
const PRECISION = 1
PRECISION is the last effective decimal digit of the price of currency pair
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BuyPriceLevel ¶
type BuyPriceLevel struct {
PriceLevel
}
type Comparator ¶
if p1 < p2, return -1, if p1 == p2 return 0, if p1 > p2, return 1.
type LevelIndex ¶
type LevelIndex struct {
// contains filtered or unexported fields
}
type LevelIter ¶
type LevelIter func(priceLevel *PriceLevel, levelIndex int)
type MatchEng ¶
type MatchEng struct { LastMatchHeight int64 Book OrderBookInterface // LotSize may be based on price level, which can be set // before any match() call LotSize int64 // PriceLimit is a percentage use to calculate the range of price // in order to determine the trade price. Though it is saved as int64, // it would be converted into a float when the match engine is created. PriceLimitPct float64 Trades []Trade LastTradePrice int64 // contains filtered or unexported fields }
func NewMatchEng ¶
NewMatchEng constructs a new MatchEng.
func (*MatchEng) DropFilledOrder ¶
DropFilledOrder() would clear the order to remove
func (*MatchEng) MatchBeforeGalileo ¶
Match() return false mean there is orders in the book the current MatchEngine cannot handle. in such case, there should be alerts and all the new orders in this round should be rejected and dropped from order books cancel order should be handled 1st before calling Match(). IOC orders should be handled after Match()
type MergedPriceLevel ¶
type MergedPriceLevel struct {
// contains filtered or unexported fields
}
func NewMergedPriceLevel ¶
func NewMergedPriceLevel(price int64) *MergedPriceLevel
func (*MergedPriceLevel) AddOrder ¶
func (l *MergedPriceLevel) AddOrder(order *OrderPart)
func (*MergedPriceLevel) AddOrders ¶
func (l *MergedPriceLevel) AddOrders(orders []*OrderPart)
type OrderBookInterface ¶
type OrderBookInterface interface { GetOverlappedRange(overlapped *[]OverLappedLevel, buyBuf *[]PriceLevel, sellBuf *[]PriceLevel) int //TODO: especially for ULList, it might be faster by inserting multiple orders in one go then //looping through InsertOrder() one after another. InsertOrder(id string, side int8, time int64, price int64, qty int64) (*PriceLevel, error) InsertPriceLevel(p *PriceLevel, side int8) error GetOrder(id string, side int8, price int64) (OrderPart, error) RemoveOrder(id string, side int8, price int64) (OrderPart, error) RemoveOrders(beforeTime int64, side int8, cb func(OrderPart)) error RemoveOrdersBasedOnPriceLevel(expireTime int64, forceExpireTime int64, priceLevelsToReserve int, side int8, removeCallback func(ord OrderPart)) error UpdateForEachPriceLevel(side int8, updater LevelIter) GetPriceLevel(price int64, side int8) *PriceLevel RemovePriceLevel(price int64, side int8) int ShowDepth(maxLevels int, iterBuy LevelIter, iterSell LevelIter) GetAllLevels() ([]PriceLevel, []PriceLevel) Clear() }
OrderBookInterface is a generic sequenced order to quickly get the spread to match. It can be implemented in different structures but here a fast unrolled-linked list, or/and google/B-Tree are chosen, still need performance benchmark to justify this.
type OrderBookOnBTree ¶
type OrderBookOnBTree struct {
// contains filtered or unexported fields
}
func NewOrderBookOnBTree ¶
func NewOrderBookOnBTree(d int) *OrderBookOnBTree
func (*OrderBookOnBTree) GetOverlappedRange ¶
func (ob *OrderBookOnBTree) GetOverlappedRange(overlapped *[]OverLappedLevel, buyLevels *[]PriceLevel, sellLevels *[]PriceLevel) int
func (*OrderBookOnBTree) InsertOrder ¶
func (ob *OrderBookOnBTree) InsertOrder(id string, side int8, time int64, price int64, qty int64) (*PriceLevel, error)
func (*OrderBookOnBTree) RemoveOrder ¶
type OrderBookOnULList ¶
type OrderBookOnULList struct {
// contains filtered or unexported fields
}
func NewOrderBookOnULList ¶
func NewOrderBookOnULList(capacity int, bucketSize int) *OrderBookOnULList
func (*OrderBookOnULList) Clear ¶
func (ob *OrderBookOnULList) Clear()
func (*OrderBookOnULList) GetAllLevels ¶
func (ob *OrderBookOnULList) GetAllLevels() ([]PriceLevel, []PriceLevel)
func (*OrderBookOnULList) GetOverlappedRange ¶
func (ob *OrderBookOnULList) GetOverlappedRange(overlapped *[]OverLappedLevel, buyBuf *[]PriceLevel, sellBuf *[]PriceLevel) int
func (*OrderBookOnULList) GetPriceLevel ¶
func (ob *OrderBookOnULList) GetPriceLevel(price int64, side int8) *PriceLevel
func (*OrderBookOnULList) InsertOrder ¶
func (ob *OrderBookOnULList) InsertOrder(id string, side int8, time int64, price int64, qty int64) (*PriceLevel, error)
func (*OrderBookOnULList) InsertPriceLevel ¶
func (ob *OrderBookOnULList) InsertPriceLevel(pl *PriceLevel, side int8) error
func (*OrderBookOnULList) RemoveOrder ¶
TODO: InsertOrder and RemoveOrder should be faster if done in batch with multiple orders
func (*OrderBookOnULList) RemoveOrders ¶
func (ob *OrderBookOnULList) RemoveOrders(beforeTime int64, side int8, cb func(OrderPart)) error
func (*OrderBookOnULList) RemoveOrdersBasedOnPriceLevel ¶
func (ob *OrderBookOnULList) RemoveOrdersBasedOnPriceLevel(expireTime int64, forceExpireTime int64, priceLevelsToReserve int, side int8, removeCallback func(ord OrderPart)) error
order beyond priceLevelsToReserve will be expired if it's placed before expireTime. All orders will be expired if they are placed before forceExpireTime
func (*OrderBookOnULList) RemovePriceLevel ¶
func (ob *OrderBookOnULList) RemovePriceLevel(price int64, side int8) int
func (*OrderBookOnULList) ShowDepth ¶
func (ob *OrderBookOnULList) ShowDepth(maxLevels int, iterBuy LevelIter, iterSell LevelIter)
func (*OrderBookOnULList) String ¶
func (ob *OrderBookOnULList) String() string
func (*OrderBookOnULList) UpdateForEachPriceLevel ¶
func (ob *OrderBookOnULList) UpdateForEachPriceLevel(side int8, updater LevelIter)
type OrderPart ¶
type OverLappedLevel ¶
type OverLappedLevel struct { Price int64 BuyOrders []OrderPart SellOrders []OrderPart SellTotal int64 AccumulatedSell int64 BuyTotal int64 AccumulatedBuy int64 AccumulatedExecutions int64 BuySellSurplus int64 BuyTakerStartIdx int SellTakerStartIdx int BuyMakerTotal int64 SellMakerTotal int64 }
func (*OverLappedLevel) HasBuyMaker ¶
func (overlapped *OverLappedLevel) HasBuyMaker() bool
func (*OverLappedLevel) HasBuyTaker ¶
func (overlapped *OverLappedLevel) HasBuyTaker() bool
func (*OverLappedLevel) HasSellMaker ¶
func (overlapped *OverLappedLevel) HasSellMaker() bool
func (*OverLappedLevel) HasSellTaker ¶
func (overlapped *OverLappedLevel) HasSellTaker() bool
type PriceLevel ¶
func (*PriceLevel) String ¶
func (l *PriceLevel) String() string
func (*PriceLevel) TotalLeavesQty ¶
func (l *PriceLevel) TotalLeavesQty() int64
type PriceLevelInterface ¶
type SellPriceLevel ¶
type SellPriceLevel struct {
PriceLevel
}
type SurplusIndex ¶
type SurplusIndex struct { LevelIndex // contains filtered or unexported fields }
type TakerSideOrders ¶
type TakerSideOrders struct {
*MergedPriceLevel
}
type Trade ¶
type Trade struct { Sid string // sell order id LastPx int64 // execution price LastQty int64 // execution quantity BuyCumQty int64 // cumulative executed quantity for the buy order SellCumQty int64 // cumulative executed quantity for the sell order Bid string // buy order Id TickType int8 SellerFee *sdk.Fee // seller's fee BuyerFee *sdk.Fee // buyer's fee }
Trade stores an execution between 2 orders on a *currency pair*. 3 things needs attention: - srcId and oid are just different names; actually no concept of source or destination; - one trade would be implemented via TWO transfer transactions on each currency of the pair; - the trade would be uniquely identifiable via the two order id. UUID generation cannot be used here.
type ULList ¶
type ULList struct {
// contains filtered or unexported fields
}
func (*ULList) AddPriceLevel ¶
func (ull *ULList) AddPriceLevel(p *PriceLevel) bool
AddPriceLevel() would only add price that doesn't exist in the list yet, otherwise return false.
func (*ULList) DeletePriceLevel ¶
func (*ULList) GetPriceLevel ¶
func (ull *ULList) GetPriceLevel(p int64) *PriceLevel
GetPriceLevel returns the PriceLevel point that has the same price as p. It will return nil if no such price.
func (*ULList) GetPriceRange ¶
func (ull *ULList) GetPriceRange(p1 int64, p2 int64, buffer *[]PriceLevel) []PriceLevel
func (*ULList) GetTop ¶
func (ull *ULList) GetTop() *PriceLevel