-->

Handling database-backed async callbacks in Grails

2019-04-15 14:32发布

问题:

I've been working on implementing an automated trading system in Grails based on Interactive Brokers' API (brief overview here: Grails - asynchronous communication with 3rd party API) for longer than I care to admit. This is a high-frequency trading strategy, so it's not as simple as placing an order for 100 shares and getting filled. There's a lot of R&D involved, so my architecture and design have been and continue to morph and evolve over time.

What has become clear over the past month or so is that the asynchronous nature of the API is killing me. I have a model of my intended position within Grails, but this does not automatically reflect the actual state at the brokerage. There is a process of creating orders, some of which get filled now, some later, and some never. There could be partial fills, canceled or rejected orders, or any number of other errors. And the asynchronous updates have turned into a nightmare of pessimistic locks, ugly relationships and dependencies between Positions, Intents, Orders, Transactions, etc. And still, with all that unelegant, smelly code, there are times when my internal model gets out of sync with the actual state of the brokerage account. And that is a very dangerous situation.

So, I'm realizing that I need some kind of async framework that will allow Grails and the IB API to maintain precisely the same state without fail. I am somewhat familiar with Gpars, Akka, Promises, and Actors but only on the surface; I have no hands-on experience with any of them. Just recently, I saw Parse's Bolt Framework Tasks and wondered if that might be a good fit. My need is not really for parallelism or multi-threading of computations or collections. All I am trying to do is make sure that the async callbacks from IB are properly reflected in the Grails domain classes at all times. And I'm hoping that the right framework will allow me to delete tons of ugly spaghetti code that I've written trying to solve this problem.

What I need is a recommendation on the right framework, model, or architecture that addresses this problem. I welcome any recommendations, whether or not I mentioned them above.