code.vegaprotocol.io/vega@v0.79.0/core/integration/features/verified/closeout-cascade.feature (about)

     1  Feature: Closeout-cascades
     2    # This is a test case to demonstrate that closeout cascade does NOT happen. In this test case, trader3 gets closed
     3    # out first after buying volume 50 at price 100, and then trader3's position is sold (via the network counterparty) to trader2 whose order is first in the order book
     4    # (volume 50 price 50)
     5    # At this moment, trader2 is under margin but the fuse breaks (if the mark price is actually updated from 100 to 50)
     6    # however, the design of the system is like a fuse and circuit breaker, the mark price will NOT update, so trader2 will not be closed out
     7    # till a new trade happens, and new mark price is set.
     8    Background:
     9      Given the margin calculator named "margin-calculator-1":
    10        | search factor | initial factor | release factor |
    11        | 1.5           | 2              | 3              |
    12      And the markets:
    13        | id        | quote name | asset | risk model                  | margin calculator   | auction duration | fees         | price monitoring | data source config     | linear slippage factor | quadratic slippage factor | sla params      |
    14        | ETH/DEC19 | BTC        | BTC   | default-simple-risk-model-4 | margin-calculator-1 | 1                | default-none | default-none     | default-eth-for-future | 10                 | 0                         | default-futures |
    15      And the following network parameters are set:
    16        | name                                    | value |
    17        | market.auction.minimumDuration          | 1     |
    18        | network.markPriceUpdateMaximumFrequency | 0s    |
    19        | limits.markets.maxPeggedOrders          | 2     |
    20        
    21    @NetworkParty
    22    @CloseOutTrades @Liquidation
    23    Scenario: Distressed position gets taken over by another party whose margin level is insufficient to support it (however mark price doesn't get updated on closeout trade and hence no further closeouts are carried out) (0005-COLL-002)
    24      # setup accounts, we are trying to closeout trader3 first and then trader2
    25  
    26      Given the insurance pool balance should be "0" for the market "ETH/DEC19"
    27  
    28      Given the parties deposit on asset's general account the following amount:
    29        | party      | asset | amount        |
    30        | auxiliary1 | BTC   | 1000000000000 |
    31        | auxiliary2 | BTC   | 1000000000000 |
    32        | trader2    | BTC   | 2000          |
    33        | trader3    | BTC   | 100           |
    34        | lpprov     | BTC   | 1000000000000 |
    35  
    36      Then the cumulated balance for all accounts should be worth "3000000002100"
    37  
    38      When the parties submit the following liquidity provision:
    39        | id  | party  | market id | commitment amount | fee   | lp type    |
    40        | lp1 | lpprov | ETH/DEC19 | 100000            | 0.001 | submission |
    41        | lp1 | lpprov | ETH/DEC19 | 100000            | 0.001 | amendment  |
    42      And the parties place the following pegged iceberg orders:
    43        | party  | market id | peak size | minimum visible size | side | pegged reference | volume     | offset |
    44        | lpprov | ETH/DEC19 | 100 | 1 | sell | ASK | 100 | 55 |
    45        | lpprov | ETH/DEC19 | 100 | 1 | buy  | BID | 100 | 55 |
    46   
    47      # place auxiliary orders so we always have best bid and best offer as to not trigger the liquidity auction
    48      # trading happens at the end of the open auction period
    49      Then the parties place the following orders:
    50        | party      | market id | side | volume | price | resulting trades | type       | tif     | reference  |
    51        | auxiliary2 | ETH/DEC19 | buy  | 5      | 5     | 0                | TYPE_LIMIT | TIF_GTC | aux-b-5    |
    52        | auxiliary1 | ETH/DEC19 | sell | 10     | 1000  | 0                | TYPE_LIMIT | TIF_GTC | aux-s-1000 |
    53        | auxiliary2 | ETH/DEC19 | buy  | 10     | 10    | 0                | TYPE_LIMIT | TIF_GTC | aux-b-1    |
    54        | auxiliary1 | ETH/DEC19 | sell | 10     | 10    | 0                | TYPE_LIMIT | TIF_GTC | aux-s-1    |
    55      When the opening auction period ends for market "ETH/DEC19"
    56      And the trading mode should be "TRADING_MODE_CONTINUOUS" for the market "ETH/DEC19"
    57      Then the auction ends with a traded volume of "10" at a price of "10"
    58      And the mark price should be "10" for the market "ETH/DEC19"
    59  
    60      And the cumulated balance for all accounts should be worth "3000000002100"
    61  
    62      # setup trader2 position to be ready to takeover trader3's position once trader3 is closed out
    63      When the parties place the following orders with ticks:
    64        | party   | market id | side | volume | price | resulting trades | type       | tif     | reference      |
    65        | trader2 | ETH/DEC19 | buy  | 50     | 50    | 0                | TYPE_LIMIT | TIF_GTC | buy-position-3 |
    66  
    67      And the parties should have the following margin levels:
    68        | party   | market id | maintenance | search | initial | release |
    69        | trader2 | ETH/DEC19 | 50          | 75     | 100     | 150     |
    70      # margin_trader2: 50*10*0.1=50
    71  
    72      Then the parties should have the following account balances:
    73        | party   | asset | market id | margin | general |
    74        | trader2 | BTC   | ETH/DEC19 | 100    | 1900    |
    75  
    76      And the cumulated balance for all accounts should be worth "3000000002100"
    77  
    78      # setup trader3 position and close it out
    79      When the parties place the following orders:
    80        | party      | market id | side | volume | price | resulting trades | type       | tif     | reference       |
    81        | trader3    | ETH/DEC19 | buy  | 50     | 100   | 0                | TYPE_LIMIT | TIF_GTC | buy-position-3  |
    82        | auxiliary1 | ETH/DEC19 | sell | 50     | 100   | 1                | TYPE_LIMIT | TIF_GTC | sell-provider-1 |
    83        | auxiliary2 | ETH/DEC19 | buy  | 50     | 10    | 0                | TYPE_LIMIT | TIF_GTC | sell-provider-1 |
    84  
    85      Then the order book should have the following volumes for market "ETH/DEC19":
    86        | side | price | volume |
    87        | sell | 1055  | 100    |
    88        | sell | 1000  | 10     |
    89        | buy  | 50    | 50     |
    90        | buy  | 10    | 50     |
    91        | buy  | 5     | 5      |
    92  
    93      When the network moves ahead "3" blocks
    94      Then the mark price should be "100" for the market "ETH/DEC19"
    95  
    96      And debug trades
    97      # trader3 got closed-out, and trader2 got close-out from the trade with network to close-out trader3, auxiliary2 was in the trade to close-out trader2
    98      And the following trades should be executed:
    99        | buyer      | price | size | seller     |
   100        | auxiliary2 | 10    | 10   | auxiliary1 |
   101        | trader3    | 100   | 50   | auxiliary1 |
   102        | network    | 100   | 50   | trader3    |
   103        | trader2    | 50    | 50   | network    |
   104        | network    | 100   | 50   | trader2    |
   105        | auxiliary2 | 10    | 50   | network    |
   106  
   107      And the cumulated balance for all accounts should be worth "3000000002100"
   108      Then the order book should have the following volumes for market "ETH/DEC19":
   109        | side | price | volume |
   110        | sell | 1055  | 100    |
   111        | sell | 1000  | 10     |
   112        | buy  | 50    | 0      |
   113        | buy  | 10    | 0      |
   114        | buy  | 5     | 5      |
   115      Then the mark price should be "100" for the market "ETH/DEC19"
   116  
   117      And the parties should have the following margin levels:
   118        | party   | market id | maintenance | search | initial | release |
   119        | trader3 | ETH/DEC19 | 0           | 0      | 0       | 0       |
   120        | trader2 | ETH/DEC19 | 0           | 0      | 0       | 0       |
   121  
   122      Then the parties should have the following profit and loss:
   123        | party   | volume | unrealised pnl | realised pnl |
   124        | trader2 | 0      | 0              | -2000        |
   125        | trader3 | 0      | 0              | -100         |
   126  
   127      Then the parties should have the following account balances:
   128        | party      | asset | market id | margin       | general      |
   129        | trader2    | BTC   | ETH/DEC19 | 0            | 0            |
   130        | trader3    | BTC   | ETH/DEC19 | 0            | 0            |
   131  
   132      # setup new mark price, which is the same as when trader2 traded with network
   133      Then the parties place the following orders:
   134        | party      | market id | side | volume | price | resulting trades | type       | tif     | reference |
   135        | auxiliary2 | ETH/DEC19 | buy  | 10     | 10    | 0                | TYPE_LIMIT | TIF_GTC | aux-b-1   |
   136        | auxiliary1 | ETH/DEC19 | sell | 10     | 10    | 1                | TYPE_LIMIT | TIF_GTC | aux-s-1   |
   137  
   138      # close-out trade price is not counted as mark price
   139      And the mark price should be "100" for the market "ETH/DEC19"
   140      And then the network moves ahead "10" blocks
   141      And the mark price should be "10" for the market "ETH/DEC19"
   142  
   143      #trader2 and trader3 are still close-out
   144      Then the parties should have the following profit and loss:
   145        | party      | volume | unrealised pnl | realised pnl |
   146        | auxiliary1 | -70    | 4500           | 0            |
   147        | auxiliary2 | 70     | 0              | -2404        |
   148        | trader2    | 0      | 0              | -2000        |
   149        | trader3    | 0      | 0              | -100         |
   150