code.vegaprotocol.io/vega@v0.79.0/core/integration/features/zero-position.feature (about) 1 Feature: Closeout scenarios 2 # This is a test case to demonstrate an order can be rejected when the trader (who places an initial order) does not have enouge collateral to cover the initial margin level 3 4 Background: 5 6 Given the log normal risk model named "log-normal-risk-model-1": 7 | risk aversion | tau | mu | r | sigma | 8 | 0.000001 | 0.1 | 0 | 0 | 1.0 | 9 #risk factor short = 3.55690359157934000 10 #risk factor long = 0.801225765 11 And the margin calculator named "margin-calculator-1": 12 | search factor | initial factor | release factor | 13 | 1.5 | 2 | 3 | 14 And the markets: 15 | id | quote name | asset | risk model | margin calculator | auction duration | fees | price monitoring | data source config | linear slippage factor | quadratic slippage factor | sla params | 16 | ETH/DEC19 | BTC | USD | log-normal-risk-model-1 | margin-calculator-1 | 1 | default-none | default-none | default-eth-for-future | 0.25 | 0 | default-futures | 17 | ETH/DEC20 | BTC | USD | log-normal-risk-model-1 | margin-calculator-1 | 1 | default-none | default-basic | default-eth-for-future | 0.25 | 0 | default-futures | 18 And the following network parameters are set: 19 | name | value | 20 | market.auction.minimumDuration | 1 | 21 | network.markPriceUpdateMaximumFrequency | 0s | 22 | limits.markets.maxPeggedOrders | 2 | 23 24 @ZeroPos @Liquidation 25 Scenario: 001, 2 parties get close-out at the same time. Distressed position gets taken over by LP, distressed order gets canceled (0005-COLL-002; 0012-POSR-001; 0012-POSR-002; 0012-POSR-004; 0012-POSR-005; 0007-POSN-015) 26 # setup accounts, we are trying to closeout trader3 first and then trader2 27 28 Given the insurance pool balance should be "0" for the market "ETH/DEC19" 29 30 Given the parties deposit on asset's general account the following amount: 31 | party | asset | amount | 32 | auxiliary1 | USD | 1000000000000 | 33 | auxiliary2 | USD | 1000000000000 | 34 | trader2 | USD | 2000 | 35 | trader3 | USD | 162 | 36 | lprov | USD | 1000000000000 | 37 | closer | USD | 1000000000000 | 38 | dummy | 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 | amendment | 44 And the parties place the following pegged iceberg orders: 45 | party | market id | peak size | minimum visible size | side | pegged reference | volume | offset | 46 | lprov | ETH/DEC19 | 100 | 10 | sell | ASK | 100 | 55 | 47 | lprov | ETH/DEC19 | 100 | 10 | buy | BID | 100 | 55 | 48 # place auxiliary orders so we always have best bid and best offer as to not trigger the liquidity auction 49 # trading happens at the end of the open auction period 50 Then the parties place the following orders: 51 | party | market id | side | price | volume | resulting trades | type | tif | reference | 52 | auxiliary2 | ETH/DEC19 | buy | 5 | 5 | 0 | TYPE_LIMIT | TIF_GTC | aux-b-5 | 53 | auxiliary1 | ETH/DEC19 | sell | 1000 | 10 | 0 | TYPE_LIMIT | TIF_GTC | aux-s-1000 | 54 | auxiliary2 | ETH/DEC19 | buy | 10 | 10 | 0 | TYPE_LIMIT | TIF_GTC | aux-b-1 | 55 | auxiliary1 | ETH/DEC19 | sell | 10 | 10 | 0 | TYPE_LIMIT | TIF_GTC | aux-s-1 | 56 When the opening auction period ends for market "ETH/DEC19" 57 And the trading mode should be "TRADING_MODE_CONTINUOUS" for the market "ETH/DEC19" 58 And the mark price should be "10" for the market "ETH/DEC19" 59 60 # trader2 posts and order that would take over position of trader3 if they have enough to support it at the new mark price 61 When the parties place the following orders: 62 | party | market id | side | price | volume | resulting trades | type | tif | reference | 63 | trader2 | ETH/DEC19 | buy | 50 | 40 | 0 | TYPE_LIMIT | TIF_GTC | buy-order-3 | 64 Then the order book should have the following volumes for market "ETH/DEC19": 65 | side | price | volume | 66 | buy | 5 | 5 | 67 | buy | 50 | 40 | 68 | sell | 1000 | 10 | 69 | sell | 1055 | 100 | 70 71 And the parties should have the following account balances: 72 | party | asset | market id | margin | general | 73 | trader2 | USD | ETH/DEC19 | 642 | 1358 | 74 | lprov | USD | ETH/DEC19 | 7114 | 999999892886 | 75 76 # # margin level_trader2= OrderSize*MarkPrice*RF = 40*10*0.801225765=321 77 # # margin level_Lprov= OrderSize*MarkPrice*RF = 100*10*3.55690359157934000=3557 78 79 # trader3 posts a limit order 80 When the parties place the following orders: 81 | party | market id | side | price | volume | resulting trades | type | tif | reference | 82 | trader3 | ETH/DEC19 | buy | 100 | 10 | 0 | TYPE_LIMIT | TIF_GTC | buy-position-31 | 83 | dummy | ETH/DEC19 | sell | 2000 | 1 | 0 | TYPE_LIMIT | TIF_GTC | dummy-sell-1 | 84 85 Then the order book should have the following volumes for market "ETH/DEC19": 86 | side | price | volume | 87 | buy | 5 | 5 | 88 | buy | 45 | 100 | 89 | buy | 50 | 40 | 90 | buy | 100 | 10 | 91 | sell | 1000 | 10 | 92 | sell | 1055 | 100 | 93 | sell | 2000 | 1 | 94 95 And the parties should have the following margin levels: 96 | party | market id | maintenance | search | initial | release | 97 | trader3 | ETH/DEC19 | 81 | 121 | 162 | 243 | 98 99 # This should create a zero position, ensure at least 1 tick with the order on the book 100 When the network moves ahead "1" blocks 101 And the parties cancel the following orders: 102 | party | reference | 103 | dummy | dummy-sell-1 | 104 Then the order book should have the following volumes for market "ETH/DEC19": 105 | side | price | volume | 106 | buy | 5 | 5 | 107 | buy | 45 | 100 | 108 | buy | 50 | 40 | 109 | buy | 100 | 10 | 110 | sell | 1000 | 10 | 111 | sell | 1055 | 100 | 112 #setup for close out 113 When the parties place the following orders: 114 | party | market id | side | price | volume | resulting trades | type | tif | reference | 115 | auxiliary2 | ETH/DEC19 | sell | 100 | 10 | 1 | TYPE_LIMIT | TIF_GTC | sell-provider-1 | 116 Then the network moves ahead "3" blocks 117 118 And the mark price should be "100" for the market "ETH/DEC19" 119 120 Then the following trades should be executed: 121 | buyer | price | size | seller | 122 | trader3 | 100 | 10 | auxiliary2 | 123 124 And the order book should have the following volumes for market "ETH/DEC19": 125 | side | price | volume | 126 | buy | 5 | 0 | 127 | buy | 45 | 0 | 128 | buy | 50 | 0 | 129 | buy | 100 | 0 | 130 | sell | 1000 | 10 | 131 | sell | 1055 | 100 | 132 133 # #trader3 is closed out, trader2 has no more open orders as they got cancelled after becoming distressed 134 And the parties should have the following margin levels: 135 | party | market id | maintenance | search | initial | release | 136 | trader2 | ETH/DEC19 | 0 | 0 | 0 | 0 | 137 | trader3 | ETH/DEC19 | 0 | 0 | 0 | 0 | 138 # trader3 can not be closed-out because there is not enough vol on the order book 139 And the parties should have the following account balances: 140 | party | asset | market id | margin | general | 141 | trader2 | USD | ETH/DEC19 | 0 | 2000 | 142 | trader3 | USD | ETH/DEC19 | 0 | 0 | 143 144 Then the parties should have the following profit and loss: 145 | party | volume | unrealised pnl | realised pnl | status | taker fees | taker fees since | maker fees | maker fees since | other fees | other fees since | funding payments | funding payments since | 146 | trader2 | 0 | 0 | 0 | POSITION_STATUS_ORDERS_CLOSED | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 147 | trader3 | 0 | 0 | -162 | POSITION_STATUS_CLOSED_OUT | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 148 | auxiliary1 | -10 | -900 | 0 | | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 149 | auxiliary2 | 5 | 475 | 586 | | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 150 And the insurance pool balance should be "0" for the market "ETH/DEC19" 151 When the parties place the following orders: 152 | party | market id | side | price | volume | resulting trades | type | tif | reference | 153 | auxiliary2 | ETH/DEC19 | buy | 1 | 10 | 0 | TYPE_LIMIT | TIF_GTC | sell-provider-1 | 154 155 When the parties place the following orders: 156 | party | market id | side | price | volume | resulting trades | type | tif | reference | 157 | auxiliary2 | ETH/DEC19 | sell | 100 | 10 | 0 | TYPE_LIMIT | TIF_GTC | sell-provider-1 | 158 | auxiliary1 | ETH/DEC19 | buy | 100 | 10 | 1 | TYPE_LIMIT | TIF_GTC | sell-provider-1 | 159 160 Then the network moves ahead "4" blocks 161 And the order book should have the following volumes for market "ETH/DEC19": 162 | side | price | volume | 163 | buy | 1 | 5 | 164 | buy | 5 | 0 | 165 | buy | 45 | 0 | 166 | buy | 50 | 0 | 167 | buy | 100 | 0 | 168 | sell | 1000 | 10 | 169 | sell | 1055 | 100 | 170 171 And the parties should have the following margin levels: 172 | party | market id | maintenance | search | initial | release | 173 | trader2 | ETH/DEC19 | 0 | 0 | 0 | 0 | 174 | trader3 | ETH/DEC19 | 0 | 0 | 0 | 0 | 175 And the parties should have the following account balances: 176 | party | asset | market id | margin | general | 177 | trader2 | USD | ETH/DEC19 | 0 | 2000 | 178 | trader3 | USD | ETH/DEC19 | 0 | 0 | 179 180 181 @ZeroMargin 182 Scenario: When a party voluntarily closes their position, a zero margin event should be sent 183 Given the insurance pool balance should be "0" for the market "ETH/DEC19" 184 And the parties deposit on asset's general account the following amount: 185 | party | asset | amount | 186 | auxiliary1 | USD | 1000000000000 | 187 | auxiliary2 | USD | 1000000000000 | 188 | trader2 | USD | 2000 | 189 | trader3 | USD | 162 | 190 | lprov | USD | 1000000000000 | 191 | closer | USD | 1000000000000 | 192 | dummy | USD | 1000000000000 | 193 194 Given the parties submit the following liquidity provision: 195 | id | party | market id | commitment amount | fee | lp type | 196 | lp1 | lprov | ETH/DEC19 | 100000 | 0.001 | submission | 197 | lp1 | lprov | ETH/DEC19 | 100000 | 0.001 | amendment | 198 And the parties place the following pegged iceberg orders: 199 | party | market id | peak size | minimum visible size | side | pegged reference | volume | offset | 200 | lprov | ETH/DEC19 | 100 | 10 | sell | ASK | 100 | 55 | 201 | lprov | ETH/DEC19 | 100 | 10 | buy | BID | 100 | 55 | 202 203 And the parties place the following orders: 204 | party | market id | side | price | volume | resulting trades | type | tif | reference | 205 | auxiliary2 | ETH/DEC19 | buy | 5 | 5 | 0 | TYPE_LIMIT | TIF_GTC | aux-b-5 | 206 | auxiliary1 | ETH/DEC19 | sell | 1000 | 10 | 0 | TYPE_LIMIT | TIF_GTC | aux-s-1000 | 207 | auxiliary2 | ETH/DEC19 | buy | 10 | 10 | 0 | TYPE_LIMIT | TIF_GTC | aux-b-1 | 208 | auxiliary1 | ETH/DEC19 | sell | 10 | 10 | 0 | TYPE_LIMIT | TIF_GTC | aux-s-1 | 209 When the opening auction period ends for market "ETH/DEC19" 210 Then the trading mode should be "TRADING_MODE_CONTINUOUS" for the market "ETH/DEC19" 211 And the mark price should be "10" for the market "ETH/DEC19" 212 213 # trader2 posts and order that would take over position of trader3 if they have enough to support it at the new mark price 214 When the parties place the following orders: 215 | party | market id | side | price | volume | resulting trades | type | tif | reference | 216 | trader2 | ETH/DEC19 | buy | 50 | 40 | 0 | TYPE_LIMIT | TIF_GTC | buy-order-3 | 217 Then the order book should have the following volumes for market "ETH/DEC19": 218 | side | price | volume | 219 | buy | 5 | 5 | 220 | buy | 50 | 40 | 221 | sell | 1000 | 10 | 222 | sell | 1055 | 100 | 223 224 And the parties should have the following account balances: 225 | party | asset | market id | margin | general | 226 | trader2 | USD | ETH/DEC19 | 642 | 1358 | 227 | lprov | USD | ETH/DEC19 | 7114 | 999999892886 | 228 229 # trader3 posts a limit order 230 When the parties place the following orders: 231 | party | market id | side | price | volume | resulting trades | type | tif | reference | 232 | trader3 | ETH/DEC19 | buy | 100 | 10 | 0 | TYPE_LIMIT | TIF_GTC | buy-position-31 | 233 | dummy | ETH/DEC19 | sell | 2000 | 1 | 0 | TYPE_LIMIT | TIF_GTC | dummy-sell-1 | 234 235 Then the order book should have the following volumes for market "ETH/DEC19": 236 | side | price | volume | 237 | buy | 5 | 5 | 238 | buy | 45 | 100 | 239 | buy | 50 | 40 | 240 | buy | 100 | 10 | 241 | sell | 1000 | 10 | 242 | sell | 1055 | 100 | 243 | sell | 2000 | 1 | 244 245 And the parties should have the following margin levels: 246 | party | market id | maintenance | search | initial | release | 247 | trader3 | ETH/DEC19 | 81 | 121 | 162 | 243 | 248 | dummy | ETH/DEC19 | 36 | 54 | 72 | 108 | 249 250 # Party 3 cancels their order 251 When the parties cancel the following orders: 252 | party | reference | error | 253 | dummy | dummy-sell-1 | | 254 255 # This should cause their margin to be set to 0 thanks to the margin levels event 256 Then the parties should have the following margin levels: 257 | party | market id | maintenance | search | initial | release | 258 | dummy | ETH/DEC19 | 0 | 0 | 0 | 0 | 259