Skip to content
All library documents

Separate Executions, Wallet Balances, and Order Risk

Article Quant Q&A · Author: PeeS

Summary

The document asks how a Bitcoin exchange should update traders’ BTC and USD wallets when limit orders are placed, cancelled, or matched. The answer separates completed trades from open-order handling: executions change the account’s position and cash, while placing, modifying, or cancelling an order affects risk and exposure rather than the transaction ledger.

For each execution, the example accounting rule adds the signed quantity to the BTC position and subtracts quantity times price from USD cash. Mark-to-market profit and loss is then cash plus position valued at the current price. Quantities and balances may be positive or negative, so the same rule can represent buys and sells. Before accepting or changing orders, risk controls should assess worst-case exposure and account for orders that are still in flight. The explanation is a compact accounting model; it does not specify exchange custody, fees, settlement, or detailed collateral and reservation policies.

Key ideas

  • Completed executions change a trader’s asset position and cash balance.
  • Signed trade quantities let one accounting rule represent buys and sells.
  • Mark-to-market profit and loss combines cash with the current value of the position.
  • Open-order placement, modification, and cancellation affect risk checks rather than completed-trade balances.
  • Risk controls should consider worst-case exposure and orders still in flight.

Tags

Full text
# Order Book vs Wallet Updates


# Order Book vs Wallet Updates












Good morning,

i am in the middle of developing my own stock exchange system and have implemented limit orders (basics) matching. The system is just a DIY solution for my own purposes and for fun mainly. The whole system is utilising my own text protocol for traders to be able to login/place orders/receive notifications. This is done for BTC exchange.

However i am stuck when it comes to calculations of the traders wallets.

I don't think i know exactly what is going on behind the scenes therefore i would really appreciate if somebody could shed a bit of light on it for me.

I believe wallets are updated only in 3 places:



- Cancel order is placed: What happens with trader wallet when he places cancel order (buy/sell)

- Match is made and a Trade is generated (full/partial) - what happens with te wallets of both traders?

What i am trying to understand is what happens with the BTC's and USD's in Traders wallets when orders are placed/cancelled/matched and how is this handled.

Thank you very much for your input into this.

## Answer by Serg (score 1, accepted)

https://quant.stackexchange.com/a/17876

Only the transactions affect the account. See example implementation below. The `position` and `money` should be initialized at the beginning. Note that both of them, and also `quantity` can be positive and negative.

```
public static class PNL {
    private double position = 0; // number of BTC
    private double money = 0.0; // USD

    public double get_position() {
        return position;
    }

    public void on_execution(double price, double quantity) {
        position += quantity;
        money -= quantity * price;
    }

    public double get_pnl(double current_price){
        return money + position * current_price;
    }
}
```

When a new order is placed or an existing order modified, it affects the risk and exposure of the account. The risk manager must validate that in worst-case-scenario the the exposure will not breach the exposure limitations. Also, it needs to take into account the In-Flight state of orders. Here is an an example how CME allows to manage it. Similarly, order cancellation also affects only the risk management.

Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.