Skip to content

Commit

Permalink
Merge pull request #11395 from vegaprotocol/fully-collateralised-margin
Browse files Browse the repository at this point in the history
Fully collateralised margin
  • Loading branch information
EVODelavega authored Jun 19, 2024
2 parents 4fb4fb7 + 50aec69 commit eb07bfd
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 15 deletions.
14 changes: 4 additions & 10 deletions core/execution/future/market.go
Original file line number Diff line number Diff line change
Expand Up @@ -3414,13 +3414,7 @@ func (m *Market) collateralAndRisk(ctx context.Context, settle []events.Transfer
}
}

var price *num.Uint
if m.capMax != nil {
price = m.capMax.Clone()
} else {
price = m.getCurrentMarkPrice()
}
crossRiskUpdates := m.risk.UpdateMarginsOnSettlement(ctx, crossEvts, price, increment, m.getAuctionPrice())
crossRiskUpdates := m.risk.UpdateMarginsOnSettlement(ctx, crossEvts, m.getCurrentMarkPriceForMargin(), increment, m.getAuctionPrice())
isolatedMarginPartiesToClose := []events.Risk{}
for _, evt := range isolatedEvts {
mrgns, err := m.risk.CheckMarginInvariants(ctx, evt, m.getMarketObservable(nil), increment, m.matching.GetOrdersPerParty(evt.Party()), m.getMarginFactor(evt.Party()))
Expand Down Expand Up @@ -4837,7 +4831,7 @@ func (m *Market) settlementDataPerp(ctx context.Context, settlementData *num.Num

// check margin balances
increment := m.tradableInstrument.Instrument.Product.GetMarginIncrease(m.timeService.GetTimeNow().UnixNano())
riskUpdates := m.risk.UpdateMarginsOnSettlement(ctx, crossEvts, m.getCurrentMarkPrice(), increment, m.getAuctionPrice())
riskUpdates := m.risk.UpdateMarginsOnSettlement(ctx, crossEvts, m.getCurrentMarkPriceForMargin(), increment, m.getAuctionPrice())
isolatedMarginPartiesToClose := []events.Risk{}
for _, evt := range isolatedEvts {
mrgns, err := m.risk.CheckMarginInvariants(ctx, evt, m.getMarketObservable(nil), increment, m.matching.GetOrdersPerParty(evt.Party()), m.getMarginFactor(evt.Party()))
Expand Down Expand Up @@ -5013,7 +5007,7 @@ func (m *Market) GetTotalOpenPositionCount() uint64 {
// getMarketObservable returns current mark price once market is out of opening auction, during opening auction the indicative uncrossing price is returned.
func (m *Market) getMarketObservable(fallbackPrice *num.Uint) *num.Uint {
// this is used for margin calculations, so if there's a max price, return that.
if m.capMax != nil {
if m.capMax != nil && m.fCap.FullyCollateralised {
return m.capMax.Clone()
}
// during opening auction we don't have a last traded price, so we use the indicative price instead
Expand Down Expand Up @@ -5173,7 +5167,7 @@ func (m *Market) switchMarginMode(ctx context.Context, party string, marginMode
var auctionPrice *num.Uint
if m.as.InAuction() {
auctionPrice = marketObservable
markPrice := m.getCurrentMarkPrice()
markPrice := m.getCurrentMarkPriceForMargin()
if markPrice != nil && marketObservable.LT(markPrice) {
auctionPrice = markPrice
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ Feature: When binary settlement is enabled, the market ignored oracle data that
| party | asset | market id | margin | general |
| party1 | USD | ETH/DEC21 | 5000 | 5500 |
| party2 | USD | ETH/DEC21 | 2500 | 7000 |
| party3 | USD | ETH/DEC22 | 2970 | 7530 |
| party3 | USD | ETH/DEC22 | 3200 | 7300 |
| party4 | USD | ETH/DEC22 | 2310 | 7190 |
| aux1 | USD | ETH/DEC21 | 3098 | 96908 |
| aux2 | USD | ETH/DEC21 | 402 | 99570 |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,10 @@ Feature: Futures market can be created with a with [hardcoded risk factors](./00
# aux2: short position of size 2, traded price at 1500, then margin: postion size * (max price - average entry price) = 3*(1100+1500*2)/3
And the parties should have the following account balances:
| party | asset | market id | margin | general |
| party1 | USD | ETH/DEC21 | 3150 | 9345 |
| party2 | USD | ETH/DEC21 | 4050 | 3455 |
| party1 | USD | ETH/DEC21 | 3148 | 9347 |
| party2 | USD | ETH/DEC21 | 4047 | 3458 |
| aux1 | USD | ETH/DEC21 | 990 | 100509 |
| aux2 | USD | ETH/DEC21 | 2430 | 96085 |
| aux2 | USD | ETH/DEC21 | 2428 | 96087 |
| aux3 | USD | ETH/DEC21 | 1260 | 98665 |

And the parties should have the following margin levels:
Expand Down
2 changes: 1 addition & 1 deletion core/integration/features/margin/0016-PFUT-026.feature
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Feature: Futures market can be created with a with [hardcoded risk factors](./00
Then the parties should have the following account balances:
| party | asset | market id | margin | general |
| party | USD | ETH/FEB23 | 37206 | 443294 |
| aux1 | USD | ETH/FEB23 | 19080 | 980920 |
| aux1 | USD | ETH/FEB23 | 17880 | 982120 |
| aux2 | USD | ETH/FEB23 | 43680 | 56320 |

#0016-PFUT-028: Updating a risk model on a futures market with [hardcoded risk factors]
Expand Down

0 comments on commit eb07bfd

Please sign in to comment.