Skip to content

Commit 3700772

Browse files
committed
Merge pull request #3331 from vegaprotocol/feature/more-caching-orderbook
add a GetCloseoutPrice method to the caching
1 parent c03b8a9 commit 3700772

3 files changed

Lines changed: 39 additions & 8 deletions

File tree

risk/engine.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ var (
2222
//go:generate go run github.com/golang/mock/mockgen -destination mocks/orderbook_mock.go -package mocks code.vegaprotocol.io/vega/risk Orderbook
2323
type Orderbook interface {
2424
GetCloseoutPrice(volume uint64, side types.Side) (uint64, error)
25+
GetIndicativePrice() uint64
2526
}
2627

2728
// 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

risk/margins_calculation.go

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,18 @@ func (e *Engine) calculateMargins(m events.Margin, markPrice int64, rf types.Ris
7777
slippagePerUnit int64
7878
)
7979
if slippageVolume > 0 {
80-
exitPrice, err := e.ob.GetCloseoutPrice(uint64(slippageVolume), types.Side_SIDE_BUY)
81-
if err != nil && e.log.GetLevel() == logging.DebugLevel {
82-
e.log.Debug("got non critical error from GetCloseoutPrice for Buy side",
83-
logging.Error(err))
80+
var (
81+
exitPrice uint64
82+
err error
83+
)
84+
if auction {
85+
exitPrice = e.ob.GetIndicativePrice()
86+
} else {
87+
exitPrice, err = e.ob.GetCloseoutPrice(uint64(slippageVolume), types.Side_SIDE_BUY)
88+
if err != nil && e.log.GetLevel() == logging.DebugLevel {
89+
e.log.Debug("got non critical error from GetCloseoutPrice for Buy side",
90+
logging.Error(err))
91+
}
8492
}
8593
slippagePerUnit = markPrice - int64(exitPrice)
8694
}
@@ -101,10 +109,18 @@ func (e *Engine) calculateMargins(m events.Margin, markPrice int64, rf types.Ris
101109
)
102110
// slippageVolume would be negative we abs it in the next phase
103111
if slippageVolume < 0 {
104-
exitPrice, err := e.ob.GetCloseoutPrice(uint64(-slippageVolume), types.Side_SIDE_SELL)
105-
if err != nil && e.log.GetLevel() == logging.DebugLevel {
106-
e.log.Debug("got non critical error from GetCloseoutPrice for Sell side",
107-
logging.Error(err))
112+
var (
113+
exitPrice uint64
114+
err error
115+
)
116+
if auction {
117+
exitPrice = e.ob.GetIndicativePrice()
118+
} else {
119+
exitPrice, err = e.ob.GetCloseoutPrice(uint64(-slippageVolume), types.Side_SIDE_SELL)
120+
if err != nil && e.log.GetLevel() == logging.DebugLevel {
121+
e.log.Debug("got non critical error from GetCloseoutPrice for Sell side",
122+
logging.Error(err))
123+
}
108124
}
109125
slippagePerUnit = -1 * (markPrice - int64(exitPrice))
110126
}

risk/mocks/orderbook_mock.go

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)