Skip to content

Commit

Permalink
fix: Price bounds updated when not active
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbarrow committed May 14, 2024
1 parent de5835d commit 0de5ebf
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 41 deletions.
89 changes: 52 additions & 37 deletions core/integration/features/price_monitoring/0032-PRIM-014.feature
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ Feature: When market is in price monitoring auction, change of a risk model or a
Given the following assets are registered:
| id | decimal places |
| ETH | 2 |
| BTC | 2 |

And the price monitoring named "my-price-monitoring":
| horizon | probability | auction extension |
Expand All @@ -23,15 +22,15 @@ Feature: When market is in price monitoring auction, change of a risk model or a

And the log normal risk model named "my-log-normal-risk-model-2":
| risk aversion | tau | mu | r | sigma |
| 0.000002 | 0.00011407711613050421 | 0 | 0.017 | 2.1 |
| 0.000001 | 0.00011407711613050421 | 0 | 0.016 | 2.0 |

And the markets:
| id | quote name | asset | risk model | margin calculator | auction duration | fees | price monitoring | data source config | linear slippage factor | quadratic slippage factor | sla params |
| BTC/ETH | BTC | BTC | default-log-normal-risk-model | default-margin-calculator | 1 | default-none | my-price-monitoring | default-eth-for-future | 0.01 | 0 | default-futures |
| id | quote name | asset | risk model | margin calculator | auction duration | fees | price monitoring | data source config | linear slippage factor | quadratic slippage factor | sla params |
| ETH/DEC24 | ETH | ETH | default-log-normal-risk-model | default-margin-calculator | 1 | default-none | my-price-monitoring | default-eth-for-future | 0.01 | 0 | default-futures |

And the following network parameters are set:
| name | value |
| market.auction.minimumDuration | 60 |
| market.auction.minimumDuration | 10 |
| limits.markets.maxPeggedOrders | 2 |
| network.markPriceUpdateMaximumFrequency | 0s |
| market.value.windowLength | 1h |
Expand All @@ -40,69 +39,85 @@ Feature: When market is in price monitoring auction, change of a risk model or a
| party | asset | amount |
| party1 | ETH | 10000000000 |
| party5 | ETH | 10000000000 |
| party1 | BTC | 10000000000 |
| party5 | BTC | 10000000000 |
| lp1 | ETH | 10000000000 |
| lp2 | ETH | 10000000000 |

When the parties submit the following liquidity provision:
| id | party | market id | commitment amount | fee | lp type |
| lpp1 | lp1 | ETH/DEC24 | 90000000 | 0.1 | submission |
| lpp2 | lp2 | ETH/DEC24 | 90000000 | 0.1 | submission |

And the parties place the following pegged iceberg orders:
| party | market id | peak size | minimum visible size | side | pegged reference | volume | offset |
| lp1 | ETH/DEC24 | 2 | 1 | buy | BID | 50 | 500 |
| lp2 | ETH/DEC24 | 2 | 1 | sell | ASK | 50 | 500 |

Scenario:

# Place some orders to get out of auction
And the parties place the following orders:
| party | market id | side | volume | price | resulting trades | type | tif |
| party1 | BTC/ETH | buy | 1 | 1000 | 0 | TYPE_LIMIT | TIF_GFA |
| party5 | BTC/ETH | sell | 1 | 1000 | 0 | TYPE_LIMIT | TIF_GTC |
| party1 | ETH/DEC24 | buy | 1 | 1000 | 0 | TYPE_LIMIT | TIF_GFA |
| party5 | ETH/DEC24 | sell | 1 | 1000 | 0 | TYPE_LIMIT | TIF_GTC |

And the opening auction period ends for market "BTC/ETH"
And the opening auction period ends for market "ETH/DEC24"
When the network moves ahead "1" blocks
Then the trading mode should be "TRADING_MODE_CONTINUOUS" for the market "BTC/ETH"
And the mark price should be "1000" for the market "BTC/ETH"
Then the trading mode should be "TRADING_MODE_CONTINUOUS" for the market "ETH/DEC24"
And the mark price should be "1000" for the market "ETH/DEC24"

# Check that the market price bounds are set
And the market data for the market "BTC/ETH" should be:
And the market data for the market "ETH/DEC24" should be:
| mark price | trading mode | horizon | min bound | max bound | target stake | supplied stake | open interest | auction start | auction end |
| 1000 | TRADING_MODE_CONTINUOUS | 30 | 994 | 1006 | 74340 | 0 | 1 | 0 | 0 |
| 1000 | TRADING_MODE_CONTINUOUS | 60 | 991 | 1009 | 74340 | 0 | 1 | 0 | 0 |
| 1000 | TRADING_MODE_CONTINUOUS | 90 | 989 | 1011 | 74340 | 0 | 1 | 0 | 0 |
| 1000 | TRADING_MODE_CONTINUOUS | 120 | 988 | 1012 | 74340 | 0 | 1 | 0 | 0 |
| 1000 | TRADING_MODE_CONTINUOUS | 30 | 994 | 1006 | 74340 | 180000000 | 1 | 0 | 0 |
| 1000 | TRADING_MODE_CONTINUOUS | 60 | 991 | 1009 | 74340 | 180000000 | 1 | 0 | 0 |
| 1000 | TRADING_MODE_CONTINUOUS | 90 | 989 | 1011 | 74340 | 180000000 | 1 | 0 | 0 |
| 1000 | TRADING_MODE_CONTINUOUS | 120 | 988 | 1012 | 74340 | 180000000 | 1 | 0 | 0 |

# Place 2 persistent orders that are outside all of the price bounds
And the parties place the following orders:
| party | market id | side | volume | price | resulting trades | type | tif | reference |
| party1 | BTC/ETH | buy | 1 | 1008 | 0 | TYPE_LIMIT | TIF_GTC | buy1 |
| party5 | BTC/ETH | sell | 1 | 1008 | 0 | TYPE_LIMIT | TIF_GTC | sell1 |
| party1 | ETH/DEC24 | buy | 1 | 1013 | 0 | TYPE_LIMIT | TIF_GTC | buy1 |
| party5 | ETH/DEC24 | sell | 1 | 1013 | 0 | TYPE_LIMIT | TIF_GTC | sell1 |

When the network moves ahead "1" blocks

# Check we have been placed in auction
Then the trading mode should be "TRADING_MODE_MONITORING_AUCTION" for the market "BTC/ETH"
Then the trading mode should be "TRADING_MODE_MONITORING_AUCTION" for the market "ETH/DEC24"

And the starting auction time for market "ETH/DEC24" is "1704067221000000000"
And the ending auction time for market "ETH/DEC24" is "1704067231000000000"

# Check we know the auction time
And the market data for the market "BTC/ETH" should be:
And the market data for the market "ETH/DEC24" should be:
| mark price | trading mode | horizon | min bound | max bound | target stake | supplied stake | open interest | auction start | auction end |
| 1000 | TRADING_MODE_MONITORING_AUCTION | 60 | 991 | 1009 | 149869 | 0 | 1 | 1704067321000000000 | 1704067381000000000 |
| 1000 | TRADING_MODE_MONITORING_AUCTION | 90 | 989 | 1011 | 149869 | 0 | 1 | 1704067321000000000 | 1704067381000000000 |
| 1000 | TRADING_MODE_MONITORING_AUCTION | 120 | 988 | 1012 | 149869 | 0 | 1 | 1704067321000000000 | 1704067381000000000 |
| 1000 | TRADING_MODE_MONITORING_AUCTION | 60 | 991 | 1009 | 150612 | 180000000 | 1 | 1704067221000000000 | 1704067231000000000 |
| 1000 | TRADING_MODE_MONITORING_AUCTION | 90 | 989 | 1011 | 150612 | 180000000 | 1 | 1704067221000000000 | 1704067231000000000 |
| 1000 | TRADING_MODE_MONITORING_AUCTION | 120 | 988 | 1012 | 150612 | 180000000 | 1 | 1704067221000000000 | 1704067231000000000 |

# Now update the risk model to deactivate all pending price bounds
Then the markets are updated:
| id | risk model |
| BTC/ETH | my-log-normal-risk-model-2 |
| id | risk model |
| ETH/DEC24 | my-log-normal-risk-model-2 |

# If we move ahead 10 blocks we should come out of auction instead of it being extended
When the network moves ahead "15" blocks


# If we move ahead 25 blocks we should come out of auction instead of it being extended
When the network moves ahead "25" blocks
Then the trading mode should be "TRADING_MODE_CONTINUOUS" for the market "ETH/DEC24"

And the market data for the market "BTC/ETH" should be:
And the market data for the market "ETH/DEC24" should be:
| mark price | trading mode | horizon | min bound | max bound | target stake | supplied stake | open interest | auction start | auction end |
| 1008 | TRADING_MODE_CONTINUOUS | 30 | 1004 | 1012 | 0 | 0 | 0 | 0 | 0 |
| 1008 | TRADING_MODE_CONTINUOUS | 60 | 1003 | 1013 | 0 | 0 | 0 | 0 | 0 |
| 1008 | TRADING_MODE_CONTINUOUS | 90 | 1001 | 1015 | 0 | 0 | 0 | 0 | 0 |
| 1008 | TRADING_MODE_CONTINUOUS | 120 | 1000 | 1016 | 0 | 0 | 0 | 0 | 0 |
| 1013 | TRADING_MODE_CONTINUOUS | 30 | 1007 | 1019 | 225372 | 180000000 | 2 | 0 | 0 |
| 1013 | TRADING_MODE_CONTINUOUS | 60 | 1004 | 1022 | 225372 | 180000000 | 2 | 0 | 0 |
| 1013 | TRADING_MODE_CONTINUOUS | 90 | 1002 | 1024 | 225372 | 180000000 | 2 | 0 | 0 |
| 1013 | TRADING_MODE_CONTINUOUS | 120 | 1001 | 1026 | 225372 | 180000000 | 2 | 0 | 0 |

Then the trading mode should be "TRADING_MODE_CONTINUOUS" for the market "BTC/ETH"

# The mark price should show the orders have traded
And the mark price should be "1008" for the market "BTC/ETH"
And the mark price should be "1013" for the market "ETH/DEC24"

And the orders should have the following states:
| party | market id | reference | side | volume | remaining | price | status |
| party1 | BTC/ETH | buy1 | buy | 1 | 0 | 1008 | STATUS_FILLED |
| party5 | BTC/ETH | sell1 | sell | 1 | 0 | 1008 | STATUS_FILLED |
| party1 | ETH/DEC24 | buy1 | buy | 1 | 0 | 1013 | STATUS_FILLED |
| party5 | ETH/DEC24 | sell1 | sell | 1 | 0 | 1013 | STATUS_FILLED |

7 changes: 7 additions & 0 deletions core/integration/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,13 @@ func InitializeScenario(s *godog.ScenarioContext) {
return steps.PartiesUndelegateTheFollowingStake(execsetup.delegationEngine, table)
})

s.Step(`^the starting auction time for market "([^"]+)" is "([^"]+)"`, func(marketID, startTime string) error {
return steps.MarketAuctionStartTime(execsetup.executionEngine, marketID, startTime)
})
s.Step(`^the ending auction time for market "([^"]+)" is "([^"]+)"`, func(marketID, endTime string) error {
return steps.MarketAuctionEndTime(execsetup.executionEngine, marketID, endTime)
})

s.Step(`^the opening auction period ends for market "([^"]+)"$`, func(marketID string) error {
return steps.MarketOpeningAuctionPeriodEnds(execsetup.executionEngine, execsetup.timeService, execsetup.markets, marketID)
})
Expand Down
35 changes: 35 additions & 0 deletions core/integration/steps/market_opening_auction_period_ends.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package steps
import (
"context"
"fmt"
"strconv"
"time"

"code.vegaprotocol.io/vega/core/integration/stubs"
Expand Down Expand Up @@ -63,3 +64,37 @@ func MarketOpeningAuctionPeriodEnds(execEngine Execution, timeStub *stubs.TimeSt
func errMarketNotFound(marketID string) error {
return fmt.Errorf("market %s not found", marketID)
}

func MarketAuctionStartTime(execEngine Execution, marketID, startTime string) error {
data, err := execEngine.GetMarketData(marketID)
if err != nil {
return errMarketDataNotFound(marketID, err)
}

st, err := strconv.ParseInt(startTime, 10, 64)
if err != nil {
return fmt.Errorf("failed to convert time to int64: %s", startTime)
}

if data.AuctionStart == st {
return nil
}
return fmt.Errorf("start auction time did not match %d != %d", data.AuctionStart, st)
}

func MarketAuctionEndTime(execEngine Execution, marketID, endTime string) error {
data, err := execEngine.GetMarketData(marketID)
if err != nil {
return errMarketDataNotFound(marketID, err)
}

et, err := strconv.ParseInt(endTime, 10, 64)
if err != nil {
return fmt.Errorf("failed to convert time to int64: %s", endTime)
}

if data.AuctionEnd == et {
return nil
}
return fmt.Errorf("end auction time did not match %d != %d", data.AuctionEnd, et)
}
4 changes: 0 additions & 4 deletions core/monitor/price/statevar.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,6 @@ func (e *Engine) updatePriceBounds(ctx context.Context, res statevar.StateVariab

func (e *Engine) updateFactors(down, up []num.Decimal) {
for i, b := range e.bounds {
if !b.Active {
continue
}

b.DownFactor = down[i]
b.UpFactor = up[i]
}
Expand Down

0 comments on commit 0de5ebf

Please sign in to comment.