positions

package
v0.68.0-prev2 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2023 License: MIT Imports: 13 Imported by: 0

README

Positions engine

The Positions Engine maintains a map of partyID to MarketPosition struct. In the struct are:

  • size: the actual volume (orders having been accepted)
  • buy and sell : volume of buy and sell orders not yet accepted

For tracking actual and potential volume:

  • RegisterOrder, called in SubmitOrder and AmendOrder, adds to the buy xor sell potential volume
  • UnregisterOrder, called in AmendOrder and CancelOrder, subtracts from the buy xor sell potential volume
  • Update deals with an accepted order and does the following:
  • transfers actual volume from the seller to the buyer:
    buyer.size += int64(trade.Size)  // increase
    seller.size -= int64(trade.Size) // decrease
  • decreases potential volume for both the buyer and seller:
    buyer.buy -= int64(trade.Size)   // decrease
    seller.sell -= int64(trade.Size) // decrease

The Position for a party is updated before an order is accepted/rejected.

The Risk Engine determines if the order is acceptable.

Settlement is done pre-trade on the old position, because a trader is liable for their position, and also on the new position.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrPositionNotFound signal that a position was not found for a given party.
	ErrPositionNotFound = errors.New("position not found")
)

Errors.

Functions

func I64MaxAbs

func I64MaxAbs(vals ...int64) int64

I64MaxAbs - get max value based on absolute values of int64 vals keep this function, perhaps we can reuse it in a numutil package once we have to deal with decimals etc...

Types

type Broker

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

Broker (no longer need to mock this, use the broker/mocks wrapper).

type Config

type Config struct {
	Level             encoding.LogLevel `long:"log-level"`
	LogPositionUpdate encoding.Bool     `long:"log-position-update"`
}

Config represents the configuration of the position engine.

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 {
	Config
	// contains filtered or unexported fields
}

Engine represents the positions engine.

func New

func New(log *logging.Logger, config Config, marketID string, broker Broker) *Engine

New instantiates a new positions engine.

func (*Engine) AmendOrder

func (e *Engine) AmendOrder(ctx context.Context, originalOrder, newOrder *types.Order) *MarketPosition

AmendOrder unregisters the original order and then registers the newly amended order this method is a quicker way of handling separate unregister+register pairs.

func (*Engine) GetClosedPositions added in v0.63.0

func (e *Engine) GetClosedPositions() []events.MarketPosition

func (*Engine) GetOpenInterest

func (e *Engine) GetOpenInterest() uint64

func (*Engine) GetOpenInterestGivenTrades

func (e *Engine) GetOpenInterestGivenTrades(trades []*types.Trade) uint64

func (*Engine) GetOpenPositionCount added in v0.63.0

func (e *Engine) GetOpenPositionCount() uint64

GetOpenPositionCount returns the number of open positions in the market.

func (*Engine) GetPositionByPartyID

func (e *Engine) GetPositionByPartyID(partyID string) (*MarketPosition, bool)

GetPositionByPartyID - return current position for a given party, it's used in margin checks during auctions we're not specifying an interface of the return type, and we return a pointer to a copy for the nil.

func (*Engine) GetPositionsByParty added in v0.56.0

func (e *Engine) GetPositionsByParty(ids ...string) []events.MarketPosition

func (*Engine) Hash

func (e *Engine) Hash() []byte

func (*Engine) Parties

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

Parties returns a list of all the parties in the position engine.

func (*Engine) Positions

func (e *Engine) Positions() []events.MarketPosition

Positions is just the logic to update buyer, will eventually return the MarketPosition we need to push.

func (*Engine) RegisterOrder

func (e *Engine) RegisterOrder(ctx context.Context, order *types.Order) *MarketPosition

RegisterOrder updates the potential positions for a submitted order, as though the order were already accepted. It returns the updated position. The margins+risk engines need the updated position to determine whether the order should be accepted.

func (*Engine) ReloadConf

func (e *Engine) ReloadConf(cfg Config)

ReloadConf update the internal configuration of the positions engine.

func (*Engine) RemoveDistressed

func (e *Engine) RemoveDistressed(parties []events.MarketPosition) []events.MarketPosition

RemoveDistressed Removes positions for distressed parties, and returns the most up to date positions we have.

func (*Engine) UnregisterOrder

func (e *Engine) UnregisterOrder(ctx context.Context, order *types.Order) *MarketPosition

UnregisterOrder undoes the actions of RegisterOrder. It is used when an order has been rejected by the Risk Engine, or when an order is amended or canceled.

func (*Engine) Update

func (e *Engine) Update(ctx context.Context, trade *types.Trade) []events.MarketPosition

Update pushes the previous positions on the channel + the updated open volumes of buyer/seller.

func (*Engine) UpdateMarkPrice

func (e *Engine) UpdateMarkPrice(markPrice *num.Uint) []events.MarketPosition

UpdateMarkPrice update the mark price on all positions and return a slice of the updated positions.

func (*Engine) UpdateNetwork

func (e *Engine) UpdateNetwork(ctx context.Context, trade *types.Trade) []events.MarketPosition

UpdateNetwork - functionally the same as the Update func, except for ignoring the network party in the trade (whether it be buyer or seller). This could be incorporated into the Update function, but we know when we're adding network trades, and having this check every time is wasteful, and would only serve to add complexity to the Update func, and slow it down.

type MarketPosition

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

MarketPosition represents the position of a party inside a market.

func NewMarketPosition

func NewMarketPosition(party string) *MarketPosition

func (*MarketPosition) AmendOrder

func (p *MarketPosition) AmendOrder(log *logging.Logger, originalOrder, newOrder *types.Order)

AmendOrder unregisters the original order and then registers the newly amended order this method is a quicker way of handling separate unregister+register pairs.

func (MarketPosition) Buy

func (p MarketPosition) Buy() int64

Buy will returns the potential buys for a given position.

func (MarketPosition) Clone

func (p MarketPosition) Clone() *MarketPosition

func (*MarketPosition) Closed added in v0.63.0

func (p *MarketPosition) Closed() bool

func (MarketPosition) Party

func (p MarketPosition) Party() string

Party returns the party to which this positions is associated.

func (MarketPosition) Price

func (p MarketPosition) Price() *num.Uint

Price returns the current price for this position.

func (*MarketPosition) RegisterOrder

func (p *MarketPosition) RegisterOrder(order *types.Order)

func (MarketPosition) Sell

func (p MarketPosition) Sell() int64

Sell returns the potential sells for the position.

func (*MarketPosition) SetParty

func (p *MarketPosition) SetParty(party string)

func (MarketPosition) Size

func (p MarketPosition) Size() int64

Size returns the current size of the position.

func (MarketPosition) String

func (p MarketPosition) String() string

String returns a string representation of a market.

func (*MarketPosition) UnregisterOrder

func (p *MarketPosition) UnregisterOrder(log *logging.Logger, order *types.Order)

func (MarketPosition) VWBuy

func (p MarketPosition) VWBuy() *num.Uint

VWBuy - get volume weighted buy price for unmatched buy orders.

func (MarketPosition) VWSell

func (p MarketPosition) VWSell() *num.Uint

VWSell - get volume weighted sell price for unmatched sell orders.

type SnapshotEngine

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

func NewSnapshotEngine

func NewSnapshotEngine(
	log *logging.Logger, config Config, marketID string, broker Broker,
) *SnapshotEngine

func (*SnapshotEngine) AmendOrder

func (e *SnapshotEngine) AmendOrder(ctx context.Context, originalOrder, newOrder *types.Order) *MarketPosition

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(_ context.Context, payload *types.Payload) ([]types.StateProvider, error)

func (*SnapshotEngine) Namespace

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

func (*SnapshotEngine) RegisterOrder

func (e *SnapshotEngine) RegisterOrder(ctx context.Context, order *types.Order) *MarketPosition

func (*SnapshotEngine) RemoveDistressed

func (e *SnapshotEngine) RemoveDistressed(parties []events.MarketPosition) []events.MarketPosition

func (*SnapshotEngine) StopSnapshots

func (e *SnapshotEngine) StopSnapshots()

StopSnapshots is called when the engines respective market no longer exists. We need to stop taking snapshots and communicate to the snapshot engine to remove us as a provider.

func (*SnapshotEngine) Stopped

func (e *SnapshotEngine) Stopped() bool

func (*SnapshotEngine) UnregisterOrder

func (e *SnapshotEngine) UnregisterOrder(ctx context.Context, order *types.Order) *MarketPosition

func (*SnapshotEngine) Update

func (e *SnapshotEngine) Update(ctx context.Context, trade *types.Trade) []events.MarketPosition

func (*SnapshotEngine) UpdateMarkPrice

func (e *SnapshotEngine) UpdateMarkPrice(markPrice *num.Uint) []events.MarketPosition

func (*SnapshotEngine) UpdateNetwork

func (e *SnapshotEngine) UpdateNetwork(ctx context.Context, trade *types.Trade) []events.MarketPosition

Jump to

Keyboard shortcuts

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