liquidity

package
v0.54.1-nocontractcheck Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2022 License: MIT Imports: 17 Imported by: 0

README

Liquidity Engine

The Liquidity Engine handles Liquidity Provisions (LiquidityProvisionSubmission) and subsequent updates to the created orders.

A LiquidityProvisionSubmission specifies how to match a given liquidity by defining a shape of orders to be created. These shape is updated each time there is a change in the book.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrLiquidityProvisionDoesNotExist  = errors.New("liquidity provision does not exist")
	ErrLiquidityProvisionAlreadyExists = errors.New("liquidity provision already exists")
	ErrCommitmentAmountIsZero          = errors.New("commitment amount is zero")
	ErrEmptyShape                      = errors.New("liquidity provision contains an empty shape")
)
View Source
var ErrPartyHaveNoLiquidityProvision = errors.New("party have no liquidity provision")

Functions

This section is empty.

Types

type Broker

type Broker interface {
	Send(e events.Event)
	SendBatch(evts []events.Event)
}

Broker - event bus (no mocks needed).

type Config

type Config struct {
	Level cfgencoding.LogLevel `long:"log-level"`
}

Config represents the configuration of the Liquidity service.

func NewDefaultConfig

func NewDefaultConfig() Config

NewDefaultConfig creates an instance of the package specific configuration, given a pointer to a logger instance to be used for logging within the package.

type Engine

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

Engine handles Liquidity provision.

func NewEngine

func NewEngine(config Config,
	log *logging.Logger,
	timeService TimeService,
	broker Broker,
	riskModel RiskModel,
	priceMonitor PriceMonitor,
	asset string,
	marketID string,
	stateVarEngine StateVarEngine,
	tickSize *num.Uint,
	priceFactor *num.Uint,
	positionFactor num.Decimal,
) *Engine

NewEngine returns a new Liquidity Engine.

func (*Engine) AmendLiquidityProvision

func (e *Engine) AmendLiquidityProvision(
	ctx context.Context,
	lpa *types.LiquidityProvisionAmendment,
	party string,
	idGen IDGen,
) ([]*types.Order, error)

func (*Engine) CalculateSuppliedStake

func (e *Engine) CalculateSuppliedStake() *num.Uint

CalculateSuppliedStake returns the sum of commitment amounts from all the liquidity providers.

func (*Engine) CanAmend

func (e *Engine) CanAmend(
	lps *types.LiquidityProvisionAmendment,
	party string,
) error

func (*Engine) CancelLiquidityProvision

func (e *Engine) CancelLiquidityProvision(ctx context.Context, party string) ([]*types.Order, error)

CancelLiquidityProvision removes a parties commitment of liquidity Returns the liquidityOrders if any.

func (*Engine) CreateInitialOrders

func (e *Engine) CreateInitialOrders(
	ctx context.Context,
	bestBidPrice, bestAskPrice num.Decimal,
	party string,
	orders []*types.Order,
	repriceFn RepricePeggedOrder,
) ([]*types.Order, error)

CreateInitialOrders returns two slices of orders, one for orders to be created and the other for orders to be updated.

func (*Engine) GetAllLiquidityOrders

func (e *Engine) GetAllLiquidityOrders() []*types.Order

func (*Engine) GetInactiveParties

func (e *Engine) GetInactiveParties() map[string]struct{}

GetInactiveParties returns a set of all the parties with inactive commitment.

func (*Engine) GetLiquidityOrders

func (e *Engine) GetLiquidityOrders(party string) []*types.Order

func (*Engine) GetPending

func (e *Engine) GetPending() []string

func (*Engine) GetPotentialShapeOrders

func (e *Engine) GetPotentialShapeOrders(
	party string,
	bestBidPrice, bestAskPrice num.Decimal,
	lps *types.LiquidityProvisionAmendment,
	repriceFn RepricePeggedOrder,
) ([]*types.Order, error)

GetPotentialShapeOrders is used to create orders from shape when amending a liquidity provision this allows us to ensure enough funds can be taken from the margin account in orders to submit orders later on.

func (*Engine) IsLiquidityOrder

func (e *Engine) IsLiquidityOrder(party, order string) bool

IsLiquidityOrder checks to see if a given order is part of the LP orders for a given party.

func (*Engine) IsLiquidityProvider

func (e *Engine) IsLiquidityProvider(party string) bool

IsLiquidityProvider returns true if the party hold any liquidity commitmement.

func (*Engine) IsPending

func (e *Engine) IsPending(party string) bool

func (*Engine) IsPoTInitialised

func (e *Engine) IsPoTInitialised() bool

func (*Engine) LiquidityProvisionByPartyID

func (e *Engine) LiquidityProvisionByPartyID(partyID string) *types.LiquidityProvision

LiquidityProvisionByPartyID returns the LP associated to a Party if any. If not, it returns nil.

func (*Engine) OnMarketLiquidityProvisionShapesMaxSizeUpdate

func (e *Engine) OnMarketLiquidityProvisionShapesMaxSizeUpdate(v int64) error

func (*Engine) OnMaximumLiquidityFeeFactorLevelUpdate

func (e *Engine) OnMaximumLiquidityFeeFactorLevelUpdate(f num.Decimal)

func (*Engine) OnMinProbabilityOfTradingLPOrdersUpdate

func (e *Engine) OnMinProbabilityOfTradingLPOrdersUpdate(v num.Decimal)

func (*Engine) OnProbabilityOfTradingTauScalingUpdate

func (e *Engine) OnProbabilityOfTradingTauScalingUpdate(v num.Decimal)

func (*Engine) OnSuppliedStakeToObligationFactorUpdate

func (e *Engine) OnSuppliedStakeToObligationFactorUpdate(v num.Decimal)

OnSuppliedStakeToObligationFactorUpdate updates the stake factor.

func (*Engine) ProvisionsPerParty

func (e *Engine) ProvisionsPerParty() ProvisionsPerParty

ProvisionsPerParty returns the registered a map of party-id -> LiquidityProvision.

func (*Engine) RejectLiquidityProvision

func (e *Engine) RejectLiquidityProvision(ctx context.Context, party string) error

RejectLiquidityProvision removes a parties commitment of liquidity.

func (*Engine) RemovePending

func (e *Engine) RemovePending(party string)

func (*Engine) SetGetStaticPricesFunc

func (e *Engine) SetGetStaticPricesFunc(f func() (num.Decimal, num.Decimal, error))

func (*Engine) StopLiquidityProvision

func (e *Engine) StopLiquidityProvision(ctx context.Context, party string) ([]*types.Order, error)

StopLiquidityProvision removes a parties commitment of liquidity Returns the liquidityOrders if any.

func (*Engine) SubmitLiquidityProvision

func (e *Engine) SubmitLiquidityProvision(
	ctx context.Context,
	lps *types.LiquidityProvisionSubmission,
	party string,
	idgen IDGen,
) error

SubmitLiquidityProvision handles a new liquidity provision submission. It's used to create, update or delete a LiquidityProvision. The LiquidityProvision is created if submitted for the first time, updated if a previous one was created for the same PartyId or deleted (if exists) when the CommitmentAmount is set to 0.

func (*Engine) Update

func (e *Engine) Update(
	ctx context.Context,
	bestBidPrice, bestAskPrice num.Decimal,
	repriceFn RepricePeggedOrder,
	orders []*types.Order,
) ([]*types.Order, []*ToCancel, error)

Update gets the order changes. It keeps track of all LP orders.

func (*Engine) UpdateMarketConfig

func (e *Engine) UpdateMarketConfig(model risk.Model, monitor PriceMonitor)

func (*Engine) ValidateLiquidityProvisionAmendment

func (e *Engine) ValidateLiquidityProvisionAmendment(lp *types.LiquidityProvisionAmendment) (err error)

func (*Engine) ValidateLiquidityProvisionSubmission

func (e *Engine) ValidateLiquidityProvisionSubmission(
	lp *types.LiquidityProvisionSubmission,
	zeroCommitmentIsValid bool,
) (err error)

type IDGen

type IDGen interface {
	NextID() string
}

IDGen is an id generator for orders.

type LiquidityProvisions

type LiquidityProvisions []*types.LiquidityProvision

LiquidityProvisions provides convenience functions to a slice of *vega/proto.LiquidityProvision.

type OrderBook

type OrderBook interface {
	GetOrderByID(orderID string) (*types.Order, error)
}

type Orders

type Orders []*types.Order

Orders provides convenience functions to a slice of *veaga/proto.Orders.

func (Orders) ByParty

func (ords Orders) ByParty() []PartyOrders

ByParty returns the orders grouped by it's PartyID.

type PartyOrders

type PartyOrders struct {
	Party  string
	Orders []*types.Order
}

type PriceMonitor

type PriceMonitor interface {
	GetValidPriceRange() (num.WrappedDecimal, num.WrappedDecimal)
}

PriceMonitor provides the range of valid prices, that is prices that wouldn't trade the current trading mode.

type ProvisionsPerParty

type ProvisionsPerParty map[string]*types.LiquidityProvision

Provisions is a map of parties to *types.LiquidityProvision.

func (ProvisionsPerParty) FeeForTarget

func (l ProvisionsPerParty) FeeForTarget(v *num.Uint) num.Decimal

func (ProvisionsPerParty) Slice

Slice returns the parties as a slice.

func (ProvisionsPerParty) TotalStake

func (l ProvisionsPerParty) TotalStake() *num.Uint

TotalStake returns the sum of all CommitmentAmount, which corresponds to the total stake of a market.

type RepricePeggedOrder

type RepricePeggedOrder func(
	order *types.PeggedOrder, side types.Side,
) (*num.Uint, *types.PeggedOrder, error)

RepricePeggedOrder reprices a pegged order. This function should be injected by the market.

type RiskModel

type RiskModel interface {
	ProbabilityOfTrading(currentPrice, orderPrice num.Decimal, minPrice, maxPrice num.Decimal, yFrac num.Decimal, isBid, applyMinMax bool) num.Decimal
	GetProjectionHorizon() num.Decimal
}

RiskModel allows calculation of min/max price range and a probability of trading.

type SnapshotEngine

type SnapshotEngine struct {
	*Engine
	// contains filtered or unexported fields
}

func NewSnapshotEngine

func NewSnapshotEngine(config Config,
	log *logging.Logger,
	timeService TimeService,
	broker Broker,
	riskModel RiskModel,
	priceMonitor PriceMonitor,
	asset string,
	market string,
	stateVarEngine StateVarEngine,
	tickSize *num.Uint,
	priceFactor *num.Uint,
	positionFactor num.Decimal,
) *SnapshotEngine

func (*SnapshotEngine) Changed

func (e *SnapshotEngine) Changed() bool

func (*SnapshotEngine) GetState

func (e *SnapshotEngine) GetState(k string) ([]byte, []types.StateProvider, error)

func (*SnapshotEngine) HasChanged

func (e *SnapshotEngine) HasChanged(k string) bool

func (*SnapshotEngine) Keys

func (e *SnapshotEngine) Keys() []string

func (*SnapshotEngine) LoadState

func (e *SnapshotEngine) LoadState(ctx context.Context, p *types.Payload) ([]types.StateProvider, error)

func (*SnapshotEngine) Namespace

func (e *SnapshotEngine) Namespace() types.SnapshotNamespace

func (*SnapshotEngine) OnMarketLiquidityProvisionShapesMaxSizeUpdate

func (e *SnapshotEngine) OnMarketLiquidityProvisionShapesMaxSizeUpdate(v int64) error

func (*SnapshotEngine) OnMaximumLiquidityFeeFactorLevelUpdate

func (e *SnapshotEngine) OnMaximumLiquidityFeeFactorLevelUpdate(f num.Decimal)

func (*SnapshotEngine) OnSuppliedStakeToObligationFactorUpdate

func (e *SnapshotEngine) OnSuppliedStakeToObligationFactorUpdate(v num.Decimal)

func (*SnapshotEngine) ReconcileWithOrderBook

func (e *SnapshotEngine) ReconcileWithOrderBook(orderbook OrderBook)

ReconcileWithOrderBook ensures that when restoring state from a snapshot the orders in the matching engine and liquidity engine are pointers to the same underlying order struct.

func (*SnapshotEngine) StopSnapshots

func (e *SnapshotEngine) StopSnapshots()

func (*SnapshotEngine) Stopped

func (e *SnapshotEngine) Stopped() bool

func (*SnapshotEngine) UpdateMarketConfig

func (e *SnapshotEngine) UpdateMarketConfig(model risk.Model, monitor PriceMonitor)

type SnapshotablePartiesOrders

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

func (*SnapshotablePartiesOrders) Add

func (o *SnapshotablePartiesOrders) Add(party string, order *types.Order)

func (*SnapshotablePartiesOrders) Delete

func (o *SnapshotablePartiesOrders) Delete(party, order string)

func (*SnapshotablePartiesOrders) DeleteParty

func (o *SnapshotablePartiesOrders) DeleteParty(party string)

func (*SnapshotablePartiesOrders) Get

func (o *SnapshotablePartiesOrders) Get(party, orderID string) (*types.Order, bool)

func (*SnapshotablePartiesOrders) GetForParty

func (o *SnapshotablePartiesOrders) GetForParty(
	party string,
) (map[string]*types.Order, bool)

GetForParty expects to read through them, not do any write operation.

func (*SnapshotablePartiesOrders) HasUpdates

func (o *SnapshotablePartiesOrders) HasUpdates() bool

func (*SnapshotablePartiesOrders) ResetForParty

func (o *SnapshotablePartiesOrders) ResetForParty(party string)

func (*SnapshotablePartiesOrders) ResetUpdated

func (o *SnapshotablePartiesOrders) ResetUpdated()

type SnapshotablePendingProvisions

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

func (*SnapshotablePendingProvisions) Add

func (*SnapshotablePendingProvisions) Delete

func (s *SnapshotablePendingProvisions) Delete(key string)

func (*SnapshotablePendingProvisions) Exists

func (s *SnapshotablePendingProvisions) Exists(key string) bool

func (*SnapshotablePendingProvisions) HasUpdates

func (s *SnapshotablePendingProvisions) HasUpdates() bool

func (*SnapshotablePendingProvisions) ResetUpdated

func (s *SnapshotablePendingProvisions) ResetUpdated()

type SnapshotableProvisionsPerParty

type SnapshotableProvisionsPerParty struct {
	ProvisionsPerParty
	// contains filtered or unexported fields
}

func (*SnapshotableProvisionsPerParty) Delete

func (s *SnapshotableProvisionsPerParty) Delete(key string)

func (*SnapshotableProvisionsPerParty) Get

func (*SnapshotableProvisionsPerParty) HasUpdates

func (s *SnapshotableProvisionsPerParty) HasUpdates() bool

func (*SnapshotableProvisionsPerParty) ResetUpdated

func (s *SnapshotableProvisionsPerParty) ResetUpdated()

func (*SnapshotableProvisionsPerParty) Set

type StateVarEngine

type StateVarEngine interface {
	RegisterStateVariable(asset, market, name string, converter statevar.Converter, startCalculation func(string, statevar.FinaliseCalculation), trigger []statevar.StateVarEventType, result func(context.Context, statevar.StateVariableResult) error) error
}

type TimeService

type TimeService interface {
	GetTimeNow() time.Time
}

TimeService provide the time of the vega node using the tm time.

type ToCancel

type ToCancel struct {
	Party    string
	OrderIDs []string
}

func (*ToCancel) Add

func (c *ToCancel) Add(id string)

func (*ToCancel) Empty

func (c *ToCancel) Empty() bool

func (*ToCancel) Merge

func (c *ToCancel) Merge(oth *ToCancel) *ToCancel

Directories

Path Synopsis
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.
mocks
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.
mocks
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.

Jump to

Keyboard shortcuts

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