0
$\begingroup$

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:

  • New Order is placed: What happens with trader wallet when he places order (buy/sell)
  • 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.

$\endgroup$
1
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ Thank you very much for a clear explanation. Therefore when i place an order, say Buy 2@100, before it is put to the orderbook a 'Risk manager' checks if i have enough money (200 here) to place this order? Because if not, i would be able to place 100 of these orders to the orderbook even if have not enough money to cover it. $\endgroup$ – PeeS May 19 '15 at 12:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.