positions

package
v0.73.0-preview.16 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2023 License: AGPL-3.0 Imports: 15 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

This section is empty.

Functions

This section is empty.

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"`
	StreamPositionVerbose 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) FlushPositionEvents added in v0.68.0

func (e *Engine) FlushPositionEvents(ctx context.Context)

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) GetPartiesLowestOpenInterestForEpoch added in v0.73.0

func (e *Engine) GetPartiesLowestOpenInterestForEpoch() map[string]uint64

GetPartiesLowestOpenInterestForEpoch will return a map of parties and minimal open interest for the epoch, it is meant to be called at the end of the epoch, and will reset the lowest open interest of the party to the latest open interest recored for the epoch.

func (*Engine) GetPartiesTradedVolumeForEpoch added in v0.73.0

func (e *Engine) GetPartiesTradedVolumeForEpoch() (out map[string]uint64)

GetPartiesTradedVolumeForEpoch will return a map of parties and their traded volume recorded during this epoch.

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) MarkDistressed added in v0.71.0

func (e *Engine) MarkDistressed(closed []string) ([]string, []string)

MarkDistressed - mark any distressed parties as such, returns the IDs of the parties who weren't distressed before.

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, passiveOrder, aggressiveOrder *types.Order) []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, passiveOrder *types.Order) []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) BuySumProduct added in v0.70.0

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

BuySumProduct - get sum of size * price of party's buy orders.

func (MarketPosition) Clone

func (p MarketPosition) Clone() *MarketPosition

func (*MarketPosition) Closed added in v0.63.0

func (p *MarketPosition) Closed() bool

func (MarketPosition) OrderReducesExposure added in v0.69.0

func (p MarketPosition) OrderReducesExposure(ord *types.Order) bool

func (MarketPosition) OrderReducesOnlyExposure added in v0.70.0

func (p MarketPosition) OrderReducesOnlyExposure(ord *types.Order) (reduce bool, extraSize uint64)

OrderReducesOnlyExposure returns true if the order reduce the position and the extra size if it was to flip the position side.

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(log *logging.Logger, order *types.Order)

func (MarketPosition) Sell

func (p MarketPosition) Sell() int64

Sell returns the potential sells for the position.

func (MarketPosition) SellSumProduct added in v0.70.0

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

SellSumProduct - get sum of size * price of party's sell orders.

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) UpdateOnOrderChange added in v0.70.0

func (p *MarketPosition) UpdateOnOrderChange(log *logging.Logger, side types.Side, price *num.Uint, sizeChange uint64, add bool)

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

Jump to

Keyboard shortcuts

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