code.vegaprotocol.io/vega@v0.79.0/core/risk/models/simple.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 models 17 18 import ( 19 "code.vegaprotocol.io/vega/core/types" 20 "code.vegaprotocol.io/vega/libs/num" 21 ) 22 23 // Simple represents a dummy risk model with fixed risk params. 24 type Simple struct { 25 factorLong, factorShort num.Decimal 26 maxMoveUp, minMoveDown num.Decimal 27 asset string 28 prob num.Decimal 29 } 30 31 // NewSimple instantiates a new simple/dummy risk model with fixed risk params. 32 func NewSimple(ps *types.SimpleRiskModel, asset string) (*Simple, error) { 33 return &Simple{ 34 factorLong: ps.Params.FactorLong, 35 factorShort: ps.Params.FactorShort, 36 maxMoveUp: ps.Params.MaxMoveUp, 37 minMoveDown: ps.Params.MinMoveDown.Abs(), // use Abs in case the value is negative 38 asset: asset, 39 prob: ps.Params.ProbabilityOfTrading, 40 }, nil 41 } 42 43 // CalculateRiskFactors returns the fixed risk factors for the simple risk model. 44 func (f *Simple) CalculateRiskFactors() *types.RiskFactor { 45 return &types.RiskFactor{ 46 Long: f.factorLong, 47 Short: f.factorShort, 48 } 49 } 50 51 // PriceRange returns the minimum and maximum price as implied by the model's maxMoveUp/minMoveDown parameters and the current price. 52 func (f *Simple) PriceRange(currentP, _, _ num.Decimal) (num.Decimal, num.Decimal) { 53 return num.MaxD(currentP.Sub(f.minMoveDown), num.DecimalZero()), currentP.Add(f.maxMoveUp) 54 } 55 56 // ProbabilityOfTrading of trading returns the probability of trading given current mark price, projection horizon expressed as year fraction, order price and side (isBid). 57 // Additional arguments control optional truncation of probability density outside the [minPrice,maxPrice] range. 58 func (f *Simple) ProbabilityOfTrading(currentP, orderP, minP, maxP, yFrac num.Decimal, isBid, applyMinMax bool) num.Decimal { 59 if applyMinMax { 60 if orderP.LessThan(minP) || orderP.GreaterThan(maxP) { 61 return num.DecimalZero() 62 } 63 } 64 return f.prob 65 } 66 67 // GetProjectionHorizon returns 0 and the simple model doesn't rely on any proabilistic calculations. 68 func (f *Simple) GetProjectionHorizon() num.Decimal { 69 return num.DecimalZero() 70 } 71 72 func (f *Simple) DefaultRiskFactors() *types.RiskFactor { 73 return &types.RiskFactor{ 74 Long: f.factorLong, 75 Short: f.factorShort, 76 } 77 }