Skip to content

Commit

Permalink
change calls in the risk engine to explicitly look the indicative pri…
Browse files Browse the repository at this point in the history
…ce in auction
  • Loading branch information
jeremyletang committed Apr 9, 2021
1 parent 8a4929f commit 28e847b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 16 deletions.
8 changes: 0 additions & 8 deletions matching/cached_orderbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,3 @@ func (b *CachedOrderBook) GetIndicativePrice() uint64 {
}
return price
}

func (b *CachedOrderBook) GetCloseoutPrice(volume uint64, side types.Side) (uint64, error) {
// yes this is moving logic in the caching layer.
if b.OrderBook.auction {
return b.GetIndicativePrice(), nil
}
return b.OrderBook.GetCloseoutPrice(volume, side)
}
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 28e847b

Please sign in to comment.