Unlocking the Secrets of Algorithmic Trading with C++

Blog | September 5, 2023 | By Vijayalakshmi K

Put simply algorithmic trading is the use of computer programs to execute trading strategies based on a set of predefined rules and market data. One of its defining features is speed. Algorithmic trading requires both technical and financial skills and, in this blog, I hope to share with you some broad insights that will enable you to use C++ for developing and testing algorithmic trading strategies.

C++ is a widely used programming language that offers high performance, flexibility, and access to a rich set of libraries for statistics, analysis, and machine learning. Whether you are a beginner or an expert in algorithmic trading, I hope you will find something useful and interesting in this blog.

Understanding Algorithmic Trading

The Fusion of Technology and Finance

Algorithmic Trading – How it works?

Algorithmic trading goes by many names including algo trading, robot trading, automated trading, and quantitative investing. As opposed to ‘manual’ or ‘discretionary’ trading that’s usually based on human intuition and gut instinct, algorithmic trading is driven by machines that follow a set of rules. Algorithmic trading isn’t your dad’s trading where trades are made based on some “market information” that only a lucky few have access to.

Algorithmic Trading Components

Decoding Trading Algorithms

The Role of Backtesting

An algorithmic trading system has three main components:

  • A trading algorithm or strategy that governs buying and selling decisions based on real-time market data.
  • A trading connection to online exchanges, brokers, or data sources, facilitating the receipt of price data and the execution of orders.
  • A backtesting mechanism to evaluate the algorithm’s expected profitability or its viability as a trading tool.

The trading algorithm is normally realized by a software program or ‘script’. However, this didn’t always involve computers. The famous ‘Turtle Trading’ system of the 1980s was a manually executed algorithm, and backtesting was done with pen and paper. Today though, it just doesn’t work. Since the markets have changed and large hedge funds have switched to algo trading, more complex algorithms are needed for achieving continuous profit with algorithmic trading. This does not mean that private traders are excluded. Advanced software tools available in the market today allow algo trading with the same methods and algorithms that large hedge funds apply.

Why and When Does Algorithmic Trading Work?

The World of Algorithmic Trading

The Evolution of Algo Trading

Why Algorithmic Trading Works

All methods of algorithmic trading seek to exploit a market inefficiency, usually for predicting a price trend or establishing a statistical advantage. Market inefficiencies cause price curve anomalies – deviations of the price curve from pure randomness. Obviously, trading strategies can only work when those inefficiencies actually exist and are large enough to overcome transaction costs. In a perfect, efficient market, prices would only be affected by real events, such as the invention of new technology or the publication of company results. All traders are ‘informed’, decide rationally and act immediately. The price curves in such a hypothetical market would be pure random-walk curves that contain no information for predicting future prices.

Fortunately for algo trading strategies, real markets are far from this theoretical ideal. Market inefficiencies are everywhere. They are caused by delayed reactions to news, incomplete information, rumors, repetitive behavioral patterns, and irrational trading decisions (such as those informed by ‘experts’ and ‘analysts’).

Such effects can be described by market models. The problem? All predictable effects on the price curve are small in relation to the ubiquitous random ‘noise’. Price curve anomalies are normally not visible to the human eye. Nor can they be reliably detected by classical “technical indicators”. Serious statistical methods, such as spectral analysis, or machine learning algorithms are often required for effectively exploiting market inefficiencies.

Four Main Categories of Trading Algorithms

Exploring Trading Strategies

Broadly there are four main categories of trading algorithms. These are:

Risk Premium Strategies:

The method used most often by professional algo traders. They accept a certain amount of risk in exchange for a certain amount of profit. The expected profit, of course, should exceed the risk. The algorithms often accumulate small frequent profits with a low, or ideally negligible, risk of a rare high loss. The most trivial example is buying and holding a stock with a high historical performance. Classical risk premium examples are options selling systems and mean-variance optimization. Algo trading systems that exploit volatility anomalies, such as grid traders, can also fall in that category.

Model-based Strategies:

These strategies are based on a model of behavior patterns of market participants, resulting in market inefficiencies and price curve anomalies. A correct model allows limited price prediction and thus profitable trading. Model-based strategies can exploit market regimes, market cycles, price borders or channels, price differences of correlated assets (statistical arbitrage), or price differences of similar assets at different exchanges (HFT arbitrage). More examples and details about building model-based strategies can be found in an article series on the Financial Hacker blog.

Data Mining Strategies:

They have nothing to do with mining bitcoin but evaluate the market with a machine learning algorithm for predicting short-term price trends. For this, they use signals that are usually derived from the order book or the price curve, but sometimes also from fundamental data, like earnings reports or the Commitment of Traders (COT) report. Even exotic data sources such as blockchain parameters or Twitter keywords can be used for data mining algorithms. Just as model-based system, data mining also exploits market inefficiencies and would not work in a totally random market. But the details of those inefficiencies and the derived trading rules are unknown to the developer – the system is a ‘black box’. More information and an example of a deep learning forex system can be found in this Data Mining article.

Indicator Soups:

They are usually based on ‘technical analysis’ – the belief that technical indicators, geometric price curve properties, or price patterns can predict future prices. Indicator soups are often used by retail traders and found on trader forums or in trading books. They do not always use indicators but can also derive trade signals from traditional candle patterns or with exotic methods such as Elliott Waves or Harmonic Patterns. Although many studies have revealed that technical analysis is mostly useless, some complex indicator soups were in fact profitable in certain market situations, at least for a limited time. If you like playing roulette, you will probably also like algorithmic trading with technical analysis and a soup of indicators.

All four algorithmic trading strategies can be applied in both fully automated and semi-automated trading modes. Fully automated systems run continuously on a computer or a rented VPS server, monitoring markets and executing trades without human intervention. Semi-automated systems, on the other hand, are manually initiated, often at regular intervals, like on the first trading day of a month. They enter an automated process of reading and analysing recent market data, opening, or closing trades, then shut down and until the next start.

Automated vs. Semi-Automated Trading

The Dynamics of Fully Automated Systems

The Flexibility of Semi-Automated Systems

Conclusion: Embarking on Your Algorithmic Trading Journey

Harnessing Knowledge for Profitable Trading

I hope reading this blog has given you some valuable insights into the fundamental knowledge of algorithmic trading strategies. Put this newfound wisdom to good use as you embark on your journey in the exhilarating world of algorithmic trading. Happy trading!

About the Author
Vijayalakshmi is a Senior C++ Developer skilled in developing applications across Trading, Investment/Retail Banking and Network Optimization Platform. With expertise lies in the analysis, design and development of distributed multi-threaded and multi-processing applications using C, C++, QT, STL & Linux Application programming.
Vijayalakshmi KSr. Software Engineer – Digital Enabling | USEReady