order

package
v0.0.0-...-848a935 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MinQty = 1
)

Variables

View Source
var (
	ErrInvalidQty          = errors.New("invalid quantity provided")
	ErrInvalidTickerSymbol = errors.New("invalid ticker symbol")
	ErrInvalidMarketPrice  = errors.New("price has to be zero for market orders")
	ErrInvalidLimitPrice   = errors.New("price has to be set for limit orders")
	ErrInvalidStopPrice    = errors.New("stop price has to be set for a stop order")
	ErrInternal            = errors.New("internal error")
)

Functions

This section is empty.

Types

type BooksFactory

type BooksFactory struct {
	Mode      string
	OrderRepo Repository
}

func (*BooksFactory) Create

func (config *BooksFactory) Create() map[string]*OrderBook

type Comparator

type Comparator func(x, y OrderTracker) bool

type Condition

type Condition int8
const (
	ConditionStop Condition = 0x1                         // stop order (has to have stop price set)
	ConditionAON  Condition = 0x2                         // all-or-nothing - complete fill or cancel
	ConditionIOC  Condition = 0x4                         // immediate-or-cancel - immediately fill what you can, cancel the rest
	ConditionFOK  Condition = ConditionIOC | ConditionAON // immediately try to fill the whole order
	ConditionGTC  Condition = 0x10                        // good-till-cancelled -  keep order active until manually cancelled
	ConditionGFD  Condition = 0x20                        // good-for-day keep order active until the end of the trading day
	ConditionGTD  Condition = 0x40                        // good-till-date - keep order active until the provided date (including the date)
)

func (Condition) Is

func (o Condition) Is(param Condition) bool

Is returns true if a parameter value matches the provided parameters (if param is a subset of o) e.g. ParamFOK.Is(ParamAON) is true, ParamFOK.Is(ParamStop) is false. ParamAON.Is(ParamAON) is true.

func (Condition) String

func (o Condition) String() string

type EventTradeSuccess

type EventTradeSuccess struct {
	ID           string
	Buyer        string
	Seller       string
	TickerSymbol string
	Qty          int64
	Price        apd.Decimal
	Total        apd.Decimal
	Timestamp    time.Time

	BidOrderID string
	AskOrderID string
}

type Kind

type Kind int8
const (
	KindMarket Kind = iota + 1
	KindLimit
)

func (Kind) String

func (o Kind) String() string

type NopRepository

type NopRepository struct {
}

func (NopRepository) FindOrder

func (n NopRepository) FindOrder(ctx context.Context, id string) (order *Order, err error)

func (NopRepository) SaveOrder

func (n NopRepository) SaveOrder(ctx context.Context, order *Order) (err error)

type Order

type Order struct {
	ID string
	// this ticker symbol
	TickerSymbol string
	// CreatedAt means this order arrive time
	CreatedAt time.Time
	// the customer id
	CustomerID string

	Kind      Kind      // order kind - market or limit
	Params    Condition // order parameters which change the way an order is stored and matched
	Qty       int64
	FilledQty int64       // currently filled quantity
	Price     apd.Decimal // used in limit orders
	StopPrice apd.Decimal // used in stop orders
	Side      Side        // determines whether an order is a bid (buy) or an ask (sell)
	Cancelled bool        // determines if an order is cancelled. A partially filled order can be cancelled.
}

func NewOrder

func NewOrder(
	tickerSymbol string,
	customerID string,
	kind Kind,
	params Condition,
	qty int64,
	price *apd.Decimal,
	stopPrice *apd.Decimal,
	side Side,
) (Order, error)

func (*Order) Cancel

func (o *Order) Cancel()

func (*Order) IsAsk

func (o *Order) IsAsk() bool

func (*Order) IsBid

func (o *Order) IsBid() bool

func (*Order) IsCancelled

func (o *Order) IsCancelled() bool

func (*Order) IsFilled

func (o *Order) IsFilled() bool

func (*Order) UnfilledQty

func (o *Order) UnfilledQty() int64

type OrderBook

type OrderBook struct {
	TickerSymbol string

	TradeEvents chan EventTradeSuccess
	// contains filtered or unexported fields
}

func NewOrderBook

func NewOrderBook(symbol string, marketPrice apd.Decimal, orderRepo Repository) *OrderBook

func (*OrderBook) Add

func (o *OrderBook) Add(ctx context.Context, order Order) (bool, error)

Add a new order. Order can be matched immediately or later (or never), depending on order parameters and order type. Returns true if order was matched (partially or fully), false otherwise.

func (*OrderBook) Cancel

func (o *OrderBook) Cancel(ctx context.Context, id string) error

Cancel an order.

func (*OrderBook) GetAsks

func (o *OrderBook) GetAsks() []Order

GetAsks Get all asks ordered the same way they are matched.

func (*OrderBook) GetBids

func (o *OrderBook) GetBids() []Order

GetBids Get all bids ordered the same way they are matched.

func (*OrderBook) GetStopAsks

func (o *OrderBook) GetStopAsks() []Order

GetStopAsks Get all stop asks.

func (*OrderBook) GetStopBids

func (o *OrderBook) GetStopBids() []Order

GetStopBids Get all stop bids.

func (*OrderBook) MarketPrice

func (o *OrderBook) MarketPrice() apd.Decimal

MarketPrice Get a market price.

func (*OrderBook) SetMarketPrice

func (o *OrderBook) SetMarketPrice(ctx context.Context, price apd.Decimal, fPrice float64)

SetMarketPrice Set a market price.

type OrderTracker

type OrderTracker struct {
	ID        string
	Kind      Kind
	Price     float64
	Side      Side
	Timestamp int64 // nanoseconds since Epoch
}

type Provider

type Provider interface {
	// Start process trade info
	Start(ctx context.Context)
	// SubmitOrder Submit order to order matching engine
	// Trade history will send by MQ when successful matching
	SubmitOrder(ctx context.Context, order Order) (err error)
	// ListAllAsks ist all asks orders. include Limit and Market orders
	ListAllAsks(ctx context.Context, symbol string) (orders []Order, err error)
	// ListAllBids List all bids orders include Limit and Market orders
	ListAllBids(ctx context.Context, symbol string) (orders []Order, err error)
}

Provider define order service layer

type Repository

type Repository interface {
	// FindOrder find order by id
	FindOrder(ctx context.Context, id string) (order *Order, err error)

	// SaveOrder save order
	SaveOrder(ctx context.Context, order *Order) (err error)
}

Repository define order repository

type Set

type Set struct {
	Bids *treemap.TreeMap[OrderTracker, bool]
	Asks *treemap.TreeMap[OrderTracker, bool]

	OrderTrackers map[string]OrderTracker
}

func NewOrderSet

func NewOrderSet(bidComparator Comparator, askComparator Comparator) *Set

func (*Set) Add

func (set *Set) Add(tracker OrderTracker)

func (*Set) Find

func (set *Set) Find(id string) (OrderTracker, bool)

func (*Set) FindAllAsksAbove

func (set *Set) FindAllAsksAbove(price float64) []OrderTracker

FindAllAsksAbove ask orders below or equal the price, sorted by time ast

func (*Set) FindAllBidsBelow

func (set *Set) FindAllBidsBelow(price float64) []OrderTracker

FindAllBidsBelow bids orders above or equal the price, sorted by time ast

func (*Set) Iterator

func (set *Set) Iterator(side Side) treemap.ForwardIterator[OrderTracker, bool]

Iterator which iterates through sorted bids or asks.

func (*Set) Len

func (set *Set) Len(side Side) int

Len returns the number of bids or asks in the set.

func (*Set) Remove

func (set *Set) Remove(id string)

type Side

type Side uint
const (
	SideBuy Side = iota + 1
	SideSell
)

Jump to

Keyboard shortcuts

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