0

Trying to calculate implied volatility for an option using PHP. The BlackScholes function calculates the price of an option, $OptionValue is the price of the option and $guess is an estimation.

I've based my attempt on similar applications but my version times out. Stepping thru the loop on paper I think makes sense to me. What did I do wrong?

function ImpliedVolatility($type, $S, $X, $T, $r, $D, $OptionValue, $guess) {

$x1 = 0.01;         //lower approx
$x2 = $guess * 2;   //upper approx
$epsilon = 0.01;

$ValueGuess = BlackScholes($type, $S, $X, $T, $r, $D, $x2); //Initialize

while ( abs($OptionValue - $ValueGuess ) > $epsilon ) {
    $xMid = ($x1 + $x2)/2;  //new approx
    $F1 =  BlackScholes($type, $S, $X, $T, $r, $D, $x1);
    $FMid =  BlackScholes($type, $S, $X, $T, $r, $D, $xMid);

    if  ($FMid > $F1 ) {
      $x1 = $xMid;
    } else {
      $x2 = $xMid;
    }

    $ValueGuess = BlackScholes($type, $S, $X, $T, $r, $D, $xMid);
}

return $xMid;

}
aynber
  • 22,380
  • 8
  • 50
  • 63
Mark
  • 73
  • 8
  • What error are you getting ? – Nadir Latif Feb 06 '17 at 13:03
  • Times out (30 seconds). If I print the $x value, it approaches one of the limits and doesn't converge on the volatility. – Mark Feb 07 '17 at 22:54
  • Well you can increase the script timeout from php.ini. The max_execution_time directive in php.ini controls the time that a script is allowed to execute. You can also step through your code using a Debugger – Nadir Latif Feb 08 '17 at 06:08

0 Answers0