Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a GetCloseoutPrice method to the caching #3331

Merged
merged 2 commits into from
Apr 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.