Documentation ¶
Index ¶
- Constants
- Variables
- 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) GetRiskFactors() (*types.RiskFactor, error)
- 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 Model
- type Orderbook
- type RiskFactorConverter
- type StateVarEngine
- type TimeService
Constants ¶
const RiskFactorStateVarName = "risk-factors"
Variables ¶
var ( ErrInsufficientFundsForMaintenanceMargin = errors.New("insufficient funds for maintenance 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 ¶
This section is empty.
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"` LogMarginUpdate 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, ) *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) GetRiskFactors ¶
func (e *Engine) GetRiskFactors() (*types.RiskFactor, error)
GetRiskFactors returns risk factors per specified asset if available and an error otherwise.
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 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 RiskFactorConverter ¶
type RiskFactorConverter struct{}
func (RiskFactorConverter) BundleToInterface ¶
func (RiskFactorConverter) BundleToInterface(kvb *statevar.KeyValueBundle) statevar.StateVariableResult
func (RiskFactorConverter) InterfaceToBundle ¶
func (RiskFactorConverter) InterfaceToBundle(res statevar.StateVariableResult) *statevar.KeyValueBundle
type StateVarEngine ¶
type StateVarEngine interface { RegisterStateVariable(asset, market, name string, converter statevar.Converter, startCalculation func(string, statevar.FinaliseCalculation), trigger []statevar.StateVarEventType, result func(context.Context, statevar.StateVariableResult) error) error NewEvent(asset, market string, eventType statevar.StateVarEventType) }