Documentation ¶
Index ¶
- Constants
- Variables
- func CalculateLiquidationPriceWithSlippageFactors(sizePosition int64, buyOrders, sellOrders []*OrderInfo, ...) (liquidationPriceForOpenVolume, liquidationPriceWithSellOrders, ... num.Decimal, ...)
- func CalculateMaintenanceMarginWithSlippageFactors(sizePosition int64, buyOrders, sellOrders []*OrderInfo, ...) num.Decimal
- type AuctionState
- type Broker
- type Config
- type Engine
- func (e *Engine) CalculateRiskFactorsForTest()
- func (e *Engine) ExpectMargins(evts []events.Margin, markPrice *num.Uint) (okMargins []events.Margin, distressedPositions []events.Margin)
- func (e *Engine) FlushMarginLevelsEvents()
- func (e *Engine) GetRiskFactors() *types.RiskFactor
- func (e *Engine) IsRiskFactorInitialised() bool
- func (e *Engine) OnMarginScalingFactorsUpdate(sf *types.ScalingFactors) error
- func (e *Engine) ReloadConf(cfg Config)
- func (e *Engine) UpdateMarginAuction(ctx context.Context, evts []events.Margin, price *num.Uint) ([]events.Risk, []events.Margin)
- func (e *Engine) UpdateMarginOnNewOrder(ctx context.Context, evt events.Margin, markPrice *num.Uint) (events.Risk, events.Margin, error)
- func (e *Engine) UpdateMarginsOnSettlement(ctx context.Context, evts []events.Margin, markPrice *num.Uint) []events.Risk
- func (e *Engine) UpdateModel(stateVarEngine StateVarEngine, calculator *types.MarginCalculator, model Model)
- type FactorConverter
- type Model
- type OrderInfo
- type Orderbook
- type StateVarEngine
- type TimeService
Constants ¶
const RiskFactorStateVarName = "risk-factors"
Variables ¶
var ( ErrInsufficientFundsForInitialMargin = errors.New("insufficient funds for initial margin") ErrRiskFactorsNotAvailableForAsset = errors.New("risk factors not available for the specified asset") )
var ( // ErrNilRiskModel ... ErrNilRiskModel = errors.New("nil risk model") // ErrUnimplementedRiskModel ... ErrUnimplementedRiskModel = errors.New("unimplemented risk model") )
Functions ¶
func CalculateLiquidationPriceWithSlippageFactors ¶
func CalculateLiquidationPriceWithSlippageFactors(sizePosition int64, buyOrders, sellOrders []*OrderInfo, currentPrice, collateralAvailable num.Decimal, positionFactor, linearSlippageFactor, quadraticSlippageFactor, riskFactorLong, riskFactorShort num.Decimal) (liquidationPriceForOpenVolume, liquidationPriceWithSellOrders, liquidationPriceWithBuyOrders num.Decimal, err error)
Types ¶
type AuctionState ¶
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 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 (*Engine) OnMarginScalingFactorsUpdate ¶
func (e *Engine) OnMarginScalingFactorsUpdate(sf *types.ScalingFactors) error
func (*Engine) ReloadConf ¶
ReloadConf update the internal configuration of the risk engine.
func (*Engine) UpdateMarginAuction ¶
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) BundleToInterface(kvb *statevar.KeyValueBundle) statevar.StateVariableResult
func (FactorConverter) InterfaceToBundle ¶
func (FactorConverter) InterfaceToBundle(res statevar.StateVariableResult) *statevar.KeyValueBundle
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.
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) }