positions

package
v0.75.8 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2024 License: AGPL-3.0 Imports: 17 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

func CalcVWAP added in v0.74.0

func CalcVWAP(vwap *num.Uint, pos int64, addVolume int64, addPrice *num.Uint) *num.Uint

CalcVWAP calculates the volume weighted average entry price.

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) ValidateAmendOrder added in v0.73.7

func (e *Engine) ValidateAmendOrder(order *types.Order, newOrder *types.Order) error

ValidateAmendOrder checks that replace the given order with a new order will no cause issues with position tracking.

func (*Engine) ValidateOrder added in v0.73.7

func (e *Engine) ValidateOrder(order *types.Order) error

ValidateOrder checks that the given order can be registered.

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) AverageEntryPrice added in v0.74.0

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

AverageEntryPrice returns the volume weighted average price.

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) UpdateInPlaceOnTrades added in v0.74.0

func (p *MarketPosition) UpdateInPlaceOnTrades(log *logging.Logger, traderSide types.Side, trades []*types.Trade, order *types.Order) *MarketPosition

UpdateInPlaceOnTrades takes a clone of the receiver position, and updates it with the position from the given trades.

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.

func (MarketPosition) ValidateOrderRegistration added in v0.73.7

func (p MarketPosition) ValidateOrderRegistration(s uint64, side types.Side) error

ValidateOrder returns an error is the order is so large that the position engine does not have the precision to register it.

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