# Fee collection during liquidation

When an account liquidation is triggered, Upvest automatically initiates collection of any outstanding AUM-based service fees as part of the same flow. This section explains the end-to-end event sequence and how to reconcile fees. We also show how to segregate collected fees from sell proceeds for post-trade settlement clients.

This only applies to clients where Upvest calculates the AUM-based fees. In addition, the account must have `fee_configuration` set and an outstanding fee amount.

For full background on fee types, configuration, and general fee collection mechanics, refer to the [Fees guide](/products/byol/guides/fees).

## How fee collection works during liquidation

Liquidation fees are collected from the cash proceeds of the account's sell orders. Unlike regular fee collection cycles, no separate sell-to-cover order is generated. Instead, the fee is deducted from the cash received as positions are sold.

Upvest initiates the fee collection immediately after receiving the liquidation request. If some of the instruments on the account take longer to trade and their orders are not executed for several days, the remaining positions will stay in account valuations and continue accumulating fees.

## End-to-end event sequence

The following is the complete sequence of events emitted during a liquidation that includes fee collection:

| **Step** | **Description** | **Event(s)** |
|  --- | --- | --- |
| **1** | Client triggers the liquidation request. | `ACCOUNT_LIQUIDATION.NEW` |
| **2** | Upvest initiates fee collection. The type is `SERVICE_FEE_LIQUIDATION`. | `FEE_COLLECTION.CREATED` |
| **3** | Sales of account positions are triggered. `initiation_flow` is set to `ACCOUNT_LIQUIDATION`. | `ORDER.NEW` × per position |
| **4a** | Emitted per sell order as each is picked up for execution. | `ORDER.PROCESSING` |
| **4b** | Emitted when the first sell order moves to processing. | `ACCOUNT_LIQUIDATION.PROCESSING` |
|  | The fee collection remains in its current state. You can use the [`GET /fees/collections/{fee_collection_id}`](/api/fees/retrieve_fee_collection) endpoint to retrieve the latest state of the fee collection. |  |
| **5a** | Emitted for each execution that occurs on the order. | `EXECUTION.FILLED` |
| **5b** | Cash balance updated with sell proceeds, immediately locked for fee processing. | `CASH_BALANCE.UPDATED` × per order execution |
| **5c** | Emitted immediately after a fill to record the cash movement due to order executions. Type is `ORDER_EXECUTION`. | `CASH_TRANSACTION.EXECUTED` × per order execution |
| **5d** | Emitted when all order executions have completed and the order is fully filled. | `ORDER.FILLED` |
| **5e** | Emitted per sell order upon settlement confirmation from the custodian. | `EXECUTION.SETTLED` × per order execution |
| **6** | Upvest reduces the cash balance when sufficient settled cash is available. Type is `FEE_COLLECTION`. *Note: This may occur before all sell orders have settled.* | `CASH_TRANSACTION.EXECUTED` |
| **7a** | Upvest generates the fee treasury report. | `TREASURY_REPORT.CREATED` |
| **7b** |  | `FEE_COLLECTION.FINALISED` |
| **8** | Emitted once all underlying sell orders have settled. | `ACCOUNT_LIQUIDATION.SETTLED` |


## Example: FEE_COLLECTION.CREATED event

The following is an example of the fee collection event emitted immediately after a liquidation is initiated:

Response
```json
{
  "id": "38fc59d0-fc42-46ef-9512-824773219b2e",
  "created_at": "2024-02-04T14:17:22Z",
  "type": "FEE_COLLECTION.CREATED",
  "webhook_id": "1b097e06-8a14-4181-b72a-de0972a3c57b",
  "object": {
    "account_group_id": "999f7566-2ca3-4007-b99d-e501be8c7222",
    "account_id": "689f7566-2ca3-4007-b99d-e501be8c7783",
    "collection_amount": "10.23",
    "created_at": "2024-02-04T14:16:22Z",
    "currency": "GBP",
    "id": "d77e6924-9bb7-4db0-addc-404442258f4b",
    "period_start": "2024-01-01",
    "period_end": "2024-02-03",
    "processed_amount": { "cash_balance": "0", "sell_to_cover": "0" },
    "status": "PROCESSING",
    "type": "SERVICE_FEE_LIQUIDATION",
    "updated_at": "2024-02-04T14:16:22Z"
  }
}
```

## How to reconcile fees

To reconcile fees collected during a liquidation, correlate events across the following identifiers:

| **#** | **Sequence** | **Identifier** |
|  --- | --- | --- |
| 1 | `ACCOUNT_LIQUIDATION` events | `account_id`, `fee_collection_id` |
| 2 | `ORDER` events | `account_id`, `order_id`, initiation flow: `ACCOUNT_LIQUIDATION` |
| 3 | `FEE_COLLECTION.CREATED` event | `account_id`, `fee_collection_id` |
| 4 | `CASH_TRANSACTION` events | type: `FEE_COLLECTION` |
| 5 | `FEE_COLLECTION.FINALISED` event | `account_id`, `fee_collection_id`, list of underlying `sell_to_cover_orders.id` |
| 6 | Fee treasury report | `fee_collection_id` |


The `fee_collection_id` is present in `account_liquidation`, `FEE_COLLECTION` events and in the `references` array of the `CASH_TRANSACTION.EXECUTED` event (type: `FEE_COLLECTION`). Use this to match the cash deduction on the account to the specific fee collection.

## How to separate fees from liquidation sell proceeds

### Pre-funding clients

For pre-funding clients, collected fee amounts are forwarded to the client in a separate treasury process.

### Post-trade settlement clients

In a post-trade settlement setup, order treasury nets daily buys and sells and performs only one cash movement per day. All liquidation orders are treated the same as other orders, and the fee amount that you need to take out of their proceeds is included in their amounts. To know which cash can be forwarded to the user and which could be part of the fee, separate the proceeds from regular sell and liquidation orders. The final separation of the fee amount can be done after the respective fee collection is finalised.

To achieve this, follow the process:

1. In daily order treasury, don't forward proceeds from the liquidation orders directly to the users right after settlement and hold them until you either confirm that there was no liquidation fee or until the liquidation fee amount is finalised and you can calculate the remaining amount. You can identify liquidation orders by the `initiation_flow` field in the order webhook events.
2. Account liquidation events contain both list of the individual orders and, starting from state `FILLED` the `fee_collection_id`. If the field `fee_collection_id` is empty in the `ACCOUNT_LIQUIDATION.FILLED` event, this means that Upvest didn't charge any fee from this account liquidation.
3. If the fee is charged and was collected successfully, you receive a `FEE_COLLECTION.FINALISED` event. This means that you can take the respective fee amount from proceeds from the related liquidation orders and forward the remaining cash to the end user.
4. You might receive treasury for some of the order executions belonging to a liquidation after finalisation of the respective fee. In this case the received cash can be forwarded to the client right away.
5. In rare cases when account liquidation proceeds are too small to cover the fee, depending on the agreements with Upvest, the fee can either be cancelled or replaced with a smaller amount.


For further guidance on fee configuration, fee models, and treasury reports, refer to the [Fee Collection](/products/byol/guides/fees/fees_collection) guides.