liquidity

package
v0.0.0-...-141c82c Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2023 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,
	orderBook OrderBook,
	asset string,
	marketID string,
	stateVarEngine StateVarEngine,
	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) error

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

func (*Engine) ClearLPOrders

func (e *Engine) ClearLPOrders()

func (*Engine) CreateInitialOrders

func (e *Engine) CreateInitialOrders(
	ctx context.Context,
	minLpPrice, maxLpPrice *num.Uint,
	party string,
	repriceFn RepriceOrder,
) []*types.Order

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

func (*Engine) GetAverageLiquidityScores

func (e *Engine) GetAverageLiquidityScores() map[string]num.Decimal

func (*Engine) GetCurrentLiquidityScores

func (e *Engine) GetCurrentLiquidityScores(bestBid, bestAsk num.Decimal, minLpPrice, maxLpPrice *num.Uint) (map[string]num.Decimal, num.Decimal)

GetCurrentLiquidityScores returns volume-weighted probability of trading per each LP's deployed orders.

func (*Engine) GetInactiveParties

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

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

func (*Engine) GetLPShapeCount

func (e *Engine) GetLPShapeCount() uint64

GetLPShapeCount returns the total number of LP shapes.

func (*Engine) GetPending

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

func (*Engine) GetPotentialShapeOrders

func (e *Engine) GetPotentialShapeOrders(
	party string,
	minLpPrice, maxLpPrice *num.Uint,
	lps *types.LiquidityProvisionAmendment,
	repriceFn RepriceOrder,
) ([]*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) 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) ResetAverageLiquidityScores

func (e *Engine) ResetAverageLiquidityScores()

func (*Engine) SaveLPOrders

func (e *Engine) SaveLPOrders()

SaveLPOrders sets LP orders that have been cancelled from the book but we need to know what they were to recalculate and redeploy.

func (*Engine) SetGetStaticPricesFunc

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

func (*Engine) SetShapesReferencesOnLiquidityProvision

func (e *Engine) SetShapesReferencesOnLiquidityProvision(
	ctx context.Context,
	lp *types.LiquidityProvision,
	buys []*types.LiquidityOrder,
	sells []*types.LiquidityOrder,
	idGen IDGen,
) []events.Event

func (*Engine) StopLiquidityProvision

func (e *Engine) StopLiquidityProvision(ctx context.Context, party string) 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) UndeployLPs

func (e *Engine) UndeployLPs(ctx context.Context, orders []*types.Order) []*ToCancel

UndeployLPs is called when a reference price is no longer available. LP orders should all be parked/set to pending and should be redeployed once possible. Pass in updated orders and update internal records first...

func (*Engine) Update

func (e *Engine) Update(
	ctx context.Context,
	minLpPrice, maxLpPrice *num.Uint,
	repriceFn RepriceOrder,
) ([]*types.Order, []*ToCancel)

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

func (*Engine) UpdateAverageLiquidityScores

func (e *Engine) UpdateAverageLiquidityScores(bestBid, bestAsk num.Decimal, minLpPrice, maxLpPrice *num.Uint)

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 OrderBook

type OrderBook interface {
	GetOrderByID(orderID string) (*types.Order, error)
	GetOrdersPerParty(party string) []*types.Order
	GetLiquidityOrders(party string) []*types.Order
}

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 Provisions

type Provisions []*types.LiquidityProvision

Provisions provides convenience functions to a slice of *zeta/proto.LiquidityProvision.

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

func (l ProvisionsPerParty) Slice() Provisions

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 RepriceOrder

type RepriceOrder func(
	side types.Side, reference types.PeggedReference, offset *num.Uint,
) (*num.Uint, error)

RepriceOrder 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,
	orderBook OrderBook,
	asset string,
	market string,
	stateVarEngine StateVarEngine,
	priceFactor *num.Uint,
	positionFactor num.Decimal,
) *SnapshotEngine

func (*SnapshotEngine) GetState

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

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

type SnapshotableProvisionsPerParty

type SnapshotableProvisionsPerParty struct {
	ProvisionsPerParty
}

func (*SnapshotableProvisionsPerParty) Delete

func (s *SnapshotableProvisionsPerParty) Delete(key string)

func (*SnapshotableProvisionsPerParty) Get

func (*SnapshotableProvisionsPerParty) Set

type StateVarEngine

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

type TimeService

type TimeService interface {
	GetTimeNow() time.Time
}

TimeService provide the time of the zeta 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