Algo-Trading 101 Part 2: Backtesting

One of the great benefits of quantitative trading models is that they can be backtested against historical price data to measure past performance. A great excercise is to find a strategy that you’re interested in (published somewhere, for example Futures & Options Trader magazine) and perform the backtest yourself. You should get the same results as the they did – this will ensure that you understand the strategy correctly and have avoided common pitfalls.

Historical Data

There are numerous sites offering various levels of quality historical stock price data. Below you can find some common sources of free historical data. Google is also your friend here, there’s lots of datasources out there.

Our aim will be to minimize costs

Algo-Trading 101 Part 1: Getting Started

So you want to get started in algo-trading huh? While the start of an algo-trading operation may seem like a daunting task, it can be possible to beat the institutional traders at their own game (or at least share in the profits), have fun and learn an immense about trading the markets autonomously in the process. That is what this post will be about, searching for quantitative strategies and a systematic approach to starting an algorithmic trading operation.

What this post will aim to do is explore the journey of the trading business startup, from finding a viable trading strategy, backtesting it for verification on historical performance, setting up the tech-infrastructure (low-cost!) and building and deploying an automated trading system (ATS) to execute your strategy.

If, like me, you prefer using Linux (personally I’m a fan of the Kubuntu Hardy Heron flavor), you will need to install a virtual machine such as vmware to run a copy of Windows. While OpenOffice is a great solution, from personal experience I have found that Excel really is the best solution when it comes to complex spreadsheets involving macros. If you’re a die-hard OpenOffice Calc fan, go ahead and try it’s macro language but be warned, its not pretty (and not compatible with Visual Basic).

Other than that we’ll be looking at using Matlab or using its cheaper alternative, R for the backtesting of some quantitative trading strategies.

Being proficient at basic stats and basic programming will definitely help, but you won’t need a PhD to put the theories into practice. Strategies will mostly be of the quantitative nature e.g. statistical arbitrage, and the aim is to keep things simple for now. StatArb deals with the simplest financial instruments: stocks, futures and sometimes currencies.

Let’s look at the process of developing a successful trading strategies and putting it into action!

Finding A Quantitative Strategy That Works (And Suits You!)

Unlike popular opinion would suggest, many strategies are publicly available and while they may not be super-profitable can be tweaked to become profitable.

The key is to backtest strategies for historical performance, thereby verifying that they represent a sound hypothesis. While this alone does not guarantee a profitable strategy, it is certainly a very good indication on the future success of a strategy.

Key things to consider when deciding on a suitable strategy are:

  • Available time: If you’re working full time and only able to trade part time, you probably don’t want to trade intraday strategies but rather stick to ones that trade overnight. Quantitative trading lends itself well to automation in this regard, since the computer can handle most things automatically, if set up correctly.
  • How programmatically inclined you are: Knowing some languages like Java, C# or C++ will definitely be a huge plus when it comes to algo-trading (after all its not called ‘algorithmic trading’ for nothing – trading algorithms are just computer programs firing off buy and sell orders to the markets.
  • Trading capital: Depending on your level of available capital, you may not be in a position to trading algos in the first place. Your capital will determine alot of things such as what type of accounts you’ll be setting up, brokerage discounts (based on volume traded), etc, etc. Generally, the more you have to begin trading with, the better.
  • Does it have a high Sharpe ratio?
  • Does it outperform a benchmark? (Say the ASX/S&P200 Index)
  • Does it have small enough drawdown and short enough drawdown duration?
  • Does the strategy’s historical performance start declining towards the more recent years compared to that of the earlier years?

System Trading

Its All About The Odds…

Las Vegas. Great food, show girls, and a multi-billion dollar gambling
business. The money made by the casinos is only matched by the profits
on Wall Street. And the profits of both are based on mathematical probabilities.

Casinos make money because “the odds” or a game’s
expectancy are in the house‘s favor. This means that if you play
long enough, the casino wins. Over the short term, the casino knows
it may win or lose. But if you play long enough, the house always wins.
The casinos increase their profits by offering games that are completed
in a short period of time – a roll of dice, a spin of a wheel
or a few cards turned over.

What does this have to do with trading systems?

  • We want the odds of a trade to favor us – We want a positive expectancy
  • We want a lot of trades – opportunity
  • We want turn over so we can compound the profits – holding time.

What we as traders must do is become the house. The odds in our trading
must favor us, we need a reasonable number of trades during the year
and the trades must be completed in a reasonable amount of time for
compounding to be effective.

Expectancy is simply the product of your profit percentage per win and
your win rate minus the product of your loss percentage per loss and
your loss rate, or:

Expectancy, E = (Ave. Win Size * % Wins) – (Ave. Lose Size * %
Losers)

For example, if we have the results of backtesting a purely
technical
system, with the following results:

  • Win percentage 6%
  • Win rate 60%
  • Loss percentage 4%
  • Loss rate 40%

The expectancy is 2.0% per trade, or (6% x 60%) – (4% x 40%).

That means, on an average trade, 2% of the money traded is yours to
keep. That’s better odds than a casino gets on blackjack. Now,
that may not sound like a lot of money. If your average trade is $10,000
– 2% is $200 profit per trade. If you have 300 trades per year,
then you have a $60,000 profit per year with an average trade of only
$10,000. This does not even include the profits if you compounded
the average trade.

If you explore the expectancy formula, you will notice that there is
no one set of numbers that could give a positive expectancy but an infinite
number of sets and therefore an infinite number of trading systems that
could be profitable.

Given that, it is possible to develop systems where the stop-loss is
larger than the profits. The stop-loss is academic, as long as your
profit expectancy is positive.

Here’s another example: we could use a 20% stop loss and a 5%
profit target and come out with the same exact 2% expectancy as long
as my win rate is high enough! An 88% win rate in this example would
yield 2.0%, the result of (5% x 88%) – (20% x 12%).

Or, you could arrive at a positive expectancy with a very low win rate.
One of the more famous expectancy numbers comes from William O’Neil,
advocate of the CANSLIM system and founder of Investors Business Daily.
If we use his stop and target numbers of 8% and 20% and his published
win rate of 30%, the expectancy can be calculated to be: (20% x 30%)
- (8% x 70%) or +0.4%.

The bottom line is: expectancy must be positive if you want to make
a profit over time. Never use a system with a zero or negative expectancy.
You will not win. You cannot beat the house over a long series of bets
or trades. Be the “House”.

No matter what your expectancy is, you will not make a great deal of
money unless you have a lot of opportunities to trade. Again, the casino
analogy. The casino may only make 1-2% per hand of blackjack, but they
turn over those hands very quickly – 30 to 40 hands per hour.
Play blackjack long enough and you will lose over 40% of your money
per hour! No wonder they can offer those wonderful comps.

We now know how to create a method, at least on paper, with a positive
expectancy. Let’s say we develop a system with 8% expectancy,
but if the system only yielded one trade per year, what good would it
be? We might as well just put the money in a savings account. Or, if
we had a method that yielded 0.2% per trade, you might pass on it? But
what if that system generated 1,000 trades per year? 1,000 times 0.2%
becomes serious money in a very short time.

The most overlooked area of trading is the “holding period.”
In order to make money, you must have a system that generates a positive
expectancy and a lot of opportunities. But you must have access
to your money
. If your trade’s hold time is too long, you
can’t take advantage of all or even most of your opportunities. Your
trading money or buying power is always tied up because you have to
wait too long to close your trades.

Casino analogy time. If the house odds in Blackjack are 2.5%, that means
for ever $2 bet, the casino makes, on average, 5 cents. If you only
play 1 game per hour, the casino makes 5 cents per hour. If you play
60 games per hour, the casino has all of your $2 in 40 minutes. All
things being equal, the game with the fastest turnover is the more profitable
for the casino.

It’s no different with trading. You will be more profitable with $100,000
that you could “turn” 250 times per year, than $500,000 that
was tied up in one trade for 12 months. As an example, let’s say we
have one trade and that trade yielded a 50% return. You just had a great
year – a $250,000 profit.

On the other hand, say you had $100,000 for stock purchases, and your
expectancy was only 1.2% per trade but you turned over your stocks 250
times in the same year. This method ends up generating $300,000 for
the year, and that assumes you never increase the position size as the
equity grows. You just had a better year. And it is easier to get 1.2%
per trade than 50%.

The bottom line for a great bottom line is:

  • A positive expectancy
  • A good number of trades
  • A short holding period

The Stock Market: Basics

The Stock Market

The stock market is essentially the term given to the place
where traders and investors can buy and sell stocks through
agents known as brokers. This takes place in a very similar
fashion to that of a cattle market or any other livestock market for
that matter, in that prices vary on a constant basis according to
supply and demand.

As more people want to buy a stock and less people
are willing to sell it, the price rises until more sellers are tempted
by the higher prices on offer and the relationship returns to a state
of equilibrium. Equally, if the circumstances change and the stock
loses favour with investors, causing the rate of selling to increase
and buying to decrease, then prices will drop until they reach a level
which entices investors back into the stock because they feel it is
now good value for money. We will show some of the ways you can determine
how fairly valued a stock is later, though this is often described
as more of an art than a science.

The Stock Exchanges

The places where stocks and other securities such as options and
futures are traded is known as an exchange. There may only
be a single exchange making up the stock market for a country,
or in some cases there may be numerous exchanges within one domestic
market. For example, in the US there are three main exchanges:

  • The American Stock Exchange (AMEX)
  • The New York Stock Exchange (NYSE); and
  • The National Association of Securities Dealers Automated Quotes
    (NASDAQ).

There are also a number of regional exchanges including the Chicago
Stock Exchange (CHX) and the Pacific Stock Exchange (PSE) in addition
to the three main exchanges above.

Some major exchanges in other worldwide markets include:

Australia

The Australian Securities Exchange
(ASX)

Great Britain

The London Stock Exchange
(GBX)

India

The National Stock Exchange of
India (NSE)

The Bombay Stock Exchange (BSE)

China

Hong Kong Stock Exchange (SEHK)

Shanghai Stock Exchange (SSE)