Algorithmic trading Nairobi Stock Exchange

Algorithmic trading at the Nairobi Stock Exchange


Algorithmic trading gives algorithms (computer programs) the discretion to make trading decisions regarding stock selection, order sizing and order placement. Any analytical technique used to drive trading strategies is a quantitative strategy, therefore algorithmic trading is a subset of quantitative trading.

A main goal of algorithmic trading is to eliminate the human element from investment decisions. Regardless of how objective we think we are, we may be biased. Similarly, fear and greed may cloud our judgment. An algorithm, on the other hand, is disciplined, lacks emotions, and consistently implements trading strategies. In addition, algorithms analyze significantly more information and execute orders much faster than a human trader. Theoretically, this can lead to higher profits.

In a series of posts, we introduce and compare algorithmic trading strategies, and show simulations (backtests) of these strategies applied at the Nairobi Stock Exchange. A backtest simulates a trading strategy using historical data. This ensures that the trading strategy does what it should do, and if not, the strategy is discarded or refined. In short, backtests form the backbone of any algorithmic trading strategy.

Before we introduce the trading algorithms, let’s briefly describe the methodology. The backtests cover 10 years, June 2008 through June 2018. We focus on four liquid stocks at the NSE: Kenya Commercial Bank (KCB), Safaricom (SCOM), Barclays Bank of Kenya (BBK), and finally, Equity Bank (EQT). For each of these stocks, we backtest three different algorithms, using the same starting capital, and compare the returns at the end of the backtest.

The first strategy simply buys 100 shares every trading day, regardless of market conditions. There are no sales; this is a simple strategy. The total amount of capital used to buy 100 shares daily for the duration of the backtest serves as the capital for the remaining two algorithms.

Our second strategy is even simpler. Instead of spreading the starting capital over every trading day, we invest the entire amount on the first trading day, with no further trades for the entire backtest. This is an extreme version of the buy-and-hold philosophy.


dual moving average algorithm

The previous two strategies are relatively simple and may not qualify as algorithmic. We now introduce the dual moving average (DMA) algorithm. This is a classic, simple, yet often profitable strategy. For every trading day, the DMA algorithm computes the average historical stock price for two periods; say for the past 20 days, and for the past 40 days. These are the short and long averages, respectively. The algorithm moves to the next trading day and recalculates both averages–hence the name “moving average.”

The figure below illustrates the DMA. The Safaricom daily price is plotted in blue, the 20-day moving average in orange, and the 40-day moving average in green (click on the figure for a larger version).

Algorithmic trading showing dual moving average

Dual moving average algorithm applied to Safaricom, showing the market price, moving averages (short ema, long ema), and the buy/sell indicators.

According to theory, when the shorter average (orange) crosses the longer average (green) from below, this portends a future rise in prices. The investor should buy (magenta indicators). Conversely, when the shorter average crosses the longer average from above, this portends a future fall of prices. The investor is advised to sell (black indicators). Observe that SELL indicators are higher than the preceding BUY indicator, and BUY indicators are lower than the preceding SELL indicator. Therefore, this obeys the old investing maxim of “buy low, sell high.”

To compare the effectiveness of the different strategies, we ran the three algorithms on each of the four stocks. For each stock, we use the same value of starting capital for the different algorithms, but the starting capital is not the same for all the stocks.



The following graphs show portfolio values for the three different algorithms.


Algorithmic trading for KCB stock at the NSE

Comparison of three different algorithmic trading strategies applied to KCB stock.



Algorithmic trading for BBK stock at the NSE

Comparison of three different algorithmic trading strategies applied to BBK stock.



Algorithmic trading for EQTY stock at the NSE

Comparison of three different algorithmic trading strategies applied to EQTY stock.



Algorithmic trading for SCOM stock at the NSE

Comparison of three different algorithmic trading strategies applied to SCOM stock.


We summarize the results below. The table compares the starting and ending values (in Ksh millions), and the net returns for the three different strategies.

  DMA100 shares/dayBuy and hold
Starting CapitalEnding ValueReturnEnding ValueReturnEnding ValueReturn


The following key takeaways are evident:

  1. In the period under consideration (June 2008-June 2018), Safaricom is the most profitable counter, regardless of trading strategy. (Note that these are not total returns, as dividends are not included.)
  2. The DMA algorithm grossly underperforms the simpler strategies. This is expected, since a single algorithmic strategy is never implemented for such a long duration (10 years). Instead, they are applied in very specific time and market domains.
  3. Simple strategies like buy-and-hold often beat complicated strategies in the long run, and often by significant margins.



Algorithmic trading strategies are not guaranteed to outperform human-discretionary trading strategies under all market situations. In future posts, we will contrast other common algorithmic strategies, and show how machine learning can be used to improve the performance of algorithmic strategies.





Portfolio values are net of commissions. We do not account for the impact of slippage in the buy-and-hold strategies.


The material on this website is for informational purposes only. Nothing on this website constitutes an offer or a recommendation to buy, sell, or trade in any asset, nor does it constitute an offer to provide investment advice. Always check with an investment professional before making any investment decisions. Mwamba Capital Limited and its associated parties do not warranty or guarantee any of the information provided on this website, including its correctness, suitability, or fitness for any purpose.

Leave a Reply