Sunday, June 23, 2013

Backtesting Software for All-Time High Stocks

The following is a guest post from Jaakko, a reader with whom I have corresponded for quite some time now. In addition to being a trader, he is a savvy computer programmer and in his spare time he created a platform for back testing trading strategies for all-time high stocks. A particularly novel aspect of the platform is its comprehensive handling of portfolio rebalancing, commissions and tax implications. Jaakko gave me a copy of the program to use. I am not affiliated with the software in any way.

---
Hi everyone,

First of all I would like to thank Andy for letting me write a guest post to his great blog.

ABOUT ME
I have always been skeptical about everything, which means that I do not believe anything until I have tried it myself. In practice this has meant that if I have read an article about a great trading strategy, I do not take it until I have thoroughly tested it. A couple of years ago I stumbled into an article written by Blackstar Funds, entitled "The Capitalism Distribution". It was about an investing strategy where one invests only in stocks which are trading at their all-time-highs and selling them instantly when the trend seems to have stopped. For me this seemed pretty logical, if a stock is making new all-time-highs the company's business idea and balance sheet must be sound. As usually the market is said to be always right why fight the trend? In addition, the one thing that hit me in the article was the fact that only a handful of companies are in charge of majority of markets returns. These stocks are the ones constantly making new highs. For every multibagger stock making new highs there are dozens of stocks losing money.


That article actually changed the way I think about investing. But, being skeptical as I am, I thought it best to paper trade the strategy first before starting to play with my own money. How did I test it? Well, I just chose a date from the past, wrote down the stocks at their all-time-highs and checked their share prices one year from that period. That was painfully slow and left much room for improvement. If I wanted to test whether the strategy actually worked or not I had to do something completely differently. As a software developer I decided to make a software which I could use to test the above strategy. That way I could get instant and honest feedback. After a couple of months hard work I was finally finished and ready for some serious backtesting!

WHAT IS THE SOFTWARE ABOUT?
What did I then actually implement? Below is an image of the user interface for illustration purposes.
In the user interface:

  • I can easily set how big of a chunk commissions eat from every transaction (minimum commission as well as a percentage amount). This is the commission structure we have in Scandinavia.
  • I can set the start and end date for the testing - obviously...
  • Starting capital - again obviously...
  • This is the part of which I am extremely proud of as I have never seen or even heard of a trading software or research paper which could add the effect of taxes precisely in the calculations!! I can with a click of a few buttons set how much taxes I need to pay from capital gains as well how many years capital losses are still deductible!
  • Next, I define which symbols (or stocks...) are to be included in the backtest. I can include a maximum of 10,000 symbols. (Note that if a symbol does not exist, the downloading stops. {editor's note: this means that the backtesting is subject to survivorship bias, which is difficult to avoid})


Once the symbols are entered and the settings are done, the program is ready to run a backtest. It takes some time to download all the end-of-day stock data from the internet, so patience is needed, but the progress bar keeps the user informed of the status. After a moment, the downloading is done, the algorithm goes to work for the backtest. Backtesting results are stored in a separate pdf file. I am pretty proud of this part since the pdf file looks nice and contains loads of useful data. It has a full log of all transactions, as you can see in the first two pages of the report, below:

Moreover, the report contains performance data, statictics, comparison to S&P 500 and so on. Some of these are shown in the excerpt below, and the entire report is available in this pdf.


Ok, now I have the results from the default trading strategy, based on a simple moving average, but I would like to see how a high standard deviation of a stock affects the result. Well, the software enables this to be done pretty easily. I just open the built-in strategy editor, add a new function for standard deviation calculations, and add a rule to the strategy that only stocks with high standard deviation are bought. This is what the editor looks like:

Voila! It compiled and then I just click the Run-button to re-start the backtest. I was able to do all this
within a couple of minutes. Honestly, I have saved myself hundreds of manhours as backtesting
is now so simple. The couple of months hard work have really paid off. The new results are output in a new report, and you can see the performance summary below. In this case the portfolio performance improved by focusing on high-volatility stocks (see the results by downloading this pdf).



ABOUT THE RESULTS
I included roughly 30 NYSE based stock in the backtest which still does not give very reliable picture of trading all-time-high stocks but it does give some guidelines. Trend trading these all-time-high stocks produced a 2793% total portfolio return with the default strategy and a whopping 9667% return with the edited strategy, versus buying-and-holding the S&P 500, which produced only 1044%. These results include 30% capital tax rate, commissions and still it outperforms the index quite clearly. Talk about that!

However, there is still a major drawback with the strategy. If you read the pdf file carefully, you must have noticed the amount of the trades. It really takes disciple to stick with the strategy as there are a lot of buying and selling due to taxes and rebalancing. E.g. when a new stock is bought, existing stocks need to be partly sold and vice versa. The same holds true when taxes need to be paid, you need to sell part of your holdings. For most of the readers of this blog, it might not come as a surprise when I say that all-time-high stock trading provides an edge but it is not a quick get-rich-strategy. But, wealth comes with time and patience.

Currently I am using my software with Scandinavian stocks because trading North American stocks is slightly expensive and time consuming (US markets close for me at midnight). Nevertheless, I can still conclude that the same results work in my markets as well.

Cheers and Thank You for reading,
-Jaakko (athtester@gmail.com)

Disclosure:
I am long on KONE, Hexagon, PKC, Wärtsilä, Vacon, Olvi, Sampo, Pohjola, Assa Abloy, Atlas Copco, Elekta, Handelsbanken, Investor, Kinnevik, Latour, Lundberg, Melker Schörling, SCA, Trelleborg and Wallenstam

“This linking together in turn lets us tap our cognitive surplus, the trillion hours a year of free time the educated population of the planet has to spend doing things they care about. In the 20th century, the bulk of that time was spent watching television, but our cognitive surplus is so enormous that diverting even a tiny fraction of time from consumption to participation can create enormous positive effects.” 
- Clay Shirky author of Cognitive Surplus

Related Posts
Subscribe
Bookmark and Share

Editor's note: this article has been modified since it was initially posted

21 comments:

Anonymous said...

1st it is nice to know there is a possibility to backtest. I was looking for a way to do this, too.

2nd I want to mention, contrary to Andes blog, which is free, I am surprised, the backtesting software is not.

3rd I assume the software has only a predefined set of rules by which it trades. I for myself would like to test different way of the darvas boxes/trends, with different time periods and candles ie daily candles, weekly candles, monthly quarterly and yearly. From this on, there are many ways to draw boxes, which does not matter much with the breakout - you can have 2 set of buys - either at the realy high or the highest close. What matters most are setting the stop loss marks, which I found for myself there you can have at least variations, which for myself are more important to test, especially on different time candles.

Anonymous said...

My apologies for the spelling mistakes and want to add what I left out in the last sentence: "at least 4 variations"

Andy said...

Thanks for your comments. I know Jaakko's ATH backtester is quite powerful but he would be the most knowledgeable about how to manipulate the stop-loss strategy.
cheers,
Andy

Jaakko said...

Hi,

Thanks for the comments from my side as well.

1) It is quite hard to find a decent backtesting software. I originally used Metatrader but I found major problems in it which limited the testing and biased the results. First, stock price data was very limited and unreliable. Second, the software somehow mysteriously added "extra" costs to trades which made the results unreliable. Third, I wanted to see the effect of taxes in the backtests as well. Fourth, I wanted a reliable and transparent white-box backtesting system.

2) If there are enough people interested in the software I can provide it for free. I have still spent quite a lot of time developing it so I would not want to give it away that easily as the source code (which has a couple of very complex algorithms) can be stolen very easily.

3) There aren't any predefined/hardcoded rules used in the software. It was originally designed to trend following but I wanted the software to easily test other strategies as well. That is why it has the strategy editor (similar to Metatrader's). If I recall correctly, in the Darvas breakout system one buys stocks when they break out from a predefined "candle" box. This is very easy to implement with my software. Only sky is the limit where the backtester can be used. If you are interested, I can help testing it.

I'm currently using my backtester so that it automatically does all the calculations by the end of the day, gives me a notification what it is about to do and after approval sends all the planned transactions to my broker. In addition, I have as well made a separate more light-weight version of the software which is using similar trading metholodogies but trades only during intra-day. But this is just in a beta-mode.

Andy said...

Jaakko - using the software as the platform for an automated trading system is very cool and I (and others I'm sure) would be interested to hear more when you move out of beta mode. ... one question comes to mind right away - what stock data are you using for your ATS?

Jaakko said...

The version you tested used Yahoo (not very reliable) but my version is using EODDATA.

Anonymous said...

First, thank you Andy and Jaakko. I can understand your point Jaakko; giving something away you put alot of effort into it really is not easy to do in today's world.

I might want to add, besides comissions and taxes, you perhaps want to consider adding annual inflation rate. It might sound funny, strange or ridiculous, but inflation really is the silent, indirect killer, if you are ignorant about it. Be carefull to have the calculations in the way that you substract the annual inflation rate AFTER taxes, not before, since the governments do not care about inflation rates on your tax papers and will not let you deduct them before. So the annual calculations go in the this order: 1st commissions, then taxes, then inflation.

At this point I want to suggest something to Andy: I can be sure you have read Darvas' book. In it he explicitly mentioned a price increase with an increase in volume. If you look at DELL, MSFT during their historical runs, there was a period where volume spiked disproportionally high during breakout, until it started to drop down while price kept rising exponentially. I was thinking, since you retrieve your data from yahoo, they also give volume. It would be nice to show and compare the current weeks volume, to the average weekly volume, say the period between breakout and all the way back to the last all time high. I can imagine the ratio should be at least above 1, more like 1.5, 2.

Furthermore, I try to find breakout stocks, which do not have a steady rise, ie a nice straight regression line to heaven, but an exponential line, which in charting shows itself in the way: each breakout you have to adjust the support lines further up. If I remember correctly, last week I saw a couple stock that fit the bill of rising price and volume – ADUS, YY, and RH. Whereas ADUS currently also has the characteristic of an exponentially rising price quite noticeable on weekly candles.

I tried to narrow my search for an exponential price increase with the help of Finviz (thanks to you for adding the link) by adding a criteria of 50% price increase in a quarter. Unfortunately you cannot screen for a month to month, week to week, or total % change over a certain period ie the time since the stock broke through all time highs.


Back to Jaakko:

With regards to testing, after I found Andy's blog, I was manually (painstakingly I might add) checking on stockcharts.com different time candles and stop losses for several weeks now. With a working and reliable backtesting system I could check many as stocks as possible, before I decide on a time candle and a stop loss that goes with it. I just hope your system does not mistake 52 week highs with all time highs, and has enough memory to get all data.

I want to know, if it is justifiable by time, effort and commissions to trade on a small time scale like daily or weekly. Daily and weeklies will require either daily or weekly check ups and I can imagine. will also generate more entry and exit signals, which in turn will generate more commissions. Assume around 0,0035-0,005 cents per share, if you are with Interactive Brokers, otherwise around $5 flat with a broker like MB trading.

From my inital manual screening over the last weeks, I can already say weekly candles seem to have alot more entries and exits than monthly candles. And not only that. On average from the few samples I looked at, the monthly candles had an at least equal or substantially better annualized return than weekly candles. Whereas the weekly entry and exit signals were sometimes 2-3 times higher than the monthly. That is why it is quite important for me to choose not only a time scale, but also a matching stop loss for that time scale.

Anonymous said...

In theory you want to keep losses as small as possible, hence for normal investors daily candles might be the most approriate time scale. If you had a black box, like many investment banks, you could perhaps even do automated trades within the cents range on minute candles, minimizing losses by a cent only. However, huge daily candles can be tricky, if you stick to your rules and put the stop loss under its low. So can be a surprise gapping drop the next day. This is one reason, which would justify going on a larger time scale, ie weekly, monthly etc to you give the stop loss some room. I am thinking, if I can have a 5-10% loss on a daily scale, why shouldn't monthly candles do just as well, which would generate less signals.

As for setting up the backtester I wonder, if you can manage to make it analize different candles ie weekly, monthly, quarterly, yearly from the daily data feed ie have a drop down box asking the user what candles to consider.

With regards to stop losses and the variations on the Darvas Boxes. I realize this might be quite hard to explain.

First I want to explain my definition of a breakout candle (BC): The candle which closes above the all time high. You can have a variation, as mentioned before, to consider the highest close. The latter seems to matter more on a daily candle basis than larger time scales ie weekly, monthly etc. With regards to entries, depending how active/impatient you are you can wait until 5 minutes of the end of the day to buy as close to the daily close, or you buy on the open on the next day.

From here on things can get complicated quite quickly. Since you can distinguish between the BC where you made your entry, and the BC's following it. You can put one rule for the inital BC (IBC) and then change and maintain the rule for all the BC's following it (FBC). I was thinking, why not have an exit rule which would be the "opposite" of the the entry:

First you can have a very simple exit rule: putting the stop loss (ST) under each low of a BC. A variation to this would be to consider a gapping BC. In this case you can put the ST under the low of the candle preceding the BC. The variation seems to matter alot on a weekly time scale.

Another simple exit rule: ST under the low of the IBC, then ST under the low of each candle following the IBC, no matter if it is a BC or not. This seems to work well on larger time scale ie monthly, quarterly, etc This also has the benefit of a „time stop“ build into it – either the price makes progress over time or you withdraw your funds. With regards to gapping IBC's in this scenario, I would not want to consider putting a stop loss under the low of the preceding candle. But nontheless I would be curious about the results.

Now to the more complicated exits. Have the inital ST under the low of the IBC. Each FBC that follows you change the rule. Depending how much room for „play“ you want to give I experimented with a difference of 1 - 2 boxes, whereas 2 boxes in general ment more profit. But more profit over a longer time period does NOT mean more annualized return! Consider 1 box: On the 3rd FBC you put the ST on the low of the 2nd FBC. On the 4th FBC you advance the ST to the low of the 3rd FBC. With 2 boxes: On the 4th FBC you advance the ST to the low of the 2nd FBC. On the 5th FBC, ST on the low of the 3rd FBC.

A variation to this would be to take the lowest low, ie you allow the price to go under the low of the FBC. For example: Initial ST on IBC, then you have 2nd FBC with ST still on the low of the IBC. Then you get a 3rd FBC, but you put the ST on the lowest low after the 2nd FBC ie between the acutal lowest point between 2nd FBC and 3rd FBC. You let the price fall under the low of the 2nd FBC and any FBC following your IBC. So, on the 4th FBC you put the ST on the low between 2nd and 3rd FBC. And so on.

Anonymous said...

I want to suggest another feature with regards to money management: Assume the inital position at breakout is only 1%, you can either leave it at 1% for the entire run or add to it each time 1% you have a new BC. Like this you can add to it up to 10% maximum. So, add each time 1% to a max of 10% until the rest of the run or leave it at 1%.

Also, I saw that your results table quite nicely show the breakup each month of the year, year by year, with a total, too. Since I believe and must assume markets change somewhat over the course of the years, I do not want to put, too much attention on the beginning of this century and more focus on the recent years. Hence, an addition of a summary for 5 and 10 year interval, with another coulumn added each time showing the progress from thereon would be quite helpfull, too.

Jaakko said...

Andy, are we making a comment record now or what? ;)

"With regards to testing, after I found Andy's blog, I was manually (painstakingly I might add) checking on stockcharts.com different time candles and stop losses for several weeks now."

-(This will sound marketing but bear with me) Yes. Most people don't even realize how much time they are losing when testing manually strategies. Investing in a decent backtester really pays off both in terms of time and money. When it takes you months to test a strategy with unreliable results, I can do it within a couple of hours reliably. Imagine that. Besides I get the backtesting results using the whole market so I can really determine whether I should invest my own money with the strategy.

"I just hope your system does not mistake 52 week highs with all time highs, and has enough memory to get all data."

-The "actual" all-time-high price is always the entry price. By "actual" I mean excluding the effect of dividends.

"That is why it is quite important for me to choose not only a time scale, but also a matching stop loss for that time scale."

Take into consideration that if the main strategy itself is not profitable, tinkering it won't make it better. What I have learned is that the higher the proportion of commission in regards to potential profit, the sooner you will be wiped out. Costs matter and the smaller you can keep them the better.

"As for setting up the backtester I wonder, if you can manage to make it analize different candles ie weekly, monthly, quarterly, yearly from the daily data feed ie have a drop down box asking the user what candles to consider."

-Yep. It's pretty simple to do that. As I said previously there is no limit what can be done with the backtester. The idea you mentioned above is just a strategy which I can easily implement with the strategy editor. If you want me to test it for you, we can discuss about that through e-mail.

Anonymous said...

Thank you Jaako for the suggestion my e-mail is ze_liquidator@yahoo.de

With regards to the price with or without dividends. Perhaps you can have a check box or drop down box asking the user whether to consider prices with or without dividends. I personally prefer "true" prices ie with dividends included, as Andy also made a convicing case for it on his site with regards to GOOG.

In fact, I noticed one stop loss can work better on a different time scale. As weird as it sounds. Or perhaps it was a bad observation. Another reason why I would want a reliabe backtesting software. Of course, if it does what you promise, anyone would pay a price to save time and effort. The question is, just like stocks "How much?"

Anonymous said...

"The "actual" all-time-high price is always the entry price" I just realized this seems a bit odd... or I did not understand your system. Having the entry price as "actual" all time high confuses me to be honest. For example if you take the period from Jan 1st 1983 to Jan 1st 2011 on DELL I do not want to "ride" any wave up. I only want to ride the wave, which is making new ground/goig discovery mode. The way I think I start to understand your set up on the backtester currently, you have some sort of trendfollowing set up much like the moving averages, ichimoku cloud and the ADX indicator, which will let you trade any price, even "inside the box". I on the other hand only want to consider prices that jump outside the box, if you understand. Hence, all time high stocks are the ones for me. Andy for example I saw makes use of an 7-ATR (by the way Andy, I tried to look on freestockcharts, and stockcharts for an overlay with the 7-ATR - they don't have it or it is named differently?), whereas I want to go about testing stop losses some my way and some after darvas boxes.

I just realize you might have quite some work ahead, if there will be people coming to ask you to test your backtesting with moving averages, ichimoku clouds, and ADX indicators as triggers. Perhaps you should have a couple of predefined set ups ready to be chosen by the user.

Jaakko said...

"I want to suggest another feature with regards to money management: Assume the inital position at breakout is only 1%, you can either leave it at 1% for the entire run or add to it each time 1% you have a new BC. Like this you can add to it up to 10% maximum. So, add each time 1% to a max of 10% until the rest of the run or leave it at 1%."

-Yeah. Money Management is definately something which could improve the results. Currently even a single trade can theoretically wipeout the whole portfolio.

"Hence, an addition of a summary for 5 and 10 year interval, with another coulumn added each time showing the progress from thereon would be quite helpfull, too."

-Thanks for the comment. Sounds useful.

"Perhaps you can have a check box or drop down box asking the user whether to consider prices with or without dividends."

-Adding the effect of dividends is problematic for 2 reasons. 1) It requires a high-quality price database which automatically adds dividends. However, this would get very expensive as all database providers require a big fee if their data is used publicly. 2) Taxes need to be paid from dividends. In this sense the original idea of adding the effect of taxes to the results would suffer if part from the dividend amount is not subtracted. This would add more complexity to the software.

""The "actual" all-time-high price is always the entry price" I just realized this seems a bit odd... or I did not understand your system. Having the entry price as "actual" all time high confuses me to be honest."

-Starting from the first available price onward, the strategy saves all prices. After enough data (at least 250, which is 1 year), it is checked whether the current price is the highest one. If it is, an entry is made. When the stock drops below its 250 SMA, it is sold. Simple as that. Yahoo provides only data starting from 1983 so all stocks which have been traded before that are sort of "resetted" for price.

Andy said...

Wow, definitely an ATH for comments! Great discussion.

Anon - For something like a 7-ATR stop in FreeStockCharts, choose 'price' then 'indicator' then 'volatility stop' and then edit it to adjust the period (say, 14 days) and multiplier (say, 7).

Jaakko - EODDATA sounds much more suitable to an ATS than Yahoo Finance! ... I also strongly agree with your point about keeping costs down.

Alexander said...

Thanks Andy. I notice the volatilty stop is nice, but it does not "behave" or draw the same as your 7-ATR, even with a 7-7 adjustment. The orange dots do adjust downwards, whereas your 7-ATR stays at one level and only adjust upwards, which is the idea.

Jaakko said...

Thanks for the great comments on how to improve the tester. I will be implementing most of them when I just have the time.

Andy - The EODDATA (or any reliable DB) is a bit problematic because then I would need to buy a commercial license for the feeds if I want to use the data publicly. Currently EODDATA charges something like 750$/month for US data (most likely even more). http://eoddata.com/products/datafeed.aspx

I don't know if anyone is interested but I'm currently planning to improve the software with another like-minded and extremely competent person so that we can add company fundamentals to the strategy. If anyone is interested about backtesting results where fundamental data (P/E, P/B, ROE, Revenue, Cash Flow etc. etc.) and technical analysis is combined, please contact me.

Alexander said...

Jaako $750/month is probably not necessary, since you are no broker to provide constant updated data feeds to your customers. All you need an accurate snapshot per se if you think yahoo's database is flawed and theirs is about 100% accurate. You can just make a one time purchase of all listed stock for a certain time, 30, 50 100 years and keep your own database, assuming you have enough space.

Alexander said...

I might want to add, considering purchasing fundamental data goes hand in hand with fundamental research. I do also fundamental research and from what I see fundamental research with regards to P/B value and P/E ratios does take much longer time to kick in, if it kicks in... This site which brought us together is more concearned with technical trading, pure prices analysis with a bit of volume - we are more momentum trades, taking the opportunity as it stands right now. Fundamentals from experience matter little with regards to short period price development ie an exponential growth rate with regards to "growth investing" might only pay off in several months/quarters, if the market "thinks the same" and then pays off. But your and your friends choice, if you decide to go into that field and purchase extra data.

Alexander said...

Though, I do know for a fact the driving factor in most powerfull runs are P/E ratios, which is also mentioned in Darvas' book. But he also stated, he disregarded fundamental data at least 1 time because the price and volume developemtn was so compelling, which turned out that there was a quiet take over/merger. And I am a strong follower/believer that price is all you need to know and has "short term" news and expectations priced into it.

Andy said...

Alexander - I think the volatility stop can be used either for long- or short-signals, so it's kindof a trend indicator (either up or down). The way I do the ATR stop it is long-only and ATH-only. I'm not sure how to replicate that exactly in FSC, but if you're looking at ATH stocks then the volatility stop will still work since ATH's are trending up by definition.

Jaakko - have you considered connecting your ATS to Collective2 as a means of making the signals available. This might avoid the data sharing issue. Systems with a solid track record on C2 seem to get a good following and they do collect fees.

Jaakko said...

Andy - Collective2 is in theory a good idea but I abandoned it as it would require several years of activity. The system can easily underperform its benchmark for several years in a row and if this happens it will take several years for the outperformance to really start to kick in. So, lots of patience would be required in case C2 would be used.