code.vegaprotocol.io/vega@v0.79.0/core/risk/model.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package risk
    17  
    18  import (
    19  	"errors"
    20  
    21  	"code.vegaprotocol.io/vega/core/risk/models"
    22  	"code.vegaprotocol.io/vega/core/types"
    23  	"code.vegaprotocol.io/vega/libs/num"
    24  )
    25  
    26  var (
    27  	// ErrNilRiskModel ...
    28  	ErrNilRiskModel = errors.New("nil risk model")
    29  	// ErrUnimplementedRiskModel ...
    30  	ErrUnimplementedRiskModel = errors.New("unimplemented risk model")
    31  )
    32  
    33  // Model represents a risk model interface.
    34  type Model interface {
    35  	CalculateRiskFactors() *types.RiskFactor
    36  	DefaultRiskFactors() *types.RiskFactor
    37  	PriceRange(price, yearFraction, probability num.Decimal) (minPrice, maxPrice num.Decimal)
    38  	ProbabilityOfTrading(currentP, orderP, minP, maxP, yFrac num.Decimal, isBid, applyMinMax bool) num.Decimal
    39  	GetProjectionHorizon() num.Decimal
    40  }
    41  
    42  // NewModel instantiate a new risk model from a market framework configuration.
    43  func NewModel(prm interface{}, asset string) (Model, error) {
    44  	if prm == nil {
    45  		return nil, ErrNilRiskModel
    46  	}
    47  
    48  	switch rm := prm.(type) {
    49  	case *types.TradableInstrumentLogNormalRiskModel:
    50  		return models.NewBuiltinFutures(rm.LogNormalRiskModel, asset)
    51  	case *types.TradableInstrumentSimpleRiskModel:
    52  		return models.NewSimple(rm.SimpleRiskModel, asset)
    53  	default:
    54  		return nil, ErrUnimplementedRiskModel
    55  	}
    56  }