Reader small image

You're reading from  Getting Started with Forex Trading Using Python

Product typeBook
Published inMar 2023
PublisherPackt
ISBN-139781804616857
Edition1st Edition
Right arrow
Author (1)
Alex Krishtop
Alex Krishtop
author image
Alex Krishtop

Alexey Krishtop is a quantitative trader and researcher with 20 years of experience in developing automated trading solutions. He is currently the head of trading and research at Edgesense Technologies and CTO at ForexVox Ltd. He develops market models and trading algorithms for FX, commodities, and crypto. He was one of the first traders who started using Python as the ultimate environment for quantitative trading and suggested a few approaches to developing trading apps that, today, have become standard among many quant traders. He has worked as a director of education with the Algorithmic Traders Association, where he developed an exhaustive course in systematic and algo trading, which covers the worlds of both quantitative models and discretionary approaches.
Read more about Alex Krishtop

Right arrow

Using Python for Trading Strategies

Python has gained a reputation as the language of choice for the development of a wide range of applications in the financial industry and has become a de facto industry standard for the research phase of trading strategy development. However, as with any other language or, better put, programming ecosystem, it has its advantages and disadvantages. So, as with any tool, it is essential to understand its strong and weak points to use the tool properly (not trying to hammer nails with a microscope, so to say).

In this chapter, not only will we consider using Python for algorithmic trading strategy development but we will also learn about the essential steps in the research and development process, discuss the difference between market modeling and event-based trading, and point to the mistakes most typically made in the research and development process. Finally, we will see the limitations of using Python for algo trading so our expectations always...

Technical requirements

You will need Python 3.10 or higher to run the code in this chapter.

The advantages of using Python in trading strategy development

Today when we say Python, we most likely mean not only the most flexible object-oriented programming language but also the most powerful and convenient interactive IDEs, such as Spyder or PyCharm, along with the vast collection of libraries that extend this language to fit virtually any domain of applied and fundamental research, from medicine to astronomy. There is no surprise that the Python ecosystem has become the de facto standard in the financial industry where numbers play a decisive role.

The advantages of using Python for the development of trading strategies are obvious and we will look into a few of these advantages in detail in the following sections.

Memory management

Python is one of the few interpreters that has robust built-in memory management services such as garbage collection and reference counting. Essentially, this means that you don’t have to care about any complex object you work...

Modeling – forecasting the future doesn’t necessarily mean practical benefits

In order to keep the discussion consistent, we need to draw a thin but solid line between trading and modeling. Sometimes, these two terms are seriously confused and may lead not only to misunderstanding but also to losing money.

Modeling is a research activity that aims at building a model that explains the observed data. For example, Ptolemy developed a geocentric model of the Solar System, whereas Nicolaus Copernicus suggested a model of the Earth spinning around its own axis and traveling around the Sun on an elliptical orbit – now known as the heliocentric model. Both models explain the observed data: that the Sun visually moves around the Earth, that day follows night, and that seasons change in order. However, the heliocentric model proved to be far more precise and easy to use, so the other one was abandoned.

In the financial world, modeling a market means finding a set...

Paper trading and backtesting – an essential part of a systemic trader’s risk management

Imagine that we have used all the power of Python and developed a trading application. Now what? Is it time to immediately launch it and try earning some money? No! Before jumping in the pool, it’s essential to make sure there’s water in it, and in our case before putting the app into production, it’s essential that it can make money, at least in theory.

In this section, we’ll consider paper trading and backtesting – two cornerstones of systematic trading that help us understand potential pitfalls with the newly developed strategy. We will learn about historical data, trade simulation, and ordering, and we’ll also quickly consider some ready-made packages that simplify this part of the development.

What are paper trading and backtesting?

After we have developed a trading algorithm, connected to the data source, and are ready to send...

The disadvantages of using Python in trading strategy development

Having praised the advantages of using Python in algo trading, it’s time to mention its important shortcomings. As with many robust and universal ecosystems, these shortcomings are the other side of its advantages.

By any means, the most annoying thing about Python is speed, or, rather, the lack of it. Partly this is pre-determined by the fact that Python is an interpreted language; however, a much greater contribution to the overall slowness is made by weak typing and the same advanced memory management that we love so much when we develop code.

For readers who are not familiar with memory management, I’d recommend starting with a simple article at https://www.geeksforgeeks.org/memory-management-in-python/, which also has references for further reading. In brief, if the language relieves the coder of the burden of declaring variables, then every time the variable is referenced, a number of routines...

Live trading – where Python faces its limits

Thus said, trading applications written in pure Python are not suitable for any live trading activity that assumes the minimization of time from the moment market data is received to the moment an order is sent. Therefore, traditional arbitrage and many high-frequency trading activities (which sometimes suggest sending thousands of orders per second) are definitely not for Python.

Besides that, there is another risk even for slow trading strategies that derive from automated memory management. We already know that trading strategies rely on price time series and the amount of processed market data may be quite large. Although both native Python and third-party libraries such as pandas offer data structures that ensure data persistence, it may become problematic to update data on the fly, especially in trading environments with high throughputs.

There are different ways to speed up Python to some extent. There are static compilers...

Summary

In this chapter, we considered the pros and cons of using Python for algorithmic trading strategy research and development. We considered various options for using native Python data structures to handle market data. We learned about the various ecosystems, third-party libraries, and environments that speed up the development process. We also learned about the most important phases of development and the essential procedures that aim to make sure that the strategy has the potential to make money in live markets.

However, as with any project in any domain, before we can proceed to actual coding, we should get acquainted with the subject. In our case, it is the market itself, its basic elements, structure, and the organization that we will consider in order to see how it operates and what we should take into account to build robust trading applications. This is what we are going to do in the very next chapter.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Getting Started with Forex Trading Using Python
Published in: Mar 2023Publisher: PacktISBN-13: 9781804616857
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Alex Krishtop

Alexey Krishtop is a quantitative trader and researcher with 20 years of experience in developing automated trading solutions. He is currently the head of trading and research at Edgesense Technologies and CTO at ForexVox Ltd. He develops market models and trading algorithms for FX, commodities, and crypto. He was one of the first traders who started using Python as the ultimate environment for quantitative trading and suggested a few approaches to developing trading apps that, today, have become standard among many quant traders. He has worked as a director of education with the Algorithmic Traders Association, where he developed an exhaustive course in systematic and algo trading, which covers the worlds of both quantitative models and discretionary approaches.
Read more about Alex Krishtop