DEV Community

An Rodriguez
An Rodriguez

Posted on • Originally published at siran.github.io on

Algo Trading: algorithms to beat the market

TL;DR:

  • 2019 was an outstanding year for the stock market.
  • With such a performance it was hard to come up with an algorithm the could beat the market.
  • Running the same algo with data from 2016 I had a small but consistent average edge of 10% over the market.
  • Define common “mean reversion” strategy and indicators as Bollinger Bands®.
  • There are alternative strategies, like using margin to multiply your earnings (or loses!).

During 2019, the stock market grew. A lot.

Take for example an ETF I like: FTEC (Fidelity MSCI Information Technology Index ETF):

During 2019, FTEC’s price grew from $49.35 to $72.48: a staggering almost 50% growth!

That’s almost double of the standard bearer S&P500 that grew “only” around 30%.

Stocks like APPL (Apple Inc.) grew even more, over 90%!

Put neatly:

S&P 500: 30%
FTEC: 50%
V: 41%
AAPL: 90%

Enter fullscreen mode Exit fullscreen mode

Some stocks return during 2019

Some stocks return during 2019

It’s easy to check out using finance.yahoo.com.

Compare that with 2016, it’s a completely different picture.

S&P 500: 12%
FTEC: 15%
V: 3.1%
AAPL: 10%

Enter fullscreen mode Exit fullscreen mode

Same stocks, but with data during 2015

Same stocks, but with data during 2015

It is evident that 2019 was a very good year for the stock market and for the “buy and hold strategy”.

I was thinking about this today and I decided to compare the performance of my algorithm comparing 2019 with 2016.

I couldn’t beat the market with data from 2019.

But running the same unpolished and very basic algo against data from 2016, my algorith had an edge of about 10% on average over the market.

When I saw this, I could finally understand why my algo trading code never beat the market. It’s hard to beat the 2019 performance of the stock market.


People mostly refer to “the market” as the return showed by the S&P 500 (Standard and Poor 500) index. As most of you know, S&P 500 “is a stock market index that measures the stock performance of 500 large companies listed on stock exchanges in the United States”. So it’s a good indicator of how well the stock market is doing as a whole.

Some say that to make money with an Algo Trading algorithm you basically need to come up with a way of «predicting the market».

I would say that this is partially true but at the same time somewhat misleading.

It’s true that in order to make a profit you need to know when to buy and when to sell. But its misleading to call this “predicting the market” as if one were an oracle, or one pretended to read the future with a magic crystal ball.

For example, some would argue that if the price of a stock increases steadily as a straight line, the only way to make money would be to “buy and hold”. This is partially true.

If the price of a stock increase steadily as a straight line, there are no moments where the price can be considered low relative to the mean since the price is always increasing.

So, in this case, it would be imposible to buy at a low price and sell at a higher price.

It would be better to buy at the beginning and sell at the end (whenever you wish to enter or exit the market) and be «exposed» to the stock the longest possible time.

However, one strategy in this case would be to use «margin».

Margin is basically borrowing money from your broker using your securities as a collateral.

In this example, using margin could multiply your winnings simply by buying more. HOWEVER, it could also multiply you loses.

Some brokers offer annual fees as low as 3.75%.

For example, if you borrow $5.000 overnight, you end up paying a $0.52 surplus over the loan.

If your stock ends up making 10%, you pay 3.75% for the loan and pocket the rest 6.25%.

Again, bear in mind that the same way your winnings can be augmented, it’s equally true for your losses. That is, if a your stock loses $1, you actually lose $2 due to margin PLUS the interest (assuming 2x margin).

Although there are countless strategies, many revolve against the concept of mean reversion (“ Mean reversion is a theory used in finance that suggests that asset prices and historical returns eventually will revert to the long-run mean or average level of the entire dataset.”).

There are also many ‘indicators’ that promise to tell you when to buy or sell, or what the market trend is.

A very basic one is to calculate the “standard deviation” of the random price fluctuations, or volatility of the stock.

The indicator named “Bollinger bands®” uses the standard deviation

The standard deviation is easily calculated with, for example, Pandas (“ pandas is an open source, BSD-licensed library providing high-performance, easy-to-use data structures and data analysis tools for the Python programming language”).

We’ll be using Pandas in future articles where I’ll share some simple code to get started with algo trading.

I don’t think any are proverbial indicator or strategies («TL;DR Nobody has cracked it. Period.*»). It’s pretty obvious because if these strategies or indicators were magical and openly disclosed, most algotraders would already be rich or making a ton of money.

Also, if these strategies or indicators were so good, it’s hard to think they would be openly available on the internet.


If you are looking for a very good Algo Trading platform, I would certainly recommend Alpaca. They are truly a platform dedicated mostly to algorithmic trading. You can also buy/sell stocks in their online platform. They offer margin. Also they have neat integrations, for example with TradingView.

Alpaca also offers a very nice paper account trading. This paper account trading let’s you trade as if you had a live account, but with «fake» money. You can test your algorithms posting buy/sell orders, etc.

But I find more useful doing very simple custom baked “backtesting” scripts. Backtesting is testing your algorithm using historical data. But remember the mantra: «Past Performance Is No Guarantee of Future Results».


I recently did a basic backtesting script using Alpaca, downloading data for free from Polygon.io and coded in Python. In fact over time I’ve done a couple. It isn’t hard.

My custom backtesting script basically loops over the data (in days, minutes, or all quotes, transactions, etc). It simulates the decision making on when to buy or sell, and keeps track of the prices. At the end I like to know how much money the algorith would have made or lost.


If you find any of this information useful, or have any questions or suggestions or comments, please leave your comments below.

I’ll be writing more articles soon sharing my experience with algo trading, and some code to get started.

Here’s my next article on how to download historical minute data using Alpaca (the data and code I was mentioning a couple of paragraphs above), and do a simple backtest for the Buy and Hold strategy.

In the third part of this series, we’ll build on our code and work on a scalping script, that even gives good results with 2016 data.

Then I might also want to write about some other strategies I’ve tried, and provide code snippets that some might find useful.

Ideas, comments?

See you next time!

Top comments (0)