code.vegaprotocol.io/vega@v0.79.0/core/integration/features/amm/0090-VAMM-006-014.feature (about)

     1  Feature: Ensure the vAMM positions follow the market correctly
     2  
     3    Background:
     4      Given the average block duration is "1"
     5      And the margin calculator named "margin-calculator-1":
     6        | search factor | initial factor | release factor |
     7        | 1.2           | 1.5            | 1.7            |
     8      And the log normal risk model named "log-normal-risk-model":
     9        | risk aversion | tau                   | mu | r   | sigma |
    10        | 0.001         | 0.0011407711613050422 | 0  | 0.9 | 3.0   |
    11      And the liquidity monitoring parameters:
    12        | name       | triggering ratio | time window | scaling factor |
    13        | lqm-params | 1.00             | 20s         | 1              |
    14        
    15      And the following network parameters are set:
    16        | name                                                | value |
    17        | market.value.windowLength                           | 60s   |
    18        | network.markPriceUpdateMaximumFrequency             | 0s    |
    19        | limits.markets.maxPeggedOrders                      | 6     |
    20        | market.auction.minimumDuration                      | 1     |
    21        | market.fee.factors.infrastructureFee                | 0.001 |
    22        | market.fee.factors.makerFee                         | 0.004 |
    23        | spam.protection.max.stopOrdersPerMarket             | 5     |
    24        | market.liquidity.equityLikeShareFeeFraction         | 1     |
    25  	  | market.amm.minCommitmentQuantum                     | 1     |
    26        | market.liquidity.bondPenaltyParameter               | 0.2   |
    27        | market.liquidity.stakeToCcyVolume                   | 1     |
    28        | market.liquidity.successorLaunchWindowLength        | 1h    |
    29        | market.liquidity.sla.nonPerformanceBondPenaltySlope | 0.1   |
    30        | market.liquidity.sla.nonPerformanceBondPenaltyMax   | 0.6   |
    31        | validators.epoch.length                             | 10s   |
    32        | market.liquidity.earlyExitPenalty                   | 0.25  |
    33        | market.liquidity.maximumLiquidityFeeFactorLevel     | 0.25  |
    34      #risk factor short:3.5569036
    35      #risk factor long:0.801225765
    36      And the following assets are registered:
    37        | id  | decimal places |
    38        | USD | 0              |
    39      And the fees configuration named "fees-config-1":
    40        | maker fee | infrastructure fee |
    41        | 0.0004    | 0.001              |
    42  
    43      And the liquidity sla params named "SLA-22":
    44        | price range | commitment min time fraction | performance hysteresis epochs | sla competition factor |
    45        | 0.5         | 0.6                          | 1                             | 1.0                    |
    46  
    47      And the markets:
    48        | id        | quote name | asset | liquidity monitoring | risk model            | margin calculator   | auction duration | fees          | price monitoring | data source config     | linear slippage factor | quadratic slippage factor | sla params |
    49        | ETH/MAR22 | USD        | USD   | lqm-params           | log-normal-risk-model | margin-calculator-1 | 2                | fees-config-1 | default-none     | default-eth-for-future | 1e0                    | 0                         | SLA-22     |
    50  
    51      # Setting up the accounts and vAMM submission now is part of the background, because we'll be running scenarios 0090-VAMM-006 through 0090-VAMM-014 on this setup
    52      Given the parties deposit on asset's general account the following amount:
    53        | party  | asset | amount  |
    54        | lp1    | USD   | 1000000 |
    55        | lp2    | USD   | 1000000 |
    56        | lp3    | USD   | 1000000 |
    57        | party1 | USD   | 1000000 |
    58        | party2 | USD   | 1000000 |
    59        | party3 | USD   | 1000000 |
    60        | party4 | USD   | 1000000 |
    61        | party5 | USD   | 1000000 |
    62        | vamm1  | USD   | 1000000 |
    63  
    64      When the parties submit the following liquidity provision:
    65        | id   | party | market id | commitment amount | fee   | lp type    |
    66        | lp_1 | lp1   | ETH/MAR22 | 600               | 0.02  | submission |
    67        | lp_2 | lp2   | ETH/MAR22 | 400               | 0.015 | submission |
    68      Then the network moves ahead "4" blocks
    69      And the current epoch is "0"
    70  
    71      And the parties place the following orders:
    72        | party  | market id | side | volume | price | resulting trades | type       | tif     | reference |
    73        | lp1    | ETH/MAR22 | buy  | 20     | 40    | 0                | TYPE_LIMIT | TIF_GTC | lp1-b     |
    74        | party1 | ETH/MAR22 | buy  | 1      | 100   | 0                | TYPE_LIMIT | TIF_GTC |           |
    75        | party2 | ETH/MAR22 | sell | 1      | 100   | 0                | TYPE_LIMIT | TIF_GTC |           |
    76        | lp1    | ETH/MAR22 | sell | 10     | 160   | 0                | TYPE_LIMIT | TIF_GTC | lp1-s     |
    77      When the opening auction period ends for market "ETH/MAR22"
    78      Then the following trades should be executed:
    79        | buyer  | price | size | seller |
    80        | party1 | 100   | 1    | party2 |
    81  
    82      And the market data for the market "ETH/MAR22" should be:
    83        | mark price | trading mode            | target stake | supplied stake | open interest | ref price | mid price | static mid price |
    84        | 100        | TRADING_MODE_CONTINUOUS | 39           | 1000           | 1             | 100       | 100       | 100              |
    85      When the parties submit the following AMM:
    86        | party | market id | amount | slippage | base | lower bound | upper bound | lower leverage | upper leverage | proposed fee |
    87        | vamm1 | ETH/MAR22 | 100000 | 0.1      | 100  | 85          | 150         | 4              | 4              | 0.01         |
    88      Then the AMM pool status should be:
    89        | party | market id | amount | status        | base | lower bound | upper bound | lower leverage | upper leverage |
    90        | vamm1 | ETH/MAR22 | 100000 | STATUS_ACTIVE | 100  | 85          | 150         | 4              | 4              |
    91  
    92      And set the following AMM sub account aliases:
    93        | party | market id | alias    |
    94        | vamm1 | ETH/MAR22 | vamm1-id |
    95      And the following transfers should happen:
    96        | from  | from account         | to       | to account           | market id | amount | asset | is amm | type                             |
    97        | vamm1 | ACCOUNT_TYPE_GENERAL | vamm1-id | ACCOUNT_TYPE_GENERAL |           | 100000 | USD   | true   | TRANSFER_TYPE_AMM_LOW |
    98  
    99    @VAMM
   100    Scenario: 0090-VAMM-006: If other traders trade to move the market mid price to 140 the vAMM has a short position.
   101      When the parties place the following orders:
   102        | party  | market id | side | volume | price | resulting trades | type       | tif     |
   103        | party4 | ETH/MAR22 | buy  | 245    | 141   | 1                | TYPE_LIMIT | TIF_GTC |
   104      # see the trades that make the vAMM go short
   105      Then the following trades should be executed:
   106        | buyer  | price | size | seller   | is amm |
   107        | party4 | 118   | 245  | vamm1-id | true   |
   108      And the network moves ahead "1" blocks
   109      # Check best offer/bid as this scenario matches 0090-VAMM-027: if other traders trade to move the market mid price to 140 quotes with a mid price of 140 (volume quotes above 140 should be sells, volume quotes below 140 should be buys).
   110      Then the market data for the market "ETH/MAR22" should be:
   111        | mark price | trading mode            | mid price | static mid price | best offer price | best bid price |
   112        | 118        | TRADING_MODE_CONTINUOUS | 140       | 140              | 141              | 139            |
   113      Then the parties should have the following profit and loss:
   114        | party    | volume | unrealised pnl | realised pnl | is amm |
   115        | party4   | 245    | 0              | 0            |        |
   116        | vamm1-id | -245   | 0              | 0            | true   |
   117  
   118    @VAMM
   119    Scenario: 0090-VAMM-007: If other traders trade to move the market mid price to 90 the vAMM has a long position.
   120      When the parties place the following orders:
   121        | party  | market id | side | volume | price | resulting trades | type       | tif     |
   122        | party4 | ETH/MAR22 | sell | 350    | 90    | 1                | TYPE_LIMIT | TIF_GTC |
   123      # see the trades that make the vAMM go long
   124      Then the following trades should be executed:
   125        | buyer    | price | size | seller | is amm |
   126        | vamm1-id | 95    | 350  | party4 | true   |
   127      And the network moves ahead "1" blocks
   128      Then the market data for the market "ETH/MAR22" should be:
   129        | mark price | trading mode            | mid price | static mid price |
   130        | 95         | TRADING_MODE_CONTINUOUS | 90        | 90               | # TODO why isn't this 90?
   131  	  Then the parties should have the following profit and loss:
   132        | party    | volume | unrealised pnl | realised pnl | is amm |
   133        | party4   | -350   | 0              | 0            |        |
   134        | vamm1-id | 350    | 0              | 0            | true   |
   135  
   136    @VAMM
   137    Scenario: 0090-VAMM-008: If other traders trade to move the market mid price to 150 the vAMM will post no further sell orders above this price, and the vAMM's position notional value will be equal to 4x its total account balance.
   138      And the market data for the market "ETH/MAR22" should be:
   139        | mark price | trading mode            | mid price | static mid price | best offer price | best bid price |
   140        | 100        | TRADING_MODE_CONTINUOUS | 100       | 100              | 101              | 99             |
   141      When the parties place the following orders:
   142        | party  | market id | side | volume | price | resulting trades | type       | tif     |
   143        | party4 | ETH/MAR22 | buy  | 500    | 155   | 1                | TYPE_LIMIT | TIF_GTC |
   144      # see the trades that make the vAMM go short
   145      Then the following trades should be executed:
   146        | buyer  | price | size | seller   | is amm |
   147        | party4 | 122   | 291  | vamm1-id | true   |
   148      And the market data for the market "ETH/MAR22" should be:
   149        | mark price | trading mode            | mid price | static mid price | best offer price | best bid price |
   150        | 100        | TRADING_MODE_CONTINUOUS | 157       | 157              | 160              | 155            |
   151  
   152      # trying to trade again causes no trades because the AMM has no more volume
   153      When the parties place the following orders:
   154        | party  | market id | side | volume | price   | resulting trades | type       | tif     |
   155        | party4 | ETH/MAR22 | buy  | 500    | 150     | 0                | TYPE_LIMIT | TIF_GTC |
   156  
   157      # the AMM's mid price has moved to 150, but it has no volume +150 so that best offer comes from the orderbook of 160
   158      Then the market data for the market "ETH/MAR22" should be:
   159        | mark price | trading mode            | mid price | static mid price | best offer price | best bid price |
   160        | 100        | TRADING_MODE_CONTINUOUS | 157       | 157              | 160              | 155            |
   161  
   162      When the network moves ahead "1" blocks
   163  	Then the parties should have the following profit and loss:
   164        | party    | volume | unrealised pnl | realised pnl | is amm |
   165        | party4   | 291    | 0              | 0            |        |
   166        | vamm1-id | -291   | 0              | 0            | true   |
   167      # Notional value therefore is 317 * 122
   168      And the market data for the market "ETH/MAR22" should be:
   169        | mark price | trading mode            | mid price | static mid price | best offer price | best bid price |
   170        | 122        | TRADING_MODE_CONTINUOUS | 157       | 157              | 160              | 155            |
   171      
   172      # vAMM receives fees, but loses out in the MTM settlement
   173      And the following transfers should happen:
   174         | from     | from account            | to       | to account              | market id | amount | asset | is amm | type                            |
   175         |          | ACCOUNT_TYPE_FEES_MAKER | vamm1-id | ACCOUNT_TYPE_GENERAL    | ETH/MAR22 | 143    | USD   | true   | TRANSFER_TYPE_MAKER_FEE_RECEIVE |
   176         | vamm1-id | ACCOUNT_TYPE_GENERAL    | vamm1-id | ACCOUNT_TYPE_MARGIN     | ETH/MAR22 | 74548  | USD   | true   | TRANSFER_TYPE_MARGIN_LOW        |
   177  
   178      When the parties place the following orders:
   179        | party  | market id | side | volume | price | resulting trades | type       | tif     |
   180        | party5 | ETH/MAR22 | buy  | 1      | 160   | 1                | TYPE_LIMIT | TIF_GTC |
   181      Then the following trades should be executed:
   182        | buyer  | price | size | seller | is amm |
   183        | party5 | 160   | 1    | lp1    | false  |
   184  
   185      When the network moves ahead "1" blocks
   186  	Then the parties should have the following profit and loss:
   187        | party    | volume | unrealised pnl | realised pnl | is amm |
   188        | party4   | 291    | 11058          | 0            |        |
   189        | party5   | 1      | 0              | 0            |        |
   190        | lp1      | -1     | 0              | 0            |        |
   191        | vamm1-id | -291   | -11058         | 0            | true   |
   192  
   193  
   194    @VAMM
   195    Scenario: 0090-VAMM-009: If other traders trade to move the market mid price to 85 the vAMM will post no further buy orders below this price, and the vAMM's position notional value will be equal to 4x its total account balance.
   196      When the parties place the following orders:
   197        | party  | market id | side | volume | price | resulting trades | type       | tif     |
   198        | party4 | ETH/MAR22 | sell | 581    | 80    | 1                | TYPE_LIMIT | TIF_GTC |
   199  
   200      # AMM is at its bound so will have no orders below 85 so best bid will be 40 which is an LP order from the test setup
   201      # best offer will be 86 which is quoted from the pool
   202      Then the market data for the market "ETH/MAR22" should be:
   203        | mark price | trading mode            | mid price | static mid price | best offer price | best bid price |
   204        | 100        | TRADING_MODE_CONTINUOUS | 63        | 63               | 86               | 40             |
   205      And the following trades should be executed:
   206        | buyer    | price | size | seller | is amm |
   207        | vamm1-id | 92    | 581  | party4 | true   |
   208  
   209      When the network moves ahead "1" blocks
   210  	Then the parties should have the following profit and loss:
   211        | party    | volume | unrealised pnl | realised pnl | is amm |
   212        | party1   | 1      | -8             | 0            |        |
   213        | party2   | -1     | 8              | 0            |        |
   214        | party4   | -581   | 0              | 0            |        |
   215        | vamm1-id | 581    | 0              | 0            | true   |
   216      # vAMM receives fees, but loses out in the MTM settlement
   217      And the following transfers should happen:
   218        | from     | from account            | to       | to account           | market id | amount | asset | is amm | type                            |
   219        |          | ACCOUNT_TYPE_FEES_MAKER | vamm1-id | ACCOUNT_TYPE_GENERAL | ETH/MAR22 | 214    | USD   | true   | TRANSFER_TYPE_MAKER_FEE_RECEIVE |
   220        | vamm1-id | ACCOUNT_TYPE_GENERAL    | vamm1-id | ACCOUNT_TYPE_MARGIN  | ETH/MAR22 | 100214 | USD   | true   | TRANSFER_TYPE_MARGIN_LOW        |
   221  
   222      # Now make sure we don't trade with vAMM below 85
   223      When the parties place the following orders:
   224        | party  | market id | side | volume | price | resulting trades | type       | tif     |
   225        | party4 | ETH/MAR22 | sell | 10     | 75    | 0                | TYPE_LIMIT | TIF_GTC |
   226      And the parties place the following orders:
   227        | party  | market id | side | volume | price | resulting trades | type       | tif     |
   228        | party3 | ETH/MAR22 | buy  | 10     | 75    | 1                | TYPE_LIMIT | TIF_GTC |
   229  
   230      # trade does not happen with the AMM
   231      Then the following trades should be executed:
   232        | buyer  | price | size | seller | is amm |
   233        | party3 | 75    | 10   | party4 | false  |
   234      When the network moves ahead "1" blocks
   235  	  Then the parties should have the following profit and loss:
   236        | party    | volume | unrealised pnl | realised pnl | is amm |
   237        | party1   | 1      | -25            | 0            |        |
   238        | party2   | -1     | 25             | 0            |        |
   239        | party3   | 10     | 0              | 0            |        |
   240        | party4   | -591   | 9877           | 0            |        |
   241        | vamm1-id | 581    | -9877          | 0            | true   |
   242      And the market data for the market "ETH/MAR22" should be:
   243        | mark price | trading mode            | mid price | static mid price | best offer price | best bid price |
   244        | 75         | TRADING_MODE_CONTINUOUS | 63        | 63               | 86               | 40             |
   245      # TODO: vamm does not appear to have any notional. Neither party nor alias work.
   246      #And the AMM "vamm1-id" has the following taker notional "4000"
   247      #And the party "vamm1" has the following taker notional "4000"
   248  
   249    @VAMM
   250    Scenario: 0090-VAMM-010: If other traders trade to move the market mid price to 110 and then trade to move the mid price back to 100 the vAMM will have a position of 0.
   251      When the parties place the following orders:
   252        | party  | market id | side | volume | price | resulting trades | type       | tif     |
   253        | party4 | ETH/MAR22 | buy  | 74     | 110   | 1                | TYPE_LIMIT | TIF_GTC |
   254      Then the market data for the market "ETH/MAR22" should be:
   255        | mark price | trading mode            | target stake | supplied stake | open interest | ref price | mid price | static mid price | best offer price | best bid price |
   256        | 100        | TRADING_MODE_CONTINUOUS | 2999         | 1000           | 75            | 100       | 110       | 110              | 111              | 109            |
   257      # see the trades that make the vAMM go short
   258      And the following trades should be executed:
   259        | buyer  | price | size | seller   | is amm |
   260        | party4 | 104   | 74   | vamm1-id | true   |
   261      When the network moves ahead "1" blocks
   262  	Then the parties should have the following profit and loss:
   263        | party    | volume | unrealised pnl | realised pnl | is amm |
   264        | party1   | 1      | 4              | 0            |        |
   265        | party2   | -1     | -4             | 0            |        |
   266        | party4   | 74     | 0              | 0            |        |
   267        | vamm1-id | -74    | 0              | 0            | true   |
   268      # now return the price back to 100, vAMM should hold position of 0
   269      When the parties place the following orders:
   270        | party  | market id | side | volume | price | resulting trades | type       | tif     |
   271        | party3 | ETH/MAR22 | sell | 74     | 100   | 1                | TYPE_LIMIT | TIF_GTC |
   272      Then the market data for the market "ETH/MAR22" should be:
   273        | mark price | trading mode            | target stake | supplied stake | open interest | ref price | mid price | static mid price | best offer price | best bid price |
   274        | 104        | TRADING_MODE_CONTINUOUS | 3119         | 1000           | 75            | 100       | 100       | 100              | 101              | 99             |
   275      And the following trades should be executed:
   276        | buyer    | price | size | seller | is amm |
   277        | vamm1-id | 104   | 74   | party3 | true   |
   278      When the network moves ahead "1" blocks
   279  	Then the parties should have the following profit and loss:
   280        | party    | volume | unrealised pnl | realised pnl | is amm |
   281        | party1   | 1      | 4              | 0            |        |
   282        | party2   | -1     | -4             | 0            |        |
   283        | party3   | -74    | 0              | 0            |        |
   284        | party4   | 74     | 0              | 0            |        |
   285        | vamm1-id | 0      | 0              | 0            | true   |
   286  
   287    @VAMM
   288    Scenario: 0090-VAMM-011: If other traders trade to move the market mid price to 90 and then trade to move the mid price back to 100 the vAMM will have a position of 0.
   289      When the parties place the following orders:
   290        | party  | market id | side | volume | price | resulting trades | type       | tif     |
   291        | party3 | ETH/MAR22 | sell | 371    | 90    | 1                | TYPE_LIMIT | TIF_GTC |
   292      Then the market data for the market "ETH/MAR22" should be:
   293        | mark price | trading mode            | target stake | supplied stake | open interest | ref price | mid price | static mid price | best offer price | best bid price |
   294        | 100        | TRADING_MODE_CONTINUOUS | 14875        | 1000           | 372           | 100       | 90        | 90               | 91               | 89             |
   295      # see the trades that make the vAMM go short
   296      And the following trades should be executed:
   297        | buyer    | price | size | seller | is amm |
   298        | vamm1-id | 94    | 371  | party3 | true   |
   299      When the network moves ahead "1" blocks
   300  	Then the parties should have the following profit and loss:
   301        | party    | volume | unrealised pnl | realised pnl | is amm |
   302        | party1   | 1      | -6             | 0            |        |
   303        | party2   | -1     | 6              | 0            |        |
   304        | party3   | -371   | 0              | 0            |        |
   305        | vamm1-id | 371    | 0              | 0            | true   |
   306      # move price back up to 100
   307      When the parties place the following orders:
   308        | party  | market id | side | volume | price | resulting trades | type       | tif     |
   309        | party4 | ETH/MAR22 | buy  | 371    | 100   | 1                | TYPE_LIMIT | TIF_GTC |
   310      Then the market data for the market "ETH/MAR22" should be:
   311        | mark price | trading mode            | ref price | mid price | static mid price | best offer price | best bid price |
   312        | 94         | TRADING_MODE_CONTINUOUS | 100       | 100       | 100              | 101              | 99             |
   313      And the following trades should be executed:
   314        | buyer  | price | size | seller   | is amm |
   315        | party4 | 94    | 371  | vamm1-id | true   |
   316  
   317      When the network moves ahead "1" blocks
   318      # vAMM should not hold a position, but apparently it does, vAMM switched sides, this is a know bug with incoming fix
   319  	Then the parties should have the following profit and loss:
   320        | party    | volume | unrealised pnl | realised pnl | is amm |
   321        | party1   | 1      | -6             | 0            |        |
   322        | party2   | -1     | 6              | 0            |        |
   323        | party3   | -371   | 0              | 0            |        |
   324        | party4   | 371    | 0              | 0            |        |
   325        | vamm1-id | 0      | 0              | 0            | true   |
   326  
   327    @VAMM
   328    Scenario: 0090-VAMM-012: If other traders trade to move the market mid price to 90 and then in one trade move the mid price to 110 then trade to move the mid price back to 100 the vAMM will have a position of 0
   329      # Move mid price to 90
   330      When the parties place the following orders:
   331        | party  | market id | side | volume | price | resulting trades | type       | tif     |
   332        | party3 | ETH/MAR22 | sell | 371    | 90    | 1                | TYPE_LIMIT | TIF_GTC |
   333      Then the market data for the market "ETH/MAR22" should be:
   334        | mark price | trading mode            | ref price | mid price | static mid price | best offer price | best bid price |
   335        | 100        | TRADING_MODE_CONTINUOUS | 100       | 90        | 90               | 91               | 89             |
   336      And the following trades should be executed:
   337        | buyer    | price | size | seller | is amm |
   338        | vamm1-id | 94    | 371  | party3 | true   |
   339      # Check vAMM position
   340      When the network moves ahead "1" blocks
   341  	Then the parties should have the following profit and loss:
   342        | party    | volume | unrealised pnl | realised pnl | is amm |
   343        | party1   | 1      | -6             | 0            |        |
   344        | party2   | -1     | 6              | 0            |        |
   345        | party3   | -371   | 0              | 0            |        |
   346        | vamm1-id | 371    | 0              | 0            | true   |
   347  
   348      # In a single trade, move the mid price to 110
   349      When the parties place the following orders:
   350        | party  | market id | side | volume | price | resulting trades | type       | tif     |
   351        | party4 | ETH/MAR22 | buy  | 440    | 110   | 2                | TYPE_LIMIT | TIF_GTC |
   352      Then the market data for the market "ETH/MAR22" should be:
   353        | mark price | trading mode            | ref price | mid price | static mid price | best offer price | best bid price |
   354        | 94         | TRADING_MODE_CONTINUOUS | 100       | 110       | 110              | 111              | 109            |
   355      And the following trades should be executed:
   356        | buyer  | price | size | seller   | is amm |
   357        | party4 | 94    | 371  | vamm1-id | true   |
   358        | party4 | 104   | 69   | vamm1-id | true   |
   359      # Check the resulting position, vAMM switched from long to short
   360      When the network moves ahead "1" blocks
   361  	Then the parties should have the following profit and loss:
   362        | party    | volume | unrealised pnl | realised pnl | is amm |
   363        | party1   | 1      | 4              | 0            |        |
   364        | party2   | -1     | -4             | 0            |        |
   365        | party3   | -371   | -3710          | 0            |        |
   366        | party4   | 440    | 3710           | 0            |        |
   367        | vamm1-id | -69    | 0              | 0            | true   |
   368  
   369      # Now return the mid price back to 100
   370      When the parties place the following orders:
   371        | party  | market id | side | volume | price | resulting trades | type       | tif     |
   372        | party5 | ETH/MAR22 | sell | 69     | 100   | 1                | TYPE_LIMIT | TIF_GTC |
   373      Then the market data for the market "ETH/MAR22" should be:
   374        | mark price | trading mode            | ref price | mid price | static mid price | best offer price | best bid price |
   375        | 104        | TRADING_MODE_CONTINUOUS | 100       | 100       | 100              | 101              | 99             |
   376      And the following trades should be executed:
   377        | buyer    | price | size | seller | is amm |
   378        | vamm1-id | 104   | 69   | party5 | true   |
   379      # Check the resulting position, vAMM should hold 0
   380      When the network moves ahead "1" blocks
   381  	Then the parties should have the following profit and loss:
   382        | party    | volume | unrealised pnl | realised pnl | is amm |
   383        | party1   | 1      | 4              | 0            |        |
   384        | party2   | -1     | -4             | 0            |        |
   385        | party3   | -371   | -3710          | 0            |        |
   386        | party4   | 440    | 3710           | 0            |        |
   387        | party5   | -69    | 0              | 0            |        |
   388        | vamm1-id | 0      | 0              | 0            | true   |
   389  
   390    @VAMM
   391    Scenario: 0090-VAMM-013: If other traders trade to move the market mid price to 90 and then move the mid price back to 100 in several trades of varying size, the vAMM will have a position of 0.
   392      When the parties place the following orders:
   393        | party  | market id | side | volume | price | resulting trades | type       | tif     |
   394        | party3 | ETH/MAR22 | sell | 350    | 90    | 1                | TYPE_LIMIT | TIF_GTC |
   395      Then the market data for the market "ETH/MAR22" should be:
   396        | mark price | trading mode            | target stake | supplied stake | open interest | ref price | mid price | static mid price | best offer price | best bid price |
   397        | 100        | TRADING_MODE_CONTINUOUS | 14035        | 1000           | 351           | 100       | 90        | 90               | 91               | 89             |
   398      # see the trades that make the vAMM go short
   399      And the following trades should be executed:
   400        | buyer    | price | size | seller | is amm |
   401        | vamm1-id | 95    | 350  | party3 | true   |
   402      When the network moves ahead "1" blocks
   403  	Then the parties should have the following profit and loss:
   404        | party    | volume | unrealised pnl | realised pnl | is amm |
   405        | party1   | 1      | -5             | 0            |        |
   406        | party2   | -1     | 5              | 0            |        |
   407        | party3   | -350   | 0              | 0            |        |
   408        | vamm1-id | 350    | 0              | 0            | true   |
   409      # move price back up to 100, in several trades of varying sizes
   410      When the parties place the following orders:
   411        | party  | market id | side | volume | price | resulting trades | type       | tif     |
   412        | party1 | ETH/MAR22 | buy  | 99     | 100   | 1                | TYPE_LIMIT | TIF_GTC |
   413        | party4 | ETH/MAR22 | buy  | 121    | 100   | 1                | TYPE_LIMIT | TIF_GTC |
   414        | party5 | ETH/MAR22 | buy  | 130    | 100   | 1                | TYPE_LIMIT | TIF_GTC |
   415      Then the market data for the market "ETH/MAR22" should be:
   416        | mark price | trading mode            | ref price | mid price | static mid price | best offer price | best bid price |
   417        | 95         | TRADING_MODE_CONTINUOUS | 100       | 100       | 100              | 101              | 99             |
   418      And the following trades should be executed:
   419        | buyer  | price | size | seller   | is amm |
   420        | party1 | 91    | 99   | vamm1-id | true   |
   421        | party4 | 94    | 121  | vamm1-id | true   |
   422        | party5 | 97    | 130  | vamm1-id | true   |
   423  
   424      When the network moves ahead "1" blocks
   425      # vAMM should not hold a position, but apparently it does, vAMM switched sides, this is a know bug with incoming fix
   426  	Then the parties should have the following profit and loss:
   427        | party    | volume | unrealised pnl | realised pnl | is amm |
   428        | party1   | 100    | 591            | 0            |        |
   429        | party2   | -1     | 3              | 0            |        |
   430        | party3   | -350   | -700           | 0            |        |
   431        | party4   | 121    | 363            | 0            |        |
   432        | party5   | 130    | 0              | 0            |        |
   433        | vamm1-id | 0      | 0              | -257         | true   |
   434  
   435    @VAMM
   436    Scenario: 0090-VAMM-014: If other traders trade to move the market mid price to 90 and then in one trade move the mid price to 110 then trade to move the mid price to 120 the vAMM will have a larger (more negative) but comparable position to if they had been moved straight from 100 to 120.
   437      # Move mid price to 90
   438      When the parties place the following orders:
   439        | party  | market id | side | volume | price | resulting trades | type       | tif     |
   440        | party3 | ETH/MAR22 | sell | 350    | 90    | 1                | TYPE_LIMIT | TIF_GTC |
   441      Then the market data for the market "ETH/MAR22" should be:
   442        | mark price | trading mode            | target stake | supplied stake | open interest | ref price | mid price | static mid price | best offer price | best bid price |
   443        | 100        | TRADING_MODE_CONTINUOUS | 14035        | 1000           | 351           | 100       | 90        | 90               | 91               | 89             |
   444      And the following trades should be executed:
   445        | buyer    | price | size | seller | is amm |
   446        | vamm1-id | 95    | 350  | party3 | true   |
   447      # Check vAMM position
   448      When the network moves ahead "1" blocks
   449  	Then the parties should have the following profit and loss:
   450        | party    | volume | unrealised pnl | realised pnl | is amm |
   451        | party1   | 1      | -5             | 0            |        |
   452        | party2   | -1     | 5              | 0            |        |
   453        | party3   | -350   | 0              | 0            |        |
   454        | vamm1-id | 350    | 0              | 0            | true   |
   455  
   456      # In a single trade, move the mid price to 110
   457      When the parties place the following orders:
   458        | party  | market id | side | volume | price | resulting trades | type       | tif     |
   459        | party4 | ETH/MAR22 | buy  | 420    | 110   | 2                | TYPE_LIMIT | TIF_GTC |
   460      Then the market data for the market "ETH/MAR22" should be:
   461        | mark price | trading mode            | ref price | mid price | static mid price | best offer price | best bid price |
   462        | 95         | TRADING_MODE_CONTINUOUS | 100       | 110       | 110              | 111              | 109            |
   463      And the following trades should be executed:
   464        | buyer  | price | size | seller   | is amm |
   465        | party4 | 94    | 350  | vamm1-id | true   |
   466        | party4 | 104   | 70   | vamm1-id | true   |
   467      # Check the resulting position, vAMM switched from long to short
   468      When the network moves ahead "1" blocks
   469  	Then the parties should have the following profit and loss:
   470        | party    | volume | unrealised pnl | realised pnl | is amm |
   471        | party1   | 1      | 4              | 0            |        |
   472        | party2   | -1     | -4             | 0            |        |
   473        | party3   | -350   | -3150          | 0            |        |
   474        | party4   | 420    | 3500           | 0            |        |
   475        | vamm1-id | -70    | 0              | -350         | true   |
   476  
   477      # Now further increase the mid price, move it up to 120
   478      When the parties place the following orders:
   479        | party  | market id | side | volume | price | resulting trades | type       | tif     |
   480        | party5 | ETH/MAR22 | buy  | 65     | 120   | 1                | TYPE_LIMIT | TIF_GTC |
   481      Then the market data for the market "ETH/MAR22" should be:
   482        | mark price | trading mode            | ref price | mid price | static mid price | best offer price | best bid price |
   483        | 104        | TRADING_MODE_CONTINUOUS | 100       | 120       | 120              | 121              | 119            |
   484      And the following trades should be executed:
   485        | buyer  | price | size | seller   | is amm |
   486        | party5 | 114   | 65   | vamm1-id | true   |
   487      # Check the resulting position, vAMM further increased their position
   488      When the network moves ahead "1" blocks
   489  	Then the parties should have the following profit and loss:
   490        | party    | volume | unrealised pnl | realised pnl | is amm |
   491        | party1   | 1      | 14             | 0            |        |
   492        | party2   | -1     | -14            | 0            |        |
   493        | party3   | -350   | -6650          | 0            |        |
   494        | party4   | 420    | 7700           | 0            |        |
   495        | party5   | 65     | 0              | 0            |        |
   496        | vamm1-id | -135   | -700           | -350         | true   |