Implied volatility

I have the following code in VBA (courtesy Peter McPhee's option trading workbook)

Function ImpliedCallVolatility(UnderlyingPrice, ExercisePrice, Time, Interest, Target, Dividend)
High = 5
Low = 0
Do While (High - Low) > 0.0001
If CallOption(UnderlyingPrice, ExercisePrice, Time, Interest, (High + Low) / 2, Dividend) > Target Then
High = (High + Low) / 2
Else: Low = (High + Low) / 2
End If
Loop
ImpliedCallVolatility = (High + Low) / 2
End Function

And i wrote the following code in AFL

Function ImpliedCallVolatility(UnderlyingPrice, ExercisePrice, Time, Interest, Target, Dividend)
{
a = 5
b = 0
Do 
{
xyz=CallOption(UnderlyingPrice, ExercisePrice, Time, Interest, (a + b) / 2, Dividend);
iif (( xyz> Target),a=(a+b)/2,b=(a+b)/2);
}While (a - b) > 0.0001;

Icv = (a+b) / 2;
return icv;
}

I am getting a standard answer for any value input, i.e. the code is not going in a loop. Please help.

Hi @shaun_pai,

Welcome to the AmiBroker forum. Glad to see you showing what you are doing, but I don't understand what your issue is.

I can't test our your code, due to syntax errors, and no CallOption() code.

Glad to see you using the code tags, but you will need to help us help you, but defining what you should be getting, and telling us what you are getting. Then we can hopefully understand where the issues are.

Some sample Inputs and Expected Outputs vs Actual Outputs might also be very helpful.

It looks like you have some code ability, but with your recent joining, we don't know what you have tried to fix or debug your code. If you don't know, use the SEARCH function (Magnifying glass) and see lots of other posts for debugging.

Hope these suggestions help.

Here is the full code that i wrote. Currently I have written it only for calls. The code may seem jumbled up as I don't have a proper programming background. The multiple variables with the same values are because I tried writing this code using a different technique as well.
About the output:
I am getting a standard output of 1.67 as the call volatility for any given value of the strike, Underlying, etc. I have also uploaded a screenshot of the output.

Output

Hope you can help me to get the implied call volatility function in a loop.

EXPIRYDATE=ParamDate("Expiry","180220",0);
timetoday=Now(format=3);
t=(expirydate-timetoday+1)/365;
time=t;
r=0;
q=0;
expirymo=Param("Month",1,1,3,1);
optionvalue=Close;


if (expirymo==1)
s=Foreign("N.NIFTY18MARFUT","CLOSE");
else
{
if (expirymo==2)
s=Foreign("N.NIFTY18APRFUT","CLOSE");
else
{
if (expirymo==3)
s=Foreign("N.NIFTY18MAYFUT","CLOSE");
}
}

//To get the expiry month
expiry=Strright(Strleft(Name(),12),3);
expmon=IIf(expiry=="JAN",1,IIf(expiry=="FEB",2,IIf(expiry=="MAR",3,IIf(expiry=="APR",4,IIf(expiry=="MAY",5,IIf(expiry=="JUN",6,IIf(expiry=="JUL",7,IIf(expiry=="AUG",8,IIf(expiry=="SEP",9,IIf(expiry=="OCT",10,IIf(expiry=="NOV",11,IIf(expiry=="DEC",12,0))))))))))));
//

//To get the strike price
xyz=Strleft(Strright(Name(),7),5);
k=StrToNum(xyz);
if (k==0)
{
xyz=Strleft(Strright(Name(),6),4);
k=StrToNum(xyz);
}
ExercisePrice=k;

callorput=StrRight(Name(),2);
//



function dOne(UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend)
{

xyz= (Log(UnderlyingPrice / ExercisePrice) + (Interest - Dividend + 0.5 * Volatility ^ 2) * Time) / (Volatility * (Sqrt(Time)));
return xyz;
}


Function NdOne(UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend)
{
 xyz= Exp(-(dOne(UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend) ^ 2) / 2) / (Sqrt(2 * 3.14159265358979));
 return xyz;
}

function dTwo(UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend)
{
xyz= dOne(UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend) - Volatility * Sqrt(Time);
return xyz;
}


Function NdTwo(UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend)
{
 xyz= NormDist(dTwo(UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend));
return xyz;
}

Function CallOption(UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend)
{
xyz= Exp(-Dividend * Time) * UnderlyingPrice * NormDist(dOne(UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend)) - ExercisePrice * Exp(-Interest * Time) * NormDist(dOne(UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend) - Volatility * Sqrt(Time));
return xyz;
}

Function PutOption(UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend)
{
 xyz=ExercisePrice * Exp(-Interest * Time) * NormDist(-dTwo(UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend)) - Exp(-Dividend * Time) * UnderlyingPrice * NormDist(-dOne(UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend));
return xyz;
}

Function CallDelta(UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend)
{
//CallDelta = NormDist(dOne(UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend))
 xyz=NormDist((Log(UnderlyingPrice / ExercisePrice) + (Interest - Dividend) * Time) / (Volatility * Sqrt(Time)) + 0.5 * Volatility * Sqrt(Time));
return xyz;
}

Function PutDelta(UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend)
{
//PutDelta = NormDist(dOne(UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend)) - 1
 xyz=NormDist((Log(UnderlyingPrice / ExercisePrice) + (Interest - Dividend) * Time) / (Volatility * Sqrt(Time)) + 0.5 * Volatility * Sqrt(Time)) - 1;
return xyz;
}

Function CallTheta(UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend)
{
CT = -(UnderlyingPrice * Volatility * NdOne(UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend)) / (2 * Sqrt(Time)) - Interest * ExercisePrice * Exp(-Interest * (Time)) * NdTwo(UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend);
xyz=CT / 365;
return xyz;
}

Function Gamma(UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend)
{
 xyz= NdOne(UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend) / (UnderlyingPrice * (Volatility * Sqrt(Time)));
return xyz;
}

Function Vega(UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend)
{
 xyz= 0.01 * UnderlyingPrice * Sqrt(Time) * NdOne(UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend);
return xyz;
}

Function PutTheta(UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend)
{
PT = -(UnderlyingPrice * Volatility * NdOne(UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend)) / (2 * Sqrt(Time)) + Interest * ExercisePrice * Exp(-Interest * (Time)) * (1 - NdTwo(UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend));
 xyz= PT / 365;
return xyz;
}

Function CallRho(UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend)
{
 xyz= 0.01 * ExercisePrice * Time * Exp(-Interest * Time) * NormDist(dTwo(UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend));
return xyz;
}

Function PutRho(UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend)
{
 xyz= -0.01 * ExercisePrice * Time * Exp(-Interest * Time) * (1 - NormDist(dTwo(UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend)));
return xyz;
}

Function ImpliedCallVolatility(UnderlyingPrice, ExercisePrice, Time, Interest, Target, Dividend)
{
Hi = 5;
Lo = 0;
Do 
{
qwe=CallOption(UnderlyingPrice, ExercisePrice, Time, Interest, (Hi+Lo) / 2, Dividend);
IIf( qwe> Target,Hi=(Hi+lo)/2,Lo=(Hi+Lo)/2);

}While ((Hi - Lo) > 0.0001);

ICV = (Hi + Lo) / 2;
return ICV;
}

//to get ce or pe vol
vol=IIf(callorput=="CE",ImpliedCallVolatility(s, k, t, r, optionvalue, q),0);
//vol=ImpliedCallVolatility(s, k, t, r, optionvalue, q);




//vol=ImpliedCallVolatility(s, k, t, r, optionvalue, q);
PRICE=CallOption(S, ExercisePrice, Time, R, VOL, Q);
Filter=1;
AddColumn(PRICE,"PRICE");
AddtextColumn(expiry,"expirymostr");
AddColumn(expmon,"expirymostr");
AddColumn(k,"strnum");
AddColumn(t,"time");
AddColumn(optionvalue,"target");
AddColumn(s,"Underlying");
AddtextColumn(callorput,"Option");
AddColumn(VOL,"Vol");

Plot(vol,"Volatility",colorWhite,styleLine);

Hi @shaun_pai,

First look (more to come) - your code to get the StrikePrice appears incorrect to me. I think you are getting the YEAR of contract from name.

Glad to see you are using an Exploration, but now, add in each item that you generate to see what you are getting. Things like: Expiry, Expmon, xyz, k (Exercise Price) to see that they are what you expect.

The screen you posted does not seem to match exploration code in your afl. Also, code in your afl uses variables that are not defined (expirymostr), so you will need to check the rest and fix those as well.

Will keep plugging through as I am able.

@shaun_pai,

Here are a couple of programming tips.

Put all Functions above your "main" running code.
Run Edit - Prettify Selection to have your code "cleaned up" and indented properly (makes it easier to read).
Check that your code does not have any missing variables.

When you go through the above, you should be able to get a clean version of your code to post.

Also a better description of what data you have as your current, and what you are referring to for Foreign will help me (us) understand what you are doing.

I am getting too many syntax and missing variables to do more now. When you get it cleaned up and posted, I will look again.


function dOne( UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend )
{

    xyz = ( Log( UnderlyingPrice / ExercisePrice ) + ( Interest - Dividend + 0.5 * Volatility ^ 2 ) * Time ) / ( Volatility * ( Sqrt( Time ) ) );
    return xyz;
}


Function NdOne( UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend )
{
    xyz = Exp( -( dOne( UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend ) ^ 2 ) / 2 ) / ( Sqrt( 2 * 3.14159265358979 ) );
    return xyz;
}

function dTwo( UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend )
{
    xyz = dOne( UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend ) - Volatility * Sqrt( Time );
    return xyz;
}


Function NdTwo( UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend )
{
    xyz = NormDist( dTwo( UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend ) );
    return xyz;
}

Function CallOption( UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend )
{
    xyz = Exp( -Dividend * Time ) * UnderlyingPrice * NormDist( dOne( UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend ) ) - ExercisePrice * Exp( -Interest * Time ) * NormDist( dOne( UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend ) - Volatility * Sqrt( Time ) );
    return xyz;
}

Function PutOption( UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend )
{
    xyz = ExercisePrice * Exp( -Interest * Time ) * NormDist( -dTwo( UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend ) ) - Exp( -Dividend * Time ) * UnderlyingPrice * NormDist( -dOne( UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend ) );
    return xyz;
}

Function CallDelta( UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend )
{
//CallDelta = NormDist(dOne(UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend))
    xyz = NormDist( ( Log( UnderlyingPrice / ExercisePrice ) + ( Interest - Dividend ) * Time ) / ( Volatility * Sqrt( Time ) ) + 0.5 * Volatility * Sqrt( Time ) );
    return xyz;
}

Function PutDelta( UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend )
{
//PutDelta = NormDist(dOne(UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend)) - 1
    xyz = NormDist( ( Log( UnderlyingPrice / ExercisePrice ) + ( Interest - Dividend ) * Time ) / ( Volatility * Sqrt( Time ) ) + 0.5 * Volatility * Sqrt( Time ) ) - 1;
    return xyz;
}

Function CallTheta( UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend )
{
    CT = -( UnderlyingPrice * Volatility * NdOne( UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend ) ) / ( 2 * Sqrt( Time ) ) - Interest * ExercisePrice * Exp( -Interest * ( Time ) ) * NdTwo( UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend );
    xyz = CT / 365;
    return xyz;
}

Function Gamma( UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend )
{
    xyz = NdOne( UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend ) / ( UnderlyingPrice * ( Volatility * Sqrt( Time ) ) );
    return xyz;
}

Function Vega( UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend )
{
    xyz = 0.01 * UnderlyingPrice * Sqrt( Time ) * NdOne( UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend );
    return xyz;
}

Function PutTheta( UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend )
{
    PT = -( UnderlyingPrice * Volatility * NdOne( UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend ) ) / ( 2 * Sqrt( Time ) ) + Interest * ExercisePrice * Exp( -Interest * ( Time ) ) * ( 1 - NdTwo( UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend ) );
    xyz = PT / 365;
    return xyz;
}

Function CallRho( UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend )
{
    xyz = 0.01 * ExercisePrice * Time * Exp( -Interest * Time ) * NormDist( dTwo( UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend ) );
    return xyz;
}

Function PutRho( UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend )
{
    xyz = -0.01 * ExercisePrice * Time * Exp( -Interest * Time ) * ( 1 - NormDist( dTwo( UnderlyingPrice, ExercisePrice, Time, Interest, Volatility, Dividend ) ) );
    return xyz;
}

Function ImpliedCallVolatility( UnderlyingPrice, ExercisePrice, Time, Interest, Target, Dividend )
{
    Hi = 5;
    Lo = 0;
    Do
    {
        qwe = CallOption( UnderlyingPrice, ExercisePrice, Time, Interest, ( Hi + Lo ) / 2, Dividend );
        IIf( qwe > Target, Hi = ( Hi + lo ) / 2, Lo = ( Hi + Lo ) / 2 );

    } While( ( Hi - Lo ) > 0.0001 );

    ICV = ( Hi + Lo ) / 2;
    return ICV;
}




//to declare time, expiry dayte, Months to expiry
EXPIRYDATE = ParamDate( "Expiry", "180220", 0 );
timetoday = Now( format = 3 );
t = ( expirydate - timetoday + 1 ) / 365;
time = t;
r = 0;
q = 0;
expirymo = Param( "Month", 1, 1, 3, 1 );
optionvalue = Close;
//

//to get underlying value
if( expirymo == 1 )
    s = Foreign( "N.NIFTY18MARFUT", "CLOSE" );
else
{
    if( expirymo == 2 )
        s = Foreign( "N.NIFTY18APRFUT", "CLOSE" );
    else
    {
        if( expirymo == 3 )
            s = Foreign( "N.NIFTY18MAYFUT", "CLOSE" );
    }
}

//
//To get the expiry month
expiry = Strright( Strleft( Name(), 12 ), 3 );
expmon = IIf( expiry == "JAN", 1, IIf( expiry == "FEB", 2, IIf( expiry == "MAR", 3, IIf( expiry == "APR", 4, IIf( expiry == "MAY", 5, IIf( expiry == "JUN", 6, IIf( expiry == "JUL", 7, IIf( expiry == "AUG", 8, IIf( expiry == "SEP", 9, IIf( expiry == "OCT", 10, IIf( expiry == "NOV", 11, IIf( expiry == "DEC", 12, 0 ) ) ) ) ) ) ) ) ) ) ) );
//

//To get the strike price
xyz = Strleft( Strright( Name(), 7 ), 5 );
k = StrToNum( xyz );

if( k == 0 )
{
    xyz = Strleft( Strright( Name(), 6 ), 4 );
    k = StrToNum( xyz );
}

ExercisePrice = k;

callorput = StrRight( Name(), 2 );
//




//to get ce or pe vol
vol = IIf( callorput == "CE", ImpliedCallVolatility( s, k, t, r, optionvalue, q ), 0 );
PRICE = CallOption( S, ExercisePrice, Time, R, VOL, Q );
Filter = 1;
AddColumn( PRICE, "PRICE" );
AddtextColumn( expiry, "expirymostr" );
AddColumn( expmon, "expirymostr" );
AddColumn( k, "strnum" );
AddColumn( t, "time" );
AddColumn( optionvalue, "target" );
AddColumn( s, "Underlying" );
AddtextColumn( callorput, "Option" );
AddColumn( VOL, "Vol" );

Above is the code that has been prettified as suggested. Thank You.

The "Current symbol" is the symbol of option i.e. ATM call option graph. The foreign symbol is the underlying i.e. the futures contract on which the option value is calculated.

"Expirymostr" is not a variable. It is just a heading for the column in exploration.
I am able to run the code. So I believe there are no missing variables.

The variables declared are as follows:
k= Strike Price as number(I am getting the strike price from the symbol name. Hence needs to be converted from sting to number)

s= Value of Underlying(Futures contract foreign symbol)
t=time to expiry
optionvalue= Current Market price of option

Hope this helps :slight_smile:

Hi @shaun_pai, Your code is working, and I wasn't thinking clearly.

After some (more awake) work on your code, I get the same 1.67 result as well.

So, the big question: Do you have sample data that should give you some known results?

As the functions generally call other functions to do calculations, you may need to work your way down to the base functions and check that they are producing proper results.

If you have the data and VB code, and it processes correctly, can you set up some "test" runs to do the base calculations and check the results with what you can test in AB?

As far as I can see, the next step is to start testing each of the low level base functions in VB and AB and compare the results.

I will do that and post the results as soon as possible. :slight_smile:

1 Like

Hope, this might get you going:

_SECTION_BEGIN( "Implied Volatility" );
	 //////////////////////////////////////////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, 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
	 
	 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 );
_SECTION_END();

Simple copy paste of the code won't make it work. The arguments of the Implied_Volatility() function are to be met in accrodance to your needs and to verify whether the calculation of the IV is correct w.r.t. the actual Option price traded in your Exchange, use EuropeanOption() function. Then IV and Back_solved_option_price can be used to plot on a chart or addcolumns in an exploration, scan, backtest - your choice!

BTW, all the best with the Goddess Vega!

Cheers!

6 Likes

Is Data N.NIFTY18MARFUT is from paid service?

Yes, It is a paid service.