code.vegaprotocol.io/vega@v0.79.0/core/positions/README.md (about)

     1  # Positions engine
     2  
     3  The Positions Engine maintains a map of `partyID` to `MarketPosition struct`. In
     4  the struct are:
     5  
     6  * `size`: the actual volume (orders having been accepted)
     7  * `buy` and `sell` : volume of buy and sell orders not yet accepted
     8  
     9  For tracking actual and potential volume:
    10  
    11  * `RegisterOrder`, called in `SubmitOrder` and `AmendOrder`, adds to the `buy`
    12  xor `sell` potential volume
    13  * `UnregisterOrder`, called in `AmendOrder` and `CancelOrder`, subtracts from
    14  the `buy` xor `sell` potential volume
    15  * `Update` deals with an accepted order and does the following:
    16  * transfers actual volume from the seller to the buyer:
    17  ```
    18      buyer.size += int64(trade.Size)  // increase
    19      seller.size -= int64(trade.Size) // decrease
    20  ```
    21  * decreases potential volume for both the buyer and seller:
    22  ```
    23      buyer.buy -= int64(trade.Size)   // decrease
    24      seller.sell -= int64(trade.Size) // decrease
    25  ```
    26  The Position for a party is updated before an order is accepted/rejected.
    27  
    28  The Risk Engine determines if the order is acceptable.
    29  
    30  Settlement is done pre-trade on the old position, because a trader is liable for
    31  their position, and also on the new position.