Forex brokers will start liquidating your positions when your account's equity falls below the maintenance margin set by the broker.

account equity = deposits - withdrawals + realized pnl + unrealized pnl

It seems that they have some kind of system that calculates the equity of every account that has open positions, in real-time, on every tick, then compares that to the maintenance margin requirement to decide whether to commence a liquidation.

While this may be feasible for a small number of positions, it doesn't seem like it scales well. As the number of accounts and the securities offered for trading increase, so does the complexity of the method described above.

So there must be some other kind of data structure or algorithm or method to do this. Yet I have not found anything about it online.

How do margin trading brokers track user account equity in real-time to determine when it falls below the maintenance margin, and hence trigger a margin call?



I don't actually know the answer to this question but the problem you describe is just a linear algebra problem. Imagine a matrix X whose rows represent the ordered positions of each client with respect to all instruments, and column vector P of realtime prices of all instruments. Then the equity of each client is the row value of E:

$$E=XP$$ Ignoring deposits and withdrawals, intraday trading which could also be considered as vectors and added to the result.

The key is that X might be very large, e.g. 1 million accounts x 10,000 instruments, but the matrix will be very sparse (each account will be expected to own a small subset of instruments), so the sparsity might be 1%, and then with appropriate sparse matrix calculators the calculation time will be that which takes 1mm x 10k x 1% = 100mm calculations, which might take a second. (https://stackoverflow.com/questions/36969886/using-a-sparse-matrix-versus-numpy-array)

Then you scan through the results of 1mm clients and do comparisons of the values relative to the margin and flag those that breach. This would also be fast.


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.