code.vegaprotocol.io/vega@v0.79.0/core/integration/features/verified/0015-INSR-insurance_pool_balance_test.feature (about) 1 Feature: Test closeout type 1: margin >= cost of closeout 2 3 @PanicDebug 4 Scenario: case 1 (using simple risk model) from https://docs.google.com/spreadsheets/d/1CIPH0aQmIKj6YeFW9ApP_l-jwB4OcsNQ/edit#gid=1555964910 (0015-INSR-001, 0015-INSR-003, 0018-RSKM-001, 0018-RSKM-003, 0010-MARG-004, 0010-MARG-005, 0010-MARG-006, 0010-MARG-007, 0010-MARG-008. 0010-MARG-009) 5 Given the simple risk model named "simple-risk-model-1": 6 | long | short | max move up | min move down | probability of trading | 7 | 1 | 2 | 100 | -100 | 0.1 | 8 9 And the margin calculator named "margin-calculator-1": 10 | search factor | initial factor | release factor | 11 | 1.3 | 1.5 | 2 | 12 13 And the markets: 14 | id | quote name | asset | risk model | margin calculator | auction duration | fees | price monitoring | data source config | linear slippage factor | quadratic slippage factor | sla params | 15 | ETH/DEC19 | USD | USD | simple-risk-model-1 | margin-calculator-1 | 1 | default-none | default-none | default-eth-for-future | 0.25 | 0 | default-futures | 16 And the following network parameters are set: 17 | name | value | 18 | market.auction.minimumDuration | 1 | 19 | network.markPriceUpdateMaximumFrequency | 0s | 20 | limits.markets.maxPeggedOrders | 2 | 21 22 Given the insurance pool balance should be "0" for the market "ETH/DEC19" 23 Given the initial insurance pool balance is "15000" for all the markets 24 Given the parties deposit on asset's general account the following amount: 25 | party | asset | amount | 26 | sellSideProvider | USD | 1000000000 | 27 | buySideProvider | USD | 1000000000 | 28 | party1 | USD | 30000 | 29 | party2 | USD | 50000000 | 30 | party3 | USD | 30500 | 31 | aux1 | USD | 1000000000 | 32 | aux2 | USD | 1000000000 | 33 | lpprov | USD | 1000000000 | 34 35 When the parties submit the following liquidity provision: 36 | id | party | market id | commitment amount | fee | lp type | 37 | lp1 | lpprov | ETH/DEC19 | 90000 | 0.1 | submission | 38 | lp1 | lpprov | ETH/DEC19 | 90000 | 0.1 | submission | 39 When the parties place the following orders: 40 | party | market id | side | volume | price | resulting trades | type | tif | reference | 41 | lpprov | ETH/DEC19 | buy | 1286 | 70 | 0 | TYPE_LIMIT | TIF_GTC | peg-1 | 42 | lpprov | ETH/DEC19 | sell | 563 | 160 | 0 | TYPE_LIMIT | TIF_GTC | peg-2 | 43 44 # setup order book 45 When the parties place the following orders: 46 | party | market id | side | volume | price | resulting trades | type | tif | reference | 47 | sellSideProvider | ETH/DEC19 | sell | 1000 | 150 | 0 | TYPE_LIMIT | TIF_GTC | sell-provider-1 | 48 | aux1 | ETH/DEC19 | sell | 1 | 300 | 0 | TYPE_LIMIT | TIF_GTC | aux-s-1 | 49 | aux1 | ETH/DEC19 | sell | 1 | 100 | 0 | TYPE_LIMIT | TIF_GTC | aux-s-2 | 50 | aux2 | ETH/DEC19 | buy | 1 | 100 | 0 | TYPE_LIMIT | TIF_GTC | aux-b-2 | 51 | buySideProvider | ETH/DEC19 | buy | 1000 | 80 | 0 | TYPE_LIMIT | TIF_GTC | buy-provider-1 | 52 | aux2 | ETH/DEC19 | buy | 1 | 20 | 0 | TYPE_LIMIT | TIF_GTC | aux-b-1 | 53 Then the parties should have the following account balances: 54 | party | asset | market id | margin | general | 55 | aux1 | USD | ETH/DEC19 | 1200 | 999998800 | 56 | aux2 | USD | ETH/DEC19 | 300 | 999999700 | 57 | sellSideProvider | USD | ETH/DEC19 | 450000 | 999550000 | 58 | buySideProvider | USD | ETH/DEC19 | 150000 | 999850000 | 59 60 Then the opening auction period ends for market "ETH/DEC19" 61 And the mark price should be "100" for the market "ETH/DEC19" 62 And the trading mode should be "TRADING_MODE_CONTINUOUS" for the market "ETH/DEC19" 63 64 # party 1 place an order + we check margins 65 When the parties place the following orders with ticks: 66 | party | market id | side | volume | price | resulting trades | type | tif | reference | 67 | party1 | ETH/DEC19 | sell | 100 | 100 | 0 | TYPE_LIMIT | TIF_GTC | ref-1 | 68 69 And the parties should have the following account balances: 70 | party | asset | market id | margin | general | 71 | party1 | USD | ETH/DEC19 | 30000 | 0 | 72 Then the parties should have the following margin levels: 73 | party | market id | maintenance | initial | 74 | party1 | ETH/DEC19 | 20000 | 30000 | 75 # margin_party1 = 100*100*2=20000 76 77 # all general acc balance goes to margin account for the order, 'party1' should have 100*100*3 78 # in the margin account as its Position*Markprice*Initialfactor 79 80 # then party2 places an order, this trades with party1 and we calculate the margins again 81 When the parties place the following orders with ticks: 82 | party | market id | side | volume | price | resulting trades | type | tif | reference | 83 | party2 | ETH/DEC19 | buy | 100 | 100 | 1 | TYPE_LIMIT | TIF_GTC | ref-1 | 84 85 And the mark price should be "100" for the market "ETH/DEC19" 86 And the insurance pool balance should be "15000" for the market "ETH/DEC19" 87 88 #check margin account and margin level 89 # And the parties should have the following account balances: 90 | party | asset | market id | margin | general | 91 | party1 | USD | ETH/DEC19 | 30000 | 0 | 92 | party2 | USD | ETH/DEC19 | 30000 | 49969000 | 93 Then the parties should have the following margin levels: 94 | party | market id | maintenance | 95 | party1 | ETH/DEC19 | 22500 | 96 | party2 | ETH/DEC19 | 12500 | 97 98 Then the order book should have the following volumes for market "ETH/DEC19": 99 | side | price | volume | 100 | sell | 300 | 1 | 101 | sell | 150 | 1000 | 102 | sell | 160 | 563 | 103 | buy | 80 | 1000 | 104 | buy | 70 | 1286 | 105 | buy | 20 | 1 | 106 107 When the parties place the following orders with ticks: 108 | party | market id | side | volume | price | resulting trades | type | tif | reference | 109 | party2 | ETH/DEC19 | buy | 1 | 126 | 0 | TYPE_LIMIT | TIF_GTC | ref-1-xxx | 110 111 Then the parties cancel the following orders: 112 | party | reference | 113 | lpprov | peg-1 | 114 115 Then the parties place the following orders: 116 | party | market id | side | volume | price | resulting trades | type | tif | reference | 117 | lpprov | ETH/DEC19 | buy | 776 | 116 | 0 | TYPE_LIMIT | TIF_GTC | peg-3 | 118 119 # Margin account balance brought up to new initial level as order is placed (despite all balance being above search level) 120 And the parties should have the following account balances: 121 | party | asset | market id | margin | general | 122 | party1 | USD | ETH/DEC19 | 30000 | 0 | 123 | party2 | USD | ETH/DEC19 | 18900 | 49980100 | 124 # New margin level calculated after placing an order 125 Then the parties should have the following margin levels: 126 | party | market id | maintenance | 127 | party1 | ETH/DEC19 | 22500 | 128 | party2 | ETH/DEC19 | 12600 | 129 130 Then the order book should have the following volumes for market "ETH/DEC19": 131 | side | price | volume | 132 | sell | 300 | 1 | 133 | sell | 160 | 563 | 134 | sell | 150 | 1000 | 135 | buy | 126 | 1 | 136 | buy | 116 | 776 | 137 | buy | 80 | 1000 | 138 | buy | 20 | 1 | 139 140 When the parties place the following orders with ticks: 141 | party | market id | side | volume | price | resulting trades | type | tif | reference | 142 | party3 | ETH/DEC19 | sell | 1 | 126 | 1 | TYPE_LIMIT | TIF_GTC | ref-1-xxx | 143 And the network moves ahead "1" blocks 144 Then the mark price should be "126" for the market "ETH/DEC19" 145 And the insurance pool balance should be "38500" for the market "ETH/DEC19" 146 147 Then the parties cancel the following orders: 148 | party | reference | 149 | lpprov | peg-3 | 150 151 Then the parties place the following orders: 152 | party | market id | side | volume | price | resulting trades | type | tif | reference | 153 | lpprov | ETH/DEC19 | buy | 2286 | 70 | 0 | TYPE_LIMIT | TIF_GTC | peg-5 | 154 155 Then the order book should have the following volumes for market "ETH/DEC19": 156 | side | price | volume | 157 | sell | 300 | 1 | 158 | sell | 160 | 563 | 159 | sell | 150 | 900 | 160 | buy | 80 | 1000 | 161 | buy | 70 | 2286 | 162 | buy | 20 | 1 | 163 164 # Margin account balance not updated following a trade (above search) 165 Then the parties should have the following margin levels: 166 | party | market id | maintenance | 167 | party2 | ETH/DEC19 | 15908 | 168 169 # MTM win transfer 170 Then the following transfers should happen: 171 | from | to | from account | to account | market id | amount | asset | 172 | market | party2 | ACCOUNT_TYPE_SETTLEMENT | ACCOUNT_TYPE_MARGIN | ETH/DEC19 | 2600 | USD | 173 174 Then the order book should have the following volumes for market "ETH/DEC19": 175 | side | price | volume | 176 | sell | 150 | 900 | 177 | sell | 300 | 1 | 178 | buy | 80 | 1000 | 179 | buy | 20 | 1 | 180 181 Then the mark price should be "126" for the market "ETH/DEC19" 182 And the insurance pool balance should be "38500" for the market "ETH/DEC19" 183 184 Then the parties should have the following account balances: 185 | party | asset | market id | margin | general | 186 | party1 | USD | ETH/DEC19 | 0 | 0 | 187 | party2 | USD | ETH/DEC19 | 21500 | 49980100 | 188 | party3 | USD | ETH/DEC19 | 426 | 30061 | 189 190 Then the parties should have the following margin levels: 191 #check margin account and margin level 192 | party | market id | maintenance | 193 | party1 | ETH/DEC19 | 0 | 194 | party2 | ETH/DEC19 | 15908 | 195 | party3 | ETH/DEC19 | 284 | 196 197 And the cumulated balance for all accounts should be worth "5050075500" 198 And the insurance pool balance should be "38500" for the market "ETH/DEC19" 199 200 # order book volume change 201 Then the parties cancel the following orders: 202 | party | reference | 203 | sellSideProvider | sell-provider-1 | 204 | buySideProvider | buy-provider-1 | 205 206 When the parties place the following orders with ticks: 207 | party | market id | side | volume | price | resulting trades | type | tif | reference | 208 | sellSideProvider | ETH/DEC19 | sell | 1000 | 500 | 0 | TYPE_LIMIT | TIF_GTC | sell-provider-2 | 209 | buySideProvider | ETH/DEC19 | buy | 1000 | 20 | 0 | TYPE_LIMIT | TIF_GTC | buy-provider-2 | 210 211 And the trading mode should be "TRADING_MODE_CONTINUOUS" for the market "ETH/DEC19" 212 213 #check margin account and margin level 214 And the parties should have the following account balances: 215 | party | asset | market id | margin | general | 216 | party2 | USD | ETH/DEC19 | 21500 | 49980100 | 217 | party3 | USD | ETH/DEC19 | 426 | 30061 | 218 Then the parties should have the following margin levels: 219 | party | market id | maintenance | 220 | party3 | ETH/DEC19 | 284 | 221 | party2 | ETH/DEC19 | 15908 | 222 223 When the parties place the following orders with ticks: 224 | party | market id | side | volume | price | resulting trades | type | tif | 225 | party2 | ETH/DEC19 | buy | 50 | 30 | 0 | TYPE_LIMIT | TIF_GTC | 226 227 #check margin account and margin level 228 And the parties should have the following account balances: 229 | party | asset | market id | margin | general | 230 | party2 | USD | ETH/DEC19 | 33312 | 49968288 | 231 | party3 | USD | ETH/DEC19 | 426 | 30061 | 232 233 Then the parties should have the following margin levels: 234 | party | market id | maintenance | 235 | party2 | ETH/DEC19 | 22208 | 236 | party3 | ETH/DEC19 | 284 | 237 238 When the parties place the following orders with ticks: 239 | party | market id | side | volume | price | resulting trades | type | tif | 240 | party3 | ETH/DEC19 | sell | 50 | 30 | 1 | TYPE_LIMIT | TIF_GTC | 241 And the insurance pool balance should be "38500" for the market "ETH/DEC19" 242 Then the mark price should be "70" for the market "ETH/DEC19" 243 # Then the order book should have the following volumes for market "ETH/DEC19": 244 | side | price | volume | 245 | sell | 500 | 1000 | 246 | sell | 300 | 1 | 247 | buy | 20 | 1001 | 248 249 Then the parties should have the following profit and loss: 250 | party | volume | unrealised pnl | realised pnl | 251 | party3 | -51 | 56 | 0 | 252 253 #check margin account and margin level 254 And the parties should have the following account balances: 255 | party | asset | market id | margin | general | 256 | party2 | USD | ETH/DEC19 | 18507 | 49977437 | 257 | party3 | USD | ETH/DEC19 | 12049 | 18144 | 258 259 Then the parties should have the following margin levels: 260 | party | market id | maintenance | 261 | party2 | ETH/DEC19 | 12338 | 262 | party3 | ETH/DEC19 | 8033 | 263 264 Then the order book should have the following volumes for market "ETH/DEC19": 265 | side | price | volume | 266 | sell | 500 | 1000 | 267 | sell | 300 | 1 | 268 269 Scenario: case 2 using lognomal risk model (0015-INSR-003, 0010-MARG-009, 0010-MARG-010, 0010-MARG-011) 270 Given the log normal risk model named "lognormal-risk-model-fish": 271 | risk aversion | tau | mu | r | sigma | 272 | 0.001 | 0.01 | 0 | 0.0 | 1.2 | 273 #calculated risk factor long: 0.336895684; risk factor short: 0.4878731 274 275 And the price monitoring named "price-monitoring-1": 276 | horizon | probability | auction extension | 277 | 1 | 0.99999999 | 300 | 278 279 And the margin calculator named "margin-calculator-1": 280 | search factor | initial factor | release factor | 281 | 1.2 | 1.5 | 2 | 282 283 And the markets: 284 | id | quote name | asset | risk model | margin calculator | auction duration | fees | price monitoring | data source config | linear slippage factor | quadratic slippage factor | sla params | 285 | ETH/DEC19 | ETH | USD | lognormal-risk-model-fish | margin-calculator-1 | 1 | default-none | default-none | default-eth-for-future | 0.001 | 0 | default-futures | 286 287 And the following network parameters are set: 288 | name | value | 289 | market.auction.minimumDuration | 1 | 290 | network.markPriceUpdateMaximumFrequency | 0s | 291 | limits.markets.maxPeggedOrders | 2 | 292 293 # setup accounts 294 Given the parties deposit on asset's general account the following amount: 295 | party | asset | amount | 296 | sellSideProvider | USD | 1000000000 | 297 | buySideProvider | USD | 1000000000 | 298 | party1 | USD | 30000 | 299 | party2 | USD | 50000000 | 300 | party3 | USD | 30000 | 301 | aux1 | USD | 1000000000 | 302 | aux2 | USD | 1000000000 | 303 | lpprov | USD | 1000000000 | 304 305 When the parties submit the following liquidity provision: 306 | id | party | market id | commitment amount | fee | lp type | 307 | lp1 | lpprov | ETH/DEC19 | 90000 | 0.1 | submission | 308 | lp1 | lpprov | ETH/DEC19 | 90000 | 0.1 | submission | 309 And the parties place the following pegged iceberg orders: 310 | party | market id | peak size | minimum visible size | side | pegged reference | volume | offset | 311 | lpprov | ETH/DEC19 | 2280 | 1 | buy | BID | 2280 | 10 | 312 | lpprov | ETH/DEC19 | 400 | 1 | sell | ASK | 400 | 10 | 313 #And the cumulated balance for all accounts should be worth "4050075000" 314 # setup order book 315 When the parties place the following orders with ticks: 316 | party | market id | side | volume | price | resulting trades | type | tif | reference | 317 | sellSideProvider | ETH/DEC19 | sell | 1000 | 150 | 0 | TYPE_LIMIT | TIF_GTC | sell-provider-1 | 318 | aux1 | ETH/DEC19 | sell | 1 | 100 | 0 | TYPE_LIMIT | TIF_GTC | aux-s-2 | 319 | aux2 | ETH/DEC19 | buy | 1 | 100 | 0 | TYPE_LIMIT | TIF_GTC | aux-b-2 | 320 | party2 | ETH/DEC19 | buy | 100 | 80 | 0 | TYPE_LIMIT | TIF_GTC | party2-b-1 | 321 | buySideProvider | ETH/DEC19 | buy | 1000 | 70 | 0 | TYPE_LIMIT | TIF_GTC | buy-provider-1 | 322 323 Then the opening auction period ends for market "ETH/DEC19" 324 And the mark price should be "100" for the market "ETH/DEC19" 325 And the trading mode should be "TRADING_MODE_CONTINUOUS" for the market "ETH/DEC19" 326 327 When the parties place the following orders with ticks: 328 | party | market id | side | volume | price | resulting trades | type | tif | reference | 329 | party1 | ETH/DEC19 | sell | 100 | 120 | 0 | TYPE_LIMIT | TIF_GTC | party1-s-1 | 330 331 # party1 margin account: MarginInitialFactor x MaintenanceMarginLevel = 4879*1.5=7318 332 Then the parties should have the following account balances: 333 | party | asset | market id | margin | general | 334 | party1 | USD | ETH/DEC19 | 7318 | 22682 | 335 336 # party1 maintenance margin level: position*(mark_price*risk_factor_short+slippage_per_unit) + OrderVolume x Mark_price x risk_factor_short = 100 x 100 x 0.4878731 is about 4879 337 Then the parties should have the following margin levels: 338 | party | market id | maintenance | search | initial | release | 339 | party1 | ETH/DEC19 | 4879 | 5854 | 7318 | 9758 | 340 341 # party1 place more order volume 300 342 When the parties place the following orders with ticks: 343 | party | market id | side | volume | price | resulting trades | type | tif | reference | 344 | party1 | ETH/DEC19 | sell | 300 | 120 | 0 | TYPE_LIMIT | TIF_GTC | party1-s-1 | 345 346 # party1 maintenance margin level: position*(mark_price*risk_factor_short+slippage_per_unit) + OrderVolume x Mark_price x risk_factor_short = 100 x 400 x 0.4878731 is about 19515 347 Then the parties should have the following account balances: 348 | party | asset | market id | margin | general | 349 | party1 | USD | ETH/DEC19 | 29272 | 728 | 350 351 Then the parties should have the following margin levels: 352 | party | market id | maintenance | search | initial | release | 353 | party1 | ETH/DEC19 | 19515 | 23418 | 29272 | 39030 | 354 355 And the order book should have the following volumes for market "ETH/DEC19": 356 | side | price | volume | 357 | sell | 150 | 1000 | 358 | sell | 120 | 400 | 359 | buy | 80 | 100 | 360 | buy | 70 | 3280 | 361 362 And the mark price should be "100" for the market "ETH/DEC19" 363 And the trading mode should be "TRADING_MODE_CONTINUOUS" for the market "ETH/DEC19" 364 365 ######################################### 366 #MTM closeout party1 367 When the parties place the following orders with ticks: 368 | party | market id | side | volume | price | resulting trades | type | tif | reference | 369 | aux1 | ETH/DEC19 | sell | 1 | 110 | 0 | TYPE_LIMIT | TIF_GTC | ref-4 | 370 | aux2 | ETH/DEC19 | buy | 1 | 110 | 1 | TYPE_LIMIT | TIF_GTC | ref-5 | 371 372 # margin on order should be mark_price x volume x rf = 110 x 400 x 0.4878731 = 21466 373 # margin account is above maintenance level, so it stays at 29272 374 Then the parties should have the following account balances: 375 | party | asset | market id | margin | general | 376 | party1 | USD | ETH/DEC19 | 29272 | 728 | 377 Then the parties should have the following margin levels: 378 | party | market id | maintenance | search | initial | release | 379 | party1 | ETH/DEC19 | 21467 | 25760 | 32200 | 42934 | 380 381 And the mark price should be "110" for the market "ETH/DEC19" 382 And the trading mode should be "TRADING_MODE_CONTINUOUS" for the market "ETH/DEC19" 383 384 When the parties place the following orders with ticks: 385 | party | market id | side | volume | price | resulting trades | type | tif | reference | 386 | aux1 | ETH/DEC19 | buy | 1 | 119 | 0 | TYPE_LIMIT | TIF_GTC | ref-4 | 387 | aux2 | ETH/DEC19 | sell | 1 | 119 | 1 | TYPE_LIMIT | TIF_GTC | ref-2 | 388 389 And the mark price should be "119" for the market "ETH/DEC19" 390 And the trading mode should be "TRADING_MODE_CONTINUOUS" for the market "ETH/DEC19" 391 392 And the order book should have the following volumes for market "ETH/DEC19": 393 | side | price | volume | 394 | sell | 150 | 1000 | 395 | sell | 120 | 400 | 396 | buy | 80 | 100 | 397 | buy | 70 | 3280 | 398 399 # margin on order should be mark_price x volume x rf = 119 x 400 x 0.4878731 = 23223 400 # margin account is above maintenance level, so it stays at 29272 401 Then the parties should have the following account balances: 402 | party | asset | market id | margin | general | 403 | party1 | USD | ETH/DEC19 | 29272 | 728 | 404 405 Then the parties should have the following margin levels: 406 | party | market id | maintenance | search | initial | release | 407 | party1 | ETH/DEC19 | 23223 | 27867 | 34834 | 46446 | 408 409 Then the parties should have the following profit and loss: 410 | party | volume | unrealised pnl | realised pnl | 411 | party1 | 0 | 0 | 0 | 412 413 When the parties place the following orders with ticks: 414 | party | market id | side | volume | price | resulting trades | type | tif | reference | 415 | aux1 | ETH/DEC19 | sell | 1 | 120 | 0 | TYPE_LIMIT | TIF_GTC | ref-4 | 416 | aux2 | ETH/DEC19 | buy | 1 | 120 | 1 | TYPE_LIMIT | TIF_GTC | ref-2 | 417 418 Then the parties should have the following account balances: 419 | party | asset | market id | margin | general | 420 | party1 | USD | ETH/DEC19 | 29272 | 728 | 421 Then the parties should have the following margin levels: 422 | party | market id | maintenance | 423 | party1 | ETH/DEC19 | 23419 | 424 425 426