18
min

Algorithmic Trading Defined and Explained

Algorithmic trading is a relatively new form of trading that has come about as a result of increased computer power.

Algorithmic trading is a relatively new form of trading that has come about as a result of increased computer power.

Algorithmic Trading Defined and Explained

Algorithmic trading is a relatively new form of trading that has come about as a result of increased computer power, increased connectivity and availability of information, and the need for faster decision making. But let us one step back to explain what is algorithm trading.

Table of Contents

  1. Learn Python Programming Language
  2. Learn How-to-use Financial Data
  3. How to write successful strategies
  4. Learn Backtesting in Algorithmic Trading
  5. Why Backtesting Trading Strategy?
  6. What is Backtrader?
  7. Why should you learn Backtrader?
  8. How Backtrader Works
  9. Learn to read Performance Metrics
  10. How-to-avoid Overfitting
  11. How to Start With Algo Trading and Quantitative Trading?

Definition of Algorithmic Trading

There are many definitions of the word "Algorithm". Here are some spray of examples:

  • A plan consisting of a number of steps precisely setting out a sequence of actions to achieve a defined task. The basic algo is deterministic, giving the same results from the same inputs every time.
  • A precise step-by-step plan for a computational procedure that begins with an input value and yields an output value.
  • A computational procedure that takes values as input and produces values as output

Here we should mention ‘parameters.’ These are values usually set by the trader, which the algorithm uses in its calculations. In rare cases the parameters are ‘adaptive’ and are calculated by the algo itself from inputs received. The right parameter setting is a key concept in algorithmic trading. It makes all the difference between winning or losing trades. Unconsciously we create little algorithmic trading strategies without having any recognition that we are performing mathematical applications all day long. The brain supercomputer carries it all out without us being aware of it to the slightest degree.

Algorithmic Trading Example

Now let's finally get back to trading. Here is an over-simplified algorithmic trading example. You want to buy 1,000 shares of Crypto-X-Coin and you are looking at a real-time data feed. The Time and Sale is printing mostly 100 volume lots hovering between $165.50 and $166.00 - but a few minutes ago it dipped to $165.00. So you decide to set your Buy algo the task: BUY 1,000 shares Crypto-X-Coin at MARKET if trade price touches $165.00.

Now for a slightly more complex example for which we would need a number of components. For the moment, just imagine these: A real-time data feed (not from one of the 15 minutes' delayed variants). This feed consists of the coin ticker symbol to identify it, the timestamp of when the trade was executed, the number of shares (trading volume) which has changed hands and finally the trade price as matched up by the buyer and seller (buy and sell orderbook) who may be represented by their respective brokerages. All this happens in what we call the "electronic pit", The electronic pit image (thousands of traders who at that instant are looking at exactly the same data on their secreens that you are also looking at). we find exceptionally useful in visualizing the price movement of a coin on cryptocurrency exchange. To be clear, you can also use algorithmic trading in classic stock market or classic asset market which is tradeable through broker exchanges.

Live Coding Editor in PEMBE.io - Left Coding Area in Native Python; Right Area: Settings

Benefits of algorithmic trading strategies

However, the benefits are immediate:

  • Cost per trade reduction is substantial in algorithmic trading
  • Self-documenting trade trail meets financial control and regulatory requirements.
  • Reduction in trading errors (we are not even talking about human failures like emotions and greed in trading)
  • Less trading staff 'burnout' as the emotional side of trading is dramatically reduced.
  • Increased speed – Algorithmic traders have the ability to make decisions more quickly than traditional traders, which can give them an edge in fast-moving markets.
  • Algorithmic traders are able to trade in smaller increments than traditional traders, which reduces their market impact and makes it easier to take advantage of small price movements.

To summerize what algorithmic trading is:

Algorithmic trading is a process that relies on mathematical models and computer programs to trade securities. The algorithm can be as simple as a trade that is executed at the same time every day or as complex as an artificial intelligence system that can make trades in milliseconds.

The idea behind algorithmic trading is to remove the human emotion from investing decisions and therefore, increase the chances of success.

An algorithm-based investment strategy has many benefits, but one of the most important ones is its ability to create a more effective allocation of funds across different types of investments.

How do I start algorithmic trading?

How can we make an investment in algorithmic trading? Understanding the market is essential but you also need a very basic of mathematical knowledge and also some very basic coding skills. One of the well-know programming language in algo trading is Python.

Why use Python for algorithmic trading?

It is particularly convenient for developers to use Python as a tool to create and test algorithms to trade with ease. The relatively easy-to-use Python language is also the selling point in trading bots. This is another reason to think about using Python for trading algorithms.

1. Learn Python Programming Language

For a successful career in data science in general, you need solid fundamentals. Whatever language you choose, you should thoroughly understand certain topics in that language. Below you will learn what you should have in the Python ecosystem for Data Science to develop successful algo trading strategies:

  • Development setup - this includes creating a virtual environment, installing the necessary libraries, packages and working with Jupyter notebooks or Google Colabs or you can use tools like PEMBE.io which have all the tools built in.
  • Data structures - some of the main Python data structures are lists, dictionaries, NumPy arrays, tuples and data sets.
  • Object-Oriented Programming - As a quant analyst or aspiring algorithmic trader, you should make sure you can write well-structured code and define the right classes. You need to learn how to deal with objects and their methods while using external packages like Pandas, NumPy, SciPy and so on.

2. Learn How to use Financial Data

You can't get anywhere in the financial system without data analysis, as it is an important part of the process. Therefore, first and foremost is learning and handling data frames with Pandas, some issues and challenges you should be aware of with trade data.

This includes analyzing data with pandas. Without a doubt, one of the most important packages in the Python Data Science stack is Panda. You can handle and solve all the important tasks with all the defined functions in this package.

Focus on creating dataframes, filtering (loc, iloc, query), descriptive statistics (summary), join/merge, grouping, and subsetting.

An important skill with time series data is to know how to handle trading data. You should understand how to re-sample or re-index data to change the frequency of the data, for example from minutes to hours or from OHLC end of day data to weekend data. Yes you need the knowledge for algorithm trading. It will help you to create better algorithmic trading strategies.

3. How to write successful algorithmic trading systems.

It's not a rocket science to write automated trading. To start a career in quantitative trading is another topic. Both require a solid understanding of statistical hypothesis testing and mathematics. This includes general concepts such as how to perform multivariate calculations, a good understanding of linear algebra, and most importantly, probability theory. These will help you to build a solid foundation for developing and creating trading strategies.

For an easy start into algo trading, you can begin by calculating moving averages on stock price data, writing simple algorithmic strategies such as the moving average crossover or mean reversion strategy, and learning about relative strength.

Step by step, this will give you an understanding of algorithmic trading. The basics should help you understand how statistical algorithms work and so you can move into more sophisticated areas of machine learning techniques. If you want to succeed here, you won't get anywhere without a deeper understanding of statistics and mathematics.

4. Learn about Backtesting Trading Strategies

If you want to backtest a strategy with Python, there are several options. Among others you can backtest with already existing libraries or you can create your own backtester which we don't recommend for beginners or the easiest variant you can use a cloud trading platform like PEMBE.io.

Why Backtesting Trading Strategy?

Imagine you have developed your algo trading and go directly live with it on the stock or crypto markets. You would lose a lot of money very quickly. That's why you first test the trading strategies on historical data.

You "backtest" them on past data to get a first feel for whether this strategy has the potential to be used without losing all your savings. Here it is a matter of evaluating the executed trades on the basis of profit and loss (P&L). You look at all the trades over a certain period of time to see if they have produced a certain desired performance. This is how algo trading works.

To evaluate a strategy with backtrader, you need to be fit in areas such as mathematics, statistics, software engineering and market microstructure. One of the best known libraries in Python for this is Backtrader. For better understanding we will show you here what you can do with Backtrader. This will boost your algorithmic trading.

What is Backtrader?

Backtrader is a Python library that supports the development and testing of trading algorithm for traders in the financial markets. This can of course be applied to crypto assets as well.

Backtrader is an open source framework that can be downloaded and installed from Github. It allows you to test your automated trading against historical data and draw conclusions about whether a trading is successful on financial markets or not.

Furthermore, you can optimize your algo strategy and use it to create visual charts. You can set in your algo trading the volume and average price, your stop losses, take profits and use hundreds of technical analysis indicators. Set buy and sell orders and test them with historical data. For this you can use visual sheets like Backstats to check the performance of your algo trading. You can also use it for live algorithmic trading, not only for test environment.

Why should you learn Backtrader?

Because using Backtrader for algorithmic trading can save you countless hours of writing trading algorithms to test out financial markets and market strategies.

Backtrader is supported by countless algorithmic traders and developers and makes use of a great community and an active forum where you can get support anytime. Besides, there are a lot of good tutorials and extensive documentation to fall back on. This is essential if you want to choose the right tools and libraries for algorithmic trading.

Backtesting

First and foremost, Backtrader allows you to backtest, of course, and it helps you with the tedious process of finding a successful strategy, cleaning the data, and iterating through it as needed to create a successful strategy. It has built-in templates and technical indicators for various data sources and facilitates data import.

Optimizing

Sometimes adjusting small parameters can do a lot and that makes the difference to a profitable or unprofitable algorithmic trading. This iteration process that you go through with backtesting ensures that you develop a robust strategy that produces positive results with just a few lines of code change.

Plotting

If you've ever worked with various Python libraries, then you surely know how difficult it is sometimes to display a complex diagram. Here at PEMBE.io we have already built a graphical interface where you can immediately see the results. And all this with just one click. Deciphering the financial markets has never been so easy.

Key Performance Indicators generated by Quantstats which is directly integrated in PEMBE.io
Key Performance Indicators generated by Quantstats which is directly integrated in PEMBE.io

Technical indicators

What would technical analysis be without the most popular indicators and they are already included in Backtrader. This is especially useful because it allows us to test very quickly whether a certain strategy works or not, instead of developing it individually in a function by ourselves. Instead of understanding the math behind it, we fall back on proven formulas that we can easily incorporate into our algorithmic trading strategy and use.

Live Trading algorithm

If you are satisfied with your results, it is easy to switch from Backtrader to paper trading and then switch to live automated trading as well. This is especially easy as on platforms like PEMBE.io by using the existing indicators and the functions without building your own system. Algorithmic trading was never easy in financial market. But you need a foundation for trading algorithms.

How Backtrader works

It's quite simple, Backtrader shows how your algorithmic trading might perform on the financial markets by applying it to historical price data. Core function of the library is iteration through historical price data and trading simulation of execution of trades based on technical indicators given by your particular strategy. Profitable trading results comes from testing testing and testing.

Additionally, when Backtrader executes and combined with Quantstats to get 200+ KPIs in one fell swoop, it simplifies the functionality, for better evaluation through the statistics provided. So you can see how e.g. your Sharpe Ratio value is and if your algorithmic trading is successful or not.

Integrating your API Keys for Paper Trading in PEMBE.io
Integrating your API Keys for Paper Trading in PEMBE.io

9. Learn to Read Performance Metrics

In algorithmic trading it's important for you to be able to explain your automated trading strategy concisely. What is the use of the best strategy if you don't know exactly what each individual code does and what influence it has on your strategy? The danger is that when external circumstances such as rules or regulations change, you no longer understand your own strategy.

After going through the process you will get a lot of performance metrics. You should look at these to see what each KPI means and what conclusions you can draw from them. After that you can decide if your algorithmic trading strategy is actually good or bad.

Trading
Key Performance Metrics after Testing Strategy

CAGR

CAGR refers to the return on growth achieved by an investment by calculating it from its initial value to its final value over a given period of time. It is calculated as follows:
CAGR = [(1 + Absolute ROI ) 1/No. of years – 1] * 100%

Sharpe Ratio

The Sharpe ratio compares the return of an investment with its risk. It divides a portfolio's excess return by a measure of its volatility to evaluate risk-adjusted performance. Excess returns are those that exceed an industry benchmark or the risk-free rate. A higher Sharpe Ratio is better when comparing similar portfolios.

Volatility

Each trading strategy has a risk value that is taken. It also quantifies the property of the Sharpe Ratio. This is because a higher volatility of an underlying asset often leads to a higher risk in an equity curve, and thus at the same time to a lower Sharpe Ratio.

Maximum Drawdown

The largest possible value, i.e. the percentage decrease of an equity curve of the trading strategy from the low to the high or vice verca. The maximum drawdown is also often studied in momentum strategies, as they are most affected by it. It is best to use the Numpy library to learn from it.

Capacity/Liquidity

It determines, in relation to the strategy and to the additional capital, the scalability. This is, among other things, one of the major problems with many funds and investment companies that suffer from these capacity issues as capital allocations to the respective algorithmic trading strategies increase.

Drawdown

In Algorithmic trading a drawdown is the biggest % drop in your portfolio. In general, you want a small drawdown compared to your returns. The smaller the drawdown to return ratio, the more likely you are to risk less to earn more. The larger the drawdown/return ratio, the more likely you are to risk too much to earn more.

Distribution of Monthly Returns
Distribution of Monthly Returns

10. How-to-Avoid Overfitting

When you start with algorithmic trading you will face sooner or later with the acronym Overfitting. What is overfitting in automated trading?

In algorithmic trading, overfitting means that a trading strategy has been developed to such an extent that it has adapted to historical data that it becomes ineffective in the future.

The danger is, especially for newcomers, that they rely too much on the historical data and may trade live and suffer massive losses. The overfitting (also called curve fitting) lies to them and inspires confidence in them that the strategy is profitable, which it is not. If you adjust and customize the backtests so well, you can even earn thousands of percent per year. However, these don't take effect in the future. That is why paper trading with future data is a way to avoid overfitting.

Image by Steve Kosterman / Towarddatascience.com
Image by Steve Kosterman / Towarddatascience.com

This images illustrates the concepts of overfitting, underfitting, and the bias-variance tradeoff through an illustrative example in Python. Steve shows a more in-depth in his book Data Science Projects with Python: A case study approach to successful data science projects using Python and pandas. Link Amazon.

Problem with historical data

Drawdowns and returns are completed historical values. Basing savings on outdated data sets can't work. It is only a sample. A look into the past and that does not always perfectly predict the future, and these numbers change over time.

The whole thing becomes dangerous when we use the numbers only for a certain period and derive estimates from them, this leads to a bias in the selection.

If a trader from the last 3 years would measure his drawdowns in the crypto market, he might find that he has lower drawdowns with high returns. But the last years for Crypocurrencies were good years as the market knew only one direction upwards. Since the beginning of 2022, the tide has turned, but you now get an idea of how the market performs in times of crisis (we are writing the end of 2022).

For this reason, quant funds look very closely at applicants for traders who have traded well in both financial crises and good times. These traders cannot complain about order books. For these traders, their past performance, that is, their historical drawdowns as well as their return volatility are a fairly accurate measure of risk management skills.

Algorithmic Trading vs Quantitative Trading

First, the distinction between algorithmic trading and quantitative trading. Both are essentially the same, except that the quantitative approach uses different data sources to develop trading strategies.

Algo trading is the precursor to quantitative trading. It is the first entry into the world of algorithmic trading systems. In algo trading we primarily access historical price data. For this we use existing libraries in Python like the Backtrader presented above, which already contains over 200+ indicators. That means we trade according to price patterns and apply existing indicators to them.

The quantitative approach is about using different sources of data sets and bringing them in line with a strategy. Imagine having access to satellite data and then scanning all the parking lots of the world's largest retailer Walmart and determining how many cars parked in the parking lots over a certain period of time. You could now compare this data with a previous period and see if Walmart had more visitors. More visitors means more sales and so you could invest in Walmart stock knowing they had a successful year/period. That is the quantitative approach.

How to Start With Algo Trading and Quantitative Trading?

1. Dive in and soak up all the information that is publicly available for your trading career

You need knowledge in the following three important areas, these would be: Finance/Trading, Mathematics (especially Statistics) and Programming. This will fully equip you for a career as a quant.

2. Develop your understanding of major components for this role

Being a quant requires that they solve complex tasks, perform them precisely and safely. Therefore, as a quantitative trader, you should constantly develop your knowledge in the following components and see that you become fit here.

Strategy Finding:

If you are starting from scratch, then most likely you will not have your own strategies. This is the process of finding suitable strategies, this in turn lies according to your trading preference such as what assets you want to trade, how often you want to trade and in what time frame you want to execute the respective trades. For this it is necessary to examine different strategies (e.g. mean reversion or momentum trading) and which method suits you better and whether they lead to the desired results.

Backtesting:

As described above, this is the process of gathering historical price data and evaluating whether your particular strategy is successful or not. You can perform this trading process to determine if your strategy is effective. However, here you need historical trading data and sample data sets to apply their strategy. Only after that you can judge whether the strategies are successful and will survive in the financial markets. If you want to save yourself the stress, you can also use one-stop-shop platforms like PEMBE.io.

Create a Free Account in PEMBE.io - Your Algo Trading Platform
Create a Free Account in PEMBE.io - Your Algo Trading Platform

Risk Management:

One of the most important functions in algo trading is risk management, this includes assessing past biases in financial trading data and reviewing technological risks, such as when selecting the appropriate broker or exchange. See the example with the crypto exchange FTX.

Trade execution systems:

An execution system can look different. Many retail traders who do algo trading develop their strategies locally using API provided by the respective brokers and exchanges. Or they opt for platforms like PEMBE.io, which offer the possibility to integrate APIs from crypto exchanges and send the respective trading signals directly to e.g. Binance, which then executes the respective buy and sell orders.

Conclusion

As you can see, we are operating in a very competitive and very complex environment in the quantitative finance market. We have tried to present here a rough overview of how to get started in algo trading to you. Countless books and literature have already been published on this topic, so that you can draw from the full here. Therefore it is important for yourself to have a solid foundation in finance/trading, mathematics (especially statistics) and programming. For the beginning we would recommend you to choose a platform where you can develop code, test it and ideally also do paper trading, and if this is successful, then do live trading. Preferably all in one tool, just like we offer here at PEMBE.io. Beginner friendly and directed for beginners. In addition to this we have a community in Discord where you can exchange ideas with like-minded people. We wish you good luck on your algo trading journey!


Main Title Image Copyrights - Yaroslav Shuraev from Pexels.com

Who we are?

Get into algorithmic trading with PEMBE.io!

We are providing you an algorithmic trading solution where you can create your own trading strategy.
Mockup

Algorithmic Trading SaaS Solution

We have built the value chain for algorithmic trading. Write in native python code in our live-editor. Use our integrated historical price data in OHLCV for a bunch of cryptocurrencies. We store over 10years of crypto data for you. Backtest your strategy if it runs profitable or not, generate with one click a performance sheet with over 200+ KPIs, paper trade and live trading on 3 crypto exchanges.