I use the method as described on pages 461 and 462 in
Options as a Strategic Investment by McMillan. Basically you take the log of the ratio of each day's price and the previous day's price.
Then you compute the standard deviation of those values.
Next, multiply the standard deviation by the square-root of the number of trading days in a year to convert to an annualized volatility so that you can use it in the Black-Scholes model.
Here's my C# source code to compute a series of historical volatilities.
public class OptionMath
{
const double AnualTradingDays = 260.0;
public static double[] HistoricalVolitilitySeries(double[] prices, int period)
{
Debug.Assert(prices.Length-1 >= period);
double[] v = new double[prices.Length-period];
for (int j=0,i=1; j < v.Length; j++,i++)
{
v[j] = HistoricalVolitility(prices, i, period);
}
return v;
}
public static double HistoricalVolitility(double[] prices, int index, int period)
{
double[] P = prices;
double[] X = new double[period];
double mean = 0;
// get the logarithms of the daily price ratio
for (int j = 0, i=index; j < period; j++,i++)
{
X[j] = Math.Log(P
/P[i-1]);
mean += X[j];
}
// compute the mean of the logarithms of the price changes
mean /= period;
// compute the deviations from the mean
double sum = 0;
for (int k = 0; k < period; k++)
{
double d = (X[k] - mean);
sum += d*d;
}
// standard deviation of the daily volatilities over the period
double v = Math.Sqrt(sum/(period-1));
// scale to an annualized value for use in the BS equation
double annualVolatility = v*Math.Sqrt(AnualTradingDays);
return annualVolatility;
}
}