How to "use Python as the glue" in your low latency code
It's not easy being a Python developer in the low latency trading space. Given that C++ is considerably faster, it's often preferred and Python developers often feel pressured to learn the more complex language.
They don't need to. Depending on what you’re lowering your code latency for, Python can absolutely be sped up. People working with low latency Python praise its versatility and compatibility with a wide range of compilers and additional languages, although they admit that in some situations C++ can't be surpassed.
C++ for HFT, Python for Crypto
If your dream is to write low latency code in a high frequency trading environment, you probably should have started learning C++ already.
Saeed Amen, co-founder of Turnleaf Analytics, says “you can see lots of evidence quant firms like using Python, AHL have been very public about it, but when it gets to high frequency trading and execution, other languages become more prominent.”
However, when high speed trade execution isn’t the only focus, Python comes into its own. Amen notes Python is predominantly used for “trading at lower frequencies and quant research.” This is because “research needs to come up with an idea quickly and implement it effectively,” which is Python's strength.
Python also works well for low latency crypto trading. Amen says the “crypto market moves slower” and as a result you don’t need the excruciating fine-tuning C++ affords.
How to speed your Python up
More often than not, the recommendation for speeding up Python is to use the NumPy library or Cython, a superset sublanguage of C and Python. In a recent webinar from trading analytics firm Profitview, former Citi and Citadel quant Antony Peacock said "if you're using NumPy it's quite likely to be built against something like the Intel Maths Kernel library which does vectorization for you."
Amen instead recommends a “great new tool that can speed up your code without having to write in a different language, Numba.”
On its website, Numba advertises itself as an "open source JIT compiler that translates a subset of Python and NumPy code into fast machine code." One of its key features is that it "generates specialized code for different array data types and layouts to optimize performance."
It’s “not a free lunch” however, Amen warns. You will need to “spend some time writing the code, especially if you focus on speeding up code with specific bottlenecks.”
You should be learning this language instead
The issue with learning C++ is that both it and Python are “general purpose languages”. If you’re looking for a more niche language to learn that will allow you to stand out, a great candidate is KDB+.
The database language specializes in handling significant amounts of information quickly, so Amen says “if you’re dealing with a large amount of high frequency data, then KDB is worth learning.”
The rarity of KDB developers also allows them to command higher salaries. For example, a buy-side KDB developer can earn a salary of up to $600k in New York. Combine that with expert knowledge in Python, and you become all the more impressive.
What makes it so great for Python coders to learn is how the two languages work together. Amen says “KDB is the heavy duty workhorse and Python is the additional analysis,” and being able to undergo the whole process “without needing to take the data out of the database and do it elsewhere” makes the code much more streamlined.
Make your code more than the sum of its parts
The best way to speed up Python however is not to fixate on any one thing, but instead look at how you can combine them effectively. Amen says you should “use Python as the glue for other libraries and languages.”
As an example, Amen says you can begin coding “with NumPy and NumbA, rewrite code with Cython then combine it with other technologies such as KDB to pre-process data on disk.”
That being said, incorporating too much alternative tech comes with risks and hindrances. Amen says “if you make your code more complicated, it’s going to be more prone to breaking, and you’ll need to do more unit tests.”
Have a confidential story, tip, or comment you’d like to share? Contact: firstname.lastname@example.org in the first instance.
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.)
Photo Credit: eFinancialCareers/Dall-E