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

     1  Feature: Check the margin scaling levels (maintenance, search, initial, release) are correctly applied to the maintenance margin that is calculated by the risk model
     2  
     3    Background:
     4      Given the log normal risk model named "log-normal-risk-model-1":
     5        | risk aversion | tau | mu | r | sigma |
     6        | 0.000001      | 0.1 | 0  | 0 | 1.0   |
     7      #risk factor short = 3.55690359157934000
     8      #risk factor long = 0.801225765
     9      And the margin calculator named "margin-calculator-0":
    10        | search factor | initial factor | release factor |
    11        | 1.2           | 1.5            | 2              |
    12      And the margin calculator named "margin-calculator-1":
    13        | search factor | initial factor | release factor |
    14        | 1.5           | 2              | 3              |
    15  
    16      And the markets:
    17        | id        | quote name | asset | risk model              | margin calculator   | auction duration | fees         | price monitoring | data source config     | linear slippage factor | quadratic slippage factor | sla params      |
    18        | ETH/DEC19 | BTC | USD | log-normal-risk-model-1 | margin-calculator-1 | 1 | default-none | default-none | default-eth-for-future | 1e0 | 0 | default-futures |
    19        | ETH/DEC20 | BTC | USD | log-normal-risk-model-1 | margin-calculator-0 | 1 | default-none | default-none | default-eth-for-future | 1e0 | 0 | default-futures |
    20      And the following network parameters are set:
    21        | name                                    | value |
    22        | market.auction.minimumDuration          | 1     |
    23        | network.markPriceUpdateMaximumFrequency | 0s    |
    24        | limits.markets.maxPeggedOrders          | 4     |
    25  
    26    @SLABug
    27    Scenario: 0010-MARG-015,0010-MARG-016,0010-MARG-017
    28      Given the parties deposit on asset's general account the following amount:
    29        | party       | asset | amount        |
    30        | auxiliary1  | USD   | 1000000000000 |
    31        | auxiliary2  | USD   | 1000000000000 |
    32        | auxiliary10 | USD   | 1000000000000 |
    33        | auxiliary20 | USD   | 1000000000000 |
    34        | trader2     | USD   | 10000         |
    35        | trader3     | USD   | 9000          |
    36        | trader20    | USD   | 10000         |
    37        | trader30    | USD   | 9000          |
    38        | lprov       | USD   | 1000000000000 |
    39  
    40      When the parties submit the following liquidity provision:
    41        | id  | party | market id | commitment amount | fee   | lp type    |
    42        | lp1 | lprov | ETH/DEC19 | 100000            | 0.001 | submission |
    43        | lp1 | lprov | ETH/DEC19 | 100000            | 0.001 | amendmend  |
    44        | lp0 | lprov | ETH/DEC20 | 100000            | 0.001 | submission |
    45        | lp0 | lprov | ETH/DEC20 | 100000            | 0.001 | amendmend  |
    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     |
    51        | auxiliary2 | ETH/DEC19 | buy  | 5      | 5     | 0                | TYPE_LIMIT | TIF_GTC |
    52        | auxiliary1 | ETH/DEC19 | sell | 10     | 1000  | 0                | TYPE_LIMIT | TIF_GTC |
    53        | auxiliary2 | ETH/DEC19 | buy  | 10     | 10    | 0                | TYPE_LIMIT | TIF_GTC |
    54        | auxiliary1 | ETH/DEC19 | sell | 10     | 10    | 0                | TYPE_LIMIT | TIF_GTC |
    55        | auxiliary2 | ETH/DEC20 | buy  | 5      | 5     | 0                | TYPE_LIMIT | TIF_GTC |
    56        | auxiliary1 | ETH/DEC20 | sell | 10     | 1000  | 0                | TYPE_LIMIT | TIF_GTC |
    57        | auxiliary2 | ETH/DEC20 | buy  | 10     | 10    | 0                | TYPE_LIMIT | TIF_GTC |
    58        | auxiliary1 | ETH/DEC20 | sell | 10     | 10    | 0                | TYPE_LIMIT | TIF_GTC |
    59        | lprov      | ETH/DEC19 | sell | 100    | 1005  | 0                | TYPE_LIMIT | TIF_GTC |
    60        | lprov      | ETH/DEC19 | buy  | 100000 | 1     | 0                | TYPE_LIMIT | TIF_GTC |
    61        | lprov      | ETH/DEC20 | sell | 100    | 1005  | 0                | TYPE_LIMIT | TIF_GTC |
    62        | lprov      | ETH/DEC20 | buy  | 100000 | 1     | 0                | TYPE_LIMIT | TIF_GTC |
    63  
    64      When the network moves ahead "2" blocks
    65      #When the opening auction period ends for market "ETH/DEC19"
    66      #When the opening auction period ends for market "ETH/DEC20"
    67      Then the mark price should be "10" for the market "ETH/DEC19"
    68      And the mark price should be "10" for the market "ETH/DEC20"
    69  
    70      # setup trader2 position to be ready to takeover trader3's position once trader3 is closed out
    71      When the parties place the following orders with ticks:
    72        | party    | market id | side | volume | price | resulting trades | type       | tif     | reference   |
    73        | trader2  | ETH/DEC19 | buy  | 40     | 50    | 0                | TYPE_LIMIT | TIF_GTC | buy-order-3 |
    74        | trader20 | ETH/DEC20 | buy  | 40     | 50    | 0                | TYPE_LIMIT | TIF_GTC | buy-order-4 |
    75  
    76      And the parties should have the following margin levels:
    77        | party    | market id | maintenance | search | initial | release |
    78        | trader2  | ETH/DEC19 | 321         | 481    | 642     | 963     |
    79        | trader20 | ETH/DEC20 | 321         | 385    | 481     | 642     |
    80  
    81      # margin level = OrderSize*MarkPrice*RF = 40*10*0.801225765=321
    82  
    83      Then the parties should have the following account balances:
    84        | party    | asset | market id | margin | general |
    85        | trader2  | USD   | ETH/DEC19 | 642    | 9358    |
    86        | trader20 | USD   | ETH/DEC20 | 481    | 9519    |
    87  
    88      When the parties place the following orders with ticks:
    89        | party       | market id | side | volume | price | resulting trades | type       | tif     | reference   |
    90        | auxiliary1  | ETH/DEC19 | sell | 40     | 50    | 1                | TYPE_LIMIT | TIF_GTC | buy-order-5 |
    91        | auxiliary10 | ETH/DEC20 | sell | 40     | 50    | 1                | TYPE_LIMIT | TIF_GTC | buy-order-6 |
    92  
    93      Then the parties should have the following profit and loss:
    94        | party    | volume | unrealised pnl | realised pnl |
    95        | trader2  | 40     | 0              | 0            |
    96        | trader20 | 40     | 0              | 0            |
    97  
    98      Then the order book should have the following volumes for market "ETH/DEC19":
    99        | side | price | volume |
   100        | sell | 1005  | 100    |
   101        | sell | 1000  | 10     |
   102        | buy  | 5     | 5      |
   103        | buy  | 1     | 100000 |
   104  
   105      Then the order book should have the following volumes for market "ETH/DEC20":
   106        | side | price | volume |
   107        | sell | 1005  | 100    |
   108        | sell | 1000  | 10     |
   109        | buy  | 5     | 5      |
   110        | buy  | 1     | 100000 |
   111  
   112      # check margin initial level
   113      # trader2 and trader20 have open position of 40 now
   114      And the parties should have the following margin levels:
   115        | party    | market id | maintenance | search | initial | release |
   116        | trader2  | ETH/DEC19 | 3602        | 5403   | 7204    | 10806   |
   117        | trader20 | ETH/DEC20 | 3602        | 4322   | 5403    | 7204    |
   118  #maintenance_margin_trader2: 40*(50-5)+40*50*0.801225765=3402
   119  
   120      Then the parties should have the following account balances:
   121        | party    | asset | market id | margin | general |
   122        | trader2  | USD   | ETH/DEC19 | 7204   | 2796    |
   123        | trader20 | USD   | ETH/DEC20 | 5403   | 4597    |
   124  
   125      # move mark price from 50 to 20, MTM, hence cash flow beween margin and general account for trader2 and trader20
   126      When the parties place the following orders with ticks:
   127        | party       | market id | side | volume | price | resulting trades | type       | tif     | reference    |
   128        | auxiliary1  | ETH/DEC19 | buy  | 1      | 20    | 0                | TYPE_LIMIT | TIF_GTC | buy-order-4  |
   129        | auxiliary10 | ETH/DEC19 | sell | 1      | 20    | 1                | TYPE_LIMIT | TIF_GTC | sell-order-4 |
   130        | auxiliary1  | ETH/DEC20 | buy  | 1      | 20    | 0                | TYPE_LIMIT | TIF_GTC | buy-order-5  |
   131        | auxiliary10 | ETH/DEC20 | sell | 1      | 20    | 1                | TYPE_LIMIT | TIF_GTC | sell-order-5 |
   132  
   133      Then the parties should have the following profit and loss:
   134        | party    | volume | unrealised pnl | realised pnl |
   135        | trader2  | 40     | -1200          | 0            |
   136        | trader20 | 40     | -1200          | 0            |
   137  
   138      And the parties should have the following margin levels:
   139        | party    | market id | maintenance | search | initial | release |
   140        | trader2  | ETH/DEC19 | 1441        | 2161   | 2882    | 4323    |
   141        | trader20 | ETH/DEC20 | 1441        | 1729   | 2161    | 2882    |
   142  
   143      Then the parties should have the following account balances:
   144        | party    | asset | market id | margin | general |
   145        | trader2  | USD   | ETH/DEC19 | 2882   | 5918    |
   146        | trader20 | USD   | ETH/DEC20 | 2161   | 6639    |
   147  
   148  # check margin release level
   149  # MTM process will reduce (50-20)*40=1200 from general account
   150  # for trader2: MTM brings margin account from 3204 to 2204 which is above release level, so margin account has been set to initial level: 1282
   151  # for trader 20: MTM brings margin account from 2403 to 1203 which is below release level, so margin account is kept at 1203
   152  
   153      When the parties place the following orders with ticks:
   154        | party       | market id | side | volume | price | resulting trades | type       | tif     | reference    |
   155        | trader2     | ETH/DEC19 | sell | 40     | 50    | 0                | TYPE_LIMIT | TIF_GTC | sell-order-6 |
   156        | trader20    | ETH/DEC20 | sell | 40     | 50    | 0                | TYPE_LIMIT | TIF_GTC | sell-order-6 |
   157        | auxiliary1  | ETH/DEC19 | buy  | 40     | 50    | 1                | TYPE_LIMIT | TIF_GTC | buy-order-6  |
   158        | auxiliary10 | ETH/DEC20 | buy  | 40     | 50    | 1                | TYPE_LIMIT | TIF_GTC | buy-order-6  |
   159  
   160      Then the parties should have the following profit and loss:
   161        | party    | volume | unrealised pnl | realised pnl |
   162        | trader2  | 0      | 0              | 0            |
   163        | trader20 | 0      | 0              | 0            |
   164  
   165      And the parties should have the following margin levels:
   166        | party    | market id | maintenance | search | initial | release |
   167        | trader2  | ETH/DEC19 | 0           | 0      | 0       | 0       |
   168        | trader20 | ETH/DEC20 | 0           | 0      | 0       | 0       |
   169  
   170      Then the parties should have the following account balances:
   171        | party    | asset | market id | margin | general |
   172        | trader2  | USD   | ETH/DEC19 | 0      | 10000   |
   173        | trader20 | USD   | ETH/DEC20 | 0      | 10000   |
   174  
   175      When the parties place the following orders with ticks:
   176        | party       | market id | side | volume | price | resulting trades | type       | tif     | reference    |
   177        | trader2     | ETH/DEC19 | sell | 20     | 50    | 0                | TYPE_LIMIT | TIF_GTC | sell-order-6 |
   178        | trader20    | ETH/DEC20 | sell | 20     | 50    | 0                | TYPE_LIMIT | TIF_GTC | sell-order-6 |
   179        | auxiliary1  | ETH/DEC19 | buy  | 20     | 50    | 1                | TYPE_LIMIT | TIF_GTC | buy-order-6  |
   180        | auxiliary10 | ETH/DEC20 | buy  | 20     | 50    | 1                | TYPE_LIMIT | TIF_GTC | buy-order-6  |
   181  
   182      Then the parties should have the following profit and loss:
   183        | party    | volume | unrealised pnl | realised pnl |
   184        | trader2  | -20 | 0 | 0 |
   185        | trader20 | -20 | 0 | 0 |
   186  
   187      And the parties should have the following margin levels:
   188        | party    | market id | maintenance | search | initial | release |
   189        | trader2  | ETH/DEC19 | 4557        | 6835   | 9114    | 13671   |
   190        | trader20 | ETH/DEC20 | 4557        | 5468   | 6835    | 9114    |
   191  
   192      Then the parties should have the following account balances:
   193        | party    | asset | market id | margin | general |
   194        | trader2  | USD   | ETH/DEC19 | 7114   | 2886    |
   195        | trader20 | USD   | ETH/DEC20 | 6835   | 3165    |
   196  
   197    @Liquidation
   198    Scenario: Assure initial margin requirement must be met
   199      Given the parties deposit on asset's general account the following amount:
   200        | party      | asset | amount        |
   201        | lprov      | USD   | 1000000000000 |
   202        | auxiliary1 | USD   | 1000000000000 |
   203        | auxiliary2 | USD   | 1000000000000 |
   204        | trader1    | USD   | 711           |
   205        | trader2    | USD   | 712           |
   206        | trader3    | USD   | 321           |
   207        | trader4    | USD   | 40            |
   208      And the parties submit the following liquidity provision:
   209        | id  | party | market id | commitment amount | fee  | lp type    |
   210        | lp1 | lprov | ETH/DEC19 | 100000            | 0.00 | submission |
   211        | lp1 | lprov | ETH/DEC19 | 100000            | 0.00 | amendmend  |
   212      And the parties place the following pegged iceberg orders:
   213        | party | market id | peak size | minimum visible size | side | pegged reference | volume | offset |
   214        | lprov | ETH/DEC19 | 2         | 1                    | sell | ASK              | 100    | 55     |
   215        | lprov | ETH/DEC19 | 2         | 1                    | buy  | BID              | 100    | 55     |
   216      And the parties place the following orders:
   217        | party      | market id | side | volume | price | resulting trades | type       | tif     | reference  |
   218        | auxiliary2 | ETH/DEC19 | buy  | 5      | 5     | 0                | TYPE_LIMIT | TIF_GTC | aux-b-5    |
   219        | auxiliary1 | ETH/DEC19 | sell | 10     | 15    | 0                | TYPE_LIMIT | TIF_GTC | aux-s-1000 |
   220        | auxiliary2 | ETH/DEC19 | buy  | 10     | 10    | 0                | TYPE_LIMIT | TIF_GTC | aux-b-1    |
   221        | auxiliary1 | ETH/DEC19 | sell | 10     | 10    | 0                | TYPE_LIMIT | TIF_GTC | aux-s-1    |
   222  
   223      When the opening auction period ends for market "ETH/DEC19"
   224      Then the market data for the market "ETH/DEC19" should be:
   225        | mark price | trading mode            | open interest |
   226        | 10         | TRADING_MODE_CONTINUOUS | 10            |
   227  
   228      When the parties place the following orders:
   229        | party   | market id | side | volume | price | resulting trades | type       | tif     | error               |
   230        | trader1 | ETH/DEC19 | sell | 10     | 10    | 0                | TYPE_LIMIT | TIF_GTC | margin check failed |
   231        | trader2 | ETH/DEC19 | sell | 1      | 10    | 0                | TYPE_LIMIT | TIF_GTC |                     |
   232  
   233      When the parties deposit on asset's general account the following amount:
   234        | party   | asset | amount |
   235        | trader1 | USD   | 1      |
   236      And the parties place the following orders:
   237        | party   | market id | side | volume | price | resulting trades | type       | tif     |
   238        | trader1 | ETH/DEC19 | sell | 10     | 10    | 0                | TYPE_LIMIT | TIF_GTC |
   239        | trader2 | ETH/DEC19 | sell | 9      | 10    | 0                | TYPE_LIMIT | TIF_GTC |
   240      # both parties end up with same margin levels and account balances
   241      Then the parties should have the following margin levels:
   242        | party   | market id | maintenance | search | initial | release |
   243        | trader1 | ETH/DEC19 | 356         | 534    | 712     | 1068    |
   244        | trader2 | ETH/DEC19 | 356         | 534    | 712     | 1068    |
   245      And the parties should have the following account balances:
   246        | party   | asset | market id | margin | general |
   247        | trader1 | USD   | ETH/DEC19 | 712    | 0       |
   248        | trader2 | USD   | ETH/DEC19 | 712    | 0       |
   249  
   250      When the parties place the following orders:
   251        | party   | market id | side | volume | price | resulting trades | type       | tif     | error               |
   252        | trader3 | ETH/DEC19 | buy  | 20     | 15    | 0                | TYPE_LIMIT | TIF_FOK | margin check failed |
   253  
   254      When the parties deposit on asset's general account the following amount:
   255        | party   | asset | amount |
   256        | trader3 | USD   | 41     |
   257      And the parties place the following orders:
   258        | party   | market id | side | volume | price | resulting trades | type       | tif     |
   259        | trader3 | ETH/DEC19 | buy  | 20     | 15    | 3                | TYPE_LIMIT | TIF_FOK |
   260      # trader2 maintenance margin = 10 * 10 * 3.556903591 = 356
   261      # trader3 maintenance margin = 20 * 10 * 0.801225765 = 161
   262      Then the parties should have the following margin levels:
   263        | party   | market id | maintenance | search | initial | release |
   264        | trader1 | ETH/DEC19 | 356         | 534    | 712     | 1068    |
   265        | trader2 | ETH/DEC19 | 356         | 534    | 712     | 1068    |
   266        | trader3 | ETH/DEC19 | 161         | 241    | 322     | 483     |
   267      And the parties should have the following account balances:
   268        | party   | asset | market id | margin | general |
   269        | trader1 | USD   | ETH/DEC19 | 712    | 0       |
   270        | trader2 | USD   | ETH/DEC19 | 712    | 0       |
   271        | trader3 | USD   | ETH/DEC19 | 322    | 40      |
   272  
   273      When the network moves ahead "1" blocks
   274      Then debug trades
   275      Then the parties should have the following profit and loss:
   276        | party   | volume | unrealised pnl | realised pnl |
   277        | trader1 | -10    | 0              | 0            |
   278        | trader2 | -10    | 0              | 0            |
   279        | trader3 | 20     | 0              | 0            |
   280  
   281      # both parties end up with same margin levels and account balances
   282      And the parties should have the following margin levels:
   283        | party   | market id | maintenance | search | initial | release |
   284        | trader1 | ETH/DEC19 | 456         | 684    | 912     | 1368    |
   285        | trader2 | ETH/DEC19 | 456         | 684    | 912     | 1368    |
   286        | trader3 | ETH/DEC19 | 361         | 541    | 722     | 1083    |
   287      And the parties should have the following account balances:
   288        | party   | asset | market id | margin | general |
   289        | trader1 | USD   | ETH/DEC19 | 712    | 0       |
   290        | trader2 | USD   | ETH/DEC19 | 712    | 0       |
   291        | trader3 | USD   | ETH/DEC19 | 362    | 0       |
   292  
   293      # party places a limit order that would reduce its exposure once it fills
   294      When the parties place the following orders with ticks:
   295        | party   | market id | side | volume | price | resulting trades | type       | tif     |
   296        | trader3 | ETH/DEC19 | sell | 1      | 10    | 0                | TYPE_LIMIT | TIF_GTC |
   297      Then the parties should have the following margin levels:
   298        | party   | market id | maintenance | initial |
   299        | trader3 | ETH/DEC19 | 361         | 722     |
   300  
   301      When the parties place the following orders with ticks:
   302        | party      | market id | side | volume | price | resulting trades | type       | tif     |
   303        | auxiliary2 | ETH/DEC19 | buy  | 2      | 10    | 1                | TYPE_LIMIT | TIF_GTC |
   304      Then the parties should have the following profit and loss:
   305        | party   | volume | unrealised pnl | realised pnl |
   306        | trader3 | 19     | 0              | 0            |
   307      And the parties should have the following margin levels:
   308        | party   | market id | maintenance | initial |
   309        | trader3 | ETH/DEC19 | 343         | 686     |
   310  
   311      When the parties place the following orders with ticks:
   312        | party   | market id | side | volume | price | resulting trades | type       | tif     |
   313        | trader3 | ETH/DEC19 | sell | 18     | 10    | 1                | TYPE_LIMIT | TIF_GTC |
   314      Then the parties should have the following profit and loss:
   315        | party   | volume | unrealised pnl | realised pnl |
   316        | trader3 | 18     | 0              | 0            |
   317      And the parties should have the following margin levels:
   318        | party   | market id | maintenance | initial |
   319        | trader3 | ETH/DEC19 | 325         | 650     |
   320  
   321      # position is long so extra buy order not allowed to skip margin check
   322      When the parties place the following orders with ticks:
   323        | party   | market id | side | volume | price | resulting trades | type       | tif     | error               |
   324        | trader3 | ETH/DEC19 | buy  | 1      | 10    | 0                | TYPE_LIMIT | TIF_GTC | margin check failed |
   325  
   326      # position would get flipped if order got filled
   327      When the parties place the following orders with ticks:
   328        | party   | market id | side | volume | price | resulting trades | type        | tif     | error               |
   329        | trader3 | ETH/DEC19 | sell | 19     | 0     | 0                | TYPE_MARKET | TIF_FOK | margin check failed |
   330  
   331      When the parties place the following orders with ticks:
   332        | party   | market id | side | volume | price | resulting trades | type        | tif     |
   333        | trader3 | ETH/DEC19 | sell | 1      | 0     | 1                | TYPE_MARKET | TIF_FOK |
   334      Then the parties should have the following profit and loss:
   335        | party   | volume | unrealised pnl | realised pnl |
   336        | trader3 | 17     | -85            | -5           |
   337      And the parties should have the following margin levels:
   338        | party   | market id | maintenance | initial |
   339        | trader3 | ETH/DEC19 | 154         | 308     |
   340  
   341      And the parties place the following orders:
   342        | party      | market id | side | volume | price | resulting trades | type       | tif     | reference  |
   343        | auxiliary2 | ETH/DEC19 | buy  | 5      | 5     | 0                | TYPE_LIMIT | TIF_GTC | aux-b-5    |
   344        | auxiliary1 | ETH/DEC19 | sell | 10     | 15    | 0                | TYPE_LIMIT | TIF_GTC | aux-s-1000 |
   345  
   346      When the parties place the following orders with ticks:
   347        | party      | market id | side | volume | price | resulting trades | type        | tif     |
   348        | trader3    | ETH/DEC19 | sell | 17     | 0     | 0                | TYPE_MARKET | TIF_FOK |
   349        | auxiliary2 | ETH/DEC19 | buy  | 20     | 10    | 1                | TYPE_LIMIT  | TIF_GTC |
   350      Then the parties should have the following profit and loss:
   351        | party   | volume | unrealised pnl | realised pnl |
   352        | trader3 | 0      | 0              | -5           |
   353      And the parties should have the following margin levels:
   354        | party   | market id | maintenance | initial |
   355        | trader3 | ETH/DEC19 | 0           | 0       |
   356      And the parties should have the following account balances:
   357        | party   | asset | market id | margin | general |
   358        | trader3 | USD   | ETH/DEC19 | 0      | 357     |
   359  
   360      And the market data for the market "ETH/DEC19" should be:
   361        | mark price | trading mode            | auction trigger             | target stake | supplied stake | open interest |
   362        | 10         | TRADING_MODE_CONTINUOUS | AUCTION_TRIGGER_UNSPECIFIED | 10670        | 100000         | 30            |