matcheng

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2023 License: MPL-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	UNKNOWN  int8 = 0
	BUYSIDE  int8 = 1
	SELLSIDE int8 = 2
)
View Source
const (
	Unknown = iota
	SellTaker
	BuyTaker
	BuySurplus
	SellSurplus
	Neutral
)

Trade status

View Source
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
}

func (*BuyPriceLevel) Less

func (l *BuyPriceLevel) Less(than bt.Item) bool

type Comparator

type Comparator func(p1 int64, p2 int64) int

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

func NewMatchEng(pairSymbol string, basePrice, lotSize int64, priceLimit float64) *MatchEng

NewMatchEng constructs a new MatchEng.

func (*MatchEng) DropFilledOrder

func (me *MatchEng) DropFilledOrder() (droppedIds []string)

DropFilledOrder() would clear the order to remove

func (*MatchEng) Match

func (me *MatchEng) Match(height int64) bool

func (*MatchEng) MatchBeforeGalileo

func (me *MatchEng) MatchBeforeGalileo(height int64) bool

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

func (ob *OrderBookOnBTree) RemoveOrder(id string, side int8, price int64) (OrderPart, error)

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) GetOrder

func (ob *OrderBookOnULList) GetOrder(id string, side int8, price int64) (OrderPart, error)

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

func (ob *OrderBookOnULList) RemoveOrder(id string, side int8, price int64) (OrderPart, error)

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 OrderPart struct {
	Id     string
	Time   int64
	Qty    int64
	CumQty int64
	// contains filtered or unexported fields
}

func (*OrderPart) LeavesQty

func (o *OrderPart) LeavesQty() int64

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

type PriceLevel struct {
	Price  int64
	Orders []OrderPart
}

func (*PriceLevel) String

func (l *PriceLevel) String() string

func (*PriceLevel) TotalLeavesQty

func (l *PriceLevel) TotalLeavesQty() int64

type PriceLevelInterface

type PriceLevelInterface interface {
	Less(than bt.Item) bool
	TotalLeavesQty() int64
	// contains filtered or unexported methods
}

type SellPriceLevel

type SellPriceLevel struct {
	PriceLevel
}

func (*SellPriceLevel) Less

func (l *SellPriceLevel) Less(than bt.Item) bool

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 NewULList

func NewULList(capacity int, bucketSize int, comp Comparator) *ULList

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) Clear

func (ull *ULList) Clear()

func (*ULList) DeletePriceLevel

func (ull *ULList) DeletePriceLevel(price int64) bool

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

func (*ULList) Iterate

func (ull *ULList) Iterate(levelNum int, iter LevelIter)

func (*ULList) String

func (ull *ULList) String() string

func (*ULList) UpdateForEach

func (ull *ULList) UpdateForEach(updater LevelIter)

Jump to

Keyboard shortcuts

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