You are on page 1of 367
Improve your trading profitability! ‘Too often, trading systems that looked good last year, in an advertisement, or during computer back-testing, lose money as soon as you start trading them. This book explains why that happens, and gives detailed techniques for developing systems that will be profitable. You will learn: How trading and investing differ ‘Why aimost ali investors are traders ‘Comparison of trading system software Introduction to AmiBroker trading software ow to evaluate trading éystems How to select a system that you will be comfortable with What to trade How often to trade How to use technical data How to use fundamental data Where to get data and how to prepare it Principles of system design and testing ‘Specific entry signals Several exit techniques Many trading system examples How to analyze in-sample and out-of-sample results ‘Statistically sound validation techniques Walk forward testing How to tell when a trading system is broken Broad market timing Intermarket analysis and filters Equity curve analysis Sector analysis and rotation methods Portfolig construction Monte Carlo analysis Position sizing Dr. Howard Bandy has both the formal education and practical experience required to write this book. He has degrees in mathematics, physics, engineering, and computer science. He was a university professor of computer science and mathematics, vice-president and designer of the major product for a company that produced programs for stock selection and timing, and senior research analyst for acommodity trading advisor where he held a Series 3 license. $US 49.95 ISBN-13 O7B-D7H1E38-0-5 1838-08 HSER 10 0-870 || | 54995 o"780979" 183805: il Quantitative Suadi Systems Quantitative Suading Systems Practical Methods For Design, Testing, and Validation Howard B. Bandy Blue Owl Press Copyright © 2007 by Howard B. Bandy. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, pho- tocopying, recording, or otherwise without the prior written permis- sion of the copyright holder, except brief quotations used in a review. AmiBroker is a trademark of AmiBroker and Tomasz Janeczko. Dakota is a trademark of BioComp Systems, Inc. ISBN-10: 0979183804 YSBN-13: 9780979183805 LCCN: 2007901091 Published by Blue Owl Press 3700 S, Westport Avenue, #1876 Sioux Falls, SD. 57106 Published 2007 Printed in the United States 10.09 08 07 10987654321 Disclaimer This book is an educational document. Nothing in this book is intended as, nor should it be construed to be, investment advice. ‘The views expressed herein are the personal views of Dr. Howard B. Bandy. Neither the author nor the publisher, Blue Owl Press, have any commercial interest in any of the products mentioned. All of the prod- ucts described were purchased by the author at regular retail prices. Investing and trading is risky and can result in loss of principal. Nei- ther this book in its entirety, nor any portion thereof, nor any follow-on. discussion or correspondence related to this book, is intended to be a recommendation to invest or trade mutual funds, stocks, commodities, options, or any other financial instrument. Neither the author nor the publisher will accept any responsibility for losses which might result from applications of the ideas expressed in the book or from techniques or trading systems described in the book. The programs used as examples have been tested and are believed to be correct, Results will depend on the specific data series used. Please verify the accuracy and correctness of all programs before using them fo trade, Contents Preface and Introduction......... Quantitative Analysis... Data... Trading System Overview ........ Measuring Success. AmiBroker. Issue Selection... Entries and Exits. Functions and Indicators. Trending Systems... Mean Reversion Systems... Seasonality Systems... Pattern Systems. Anticipating Signals...... Sector Analysis... Rotation ... Portfolios....... Filters and Timin In-Sample Out-of-Sample Statistical Tests.. Walk Forward........ Survivor Bias... .. 285 Monte Carlo Analysis... Appendix A Extending AmiBroker Appendix B Glossary..... Appendix C Resources... 2351 Index... vos soesuntentnnentne 35S Preface and Introduction Quantitative TRADING SysreMs focuses on three topics: Quantitative analysis techniques as applied to stocks, mutual funds, exchange traded funds, futures contracts, and currencies. An introduction to the AmiBroker program. Design, testing, validation, and implementation of trading sys- tems. Empnasis is given to: Techniques that have mathematical grounding - for example, equations rather than chart patterns. Techniques that are mechanical and testable rather than discre- tionary. Selection of issues to be traded, optimal holding periods, and ex- pected drawdowns. Analysis of entry methods, exit methods, stop placement, money management, and position sizing, Analysis of intermarket variables. Methods for broad market timing. Methods for validation of trading systems, including whether a trading system is sufficiently reliable to trade. 10 Quantitative Trading Systems Tue auraor, Dr. Howard Bandy: * Has university degrees in mathematics, physics, engineering, and computer science. * Has specialized in artificial intelligence, applied mathematics, modeling and simulation. * Was professor of computer science and mathematics, and a univer sity dean. * Designed and programmed a well-known program for stock selec- tion and timing. * Was a senior research analyst for a CTA trading firm: ‘Tne proGresston of trading system development that most people use is: 1. Invent, guess, read about, or buy a trading idea. Usually that means little more than an entry technique. Then: 2A. Begin trading using the idea or system tomorrow. The market lets them know whether it works or not. 28. Program that idea into AmiBroker or some other language, test the idea using historical data, and evaluate whether it would have worked. Try to improve the idea. Probably repeatedly. Either reject the idea or eventually begin trading. The market lets them know whether it works or not. The purpose of this book is to guide you through path 2B so that when you begin trading there is a higher likelihood that it is profitable. Wuat YOU WILL FIND IN THIS BOOK * An introduction to many aspects of technical analysis and quanti- tative analysis. Emphasis is on removing the mystery from things that work that you should know about, and debunking things that do not work so you do not need to spend time finding them out for yourself. * A thorough explanation of methods used to measure the goodness of a trading system, and useful metrics to incorporate into the ob- jective function. * Identification of the key aspects of trading system development, and techniques to incorporate them into your own trading sys- tems. Preface and Introduction VW Detailed explanation of data splitting into in-sample and out-of- sample periods, and the proper use of each. Enough mathematical rigor to (hopefully) be convincing without being overwhelming, WHAT YOU WILL NOT FIND IN THIS BOOK No Gann, no divergences, no chart patterns, no flags, no pennants, no trendlines, no Fibonacci retracements, no Elliott waves — noth- ing that relies on subjective judgment. All indicators and signals will be expressed in terms of unambiguous mathematical state- ments. No indicators or trading signals that change when additional data is received. All indicators and signals that are created will depend only on data already available. No guarantees of great wealth in a short time with little effort. No investment advice. This book is intended to be an educational text. Trading systems described are meant to be examples which readers can experiment with, expand, and develop for their own use. Neither the author nor the publisher will be held responsible for losses that result from application of the techniques described herein. No pricing for exotic derivatives. This is elementary quantitative analysis. The title was chosen to distinguish it from fundamental analysis and that part of technical analysis that focuses on subjec- tive analysis. No vague statements. I am regularly frustrated by advertising or jacket copy that tells me a book explains 18 profitable trading sys- tems in complete detail, only to find a few hand drawn charts, an incomplete set of vague entry rules, and no performance summa- ry. It is my sincere hope that none of you feels this book falls into that category. COMMENTS ABOUT THE EXAMPLES The exampies of AmiBroker statements, systems, and programs are in- tended to be educational. Whenever there was a decision to be made whether to write code that was clear and illustrative of the point being made, or efficient in terms of minimizing the number of lines of code or the execution time, the choice was always made in favor of the educa- 12 Quantitative Trading Systems tional clarity rather than the computational efficiency. Experienced us- ers of AmiBroker may wish to recode for execution efficiency. But, in my experience, if the program is clear and correct, recode it for efficiency only if the execution time is unacceptable. Every example has been carefully checked and rechecked. Neverthe- less, some errors are likely to slip through. With regard to AmiBroker code, refer to the AmiBroker documentation and help files. When in doubt, the AmiBroker compiler itself is the ultimate arbiter of correctness. Readers should be able to reasonably replicate the results of the pro grams listed. Exact replication would require using exactly the same data. Concerts AND TECHNIQUES Many of the concepts and techniques described can be applied in many different applications. Usually, the concept or technique is described in detail, often including, an example, when itis first mentioned. This book would be many pages longer than it is if every possible reference to ev- ery possible alternative was explained in detail. To save space, and to al- low the focus to continue to be on new material as the book progresses, later references to concepts explained earlier may be mentioned briefly, or may be omitted completely, with the expectation that the reader will remember them and apply them as desired. ORGANIZATION OF Topics Tt is difficult to describe all of the features of AmiBroker and all of the con- cepts of quantitative trading system design in a linear manner. Capabili- ties of the programs, techniques for using the programs, and concepts of trading system design are introduced in an interwoven manner, in what is hoped to be a more natural flow than a traditional user’s manual. This book is not intended to be a user’s manual for any program. There are many features of AmiBroker that are not covered. Quantitative Analysis By QUANTITATIVE ANALysis, I mean the application of mathematically based trading system models to financial data. Quantitative techniques can be very sophisticated, require advanced mathematics, and be applied to exotic financial instruments. But many of the concepts can be applied to trading stocks, mutual funds, and fu- tures using readily accessible data, computers, and programs. This book presents some techniques that should help traders ex- pand from fundamental analysis and the chartist aspects of technical analysis to more mathematical techniques that lend themselves to sta- tistical validation. I may be preaching to the choir in this first section, but I think it is important to clarify some distinctions about trading, investing, and the sources of information. FUNDAMENTAL ANALYSIS Fundamental analysis is based on the premise that a stock, bond, fund, commodity, or a market as a whole has an underlying intrinsic value. By analysis of the fundamental characteristics, such as the assets, liabilities, income, supply, or demand, that value can be determined. Fundamental data for a company includes earnings, sales, inventory turnovers, price to earnings ratios, price to sales ratios, dividend payout ratios, and any other information that might be reported on a balance sheet or income statement. Fundamental data extends to government and private research bureau reports, including gross domestic product, 13. 14 Quantitative Trading Systems inflation, balance of payments, and any other data reported periodical- ly. In general, fundamental data is only gathered, summarized, and re- ported — it does not represent trades. Economists and security analysts who focus on fundamental informa- tion have developed mathematical models of the fair price for one share of the stock — it is the current book value of the stock plus the present value of all future dividends that will be paid to that one share. Any dif ference between the actual price of the stock and this fundamental value of the stock represents an opinion on the part of shareholders. The fundamental analyst uses data that is reported by a company or agency to create subjective models. She may use charts to gauge overall price activity, but with few mathematically defined indicators. While fundamental analysis may have value in its own right, there are several problems associated with incorporating it into trading models. 1. There is a great disparity in the granularity of the data. Mutual funds trade every day. That means there are 252 data points, each representing a mutual fund trade, or trading opportunity, ina year. Stocks and futures trade every minute — even every second. There are thousands or millions of data points per year for each stock or future. But there are many fewer data points for each fundamental data stream. Four in the case of quarterly GDP, twelve in the case of Michigan Consumer Sentiment, and so forth. If we want to use some fundamental data along with our daily stock prices, we have the problem of deciding which day is the fundamental data value assigned to and what to do with the days in between. One solution is to work with monthly data rather than daily data. Any data reported any time during the month would be assigned to the last day of the month. Using monthly data would mean we have only 12 possible trading opportunities each year, and this would make validation more difficult. 2. There is a lag between the time period being described and the release of the report. First quarter company earnings are released some time after the end of the first quarter. Similarly for GDP. 3. There is often revision of previously released data. For government reports, these revisions are well publicized and expected. As an example, the unemployment rate is released the first Friday of each month, reporting on the previous month. The data representing August is reported around September 5. There are usually two revisions to the employment report. The first revision is in October, ard the final revision is released the first Quantitative Analysis 15 Friday in November. If Iam building a trading system that uses the unemployment rate in some way, I have three very serious problems. First, the reporting granularity is monthly. Second, the reporting date is inconsistent and does not readily align with other monthly data, such as end-of-month closing prices. Third, the data point for August is very preliminary on September 5. The final number is released on about November 5. When I download the historical data for monthly unemployment, the figures reported are the final revision figures. To use this series properly, I must provide for a three month lag — August activity to November reporting. If I want to trade at the release of the August data, I must use a preliminary figure; if | want to backtest the trading system, the data is all final figures. These are two distinct data series, but only one of them is easily obtained. Granted, the revisions to the monthly unemployment rate are usually minor, but other series are often revised so extremely that the sign of the reported number changes. For corporate reports, before about 1980 revisions and restate- ments were unusual — about 3 of the S&P 500 companies each year. Since 1995, and particularly since 2000, revisions have be- come much more common — about 200 of the S&P 500 each year. Assume we are following the XYZ stock. And assume we are bas- ing our investment and trading on their earnings per share. We open Value Line, Moody's, or an Internet site and see quarter by quarter earnings from the most recent quarter back for twenty years ~ eighty data points. Tomorrow, the CFO of XYZ announces that the earnings for the past three years will be revised and gives the restated figures. Next month when we open Value Line, we will see only the new, revised earnings. That causes us to wonder which of the figures we see are original data and which are revi- sions. The revision of the previous three years data might cause buy and sell signals that were based on the original data to change when the revised data replaces the original data. In company reports, there is the regular use of one-time en- tries that seriously distort data. A company may have failed to successfully incorporate a new subsidiary into its parent. Its earn- ings statement reports $1.25 per share profit from ongoing opera- tions. In a footnote it reports a one-time charge of so.80 per share due to the problems with the subsidiary. The first question we ask is “which figure fits into the definition of our data series — $1.25

You might also like