risk

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: 13 Imported by: 0

Documentation

Index

Constants

View Source
const RiskFactorStateVarName = "risk-factors"

Variables

View Source
var (
	ErrInsufficientFundsForInitialMargin = errors.New("insufficient funds for initial margin")
	ErrRiskFactorsNotAvailableForAsset   = errors.New("risk factors not available for the specified asset")
)
View Source
var (
	// ErrNilRiskModel ...
	ErrNilRiskModel = errors.New("nil risk model")
	// ErrUnimplementedRiskModel ...
	ErrUnimplementedRiskModel = errors.New("unimplemented risk model")
)

Functions

This section is empty.

Types

type AuctionState

type AuctionState interface {
	InAuction() bool
	CanLeave() bool
}

AuctionState represents the current auction state of the market, previously we got this information from the matching engine, but really... that's not its job.

type Broker

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

Broker the event bus broker.

type Config

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

	StreamMarginLevelsVerbose encoding.Bool `long:"log-margin-update"`
}

Config represent the configuration of the Risk 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 is the risk engine.

func NewEngine

func NewEngine(log *logging.Logger,
	config Config,
	marginCalculator *types.MarginCalculator,
	model Model,
	ob Orderbook,
	as AuctionState,
	timeSvc TimeService,
	broker Broker,
	mktID string,
	asset string,
	stateVarEngine StateVarEngine,
	positionFactor num.Decimal,
	riskFactorsInitialised bool,
	initialisedRiskFactors *types.RiskFactor,
	linearSlippageFactor num.Decimal,
	quadraticSlippageFactor num.Decimal,
) *Engine

NewEngine instantiate a new risk engine.

func (*Engine) CalculateRiskFactorsForTest

func (e *Engine) CalculateRiskFactorsForTest()

CalculateRiskFactorsForTest is a hack for testing for setting directly the risk factors for a market.

func (*Engine) ExpectMargins

func (e *Engine) ExpectMargins(
	evts []events.Margin, markPrice *num.Uint,
) (okMargins []events.Margin, distressedPositions []events.Margin)

ExpectMargins is used in the case some parties are in a distressed positions in this situation we will only check if the party margin is > to the maintenance margin.

func (*Engine) FlushMarginLevelsEvents

func (e *Engine) FlushMarginLevelsEvents()

func (*Engine) GetRiskFactors

func (e *Engine) GetRiskFactors() *types.RiskFactor

GetRiskFactors returns risk factors per specified asset.

func (*Engine) IsRiskFactorInitialised

func (e *Engine) IsRiskFactorInitialised() bool

func (*Engine) OnMarginScalingFactorsUpdate

func (e *Engine) OnMarginScalingFactorsUpdate(sf *types.ScalingFactors) error

func (*Engine) ReloadConf

func (e *Engine) ReloadConf(cfg Config)

ReloadConf update the internal configuration of the risk engine.

func (*Engine) UpdateMarginAuction

func (e *Engine) UpdateMarginAuction(ctx context.Context, evts []events.Margin, price *num.Uint) ([]events.Risk, []events.Margin)

func (*Engine) UpdateMarginOnNewOrder

func (e *Engine) UpdateMarginOnNewOrder(ctx context.Context, evt events.Margin, markPrice *num.Uint) (events.Risk, events.Margin, error)

UpdateMarginOnNewOrder calculate the new margin requirement for a single order this is intended to be used when a new order is created in order to ensure the party margin account is at least at the InitialMargin level before the order is added to the book.

func (*Engine) UpdateMarginsOnSettlement

func (e *Engine) UpdateMarginsOnSettlement(
	ctx context.Context, evts []events.Margin, markPrice *num.Uint,
) []events.Risk

UpdateMarginsOnSettlement ensure the margin requirement over all positions. margins updates are based on the following requirement

---------------------------------------------------------------------------------------

| 1 | SearchLevel < CurMargin < InitialMargin | nothing to do / no risk for the network | | 2 | CurMargin < SearchLevel | set margin to InitialLevel | | 3 | CurMargin > ReleaseLevel | release up to the InitialLevel |

---------------------------------------------------------------------------------------

In the case where the CurMargin is smaller to the MaintenanceLevel after trying to move monies later, we'll need to close out the party but that cannot be figured out now only in later when we try to move monies from the general account.

func (*Engine) UpdateModel

func (e *Engine) UpdateModel(stateVarEngine StateVarEngine, calculator *types.MarginCalculator, model Model)

type FactorConverter

type FactorConverter struct{}

func (FactorConverter) BundleToInterface

func (FactorConverter) InterfaceToBundle

type Model

type Model interface {
	CalculateRiskFactors() *types.RiskFactor
	DefaultRiskFactors() *types.RiskFactor
	PriceRange(price, yearFraction, probability num.Decimal) (minPrice, maxPrice num.Decimal)
	ProbabilityOfTrading(currentP, orderP, minP, maxP, yFrac num.Decimal, isBid, applyMinMax bool) num.Decimal
	GetProjectionHorizon() num.Decimal
}

Model represents a risk model interface.

func NewModel

func NewModel(prm interface{}, asset string) (Model, error)

NewModel instantiate a new risk model from a market framework configuration.

type Orderbook

type Orderbook interface {
	GetCloseoutPrice(volume uint64, side types.Side) (*num.Uint, error)
	GetIndicativePrice() *num.Uint
}

Orderbook represent an abstraction over the orderbook.

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
	NewEvent(asset, market string, eventType statevar.EventType)
}

type TimeService

type TimeService interface {
	GetTimeNow() time.Time
}

TimeService.

Directories

Path Synopsis
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