Kamil Choudhury

#define ZERO -1 // oh no it's technology all the way down

Trade Model Thoughts

This blog post started out as an internal memo, but nothing it reveals is terribly confidential, so I thought I'd post an extended version of it online and see what people have to say about it. I started working on it after posting this on Twitter:

The tweet jokingly cuts to the heart of what a modern financial institution needs from its technology stack: an accurate accounting of what is on its books, and what those positions are currently worth. Risk, analytics and trading systems are layered on top of this capability. Get this wrong function wrong, and your chances of having to do an expensive, productivity-killing technical retrofit approaches 1.

The question then becomes: how do you build such a tracking system?

Double Entry Book Keeping Rules Everything Around You

Counterintuitively, the first step in building a trade capture system is to stop thinking in terms of individual trades.

Instead, start thinking about position effects in double entry accounting. A quick refresher:

  1. You have a ledger of accounts
  2. A transaction that creates a credit or debit in one account must be countered by equal and opposite transaction notation in one (or more -- this becomes important later) other accounts
  3. If everything doesn't tie out, there is a break.

Trades are by-products of recording the position effects of transactions between two accounts; going long a security creates a positive position effect in one account, and a negative one elsewhere.

Building Blocks

I think it's easier to think about these things with a couple of simple examples. But first we need some primitives though, so here are a few of them in Python. They should translate pretty easily to other languages.

Accounts are simple:

class Account(object):

    def add_position_effects(self, positions=[]):
        Adds securities to account position store

And here we have position effects:

class PositionEffect(object):

    Container class to store positions in security:value

    def __init__(self, pos_effects):

        self._position_effects = pos_effects

    def inverse(self):
        Return position effect object with quantities inverted

        Trust me.
        return PositionEffects({
            for security, amount
            in self._position_effects.items()

And what might a trade look like?

class Trade(object):

    def __init__(self, poseffects, portfolio1, portfolio2):


A Super Simple Example