Optimizing moving averages by Charles J. McGuinness
One of the first techniques you'll come across in almost any book on technical analysis is moving averages. Moving averages are amazingly handy in predicting the movement of securities. By themselves, they offer a simple means of generating reliable buy/sell signals—many people monitor a 200-day moving average to catch long-term trends. Moving averages can be used in conjunction with other indicators, helping to filter out the rapid buy-sell signals that minor fluctuations in price often generate. Perhaps the most alluring thing about moving averages is that they are easy to compute and easy to apply. Hardly a market timing indicator exists that doesn't incorporate them.
This simplicity can be deceptive when it is time to apply moving averages. A straightforward translation of a moving average algorithm into program code can produce an application that generates its results slowly. The first time I wrote a charting program I did nothing intricate in implementing the moving average subroutines; when I plotted the first moving average it was drawn across the screen at a snail's pace. It was so slow I assumed I had made some sort of drastic error in my coding! The problem was that the moving average algorithms on which I had based my code were designed to express the algorithms' concepts as simply as possible and designed without taking into account efficiency and real-world implementation issues.
If you use a lot of moving averages—for example, in a charting program—this slowness can limit your ability to perform the kind of analysis you want to do. Here are some techniques for producing fast and efficient moving average routines. Whether you're experimenting with some simple programs in BASIC or writing complex optimization programs in C, these techniques can help turn a sluggish number cruncher into a speedy tool.