How Do Stock Trading Algorithms Work?

Stock trading algorithms make millions everyday through the practical use of Machine Learning.
Trevor English

The stock market can be a voracious beast to those that don't understand it, but nowadays, you don't even need to understand it to make money. The rise of the digital information age and AI has brought about a new way of stock trading called algorithmic trading.

Sometimes referred to as automated trading or black-box trading, this is essentially a program that can trade stocks at high speeds and frequencies, perfectly in line with the market.

These programs are given constraints and instructions like timing, price, amount, etc. and a user can fine-tune how they exactly work. So how does this all work then...? Let's take a look.


Take, for example, an average trader. They buy 50 shares of a company when the 50-day moving average goes above the 200-day moving average. This essentially is a technical indicator that the stock is due to rise in the short term. Then that same trader would sell that stock when the 50-day moving average goes below the 200-day moving average, or that the stock is entering into a downtrend.

These two principles are fairly simple aspects of technical trading, but that trader would have to monitor a lot of data continuously, and they could often be swayed in the wrong direction by emotion.

For algorithmic trading, a computer program would be designed to sell and buy according to those previous conditions, and the trader would no longer have to consistently monitor data.

How Do Stock Trading Algorithms Work?
Source: Tertius51/Wikimedia

It's this principle expounded upon that makes up the basics of algorithmic trading.

The basics

Through algorithmic trading, you can make sure trades are executed at exactly the right time, order amounts are perfectly accurate, you can simultaneously check multiple market indicators, and you can reduce the risk of manual errors.

Algorithmic trading can be done on a small scale, but most modern algo-trading is done in a manner called high-frequency trading (HFT). This means that the algorithm places a high number of trades in rapid succession, making a little bit of money on each trade, which then adds up to a large amount. 

This trading technique became popular when stock exchanges around the world offered incentives to companies to make their stock more liquid, or easier to sell. The New York Stock Exchange, for example, has a group of companies that add competition and liquidity to stock quotes on the market. The NYSE pays a fee for providing more liquid stocks, which in turn helps the stock exchange broker more deals.

Having more liquid stocks also gives investors more security in their investment as they know that they'll be able to get out rapidly in the future if needed. This high-liquidity is what allows high-frequency trading to happen, and it can be VERY profitable. 

The key benefit to the introduction of HFT for all markets is that it increases the bid-ask spread, which allows for higher profits to investors. The biggest downside, though, is that since these algorithms make thousands of moves per minute, entire markets can rise or fall in an instant.

How Do Stock Trading Algorithms Work?
Source: Christiaan Colen/ Flickr

For example, on May 6, 2010, the DOW Jones dropped 1,000 points, 10% of its value, in just 20 minutes before it rose again. It was later found that a massive order caused a succession of algorithmic traders to sell-off quickly.

Getting into the nitty-gritty of algorithmic trading a little more, we can start to look at strategies. The most common of which are trend-following strategies.

Trend-following strategies

Trend following algorithmic trading essentially means that these algorithms buy and sell based on moving averages, breakouts, price movements, and other highly technical indicators. These strategies are common because they're simple and rely on readily available data with little complex analysis. Comparing this strategy to mathematics, they would be like simple addition and division to computers.

This simplicity also means that your opportunity to make a lot of money isn't as high with these techniques, but they provide greater security.

Arbitrage trading

Another common technique of algorithmic trading is arbitrage — which means the difference in prices.

If one gas station was selling a candy bar for a dollar and the other was buying them for 2, you could buy tons of candy from the first and sell it to the latter at a profit of a dollar per bar. This is arbitrage trading.

Arbitrage trading algorithms buy a stock that is listed on different exchanges. Since each exchange is a different market, prices aren't always aligned, but they're usually close. Implementing an algorithm to identify price differences allows you to exploit these opportunities. Usually, these arbitrages change quickly and aren't very large, so a human could never do it fast enough, but a computer certainly can.


There are a ton of different strategies to algorithmic trading that extend far beyond the purpose of this introductory article. It's safe to assume that algorithms can be adjusted based on what specific results you want, how risky you want to be, and for which indicators you want to trade on.

Through machine learning, some algorithms are even being developed that can take trade data, determine whether an algorithm was behind those trades, figure out how that algorithm works, and then beat that competing algorithm at its own game or at least decrease its margin.

Setting up algorithmic trading

Setting up an algorithm to do your trading does require some technical prowess, and it's usually relegated to firms with the means to do so. 

How Do Stock Trading Algorithms Work?

Not only do you have to develop computer code to build the algorithm, but then you need to implement the algorithm onto your computer. The biggest challenge is transforming the initial algorithm into one that can actually be integrated into your trading account.

Here's what you'll need:

  • Computer programming skills and knowledge of trading strategy or the ability to purchase a premade algorithm.

  • Active network connectivity to place orders.

  • Access to market data feeds integrated into your algorithms.

  • Infrastructure to backtest the system in prior markets to validate that it actually works without losing any money.

To close out this introduction to algorithm trading, let's work through a final theoretical example.

A given stock, let's say for Concerning Reality, abbreviated CORE, trades on the YouTube and Facebook Stock exchanges. These stock exchanges open at different times and in different currency values. You'll need an algorithm that trades in both currencies (likes versus subscribers) and one that can account for the time differences accordingly.

In order to make money through arbitrage, the difference of the stock's price on the difference exchanges, you'll need an algorithm that has a live feed of current market prices from both exchanges, an integrated exchange calculator, an order-placing integration with a stockbroker/provider, and backtesting ability to see how CORE traded prior to implementing the algorithm.

The algorithm would read the incoming prices from both exchanges, convert them through exchange rates, determine if the arbitrage is large enough to make money (factoring in brokerage fees) and then buy and sell accordingly. If implemented properly, the algorithm will slowly amass more and more profit.

It all sounds simple in theory, but in practice, issues can arise. Prices can fluctuate on the millisecond, so if your algorithm is slow in processing data, then it could end up consistently losing money. You also have risks such as system errors and network outages that could cause your algorithm to spend too much money or just not be able to trade anymore.

Algorithm trading probably isn't in your future, but hopefully, now you understand a little bit more about the process that drives modern markets.

Add Interesting Engineering to your Google News feed.
Add Interesting Engineering to your Google News feed.
message circleSHOW COMMENT (1)chevron
Job Board