When first getting started with algorithmic trading, one of the first important decisions you’ll have to face is the choice of programming language. Python stands out as both the most recommended and criticized programming language, and in this article, I will discuss whether or not it is a good language for trading applications.
Due to its popularity in the data science community, Python is the recommended programming language for algorithmic trading. It has the most mature ecosystem for quantitative analysis, ranging from data analysis libraries and backtesting frameworks to libraries that allow programmers to interface with stock brokers and cryptocurrency exchanges easily.
Before getting started, it’s worth mentioning that I recommend using BacktestXL if you’re not a proficient Python programmer. BacktestXL is a backtesting engine that works natively in Microsoft Excel, and its free plan allows users to fetch historical stock prices without a single line of code. Disclaimer: we’re the developers, but it really is the software we use for our personal research!
Is Python Popular in Trading?
Python is, without a doubt, the programming language that is most widely used for trading purposes. This is not only valid in the retail trading community but also in the financial industry.
The following chart displays the popularity of the most widely used programming languages for trading purposes. The period covered goes from January 2010 to November 2022 and is based on Google searches of terms related to trading.
Is Python fast enough for trading?
Although slower than other programming languages such as Java, C++, or C#, it is more than fast enough for most trading applications. The fact that most automated trading strategies are nowadays implemented in Python is a testament to its suitability.
Nonetheless, Python is unsuitable for high-frequency trading algorithms and other applications where one extra millisecond of processing time is critical.
One of our machine learning strategies trades 152 stocks concurrently in 60 seconds intervals. It takes the algorithm a total of 6 seconds to fetch each company’s data, calculate the individual forecasts and create the subsequent orders.
The code could be further optimized by storing the models in memory instead of loading them on each iteration. However, since our average holding period is 32 minutes, subtracting an additional second has no marginal value.
Regarding most benchmarks available, the Python implementation tends to be flawed, and its slowness is thus exaggerated. Popular libraries used for calculations, such as Numpy, are written in C, allowing Python to leverage low-level implementations to increase its efficiency.
Last but not least, suboptimal implementations in high-performing programming languages can result in slower execution times.
Advantages of Python for Trading
Best programming language for data science
Python has the most comprehensive and mature ecosystem of libraries for data science, which makes it a perfect programming language for algorithmic trading. Most strategies rely on technical indicators, time series models, or machine learning algorithms, and Python is the ideal language for implementing them.
You might have heard of Pandas, Numpy, Scikit-Learn, PyTorch, and Tensorflow. In isolation, Python would be just another programming language, but this rich tech stack makes it the go-to choice for algorithmic trading.
Lots of Backtesting Frameworks
Unlike other programming languages, python features an extensive array of open-source backtesting frameworks. This should not be understated since having a reliable, well-tested, and popular backtesting engine is one of the most important aspects of researching and developing trading strategies.
Having too many backtesting frameworks raises the issue of not knowing which one to choose. You should choose an active repository with many contributors, good documentation, a vibrant community of users, and a few tutorials to get started.
I wrote an entire article on python backtesters, which you can read here. If you just want to know which you should choose, use VectorBT if you require processing speed or Backtesting.py if you want a feature-rich and easy-to-learn engine.
Mature libraries for most brokers
Algorithmic trading gained popularity in 2017 due to increased interest in cryptocurrencies. By that time, Python was already the most popular data science library. As a result, it was easy for brokers and exchanges to choose Python as the primary language for interfacing with their APIs.
Additionally, those brokers that don’t actively maintain an official Python library have unofficial packages developed by the community.
Easy to learn
Python is a programming language that prioritizes readability, making it the easiest to learn if you’re interested in algorithmic trading.
You’ll be able to automate repetitive tasks and develop simple scripts in just a few days. You’ll still require a few years of coding experience to become an employable coder.
Excellent for rapid prototyping
Python is the perfect programming language for quickly researching, testing, and prototyping trading strategies. If you consider that most trading ideas will not be profitable, being able to test and discard hypotheses is crucial quickly.
After coming up with an idea, you should immediately develop a test to evaluate whether it is worth pursuing. Allocating resources to a useless idea is costly and will slow down the development process, which might eventually lead to a profitable strategy. Python is the perfect programming language for these types of endeavors.
Demanded programming language in the job market
In recent years, proprietary firms and quantitative hedge funds have leaned towards applying data science and machine learning for researching trading strategies. Additionally, using alternative data has gained popularity and proven profitable in many cases.
For these reasons, using a programming language with a rich data science ecosystem has become increasingly important. Consequently, Python has emerged as the most sought-after programming language in job searches.
Disadvantages of Python for Trading
Not suitable for High-Frequency Trading
Being a high-level programming language, Python is too slow for high-frequency trading applications. Current HFT implementations achieve latencies of only 40 microseconds or 0.04 milliseconds (the blink of an eye takes between 100 to 400 milliseconds).
These extremely high-performing algorithms require optimized implementations in low-latency programming languages such as C or C++.
Additionally, you would require custom hardware colocated next to the exchange. To provide context, the latency of AWS between its Oregon and North Virginia is 34 milliseconds, meaning that colocation to the exchange is a requirement.
Free Resources for Learning Python for Trading
If you want to learn how to create a trading bot and interface it to a live broker, I recommend the following tutorials I created
- Learn how to fetch live data (Yahoo Finance)
- Implement a Trading Strategy (Yahoo Finance)
- Create a Trading Bot for Stocks (Alpaca): Article + Video
- Create a Trading Bot for Cryptocurrencies (Binance*): Article + Video
Luckily for me, I did not make the tutorial for FTX.
Python versus other programming languages
Python vs. C++ for Trading
Python and C++ are very different programming languages that cater to different audiences. Python automatically takes care of lots of tasks that lower-level programming languages such as C++ require to do manually, which makes it much more friendly to learn for people that don’t have previous exposure to coding. Whereas Python has an inbuilt garbage collector, C++ requires the coder to allocate a variable and explicitly deallocate it.
You should choose to use C++ if you want to develop high-frequency trading strategies, which also require custom hardware colocated next to an exchange. These strategies are outside the realm of most people’s possibilities since they have a highly technical background in computer science and electronic engineering, in addition to a large budget for hardware and colocation fees.
In short, if you have doubts about whether to learn Python or C++ for creating trading strategies, definitely choose the former.
Python vs. R for Trading
Both Python and R are programming languages that feature a rich ecosystem for data scientists. Whereas R is more commonly used in academia, Python is the default programming language in the industry. More specifically, Python has a much more mature set of trading-oriented libraries available.
Most brokers and exchanges do not officially maintain R packages, and you will probably have to create your own classes to interface with their APIs.
The most crucial advantage of Python over R is its mature backtesting frameworks. Although R has a few projects, they are not as feature-rich and rigorously tested as VectorBT, Backtrader of Backtesting.py.
Python vs. Pinescript (TradingView) for Trading
Pinescript, Tradingview’s proprietary language, is often cited as the most accessible programming language for traders, but this is only partially true.
You can create a simple trading script in just a few minutes, but you’ll be restricted by its functionalities and Tradingview limitations.
Although it has improved in recent years, the backtesting engine offered is noticeably inferior to Python open-source alternatives. Moreover, the output metrics are somewhat limited and catered to the day trading community, meaning that people with a firm grasp of statistics will demand more rigorous reports.
Last but not least, you will be limited to trading strategies based on technical indicators. Any machine learning, econometrics, pair trading, or portfolio rebalancing strategies are impossible to implement using Pinescript.