This program calculates the annualized historical volatility for one or more stocks over a user-specified number of N trading days. The program uses daily closing prices in the calculations. If not specified, the program defaults to N=20 previous trading days.
The user may supply either a single ticker symbol or a cell array of ticker symbols. Using the supplied N, the program will then calculate the historical volatility for each stock.
In order to run the program, the function 'hist_stock_data.m' must be downloaded and stored in the same directory. This function may be found in my MathWorks File Exchange.
Thanks for pointing out the bug. I've made changes to the code now which should handle dates with days < 10.
I think there is a bug in the code where the day has a leading zero. For instance, today, the "ddmmyyyy" is 07102010. When the for loop uses the str2double feature, I think the leading zero gets truncated such that when it is recast to a string, the data is of the form 7102010 instead of 07102010, and the hist_stock_data function does not handle this.
If you are using 2009b You will need to add the command: today=now to the first cell before the date variables are declared.
Fixed error in date handling