Sunday, January 31, 2010

DSP: Using a basic digital filter: Stock market example

Digital filters are very powerful tools in DSP. In this example we will use a digital filter to help with stock market analysis. In the figure above, you can see the raw data plotted with a 5 day moving average and a 20 day moving average. These two moving average lines can be used for buy/sell predictions.

To calculate the 20 day moving average, the window size and filter coefficients must be established. The following Matlab code sets the window size and calculates the filter results. The five day filter is implemented using the same code, with a windowSize = 5. The matrix x in the example contains the raw stock data. For more info on the filt function:

windowSize = 20; % Twenty day average
% The following filter applies the 1/windowSize coefficients
twentyDay = filter(ones(1,windowSize)/windowSize,1,x);

No comments: