In algorithmic trading, as with every high-performance programming pursuit, each nanosecond counts. Your tick to trade latency is often the deciding factor in getting ahead of your competitors and buying or selling at the best price. As C++ continues to reinvent itself, becoming something of an entirely new language as each tri-yearly release hits the market, one of its most important innovations for algorithmic traders is a technique used as early as 1958: coroutines.
The term coroutines originated with computer Scientist Melvin Conway. It’s used to describe the processes of generalizing subroutines in order to prevent context switching. In its infancy, computers that would have cost upwards of millions of dollars when adjusted for inflation today were incapable of handling multiple users. By using coroutines, cooperative multitasking became a possibility. As operating systems have evolved to facilitate such multitasking, coroutines have found their relevance diminished but have found salvation as a niche optimization strategy in code that requires very high levels of performance.
In algorithmic trading, coroutines’ major advantage is their elimination of the need to surrender control to the kernel, a timely process that can cost valuable microseconds when dealing with sensitive markets.
There’s a reason coroutines are becoming popular among C++ users in financial services now. For corourtines to work, there needs to be library support to simplify standardization. Though it has been a part of other programming languages for decades, initial library support only came to C++ in the 2020 edition, and even then, many compilers are still in the process of making it compatible. This is not to say that coroutines are non-existent in C++; oftentimes the cutting edge programmer would create their own semi-standardized version in lieu, but these could sometimes be far from perfect and even the most optimal replacements may not have been universally translatable across other projects. The process of writing them using continuations has been difficult and unintuitive.
In a recent webinar, Richard Hickling, a former software engineer at Bank of America, BNP Paribas, Barclays and elsewhere who now runs crypto analytics firm ProfitView, pointed out the irony of coroutines’ modern implementation. Rather than assisting the operating system, Hickling said they are now used “so that we can have something thread-like, without the operating system butting in.”
Not only are more compilers expected to provide support for coroutines in the near future, the next edition of the language, C++23, is planning on expanding the library support for the technique. We will soon see the implementation of a generator to match how coroutines are used in other programming languages, with the result that coroutines are expected to proliferate in programming in the coming years.
Until then, it is important to note that no feature is given support by C++ if it has not been fine-tuned and optimized and we cannot expect to see total coroutine implementation until at least C++26. Nonetheless, if you’re an algorithmic trader or C++ developer, watch this space and watch it very closely.
Bear with us if you leave a comment at the bottom of this article: all our comments are moderated by human beings. Sometimes these humans might be asleep, or away from their desks, so it may take a while for your comment to appear. Eventually it will – unless it’s offensive or libelous (in which case it won’t.)