<aside> 🛠There is a GitHub repository associated with this document, which shows a work in progress implement of the design outlined in this document!
</aside>
I previously wrote a "toy" implementation of a crypto trading bot: it streamed market data from an exchange, transformed the payloads to a pandas.Dataframe
and piped it through to a user defined "strategy" - an object that could apply computations to the incoming data and determine whether or not an action was required. Sounds cool, but it was a toy.
Well, over dinner recently I was asked whether I'd given anymore thought to that project, and - after thinking "hah, I can lose money in much more enjoyable ways" - I realised it would be pretty interesting to think about how to refactor and productionise it.
Behind the complexity of actual trading logic, the functional requirements are pretty simple - and they also lead quite naturally to a nice and simple design.
This system doesn't care about what strategy you want to run in it. All it knows about the strategy is that it wants market data of a given format, and it needs to be able to handle making buy/sell calls.
<aside> 🎯 I have very little domain knowledge - the knowledge I do have comes from reading a couple of books, and browsing MOOC content out of curiosity. This is yet another reason for not bike-shedding on strategy.
</aside>
This design contains 3 components, all with clear responsibilities - and although the Strategy component may at first appear to be "doing too much", it's design is to allow the end-user to completely customise as much of the system as they want.
The API Adapter can be viewed as a transformational proxy with some connection management logic thrown in - i.e. retry attempts with exponential backoff, PING
/PONG
messages and reconnection for websocket streams.