Starting out into the world of Algorithmic Trading | Python

Arnav Gupta
SoCollab
Published in
13 min readJun 3, 2021

--

“We’re one to two years away from a machine that can debate with you on your investment hypothesis.” — Pavel Abdur-Rahman, IBM

When we first think of the Stock Market, what comes to your mind? Let me show you…

London Metal Exchange

A stressful, rowdy, and uproar platform of formally dressed-up men and women battling against the odds of trade negotiations with buyers and sellers across the globe! However, this situation has much changed since the wake of the 1987 crash, which first marked the death of the Trading floor.

Today, the trading floor still exists but is responsible for less than 10% of overall market activity. The same brokers and traders are now surrounded by highly performing computers that automate the process of buying and selling stocks for various trading accounts. The London Stock Exchange was among the first in the world to move to an automated system in 1986 (Investopedia, 2020) and since then we have seen an emergence of Algorithmic Trading or less popularly known as the ‘Black-Box Trading’, which now accounts for about 70–80 percent of overall trading volume in the US Stock Market.

So What Exactly is Algorithmic Trading?

Put simply, Algorithmic trading deals with designing computer programs that follow a structured set of instructions to execute trade orders with the ultimate goal of generating profits at frequencies that are impossible for human traders.

Building Blocks of Algorithmic Trading

However, much since its introduction in 1980, Algorithmic Trading has successfully stemmed out to a very popular branch known as; ‘High — Frequency Trading’ — which processes a large number of orders in fractions of a second to make short-term capital gains. The markets have even seen the emergence of ‘Deep Learning’ as a promising candidate for executing perfect trade orders by constantly re-training their model on large sets of neural network architectures.

While we keep witnessing groundbreaking technological innovations in financial markets, we need to realize that we can only keep up with this fast-paced change if we are able to understand the most basic fundamentals of this space. So, I decided to augment the scope of this article by even providing a step-by-step walkthrough of how I developed a simple trading algorithm using the Moving Average Convergence Divergence (MACD) strategy in python!

Oh well! Consider it to be the best of both worlds :)

What strategies does Algorithm Trading make use of?

While Algorithm Trading can deploy numerous strategies, I will be listing the most widely used ones:
1. Momentum & Trend Based Strategies
2. Arbitrage Based Strategies
3. Mean Reversion Strategies
4. Statistical Arbitrage Strategies
5. Weighted Average Price Strategies

Out of these, I will be making use of the ‘Momentum & Trend Based’ strategy, simply because they are the simplest and most widely used ones. The Moving Average Convergence Divergence indicator is known to be the most promising one out of this group and I will be exploring more of it in the upcoming sections.

Moving Average Convergence Divergence (MACD)

Moving Average Convergence Divergence is a trading indicator used in technical analysis of stock prices designed to reveal changes in the strength, direction, momentum, and duration of a trend in a stock’s price. To completely understand this tool, we would need to account for 2 important values: the MACD line & the Signal line.

The MACD line is calculated by subtracting the 26-period exponential moving average (EMA) from the 12-period EMA, while the Signal line is the nine-day EMA of the MACD. The Signal line is then plotted on top of the MACD line, which can function as a trigger for buy and sell signals by allowing investors to understand whether the bullish or bearish movement in the price is strengthening or weakening.

Moving Average Convergence Divergence

In the following chart, we can see how the two EMAs applied to the price chart correspond to the MACD line crossing above or below its baseline. In general, Traders may buy the security when the MACD crosses above its signal line (bullish crossover) and sell — or short — the security when the MACD crosses below the signal line (bearing crossover).

Since the signal line is a 9-day EMA of the MACD line, it generally trails the MACD and makes it easier to spot any shifts in the stock chart. Whenever the MACD crosses the signal line, investors are made known that a new trend is forming and hence should either buy or sell the stock to make respective gains.

Being an easy-to-interpret tool, the MACD oscillator is widely used by both novice and experienced traders, making it one of the most popular indicators among financial investors and hedge funds. The trend-following and momentum forecasting abilities of this tool present the kind of adaptability and versatility traders covet. However, despite the many strengths, it underperforms in sideways markets given the fact that the MACD is primarily based on underlying price points.

Although this technical indicator is not very useful to instraday trading, it can still be applied to daily, weekly or even monthly price charts.

Stock? Index? ETFs? — What do we work with?

For this analysis, I will be working with three different securities to generalize the findings across varying asset classes. I have decided to test MACD on securities whose volatility significantly differs from each other, so there is no bias in my choice. The 3 securities I will be working with are:

  1. Tesla, Inc. (TSLA): Over the last five years, Tesla has been roughly 60 percent more volatile than the S&P 500, according to Bloomberg data.
  2. The Standard and Poor’s 500. (GSPC): Free-float weighted measurement stock market index of 500 of the largest companies listed on stock exchanges in the United States.
  3. Johnson & Johnson. (JNJ): is less volatile than 75% of US stocks over the past 3 months, typically moving +/- 2% a week.

Extracting Real-Time Stock Market Data

In this project, I am going to test the latest release from the Yahoo Finance API which provides the possibility of extracting real-time stock data with less than 0.1-second lag. So let’s dive into coding the project out!

The first step would be to import all neccessary libraries in python. You can further check out the Yahoo Finance documentation at https://pypi.org/project/yfinance/

Importing Packages

We can now connect to the Yahoo Finance API, to extract data of our desired stocks. This API will need 3 mandatory arguments: (1) Tickers, (2) Period, and (3) Interval. The image below details the full list of interval possibilities that can be required.

Interval Parameter Options

For our case, we would be extracting data for Tesla, S&P 500, and Johnson & Johnson for a time period of one year. I have deliberately chosen this timeframe to pan out the financial disruptions and economic uncertainty caused by COVID-19. The past year in the capital markets has underscored the growing pace of change. It suggests a short window of time to prepare your company for disruption, potentially requiring you to pivot your strategic goals and visions for the economic good. The technical analysis for this timeframe will be challenging but also promising as we can evaluate the accuracy of this indicator at times of financial havoc.

The following piece of code will be calling the Yahoo Finance API and requesting data for the mentioned stocks over the respective timeperiods. I have structured this dataset by setting the parameter ‘index_as_date’ to be true so the date column is the new index column. The ‘interval’ just means we are interested in the closing price of each day for a ‘period’ of 252 trading days.

Calling the Yahoo Finance API

For instance, the tesla dataframe would look like this! We can get this output from a single line of code: tesla.head(10)

Tesla Historical Data

Visualizing Stock Price Charts

For a visual interpretation, we can plot out the stock chart for each of these three assets using matplotlib. The chart looks like this:

Tesla Stock Price Chart

Market Cap: 601.02B | PE Ratio: 628.73 | EPS: 1.0

Johnson & Johnson Stock Price Chart

Market Cap: 435.91B | PE Ratio: 29.24 | EPS: 2.59

S&P 500 Stock Price Chart

Market Cap: 31.61T | PE Ratio: 37.24 | EPS: 47.01

As we can see from the above three charts, Tesla illustrates the highest volatility by having tremendous price fluctuations over the last year, while Johnson & Johnson maintains a rather stable price growth. Tesla’s historical volatility stands at 62.61% over the last 253 trading days as compared to Johnson & Johnson’s 16.35%.

Before we go through the code for plotting these functions, I will briefly introduce what these technical statistics mean.

Market capitalization refers to how much a company is worth as determined by the stock market. It is defined as the total market value of all outstanding shares (Investopedia, 2021). This figure can give investors an indication of the size of the company and can even be used to compare the size of one company to another.

P/E Ratio helps investors determine the market value of a stock as compared to the company’s earnings. In short, the P/E shows what the market is willing to pay today for a stock based on its past or future earnings. This figure allows investors to quickly identify if a stock is over or undervalued!

Growth in EPS is a vital measure of management performance because it shows how much money the company is making for its shareholders. EPS is typically considered good when a corporation’s profits outperform those of similar companies in the same sector (Zacks, 2019)

I used the following chunk of code to produce the stock price charts. This is a very basic code snippet which can be extended to further customize the output.

Plotting Stock Price Charts

Calculating Statistics

I will now be writing few functions to calculate the Short-term EMA, Long term EMA, MACD line as well as the Signal line.

Functions to Calculate EMAs, MACD & Signal Line

To calculate the MACD, I have selected a 12-period and a 26-period EMA for the short and long term respectively. While these time periods do not necessarily need to be the same, they should be significantly apart to visualize the effect of this technical indicator.

For a visual representation, I have plotted the MACD and Signal line for Tesla, to identify bullish or bearish movements in stock price. The following piece of code can be used to produce such a graph.

Plotting the MACD indicator for TSLA
MACD Analysis Graph for TSLA

The above chart clearly allows investors to recognize when the MACD line crosses over the signal line and indicate whether they should buy or sell the stock! Of course, no strategy is perfect on its own but here we can get an idea of the future trend of this stock.

Calculating & Plotting Buy and Sell Signals

To put this logic into code, we will be defining a function which would signal us at what price points should we buy or sell the stock! Below, I have defined a signal_points(dataframe) function.

Function to signal when to buy or sell an asset

Code Explanation: I looped through each row in the data frame to identify at what price points does the MACD line crosses the Signal Line. The variable ‘Flag’ exists to tell us if there is a momentum shift at a specific price point and is only changed when the two lines cross each other. If the MACD line equals the Signal line, we will add a ‘nan’ value as it is neither a buy nor a sell signal.

Using Pandas, I have created new columns in the dataframes to store these buy and sell signals. You can view the code for this in my github repositry: https://github.com/arnav0401/Algo-Trading-MACD

I have also created a function which will plot the stock chart for any given dataframe with the buy and sell signals labelled as ‘green’ and ‘red’ respectively. This is a powerful function which will instantly allow investors to visualize when to buy or sell a stock over a given timeframe. Below, I have attached this code. The ‘alpha’ parameter is used to adjust the transparency of the graph and I have selected a lower alpha for the close price line (black) — so the price indicators (arrows) are more visually seen!

Function to Plot Buy & Sell Signals

We can test the output by running this on the Johnson & Johnson Dataframe. The output is as follows:

Closing Price Buy & Sell Signals

Backtesting

Backtesting is a very popular term used in Statistical Analysis which refers to testing a predictive model on historical data. In a nutshell, Backtesting assesses the viability of a trading strategy by discovering how it would play out using historical data. If backtesting works, traders and analysts may have the confidence to employ it going forward (CFI, 2020).

To evaluate my model, I constructed a backtesting function which would calculate the net relative profit made by the algorithm over the desired timeframe. In this study, I used ‘one — year’ as the timeframe to chart out the market uncertainties imposed by COVID-19. Below, I have attached the code snippet!

Back — Testing Function

Code Explanation: I will be breaking down the code explanation into three interlinked segments.

Segment 1| Since we want to calculate the price at which the real trade order was executed, we would need to calculate the ‘opening price’ of the [i + 1] day. Here ‘i’ refers to the buy or sell signal day. Remember, these signals actually correspond to ‘closing price’, and to take into account the actual price at which the stock was traded, finding out the opening price is essential.

Segment 2 |Before calculating the relative profit made by each buy-sell cycle, we would need to drop the following signals:
(i) Sell signals which are not preceded with a buying signal
(ii) Buy signals which are not followed with a selling signal

Segment 3 |We finally are able to calculate the mean relative profit by dividing the sum of all relative profit data points by the number of these data points.

We will now output the mean relative profit of each of the three securities to evaluate how well our algorithm performed.

Mean Relative Profit over the last year

We can notice, that if we relied solely on our algorithm for trading the Tesla, Johnson & Johnson, and S&P500 securities — we would have made about 7.08%, -4.27%, and 5.4% net return respectively over the past year. These findings imply that while this strategy performs decently well across two out of three assets, we often need to use a pool of technical strategies in making investment decisions! A combination of technical indicators is often a very promising choice as the limitations of one indicator can be corrected by the overperformance of the other.

MACD — As a standalone technical indicator?

This tool, if used as a standalone technical indicator, suffers from few limitations primarily producing false positives i.e signaling a possible reversal when no actual reversal takes place! Secondly, since it is based on underlying price points, overbought and oversold signals are not as effective as a pure volume-based oscillator.

However, despite these limitations, MACD is one of the most commonly used technical indicators that has produced promising results when complemented with strong fundamental analysis.

My take on this

This project does not aim to promote this indicator in making investment decisions but rather explores the power of algorithmic trading using simple technical strategies like the Moving Average Convergence Divergence.

While this project has its own limitations, I believe it is a strong start to understanding the foundations of Algorithmic Trading! Backtesting and Optimisation is the backbone of any trading algorithm as it allows investors to evaluate the performance of the model on historical data and prepare it to experience the unseen future.

I hope at the end of the day, you have equipped yourselves with new knowledge regarding Capital Markets and Python, and eventually appreciated the endless possibilities of Financial Technology [FinTech]. As time passes by, we will constantly witness more efficient and large-scaled algorithms but they would mean nothing to us if we fall short of having strong fundamental knowledge about such concepts

Where can I go next?

After reading this article, you can research more about ‘Algorithmic Trading’ and the different strategies they employ to strengthen their code architectures. You can visit https://algotrading101.com/ or even visit https://www.coursera.org/ and https://www.edx.org/ for interesting courses that explore different segments of this domain.

Lastly, if you wish to make use of this same momentum-based strategy — feel free to clone my Github repository and experiment with different stocks, timeframes, intervals, and maybe even a slightly tweaked logic! I hope you enjoyed reading this piece :D

“Do not follow where the path may lead. Go instead where there is no path and leave a trail”

References:
- Investopedia
- Yahoo Finance
- Towards Data Science
- Medium
- The Trade News

--

--

Arnav Gupta
SoCollab

Business Analytics Student at National University of Singapore | Passionate and Inquisitive about Finance, Technology, Data Science, and Entrepreneurship