I have written a fairly basic “Effective Position & Risk Management System” in R, for managing asset positions with several option legs.
Every morning, it paints me a pie chart of where I’m getting my theta decay, as well as my effective exposure to each of the major asset classes.
One of the requirements, is calculating the Implied Volatility of each option. I found some approximations, interesting, but none seem to be as intelligent as using a brute force method or bi-section method.
I wrote a recursive function and used it for a month, but it took forever, or got lost and produced errors. Obviously, I was faced with playing with step size/factor and accuracy trade-offs.
So, I’m currently using a brute-force method, which seems to work well and faster than the recursive algorithm. It’s not perfect, but it’s working. If anybody out there on the interwebs has a better or more beautiful solution, let me know. Here it is:
FindIV <- function(OptionMarketPrice, AssetPrice, Time, Strike, r, LowRange, HighRange, Right)
{
Range <- seq(LowRange,HighRange,by=((HighRange-LowRange)/100000))
OptionBasedOnGuess <- GBSOption(Right, AssetPrice, Strike, Time, r, 0.000, Range, "wtf", "xx")
Error <- OptionBasedOnGuess@price - OptionMarketPrice
Key <- which.min(abs(Error))
Range[Key]
}