Skip to content

Commit

Permalink
Merge pull request #3331 from vegaprotocol/feature/more-caching-order…
Browse files Browse the repository at this point in the history
…book

add a GetCloseoutPrice method to the caching
  • Loading branch information
jeremyletang committed Apr 9, 2021
2 parents 21368ac + 28e847b commit 605c429
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
1 change: 1 addition & 0 deletions risk/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var (
//go:generate go run github.com/golang/mock/mockgen -destination mocks/orderbook_mock.go -package mocks code.vegaprotocol.io/vega/risk Orderbook
type Orderbook interface {
GetCloseoutPrice(volume uint64, side types.Side) (uint64, error)
GetIndicativePrice() uint64
}

// 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
Expand Down
32 changes: 24 additions & 8 deletions risk/margins_calculation.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,18 @@ func (e *Engine) calculateMargins(m events.Margin, markPrice int64, rf types.Ris
slippagePerUnit int64
)
if slippageVolume > 0 {
exitPrice, err := e.ob.GetCloseoutPrice(uint64(slippageVolume), types.Side_SIDE_BUY)
if err != nil && e.log.GetLevel() == logging.DebugLevel {
e.log.Debug("got non critical error from GetCloseoutPrice for Buy side",
logging.Error(err))
var (
exitPrice uint64
err error
)
if auction {
exitPrice = e.ob.GetIndicativePrice()
} else {
exitPrice, err = e.ob.GetCloseoutPrice(uint64(slippageVolume), types.Side_SIDE_BUY)
if err != nil && e.log.GetLevel() == logging.DebugLevel {
e.log.Debug("got non critical error from GetCloseoutPrice for Buy side",
logging.Error(err))
}
}
slippagePerUnit = markPrice - int64(exitPrice)
}
Expand All @@ -101,10 +109,18 @@ func (e *Engine) calculateMargins(m events.Margin, markPrice int64, rf types.Ris
)
// slippageVolume would be negative we abs it in the next phase
if slippageVolume < 0 {
exitPrice, err := e.ob.GetCloseoutPrice(uint64(-slippageVolume), types.Side_SIDE_SELL)
if err != nil && e.log.GetLevel() == logging.DebugLevel {
e.log.Debug("got non critical error from GetCloseoutPrice for Sell side",
logging.Error(err))
var (
exitPrice uint64
err error
)
if auction {
exitPrice = e.ob.GetIndicativePrice()
} else {
exitPrice, err = e.ob.GetCloseoutPrice(uint64(-slippageVolume), types.Side_SIDE_SELL)
if err != nil && e.log.GetLevel() == logging.DebugLevel {
e.log.Debug("got non critical error from GetCloseoutPrice for Sell side",
logging.Error(err))
}
}
slippagePerUnit = -1 * (markPrice - int64(exitPrice))
}
Expand Down
14 changes: 14 additions & 0 deletions risk/mocks/orderbook_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 605c429

Please sign in to comment.