Thursday, December 30, 2010

Building a Trading System: Part 3 - Getting Started

Now that we have a basic theory to work from we can start building our basic trading system. First, let me quickly point out what we are trying to accomplish. Investopedia describes a Trading System like this:

"A trading system is simply a group of specific rules, or parameters, that determine entry and exit points for a given equity. These points, known as signals, are often marked on a chart in real time and prompt the immediate execution of a trade."

Ernie Chan, in his book Quantitative Trading, is even a bit more precise:

"Quantitative trading, also known as algorithmic trading, is the trading of securities based strictly on the buy/sell decisions of computer algorithms."

To be crystal clear then, we are trying to create a basic set of rules to tell us when to buy and sell, which are clear enough to be written into code that a computer can execute. Further, we want the code to embody the Mean Reversion theory 8we talked about in the last post.

As we said last time, there are lots of ways to measure mean reversion, so we just need to pick one. Since I mentioned the 5-day moving average last time, we might as well stick with that as a measure of the mean. (Disclaimer: Please remember, this is just an illustration. There are many ways to measure the mean or norm, and I don't recommend trading it this way. I also don't recommend any particular strategy, algorithm, or technique you might see here. These are all just examples to explain how a trading system might be developed.)

With the 5-day MA used as our norm, we next have to determine what constitutes variance from this norm. In other words, what are we looking for that says, "Hey, things are out of whack?" I'm going to start with something simple. Here are my basic rules:

1) ENTRY: When the price is more than 5% below the 5-day moving average, buy at the close.
2) EXIT: When the price closes above the 5-day moving average, sell at the close.

That's pretty simple, eh? I wonder if it works. Stay with me and we'll check it out in just a few moments. This illustrates an important point about system development, however. Simple is better. When rules start looking like "Buy when the price is down 7.3% and the Stochastic is turning down, and it's Tuesday, and the sky is blue, and the MACD is crossing over the triple moving average of the price of gold, and..." Well, you get the idea. Complicated rules have a tendency towards curve fitting. In other words, they fit the rules to the data so the outcome is predetermined. This sort of "fitting" reduces the ability of the system to work under normal conditions, and therefore to work in the future. Good systems are based on a theory that makes sense, and are enforced with minimal and straightforward rules.

Now, let's see how this simple set of rules will perform. I plugged it into Amibroker with some very simple code. In everyday language, this line of code says, “Buy when the Close is less than the 5-day MA times .95 (that's 5% less than the 5-day moving average); AND Sell when the Close is greater than the 5-day moving average.” Here's the code:

Buy = Close < MA(C,5) * 0.95;
Sell = Close > MA(C,5);

In order to do a backtest I had to choose an underlying group of stocks to test. For this example I chose the SP500. It could be anything (NDX100, SP100, All NYSE stocks, ETF's, etc.), this just happens to be what I'm using for this example. I also had to choose a time period. For purposes of this test I'm using the period from 1/1/1999 to 12/31/2004 as our "in-sample" data. The idea is that I will use a chunk of data for developing the system, and then later test it on some fresh data to make sure that it still works on data that I haven't fit it to. That's called "out-of-sample" testing. For that I have reserved the data from 1/1/2005 to the current day.

Geek note: For those who care, I have set the buy and sell prices to be at the close. I have not included any transaction costs (they will be added later). No stops are used. Position size is set to 20% of the portfolio, and 50% margin is being used. Other settings are default.

So how did we do?



Above are some of the stats from the first pass at our system. At first glance the CAGR looks interesting, but I'm not sure many of us could stomach the 81% drawdown. The low Sharpe Ratio of 0.46 is scary to me. It means that we have a system where my account balance will be going up a down a lot. On the positive side, the system makes money. It also sees plenty of action (over 3000 trades), and has a nice win ratio, at more than 62%. Personally, however, I could never trade such a volatile system, so we'll need to improve it. To give a better idea of what this volatility means to the trading account, take a gander at the equity chart:


2002 would have been a painful year, but even beyond that you can see that there are many extreme peaks and valleys to get through. Yes, in the end the system made money, but it would take a tremendous amount of discipline to not abandon ship at times. In the next post, we'll introduce some ideas to smooth things out a bit.

Good Trading...

No comments:

Post a Comment