Documentation ¶
Index ¶
- Variables
- type Order
- type OrderBook
- func (ob *OrderBook) CalculateMarketPrice(side Side, quantity decimal.Decimal) (price decimal.Decimal, err error)
- func (ob *OrderBook) CancelOrder(orderID string) *Order
- func (ob *OrderBook) Depth() (asks, bids []*PriceLevel)
- func (ob *OrderBook) MarshalJSON() ([]byte, error)
- func (ob *OrderBook) Order(orderID string) *Order
- func (ob *OrderBook) ProcessLimitOrder(side Side, orderID string, quantity, price decimal.Decimal) (done []*Order, partial *Order, partialQuantityProcessed decimal.Decimal, ...)
- func (ob *OrderBook) ProcessMarketOrder(side Side, quantity decimal.Decimal) (done []*Order, partial *Order, ...)
- func (ob *OrderBook) String() string
- func (ob *OrderBook) UnmarshalJSON(data []byte) error
- type OrderQueue
- func (oq *OrderQueue) Append(o *Order) *list.Element
- func (oq *OrderQueue) Head() *list.Element
- func (oq *OrderQueue) Len() int
- func (oq *OrderQueue) MarshalJSON() ([]byte, error)
- func (oq *OrderQueue) Price() decimal.Decimal
- func (oq *OrderQueue) Remove(e *list.Element) *Order
- func (oq *OrderQueue) String() string
- func (oq *OrderQueue) Tail() *list.Element
- func (oq *OrderQueue) UnmarshalJSON(data []byte) error
- func (oq *OrderQueue) Update(e *list.Element, o *Order) *list.Element
- func (oq *OrderQueue) Volume() decimal.Decimal
- type OrderSide
- func (os *OrderSide) Append(o *Order) *list.Element
- func (os *OrderSide) Depth() int
- func (os *OrderSide) GreaterThan(price decimal.Decimal) *OrderQueue
- func (os *OrderSide) Len() int
- func (os *OrderSide) LessThan(price decimal.Decimal) *OrderQueue
- func (os *OrderSide) MarshalJSON() ([]byte, error)
- func (os *OrderSide) MaxPriceQueue() *OrderQueue
- func (os *OrderSide) MinPriceQueue() *OrderQueue
- func (os *OrderSide) Orders() (orders []*list.Element)
- func (os *OrderSide) Remove(e *list.Element) *Order
- func (os *OrderSide) String() string
- func (os *OrderSide) UnmarshalJSON(data []byte) error
- func (os *OrderSide) Volume() decimal.Decimal
- type PriceLevel
- type Side
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalidQuantity = errors.New("orderbook: invalid order quantity") ErrInvalidPrice = errors.New("orderbook: invalid order price") ErrOrderExists = errors.New("orderbook: order already exists") ErrOrderNotExists = errors.New("orderbook: order does not exist") ErrInsufficientQuantity = errors.New("orderbook: insufficient quantity to calculate price") )
OrderBook erros
Functions ¶
This section is empty.
Types ¶
type Order ¶
type Order struct {
// contains filtered or unexported fields
}
Order strores information about request
func NewOrder ¶
func NewOrder(orderID string, side Side, quantity, price decimal.Decimal, timestamp time.Time) *Order
NewOrder creates new constant object Order
func (*Order) MarshalJSON ¶
MarshalJSON implements json.Marshaler interface
func (*Order) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler interface
type OrderBook ¶
type OrderBook struct {
// contains filtered or unexported fields
}
OrderBook implements standard matching algorithm
func (*OrderBook) CalculateMarketPrice ¶
func (ob *OrderBook) CalculateMarketPrice(side Side, quantity decimal.Decimal) (price decimal.Decimal, err error)
CalculateMarketPrice returns total market price for requested quantity if err is not nil price returns total price of all levels in side
func (*OrderBook) CancelOrder ¶
CancelOrder removes order with given ID from the order book
func (*OrderBook) Depth ¶
func (ob *OrderBook) Depth() (asks, bids []*PriceLevel)
Depth returns price levels and volume at price level
func (*OrderBook) MarshalJSON ¶
MarshalJSON implements json.Marshaler interface
func (*OrderBook) ProcessLimitOrder ¶
func (ob *OrderBook) ProcessLimitOrder(side Side, orderID string, quantity, price decimal.Decimal) (done []*Order, partial *Order, partialQuantityProcessed decimal.Decimal, err error)
ProcessLimitOrder places new order to the OrderBook Arguments:
side - what do you want to do (ob.Sell or ob.Buy) orderID - unique order ID in depth quantity - how much quantity you want to sell or buy price - no more expensive (or cheaper) this price * to create new decimal number you should use decimal.New() func read more at https://github.com/shopspring/decimal
Return:
error - not nil if quantity (or price) is less or equal 0. Or if order with given ID is exists done - not nil if your order produces ends of anoter order, this order will add to the "done" slice. If your order have done too, it will be places to this array too partial - not nil if your order has done but top order is not fully done. Or if your order is partial done and placed to the orderbook without full quantity - partial will contain your order with quantity to left partialQuantityProcessed - if partial order is not nil this result contains processed quatity from partial order
func (*OrderBook) ProcessMarketOrder ¶
func (ob *OrderBook) ProcessMarketOrder(side Side, quantity decimal.Decimal) (done []*Order, partial *Order, partialQuantityProcessed, quantityLeft decimal.Decimal, err error)
ProcessMarketOrder immediately gets definite quantity from the order book with market price Arguments:
side - what do you want to do (ob.Sell or ob.Buy) quantity - how much quantity you want to sell or buy * to create new decimal number you should use decimal.New() func read more at https://github.com/shopspring/decimal
Return:
error - not nil if price is less or equal 0 done - not nil if your market order produces ends of anoter orders, this order will add to the "done" slice partial - not nil if your order has done but top order is not fully done partialQuantityProcessed - if partial order is not nil this result contains processed quatity from partial order quantityLeft - more than zero if it is not enought orders to process all quantity
func (*OrderBook) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler interface
type OrderQueue ¶
type OrderQueue struct {
// contains filtered or unexported fields
}
OrderQueue stores and manage chain of orders
func NewOrderQueue ¶
func NewOrderQueue(price decimal.Decimal) *OrderQueue
NewOrderQueue creates and initialize OrderQueue object
func (*OrderQueue) Append ¶
func (oq *OrderQueue) Append(o *Order) *list.Element
Append adds order to tail of the queue
func (*OrderQueue) Head ¶
func (oq *OrderQueue) Head() *list.Element
Head returns top order in queue
func (*OrderQueue) MarshalJSON ¶
func (oq *OrderQueue) MarshalJSON() ([]byte, error)
MarshalJSON implements json.Marshaler interface
func (*OrderQueue) Price ¶
func (oq *OrderQueue) Price() decimal.Decimal
Price returns price level of the queue
func (*OrderQueue) Remove ¶
func (oq *OrderQueue) Remove(e *list.Element) *Order
Remove removes order from the queue and link order chain
func (*OrderQueue) String ¶
func (oq *OrderQueue) String() string
String implements fmt.Stringer interface
func (*OrderQueue) Tail ¶
func (oq *OrderQueue) Tail() *list.Element
Tail returns bottom order in queue
func (*OrderQueue) UnmarshalJSON ¶
func (oq *OrderQueue) UnmarshalJSON(data []byte) error
UnmarshalJSON implements json.Unmarshaler interface
func (*OrderQueue) Volume ¶
func (oq *OrderQueue) Volume() decimal.Decimal
Volume returns total orders volume
type OrderSide ¶
type OrderSide struct {
// contains filtered or unexported fields
}
OrderSide implements facade to operations with order queue
func (*OrderSide) GreaterThan ¶
func (os *OrderSide) GreaterThan(price decimal.Decimal) *OrderQueue
GreaterThan returns nearest OrderQueue with price greater than given
func (*OrderSide) LessThan ¶
func (os *OrderSide) LessThan(price decimal.Decimal) *OrderQueue
LessThan returns nearest OrderQueue with price less than given
func (*OrderSide) MarshalJSON ¶
MarshalJSON implements json.Marshaler interface
func (*OrderSide) MaxPriceQueue ¶
func (os *OrderSide) MaxPriceQueue() *OrderQueue
MaxPriceQueue returns maximal level of price
func (*OrderSide) MinPriceQueue ¶
func (os *OrderSide) MinPriceQueue() *OrderQueue
MinPriceQueue returns maximal level of price
func (*OrderSide) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler interface
type PriceLevel ¶
type PriceLevel struct { Price decimal.Decimal `json:"price"` Quantity decimal.Decimal `json:"quantity"` }
PriceLevel contains price and volume in depth
type Side ¶
type Side int
Side of the order
func (Side) MarshalJSON ¶
MarshalJSON implements json.Marshaler interface
func (*Side) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler interface