Implied Volatility Value coming very high

Hi All,

First of all I want to thanks Lennon, as I used IV Code given by him for calculating IV in options....

But Surprisingly, the values which are coming are huge, IV value is coming like,
19,093,413,429,248. although I am dividing it by 1000000000000, and giving my eyes a respectable number to see, and I can plot the IV as well, but the number is huge.

When I am plotting IV, it is not asking for a Parameter Date to be given as an input, although dialog box appears, but it does not ask for a Date.

Another surprising thing is that IV on CE side is coming out to be positive, and on put side it is coming out to be negative, can anyone please have a look, and suggest me what corrections I need to make.

_SECTION_BEGIN( "Implied Volatility" );
	// To gather Expiry Date and calculate ParamT
	EXPIRYDATE = ParamDate( "Expiry", "17-08-23", 0);
	ExpiryTime = ParamTime( "Expiry Time", "15:30:00" );
	Expirydatetime = DateTimeConvert(2, ExpiryDate, ExpiryTime);
	TickerCurrentTime = DateTime();
	paramT = Expirydatetime - TickerCurrentTime + 1;
	
	//candle time difference
	//timeleftindays = DateTimeDiff(ExpiryDateTime,DateTime())/( 60*60*24 );

	//difference from current time to expiry
	//timeleftnowindays = DateTimeDiff(ExpiryDateTime,StrToDateTime(now()))/( 60*60*24 );
		
		//paramT = ( expirydate - timetoday + 1 ) / 365;	
		//to declare time, expiry dayte, Months to expiry
		
	//Current Traded price of the option
	paramOptPr = Ref(C, 0);
	 //////////////////////////////////////////Mathematical Functions//////////////////////////////////////////
	 function RoundDec( Number, decimal )
	 {
		 pwr = 10^decimal;
		 result = round( Number * pwr ) / pwr;
		 return result;
	 }

	 function k( x )
	 { return 1 / ( 1+0.2316419*x ); }
 
	 function HASpre( x )
	 { return 1 - ( exp( -0.5*x^2 )/sqrt( 2*3.141592654 ) )*( 0.31938153*k( x )-0.356563782*k( x )^2+1.781477937*k( x )^3-1.821255978*k( x )^4+1.330274429*k( x )^5 ); }

	 function NormSDist( x )
	 { return IIf( x<-6, 0, IIf( x>6, 1, IIf( x>0, HASpre( x ), 1 - HASpre( -x ) ) ) ) ; }
	 //////////////////////////////////////////////////////////////////////////////////////////////////////////

	 function EuropeanOption( eCallOrPut, eS, eK, ev, er, eT, eq )
	 {
		 d1 = ( Log( eS / eK ) + ( er - eq + 0.5 * ev ^ 2 ) * eT ) / ( ev * sqrt( eT ) );
		 d2 = ( Log( eS / eK ) + ( er - eq - 0.5 * ev ^ 2 ) * eT ) / ( ev * sqrt( eT ) );
		 nd1 = NormSDist( d1 );
		 nd2 = NormSDist( d2 );
		 nnd1 = NormSDist( -d1 );
		 nnd2 = NormSDist( -d2 );
		 If ( eCallOrPut  ==  "Call" )
		 {
			 EuropeanOpt = ( eS * Exp( -eq * eT ) * nd1 ) - ( eK * Exp( -er * eT ) * nd2 );
		 }
		 else
		 {
			 EuropeanOpt = ( -eS * Exp( -eq * eT ) * nnd1 ) + ( eK * Exp( -er * eT ) * nnd2 );
		 }
		 return EuropeanOpt;
	 }

	 function Implied_Volatility( CallOrPut, S, K, r, T, q, OptionValue, guess )
	 {
		 dVol = 0.00001;
		 maxIter = 100;
		 vol_1 = guess;
	
		 for( i=1; i<maxIter; i++ )
		 {
			 Value_1 = EuropeanOption( CallOrPut, S, K, vol_1, r, T, q );
			 vol_2 = vol_1 - dVol;
			 Value_2 = EuropeanOption( CallOrPut, S, K, vol_2, r, T, q );
			 dx = ( Value_2 - Value_1 ) / dVol;
			 vol_1 = Nz( vol_1 - ( OptionValue - Value_1 ) / (dx+0.00001), LastValue( Ref( vol_1, -1 ) ) );
		 }
	
		 oDiff = RoundDec( EuropeanOption( CallOrPut, S, K, vol_1, r, T, q ), 2 ) - RoundDec( paramOptPr, 2 );
		 ImpliedVolatility = IIf( oDiff == 0, vol_1, Ref( vol_1, -1 ) );

		 return ImpliedVolatility*100;
	 }

	 //paramCallOrPut = Call or Put
	 //paramS = Spot Price
	 //paramK = Strike Price
	 //paramr = Risk-free interest rate
	 
	 //Time to Maturity - You need to work it out based on what you need is and your exchange timings
	 //paramTtm = ParamList( "Time to Maturity", "Real Time-Decay|Exchange Time-Decay", 1 );
	 //Exchange Time-Decay = Tcurr + Tother + Tsettle
	 //Time-Decay = 510mins + ( rem. days x 24 x 60 )mins + 930mins OR 1440 mins + ( rem. days x 24 x 60 )mins
	 //dateExt = Extracting expiry date from the option symbol 
	 //lstMinExpDay = dateExt + " 4:30:00 PM"; //Your exchange closing bell time on the expiry date
	 /*
	 paramExpDate = StrToDateTime( dateExt );
	 if( paramTtm == "Real Time-Decay" )
	 {
		 paramT = DateTimeDiff( StrToDateTime( lstMinExpDay ), DateTime() )/( 60*60*24 );
	 }
	 else
	 {
		 paramT = IIf( Prec( DateTimeDiff( paramExpDate, DateTime() )/( 60*60*24 ), 0 )  ==  0, DateTimeDiff( StrToDateTime( lstMinExpDay ), DateTime() )/( 60*60*24 ), Prec( DateTimeDiff( paramExpDate, DateTime() )/( 60*60*24 ), 0 ) ); //In days
	 }
	 */
	 
	 //paramq = Dividend yield
	 //paramOptPr = Live Option Close price currently being traded in the market
	 //paramGuess = Guess volatility. Can be played with the value but with me Param( "Guess volatility", 25/100, 5/100, 100/1000, 1, 0 ) has worked
	 
	 //Call or Put Flag
	 if (StrRight( Name(), 2 )=="CE")
	    paramCallOrPut = "Call";
	 else
		paramCallOrPut = "Put";
		
	
	//Getting SPOT and Strike Price
	Ticker = Name();

	if (StrLeft(Ticker,5)=="NIFTY")
	{
		Strikeprice=StrLeft(StrRight(Ticker,strlen(Ticker)-5),5);
		SPOTPrice = SetForeign("NIFTY", True, True );
		SPOTPrice=Ref(C,0);
	} else
	{
		Strikeprice=StrLeft(StrRight(Ticker,strlen(Ticker)-9),5);
		SPOTPrice = SetForeign("BANKNIFTY", True, True );
		SPOTPrice=Ref(C,0);
	}
	RestorePriceArrays();
	
	//Getting StrikePrice
	StrPrice= StrToNum(Strikeprice);
	
	
	paramS = SPOTPrice;
	paramK = StrPrice;
	paramR = 0.01;



	paramq=0;
	paramGuess = 0.05;
	
	 IV = Implied_Volatility( paramCallOrPut, paramS, paramK, paramr, paramT/365, paramq, paramOptPr, paramGuess );
	 Back_solved_option_price = EuropeanOption( paramCallOrPut, paramS, paramK, IV/100, paramr, paramT/365, paramq );
	 DisplayIV=IV/1000000000000;
	 //Filter = 1;
	 //AddColumn(DisplayIV,"IV",1.4);
	 Plot(DisplayIV,"IV",colorAqua,style=styleLine);
_SECTION_END();

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.