Logo

dev-resources.site

for different kinds of informations.

Algo Trading: algorithms to beat the market

Published at
8/29/2024
Categories
investing
trading
python
strategies
Author
anrodriguez
Author
11 person written this
anrodriguez
open
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!

strategies Article's
27 articles in total
Favicon
How I Boosted My Blog Traffic Using Free SEO Strategies Even Big Companies Swear By
Favicon
Data Center Resilience with Comprehensive Disaster Recovery Strategies
Favicon
The Most Popular Database Caching Strategies Explained
Favicon
Top Ways MSPs Can Strengthen Their Data Protection Game in 2024
Favicon
Marketing Strategies
Favicon
Mastering Data Security: A Comprehensive Look at NAS Backup Strategies
Favicon
Strategic Web Development: Turning Ideas into Seamless Online Experiences
Favicon
Mastering Locator Strategies in Appium
Favicon
Top Creative Real Estate Investment Strategies
Favicon
Unlocking Free Traffic: Strategies to Boost Visitors to Your Website
Favicon
Unlocking Free Traffic: Strategies to Boost Visitors to Your Website
Favicon
8 tips to embrace API documentation with both arms instead of one
Favicon
Take your brand to the next level with our advanced digital marketing strategies!
Favicon
Fault Tolerance in Distributed Systems: Strategies and Case Studies
Favicon
Mastering Problem-Solving: A 5-Step Approach for Coding Success
Favicon
Six Tips and Strategies for Handling the mess left behind
Favicon
7 Effective Affiliate Marketing Strategies to Boost Your Online Revenue
Favicon
How do you stay motivated when there's no company bonus?
Favicon
6(Rā€™) Strategies for Cloud Migration - All IN ONE
Favicon
Construction marketing strategies
Favicon
Cost reduction techniques: Lesson for travel app development
Favicon
Not For Profit Forum With Matthew Brannelly
Favicon
Strategies for building a successful Community
Favicon
Algorithmic Trading: algorithms to beat the market
Favicon
Algo Trading: algorithms to beat the market
Favicon
When and how do you make time to learn?
Favicon
5 Strategies That Will Really Grow Your Instagram Audience

Featured ones: