Create your own trading algorithm
Code, backtest and trade!
12 March 2021 • 9 min read
Got Python? If you’re serious about financial markets and algorithmic trading, then you’re going to need it. Python is a computer programming language that is used by institutions and investors alike every day for a range of purposes, including quantitative research, i.e. data exploration and analysis, and for prototyping, testing, and executing trading algorithms. In the recent past, however, only the big institutional players had the money and tech know-how to harness the benefits of algorithmic trading, but the times they are a-changin’. Before we dig deeper into the finer points of Python and how to get started in algorithmic trading with Trality, let’s take a brief trip back to the future.
The prehistoric era
The 1960s. Black and white television. Analog radio. Telephone trades. It was a halcyon period built around human-to-human interactions: an investor with money and a hot hunch would call his broker, who would enter the order into his own system. Done deal. The good old days. Or were they?
During the 1970s and 1980s, trading got complicated. In 1976 the NYSE introduced its “Designated Order Turnaround” (DOT) system, which allowed brokers to route 100-share orders directly to specialists on the floor. By 1984, the NYSE had a more sophisticated “SuperDOT” system, which allowed for orders up to 100,000 shares to be routed directly to the floor. Suddenly, it was no longer man vs. man, but man vs. machine.
Fast-forward to the present
Then came the fintech disruptors in the new millennium. Decimilization, algorithmic trading, high-frequency trading. Faster, more sophisticated hardware enabled programmers to create more sophisticated algorithms, which in turn allowed computers to decide the timing, pricing, and quantity of trades based on pre-established rules. Instead of one big order, traders could now create hundreds of tiny orders. Ever-more sophisticated algorithms paved the way for high-frequency trading. Think millions of trades each day at blazing-fast speeds. The future of machine vs. machine was now.
If you want to unlock the secrets of a particular culture or country, then you have to learn the language. And it’s the same thing with algorithmic trading. But which programming language is the right one for the job? After all, you can’t learn them all at once, and so inevitably you need to start with one, with things such as cost, performance, resiliency, modularity and various other trading strategy parameters driving your decision. There are basically five programming languages from which an aspiring trader can choose: Python, C++, Java, C# and R.
While we’ll focus on three of these (Python, C++ and R) in greater detail a bit later in this article, a few words on Python at this stage should prove useful. One of the things that is particularly convenient about Python is the extent to which it makes writing and evaluating algorithmic trading structures easier thanks to its functional programming approach. In fact, relative ease and simplicity of use are some of Python’s main selling points.
There’s even something called “The Zen of Python”—beautiful is better than ugly; explicit is better than implicit; simple is better than complex; complex is better than complicated; and readability counts. Cool, right?
OK, I know what you’re thinking: enough about Zen and the art of algo trading with Python. What are some of the benefits and drawbacks of using it?
For people new to algorithmic trading, Python code is readable and accessible. Unlike other coding languages, there’s simply less of it, which means that trading with Python requires fewer lines of code due to the availability of extensive libraries.
Python is an “interpreted” language. An interpreter executes code statements “one-by-one,” unlike a compiler that executes code in its entirety, listing all possible errors at once. Debugging in Python is comprehensive and thorough, as it permits live changes to code and data, increasing execution speed since single errors (rather than multiple ones) appear and can be cleared.
In a word: popularity. Chances are that the algorithmic platforms and tools for trading already on your radar are using Python. The culture of algorithmic trading is done in the language of Python, making it easier for you to collaborate, trade code, or crowdsource for assistance.
Parallelization and Python’s tremendous computational power endow your portfolio with scalability. Compared to other languages, it’s easier to fix new modules to Python and make it expansive. And because of the existing modules, it’s easier for traders to share functionality between different programs
Python’s extensive, comprehensive support libraries mean that most highly used programming tasks are already scripted into it, limiting the length(s) of the code(s) to be written.
One of Python’s primary strengths is also one of its weaknesses. Because of its ease of use, features and extensive libraries, Python users can have trouble learning and working in other programming languages.
Python excels in desktop and server applications, but less so in mobile computing according to some users.
Variables are considered objects, which can translate into memory leaks and performance bottlenecks (i.e. millions of variables are stored) stemming from inefficient memory management.
Python vs. C++ vs. R
When compared with C++ and R, Python is a user-friendly language that has proven to be a winner for traders learning to code as well as for more advanced users fine-tuning their crypto trading bots. Whereas C++ is a complicated language, Python is a confidence-booster, making it easy for beginners to read, write and learn with a comparatively low learning curve. It can be used to develop some fantastic trading algorithms that might otherwise be a hassle or too time-consuming when using C++.
While Python is slower than C++, it is nevertheless widely used in quant trading because it is a high-level language. Things such as research and prototyping are much easier to accomplish due to Python’s high-performing libraries. Additionally, given the extensive offerings of libraries in Python, algo traders can perform virtually any type of data analysis at an execution speed comparable to compiled languages such as C++ because Python libraries actually utilize C++/C or Fortran code.
Trading frequency will largely determine the language to be used for trade execution. Typically, if the trading frequency is in the sub one second range, a compiled language such as C++ would be an ideal choice. This, however, does not affect backtesting and research, since execution times do not matter in a backtesting scenario. Additionally, high performance libraries such as Pandas and NumPy can be utilized to decrease the computing time for backtest.
When first starting out, your crypto trading algorithm might be a simple one, but, as you begin to progress, you might want to experiment a bit with more advanced techniques, such as optimizing parameters with deep-learning using state-of-the-art techniques and neural networks, in which case Python is the most popular language (with R and C++ a close second and third).
You’ll notice by now that R hasn’t been mentioned much. A few years ago, R and Python were on equal footing in the eyes of some, but Python now has superior support for modern software development tools and practices. And with its package libraries having met or exceeded R in virtually every respect, not to mention its ease of use, Python comes out on top. R just doesn’t have the look or feel of a fully featured, organized language employing a clean and consistent syntax with object- oriented features and packages that are easily extensible.
By bridging economics, finance and data science, Python has become one of the most popular programming languages for fintech companies and consistently ranks among the top three most popular languages in financial services. In fact, Python is among only a handful of programming languages that offer the greatest number of job opportunities in absolute terms within the banking sector. According to research done in 2020, there were nearly 1,500 Python jobs, with 14 other Python programmers chasing each one. Big players such as Citigroup now offer Python coding classes to banking analysts and traders as a part of their continuing education program. For many of the reasons mentioned earlier in this article, Python has a great deal to offer traders as well as analysts and researchers.
If you’re interested in a job in banking, then Python should definitely interest you. Bank of America’s Quartz program uses Python as its core language. In the words of former BoA tech guru Kirat Singh, “Everyone at JPMorgan now needs to know Python and there are around 5,000 developers using it at Bank of America,” adding that “There are close to 10 million lines of Python code in Quartz and we got close to 3,000 commits a day. It’s a good scripting language and easily integrated into both the front and back ends, which was one of the reasons we chose it in the first place.”
Because of its analytics tools, Python is widely used in quantitative finance. Thanks to libraries such as Pandas, Python users benefit from easier data visualization and sophisticated statistical calculations. Financial services providers can also harness powerful machine-learning algorithms and their predictive analytics with Python-based solutions that utilize libraries such as Scikit or PyBraing.
Closer to home, traders require robust tools for conducting comprehensive market analysis in order to discern trends and insights and then make predictions and forecasts based on their findings. With Python algorithmic traders can create super creative trading strategies and benefit from predictive analytical insights into the conditions of specific markets.
And Python isn’t just a fantastic programming language for algorithmic traders. From multi-billion dollar corporations to start-up companies, it’s the language driving some of today’s biggest brands and likely the stars of tomorrow. Google, Facebook, and Microsoft use Python for things such as web applications, data science, AI, machine learning, deep learning, and task automation, while Instagram, Spotify and Uber use Python to power their websites.
With Trality’s industry-leading technology, anyone can take advantage of Python in order to build a crypto trading bot and gain a leg up in algorithmic trading. Our world-beating Code Editor is the world’s first browser-based Python Bot Code Editor, which comes with a state-of-the-art Python API, numerous packages, a debugger and end-to-end encryption.
We offer the highest levels of flexibility and sophistication available in private trading. In fact, it’s the core of what we do at Trality.
If you’re already proficient in Python, then take a look at the informative video that Trality co-founder and CEO Moritz Putzhammer has put together about coding your first (or next) bot. Follow the step-by-step guide, which covers topics including choosing a bot template, the four basic steps in algorithm creation, Trality’s all new Position Management System (tracks key metrics automatically), backtesting, fine-tuning your strategy, adding exchanges, and virtual/live trading. We also urge you to take advantage of Trality Documentation, a really useful tool that provides a detailed introduction to our Code Editor (e.g. core concepts, APIs, and our Cook Book).
Trality Code Editor Walkthrough
Getting started couldn’t be easier. Simply visit our website, enter your e-mail address, choose a password, click on the confirmation link we send you and you’re all set.
Sprint, swim, cycle—algorithmic trading is a lot like being a triathlete. Now I know what you’re thinking: not another one of those inspirational sports analogies...
Just like triathletes, though, traders must master three essential skills in order to succeed: math, finance and coding. You can be brilliant at math and know coding inside-out, but if you don’t know much about finance then you’re going to have difficulty making it to the finish line. You need to have creative ideas about how to trade, you need to be able to translate those ideas into mathematical models, and finally implement them in code.
But it’s more than just mastering technical skills. Anyone can learn to swim. Or become good at running. Or be a whiz on a bike. Those are the things that will get you past the qualifying stage and into the race. But to really outperform others or exceed what you thought was possible for yourself, you’ve got to love the feel of the water and the ground beneath your feet, and that metal frame, with its gears, pedals and wheels, needs to become an extension of your body.
At Trality, we can equip you with world-class, state-of-the-art tools to put you in the best position possible when it comes to the big race.
The rest is up to you.
BEFORE YOU GO!
Code, backtest and grow profits!
Use Python to code your algorithms
Access financial data with our easy-to-use API
Blazing-fast backtesting module