📜 ⬆️ ⬇️

Infrastructure and trading robots: What programming languages ​​are used in finance



Exchange trading is a high-tech industry. In our blog on Habré, we talked about which protocols are used to transmit financial information ( one , two , three , four ), demonstrated the infrastructure of a financial trading hub, and described the process of optimizing the performance of a trading terminal.

Today we will talk about which programming languages ​​are used in the field of finance, for what tasks they are used, and which of them to stop the choice in each particular case.
')

Finance and Programming: Overview


Traditionally, in the financial industry, programming languages ​​are in demand, which allow you to create highly loaded data processing and transmission systems that interact with a variety of hardware.

A significant part of the infrastructure of brokerage companies and stock exchanges is created using the C ++ language — it creates a wide variety of products, from libraries for calculating price models of derivative instruments to data stream processing modules. It is not surprising that professionals who know this language are extremely in demand in the field of finance.

In addition, C # and Java are extremely popular programming languages . These languages ​​are less commonly used for direct solution of infrastructure problems, but the market can often find trading robots, the modules of which are implemented in these languages. In addition, financial companies can use them to implement various front-line services. Specialists in C # and Java are also in demand in such companies.

A separate layer are scripting languages ​​- for example, Python, MATLAB and R. They are often used to describe trading strategies and prototyping quantum models. Often, traders write code for their prototypes in scripting languages, and later describe them using more productive languages ​​like C ++.

However, in some cases, mechanical trading systems can be entirely implemented in scripting languages. Some of these tools, such as TradeScript , even include in trading terminals, which allows users to quickly “jot down” a trading strategy without being distracted from analyzing quotes and charts.

Below is the TradeScript code that describes a trading strategy using Parabolic SAR indicators and exponential moving average (EMA) for generating signals. In this case, the 20-period and 60-period EMA are used to enter the position, and the parabolic system is used to exit the position.

The code of a simple trading system on TradeScript
Buy Signals # 20- EMA  - 60- EMA CROSSOVER(EMA(CLOSE, 20), EMA(CLOSE, 60)) Sell Signals # 20- EMA    60- EMA CROSSOVER(EMA(CLOSE, 60), EMA(CLOSE, 20)) Exit Long #    - Parabolic SAR CROSSOVER(CLOSE, PSAR(CLOSE, 0.02, 0.2)) Exit Short #      Parabolic SAR CROSSOVER(PSAR(CLOSE, 0.02, 0.2), CLOSE) 

Writing trading robots


One of the most dynamically developing areas in the financial industry in recent years has been algorithmic trading and the creation of trading robots. Let's talk about which programming languages ​​and why are used in this case.

First of all, as in the case of any software products, the developers of trading robots somehow have to make various compromises that allow you to take into account the main factors affecting the success of work in the market:


Accordingly, in those cases where speed is important (for example, in the case of HFT trading), effective low-level languages ​​are used - C ++ and even pure C.

If it is planned to program a trading system that will work according to a strategy that does not involve making a large number of transactions, then at first glance it’s not the speed that works, but the convenience and speed of the development itself - in this case, not the fastest languages ​​are used, for example, on the platform .NET (the same C # or Visal Basic).

Another option is a situation in which significant computations are required, but not necessarily in real time. In such cases, the use of interpreted Python, R, or TradeScript languages ​​can be justified.

In other words, the corresponding programming languages ​​are used for different modules of the trading robot:


Expert Tips


In order to better choose a programming language for a specific situation, a theoretical description of the scope of these tools may not be enough. Therefore, you should also learn the experience of other developers of trading systems.

Below we have collected a few tips from Russian and foreign experts involved in the creation of trading robots and financial software.

During the discussion on the choice of programming languages ​​for creating trading robots at Quora PhD, Professor at Princeton University and head of development at Flexport, Andrew Ledwin, spoke about what tools you need to choose to describe the trading strategy itself:

Here we need to generate profitable operating rules based on different signals. [...] At this level, the key point is the expressiveness of the language. It often uses languages ​​like Python or even C ++ with Java, but I consider it ideal for Haskell or Ruby as a “wrapper” with the subsequent development of key modules in lower-level languages.

Another important step is the search for signals. Most often this is offline activity, during which a large amount of statistics is analyzed taking into account the limitations of real-time systems. They use tools like R or Matlab (like the same Goldman). Important is the possibility of machine learning and working with large amounts of data with minimal effort. But in general, the main result here is - if you manage to achieve it using Excel, then who cares?

In the same thread, the YCharts service developer Jeffrey Shek, in cases when it is not a matter of high-frequency trading, advised to pay attention to C #, Java or Python, and also not to forget about the development tools that ensure the operation of the “accounting” part of the trading system:

If you need to aggregate data for analysis, then most likely you will want to use something with a large number of libraries, a fairly high-speed, with a large number of already written code for solving different problems (so as not to reinvent the wheel every time). Most often it will be something like C #, Java or Python.

Also extremely important are the languages ​​for data processing. Want to save every application for Microsoft shares per day? Try to do this with a SQL database (MySQL, SQLite, etc.). It is very likely that in this case you will have to spend many hours on optimization before you can make a deal based on this data.

I would recommend to pay attention to languages ​​that allow storing data in a column format (and not in a string) - this is well suited for tick data. Among such technologies can be called KDB , OneTick , SECDB (created in Goldman Sachs). If you just need to store stock prices, then MySQL with PosgreSQL will work just fine.
.
In an interview with our blog, SmartX terminal developer Andrei Gorkovenko said that he created his own trading robot using C # and C ++:

Initially, I wrote a project in C # because it was easier and faster for me to make a finished product. The functionality available in the .NET platform is quite rich (working with containers, streams, files, with XML, having a GUI), which makes it a good choice. The reason for switching to C ++ is trivial - not enough performance. Now the requirements for software in this regard are very high — the time for recalculating strategies is measured in units of microseconds. Every microsecond counts.

This bundle of languages ​​- C # and C ++ - is extremely viable. It takes an hour or two to write a new trading strategy or modify the old one. In the case of complex mathematics - a day and a half. At the same time, the backtest function is built into the system, a new strategy can be immediately tested on historical data. In addition, there are genetic algorithms that can be used to optimize strategies.

Alexey Afanasyevsky, head of algorithmic trading at Finam, JSC, described the following programming languages ​​for creating fast trading robots:
For these tasks, C ++ and pure C are very well suited. It also happens that fast robots are created almost in assembly language - here it is worth mentioning the mechanisms of direct reading and writing data into the memory of a network card, bypassing the standard mechanisms for working through drivers, as well as work with “super fast programmable hardware” like FPGA.

The founder of the StockSharp platform, Mikhail Sukhov, describes the areas of application of various programming languages:

Languages ​​in trading can be divided into two subgroups:

1) Platform Languages. R, Matlab and so on. The quality of languages ​​is inseparably connected with the platform itself; therefore, the analysis of a separate language and its comparison with its analogue does not bear any particular meaning. If any platform is perfect for you, but the language is extremely inconvenient, then you will have to use it regardless of any shortcomings. And, on the contrary, in the case of a perfectly convenient language on an inconvenient platform, this language will still not be selected for development.

2) Platform-independent languages. C #, Java, C ++, Python, etc ...

a) Java and C # (= .NET) are almost identical platforms. Java is mainly used on * nix-like systems. C # on Windows. We chose C # as the basis of our StockSharp platform, because there are more Windows traders than traders for other systems (Android doesn’t count, as mobile platforms are complementary and not basic).

b) As for C ++, it is practically not developed among traders due to its complexity. But it is used by professional programmers, writing systems with a large load and speed of reaction.

c) Python (in this category you can add languages ​​like List, Haskell) are simple tools for solving algorithmic problems. Traders more often choose this language. But for building a platform, such languages ​​are limited due to poor integration with operating systems, poor performance and the lack of good graphical libraries.

In the end, I can say that there is no language that is universally convenient to any task and for any category of users. Therefore, it is important to understand, before choosing a language, not the advantages of one or another tool, but your own task and your capabilities.

Conclusion


Choosing a programming language (or languages) to create a trading robot or other system operating in the financial market is not an easy task. In order to select suitable development tools, a deep analysis of the architecture of the entire future system is needed. It is important to consider various factors - performance, ease of development, testing, availability of source code of related libraries and their stability, and so on.

The very nature of the trading systems contributes to the use of different technologies at different stages of its life - it is impossible to program an algorithm once that will bring profit over the years. The project will constantly evolve and modernize, and throughout its life, a variety of programming languages ​​may be suitable for solving specific problems.

As a result, there may well exist a system in which C ++ or CUDA is used for fast work with the processor, Java is used for data processing, Python is used for testing historical data, and reports are generated using Excel / VBA.

Source: https://habr.com/ru/post/271493/


All Articles