market

package
v0.0.0-...-9ba184a Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2023 License: Unlicense Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Broker

type Broker struct {
	Volume Decimal
	Len    int
	Depth  int
	// contains filtered or unexported fields
}

func NewBroker

func NewBroker() *Broker

func (*Broker) Add

func (m *Broker) Add(order *Order) *LinkedListElement

Add appends order to definite price level

func (*Broker) GreaterThan

func (m *Broker) GreaterThan(price Decimal) *OrderQueue

GreaterThan returns the nearest OrderQueue with price greater than given

func (*Broker) LessThan

func (m *Broker) LessThan(price Decimal) *OrderQueue

LessThan returns the nearest OrderQueue with price less than given

func (*Broker) MaxPriceQueue

func (m *Broker) MaxPriceQueue() *OrderQueue

MaxPriceQueue returns maximal level of price

func (*Broker) MinPriceQueue

func (m *Broker) MinPriceQueue() *OrderQueue

MinPriceQueue returns maximal level of price

func (*Broker) Remove

func (m *Broker) Remove(e *LinkedListElement) *Order

Remove removes order from definite price level

type Decimal

type Decimal = decimal.Decimal

func NewDecimalValue

func NewDecimalValue(value int64) Decimal

func NewZeroDecimal

func NewZeroDecimal() Decimal

type Kind

type Kind int
const (
	Sell Kind = iota
	Buy
)

type LinkedList

type LinkedList struct {
	Len int // current list length excluding (this) sentinel element
	// contains filtered or unexported fields
}

LinkedList represents a doubly linked list. The zero value for LinkedList is an empty list ready to use.

func NewList

func NewList() *LinkedList

NewList returns an initialized list.

func (*LinkedList) Append

func (l *LinkedList) Append(v *Order) *LinkedListElement

Append inserts a new element e with value v at the back of list l and returns e.

func (*LinkedList) Back

func (l *LinkedList) Back() *LinkedListElement

Back returns the last element of list l or nil if the list is empty.

func (*LinkedList) Front

func (l *LinkedList) Front() *LinkedListElement

Front returns the first element of list l or nil if the list is empty.

func (*LinkedList) Init

func (l *LinkedList) Init() *LinkedList

Init initializes or clears list l.

func (*LinkedList) Remove

func (l *LinkedList) Remove(e *LinkedListElement) any

Remove removes e from l if e is an element of list l. It returns the element value e.OrderQueue. The element must not be nil.

type LinkedListElement

type LinkedListElement struct {
	Order *Order
	// contains filtered or unexported fields
}

func (*LinkedListElement) Next

Next returns the next list element or nil.

func (*LinkedListElement) Prev

Prev returns the previous list element or nil.

type Market

type Market struct {
	// contains filtered or unexported fields
}

func NewMarket

func NewMarket() *Market

func (*Market) CancelOrder

func (m *Market) CancelOrder(orderID string) *Order

func (*Market) Depth

func (m *Market) Depth() ([]PriceVolume, []PriceVolume)

Depth returns price levels and volumes at price level

func (*Market) MakeBuyPrice

func (m *Market) MakeBuyPrice(volume Decimal) (Decimal, error)

MakeBuyPrice returns total market buy price for requested volume

func (*Market) MakeSellPrice

func (m *Market) MakeSellPrice(volume Decimal) (Decimal, error)

MakeSellPrice returns total market sell price for requested volume

func (*Market) Order

func (m *Market) Order(orderID string) *Order

func (*Market) ProcessBuy

func (m *Market) ProcessBuy(volume Decimal) ([]*Order, *Order, Decimal, Decimal, error)

ProcessBuy - buys for a volume

func (*Market) ProcessBuyOrder

func (m *Market) ProcessBuyOrder(orderID string, volume, price Decimal) ([]*Order, *Order, Decimal, error)

ProcessBuyOrder places new buy order to the Market

orderID - unique order ID
volume - how much volume you want to buy
price  - no more expensive this price

Result :

 A slice of 'done' orders - if your order satisfies another order, these orders will be added to this slice.
 If your own order is 'done' too, it will be placed into this slice as well
	partial - if your order has been 'done' but the top order is not fully done, or if your order is
	          'partial done' and placed to the market without full volume - partial will contain your order with volume left
	partialVolume - if partial order is not nil this result contains processed volume from partial order
	error   - not nil if volume (or price) is less or equal 0. Or if order with given ID is exists

func (*Market) ProcessSell

func (m *Market) ProcessSell(volume Decimal) ([]*Order, *Order, Decimal, Decimal, error)

ProcessSell - sells a volume

func (*Market) ProcessSellOrder

func (m *Market) ProcessSellOrder(orderID string, volume, price Decimal) ([]*Order, *Order, Decimal, error)

ProcessSellOrder places new sell order to the Market

orderID - unique order ID
volume - how much volume you want to sell
price - no less cheap than this price

Result :

 A slice of 'done' orders - if your order satisfies another order, these orders will be added to this slice.
 If your own order is 'done' too, it will be placed into this slice as well
	partial - if your order has been 'done' but the top order is not fully done, or if your order is
	          'partial done' and placed to the market without full volume - partial will contain your order with volume left
	partialVolume - if partial order is not nil this result contains processed volume from partial order
	error   - not nil if volume (or price) is less or equal 0. Or if order with given ID is exists

type Order

type Order struct {
	Time   time.Time
	ID     string
	Volume Decimal
	Price  Decimal
	Kind   Kind
}

func NewBuy

func NewBuy(orderID string, quantity, price Decimal, timestamp time.Time) *Order

func NewSell

func NewSell(orderID string, quantity, price Decimal, timestamp time.Time) *Order

type OrderQueue

type OrderQueue struct {
	Volume Decimal
	Price  Decimal
	// contains filtered or unexported fields
}

OrderQueue stores and manage chain of orders

func NewQueue

func NewQueue(price Decimal) *OrderQueue

NewQueue creates and initialize OrderQueue object

func (*OrderQueue) Add

func (q *OrderQueue) Add(order *Order) *LinkedListElement

Add adds order to tail of the queue

func (*OrderQueue) Head

func (q *OrderQueue) Head() *LinkedListElement

Head returns top order in queue

func (*OrderQueue) Len

func (q *OrderQueue) Len() int

Len returns amount of orders in queue

func (*OrderQueue) Remove

func (q *OrderQueue) Remove(e *LinkedListElement) *Order

Remove removes order from the queue and link order chain

func (*OrderQueue) Tail

func (q *OrderQueue) Tail() *LinkedListElement

Tail returns bottom order in queue

func (*OrderQueue) Update

func (q *OrderQueue) Update(element *LinkedListElement, order *Order) *LinkedListElement

Update sets up new order to list value

type PriceVolume

type PriceVolume struct {
	Price  Decimal
	Volume Decimal
}

type Processed

type Processed struct {
	PartialVolume Decimal
	VolumeLeft    Decimal
	Partial       *Order
	Done          []*Order
}

type RedBlackTree

type RedBlackTree struct {
	Root *TreeNode
	// contains filtered or unexported fields
}

RedBlackTree holds elements of the red-black tree

func (*RedBlackTree) Empty

func (t *RedBlackTree) Empty() bool

Empty returns true if tree does not contain any nodes

func (*RedBlackTree) Max

func (t *RedBlackTree) Max() (*OrderQueue, bool)

Max gets the max value and flag if found

func (*RedBlackTree) Min

func (t *RedBlackTree) Min() (*OrderQueue, bool)

Min gets the min value and flag if found

func (*RedBlackTree) Put

func (t *RedBlackTree) Put(price Decimal, queue *OrderQueue)

Put inserts node into the tree.

func (*RedBlackTree) Remove

func (t *RedBlackTree) Remove(price Decimal)

Remove the node from the tree.

type TreeNode

type TreeNode struct {
	Queue *OrderQueue
	Left  *TreeNode
	Right *TreeNode

	Price Decimal
	// contains filtered or unexported fields
}

TreeNode is a single leaf of the tree

func (*TreeNode) Color

func (n *TreeNode) Color() dye

func (*TreeNode) Size

func (n *TreeNode) Size() int

Size returns the number of elements stored in the subtree. Computed dynamically on each call, meaning the subtree is traversed to count the number of the nodes.

func (TreeNode) String

func (n TreeNode) String() string

Jump to

Keyboard shortcuts

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