Missing arguments error

function dOne(UnderlyingPrice, ExercisePrice, TimeLeft, Interest, Volatility, Dividend)
{
return (Log(UnderlyingPrice / ExercisePrice) + (Interest - Dividend + 0.5 * Volatility ^ 2) * TimeLeft) / ( Volatility * (sqrt(TimeLeft)));
}
function NdOne(UnderlyingPrice, ExercisePrice, TimeLeft, Interest, Volatility, Dividend)
{
return Exp(-(dOne(UnderlyingPrice, ExercisePrice, TimeLeft, Interest, Volatility, Dividend) ^ 2) / 2) / (sqrt(2 * 3.14159265358979));
}
function dTwo(UnderlyingPrice, ExercisePrice, TimeLeft, Interest, Volatility, Dividend)
{
return dOne(UnderlyingPrice, ExercisePrice, TimeLeft, Interest, Volatility, Dividend) - Volatility * sqrt(TimeLeft);
}
function NdTwo(UnderlyingPrice, ExercisePrice, TimeLeft, Interest, Volatility, Dividend)
{
return NormDist(dTwo(UnderlyingPrice, ExercisePrice, TimeLeft, Interest, Volatility, Dividend));
}
function CallOption(UnderlyingPrice, ExercisePrice, TimeLeft, Interest, Volatility, Dividend)
{
return Exp(-Dividend * TimeLeft) * UnderlyingPrice * NormDist(dOne(UnderlyingPrice, ExercisePrice, TimeLeft, Interest, Volatility, Dividend)) - ExercisePrice * Exp(-Interest * TimeLeft) * NormDist(dOne(UnderlyingPrice, ExercisePrice, TimeLeft, Interest, Volatility, Dividend) - Volatility * sqrt(TimeLeft));
}
function PutOption(UnderlyingPrice, ExercisePrice, TimeLeft, Interest, Volatility, Dividend)
{
return ExercisePrice * Exp(-Interest * TimeLeft) * NormDist(-dTwo(UnderlyingPrice, ExercisePrice, TimeLeft, Interest, Volatility, Dividend)) - Exp(-Dividend * TimeLeft) * UnderlyingPrice * NormDist(-dOne(UnderlyingPrice, ExercisePrice, TimeLeft, Interest, Volatility, Dividend));
}
function CallDelta(UnderlyingPrice, ExercisePrice, TimeLeft, Interest, Volatility, Dividend)
{
return NormDist(dOne(UnderlyingPrice, ExercisePrice, TimeLeft, Interest, Volatility, Dividend));
//CallDelta = NormDist((Log(UnderlyingPrice / ExercisePrice) + (Interest - Dividend) * TimeLeft) / (Volatility * sqrt(TimeLeft)) + 0.5 * Volatility * sqrt(TimeLeft))
}
function PutDelta(UnderlyingPrice, ExercisePrice, TimeLeft, Interest, Volatility, Dividend)
{
return NormDist(dOne(UnderlyingPrice, ExercisePrice, TimeLeft, Interest, Volatility, Dividend)) - 1;
//PutDelta = NormDist((Log(UnderlyingPrice / ExercisePrice) + (Interest - Dividend) * TimeLeft) / (Volatility * sqrt(TimeLeft)) + 0.5 * Volatility * sqrt(TimeLeft)) - 1
}
function CallTheta(UnderlyingPrice, ExercisePrice, TimeLeft, Interest, Volatility, Dividend)
{
CT = -(UnderlyingPrice * Volatility * NdOne(UnderlyingPrice, ExercisePrice, TimeLeft, Interest, Volatility, Dividend)) / (2 * sqrt(TimeLeft)) - Interest * ExercisePrice * Exp(-Interest * (TimeLeft)) * NdTwo(UnderlyingPrice, ExercisePrice, TimeLeft, Interest, Volatility, Dividend);
return CT / 365;
}
function OptionGamma(UnderlyingPrice, ExercisePrice, TimeLeft, Interest, Volatility, Dividend)
{
return NdOne(UnderlyingPrice, ExercisePrice, TimeLeft, Interest, Volatility, Dividend) / (UnderlyingPrice * (Volatility * sqrt(TimeLeft)));
}
function Vega(UnderlyingPrice, ExercisePrice, TimeLeft, Interest, Volatility, Dividend)
{
return 0.01 * UnderlyingPrice * sqrt(TimeLeft) * NdOne(UnderlyingPrice, ExercisePrice, TimeLeft, Interest, Volatility, Dividend);
}
function PutTheta(UnderlyingPrice, ExercisePrice, TimeLeft, Interest, Volatility, Dividend)
{
PT = -(UnderlyingPrice * Volatility * NdOne(UnderlyingPrice, ExercisePrice, TimeLeft, Interest, Volatility, Dividend)) / (2 * sqrt(TimeLeft)) + Interest * ExercisePrice * Exp(-Interest * (TimeLeft)) * (1 - NdTwo(UnderlyingPrice, ExercisePrice, TimeLeft, Interest, Volatility, Dividend));
return PT / 365;
}
function CallRho(UnderlyingPrice, ExercisePrice, TimeLeft, Interest, Volatility, Dividend)
{
return 0.01 * ExercisePrice * TimeLeft * Exp(-Interest * TimeLeft) * NormDist(dTwo(UnderlyingPrice, ExercisePrice, TimeLeft, Interest, Volatility, Dividend));
}
function PutRho(UnderlyingPrice, ExercisePrice, TimeLeft, Interest, Volatility, Dividend)
{
return -0.01 * ExercisePrice * TimeLeft * Exp(-Interest * TimeLeft) * (1 - NormDist(dTwo(UnderlyingPrice, ExercisePrice, TimeLeft, Interest, Volatility, Dividend)));
}
function ImpliedCallVolatility(UnderlyingPrice, ExercisePrice, TimeLeft, Interest, Target, Dividend)
{
Hi = 5;
Lo = 0;
while((Hi - Lo) > 0.0001)
{
if(CallOption(UnderlyingPrice, ExercisePrice, TimeLeft, Interest, (Hi + Lo) / 2, Dividend) > Target)
{
Hi = (Hi + Lo) / 2;
}
else
{
Lo = (Hi + Lo) / 2;
}
}
return (Hi + Lo) / 2;
}

function ImpliedPutVolatility(UnderlyingPrice, ExercisePrice, TimeLeft, Interest, Target, Dividend)
{
Hi = 5;
Lo = 0;
while((Hi - Lo) > 0.0001)
{
if(PutOption(UnderlyingPrice, ExercisePrice, TimeLeft, Interest, (Hi + Lo) / 2, Dividend) > Target)
Hi = (Hi + Lo) / 2;
else
Lo = (Hi + Lo) / 2;
}
return (Hi + Lo) / 2;
}

function OTW_BlackScholes(callputstock, Output, UnderlyingPrice, ExercisePrice, TimeLeft, Interest, Volatility, Dividend)
{

result=0;
// Stock Calculations
switch(StrToLower(StrLeft(callputstock, 1)))
{
case "s":
{
	// Stock Output
	switch(StrToLower(StrLeft(Output, 1)))
	{
	case "p":
		result = UnderlyingPrice;
		break;
	case "d":
		result = 1;
		break;
	case "g":
		result = 0;
		break;
	case "t":
		result = 0;
		break;
	case "v":
		result = 0;
		break;
	case "r":
		result = 0;
		break;
	}
//End Stock Output
}
}
//End Stock Calculations

// Call Option Calculations
switch(StrToLower(StrLeft(callputstock, 1)))
{
case "c":
	// Call Option Output
	switch(StrToLower(StrLeft(Output, 1)))
	{
	case "p":
		result = Exp(-Dividend * TimeLeft) * UnderlyingPrice * NormDist(dOne(UnderlyingPrice, ExercisePrice, TimeLeft, Interest, Volatility, Dividend)) - ExercisePrice * Exp(-Interest * TimeLeft) * NormDist(dOne(UnderlyingPrice, ExercisePrice, TimeLeft, Interest, Volatility, Dividend) - Volatility * sqrt(TimeLeft));
		break;
	case "d":
		result = NormDist(dOne(UnderlyingPrice, ExercisePrice, TimeLeft, Interest, Volatility, Dividend));
		break;
	case "g":
		result = NdOne(UnderlyingPrice, ExercisePrice, TimeLeft, Interest, Volatility, Dividend) / (UnderlyingPrice * (Volatility * sqrt(TimeLeft)));
		break;
	case "t":
		CT = -(UnderlyingPrice * Volatility * NdOne(UnderlyingPrice, ExercisePrice, TimeLeft, Interest, Volatility, Dividend)) / (2 * sqrt(TimeLeft)) - Interest * ExercisePrice * Exp(-Interest * (TimeLeft)) * NdTwo(UnderlyingPrice, ExercisePrice, TimeLeft, Interest, Volatility, Dividend);
		result = CT / 365;
		break;
	case "v":
		result = 0.01 * UnderlyingPrice * sqrt(TimeLeft) * NdOne(UnderlyingPrice, ExercisePrice, TimeLeft, Interest, Volatility, Dividend);
		break;
	case "r":
		result = 0.01 * ExercisePrice * TimeLeft * Exp(-Interest * TimeLeft) * NormDist(dTwo(UnderlyingPrice, ExercisePrice, TimeLeft, Interest, Volatility, Dividend));
		break;
}

//End Call Output
}
//End Call Option Calculations

// Put Option Calculations
switch(StrToLower(StrLeft(callputstock, 1)))
{
case "p":
	// Put Option Output
	switch(StrToLower(StrLeft(Output, 1)))
	{
	case "p":
		result = ExercisePrice * Exp(-Interest * TimeLeft) * NormDist(-dTwo(UnderlyingPrice, ExercisePrice, TimeLeft, Interest, Volatility, Dividend)) - Exp(-Dividend * TimeLeft) * UnderlyingPrice * NormDist(-dOne(UnderlyingPrice, ExercisePrice, TimeLeft, Interest, Volatility, Dividend));
		break;
	case "d":
		result = NormDist(dOne(UnderlyingPrice, ExercisePrice, TimeLeft, Interest, Volatility, Dividend)) - 1;
		break;
	case "g":
		result = NdOne(UnderlyingPrice, ExercisePrice, TimeLeft, Interest, Volatility, Dividend) / (UnderlyingPrice * (Volatility * sqrt(TimeLeft)));
		break;
	case "t":
		PT = -(UnderlyingPrice * Volatility * NdOne(UnderlyingPrice, ExercisePrice, TimeLeft, Interest, Volatility, Dividend)) / (2 * sqrt(TimeLeft)) + Interest * ExercisePrice * Exp(-Interest * (TimeLeft)) * (1 - NdTwo(UnderlyingPrice, ExercisePrice, TimeLeft, Interest, Volatility, Dividend));
		result = PT / 365;
		break;
	case "v":
		result = 0.01 * UnderlyingPrice * sqrt(TimeLeft) * NdOne(UnderlyingPrice, ExercisePrice, TimeLeft, Interest, Volatility, Dividend);
		break;
	case "r":
		result = -0.01 * ExercisePrice * TimeLeft * Exp(-Interest * TimeLeft) * (1 - NormDist(dTwo(UnderlyingPrice, ExercisePrice, TimeLeft, Interest, Volatility, Dividend)));
		break;
	}
//End Put Output
}
//End Put Option Calculations
return result;
}

function OTW_IV(CallPut, UnderlyingPrice, ExercisePrice, TimeLeft, Interest, Target, Dividend)
{
result=-1;
switch(StrToLower(StrLeft(CallPut, 1)))
{
case "c":
	Hi = 5;
	Lo = 0;
	while((Hi - Lo) > 0.0001)
	{
	if(OTW_BlackScholes("c", "p", UnderlyingPrice, ExercisePrice, TimeLeft, Interest, (Hi + Lo) / 2, Dividend) > Target)
	Hi = (Hi + Lo) / 2;
	else
	Lo = (Hi + Lo) / 2;
	}
	result = (Hi + Lo) / 2;

case "p":
	Hi = 5;
Lo = 0;
while((Hi - Lo) > 0.0001)
{
if(OTW_BlackScholes("p", "p", UnderlyingPrice, ExercisePrice, TimeLeft, Interest, (Hi + Lo) / 2, Dividend) > Target)
Hi = (Hi + Lo) / 2;
else
Lo = (Hi + Lo) / 2;
}
result = (Hi + Lo) / 2;
}
return result;
}

function ExpiryCalc(DateOfCurAr,OptExpPeriod,OptExpNumber)
{
	result="";
	
	if(OptExpPeriod==0)
	{
		DateOfCur=LastValue(DateOfCurAr);
		DayRes=StrToNum(DateTimeFormat("%w",DateOfCur));
		ExpiryDay=DateTimeAdd(DateOfCur,IIf(DayRes>4,10-DayRes,4-DayRes)+7*(OptExpNumber-1));
		result=StrToDateTime(DateTimeToStr(ExpiryDay,4)+" 15:30");
	}
	if(OptExpPeriod==1)
	{
		ExpiryDay=DateTimeAdd(DateOfCur,(OptExpNumber-1),inMonthly);
		MonthRes=StrToNum(DateTimeFormat("%m",DateOfCur));
		//while(
		result=ExpiryDay;
	}
	return result;
}


function OTW_IV_array(optionGap,Underlying,OptFromStrike,OptType,OptExpPeriod,OptExpNumber)
{
	Interest=0.05;
	Dividend=0;
	result=Underlying;
	StrikePr=optionGap*round(Underlying/optionGap);
	tickerName=StrExtract(Name(),0,'_');
	
	ExpiryDate=ExpiryCalc(DateTime(),OptExpPeriod,OptExpNumber);
	ExpiryInfo=DateTimeFormat("%y",ExpiryDate)+StrTrim(DateTimeFormat("%m",ExpiryDate),"0",1)+DateTimeFormat("%d",ExpiryDate);
	
	for( i = 0; i <=BarCount-1;i=i+1)
{
	StrkPr=NumToStr((optionGap*round(LastValue(Ref(Underlying,i+1-BarCount))/optionGap))+OptFromStrike,1.0,False);
	
	
	tickerCur=tickerName+ExpiryInfo+StrkPr+OptType;
	OptPr=LastValue(Ref(Foreign(tickerCur,"C"),i+1-BarCount));
	SymPr=LastValue(Ref(Underlying,i+1-BarCount));
	result[i] = OTW_IV(OptType, SymPr, StrToNum(StrkPr), DateTimeDiff(ExpiryDate,LastValue(Ref(DateTime(),i+1-BarCount)))/(60*60*24*365), Interest, OptPr, Dividend);
}
//return result;
}

// control IDs
Plot(OTW_IV_array(50,Close,0,"CE",0,1),"IV");

I am new at AFL. I am trying to plot IV of NSE symbols but the attached code keeps throwing "missing argument error" in the for loop in OTW_IV_array function. Could someone please explain what mistake i am making over here.(When i print the values using trace, they are getting printed correctly)

Your code is simply incorrect. Missing arguments error means that you are calling function without providing all required arguments, see: Error 17. Missing arguments

To get better understanding of what is happening in your code and how functions work, use advice given here: How do I debug my formula?

First and foremost you should do Edit->Prettify your code because in current no-formatting shape it is totally unreadable.

You've got missing argument in the PLOT call that requires: array, name and color. You forgot to specify color in the very last line.

Last line should read:

// control IDs
Plot( OTW_IV_array( 50, Close, 0, "CE", 0, 1 ), "IV", colorRed ); // don't forget the color

It is worth noting that user's manual mentions missing arguments in Plot() call too Error 17. Missing arguments

Also properly written for loop should look like this:

for( i = 0; i < BarCount; i++ )
{
  // body
}

Assignment i = i + 1 is not needed, i++ increments i variable in place

and don't forget to uncomment return statement at the end of the function

function OTW_IV_array( optionGap, Underlying, OptFromStrike, OptType, OptExpPeriod, OptExpNumber )
{
    Interest = 0.05;
    Dividend = 0;
    result = Underlying;
    StrikePr = optionGap * round( Underlying / optionGap );
    tickerName = StrExtract( Name(), 0, '_' );

    ExpiryDate = ExpiryCalc( DateTime(), OptExpPeriod, OptExpNumber );
    ExpiryInfo = DateTimeFormat( "%y", ExpiryDate ) + StrTrim( DateTimeFormat( "%m", ExpiryDate ), "0", 1 ) + DateTimeFormat( "%d", ExpiryDate );

    for( i = 0; i < BarCount; i++ )
    {
        StrkPr = NumToStr( ( optionGap * round( LastValue( Ref( Underlying, i + 1 - BarCount ) ) / optionGap ) ) + OptFromStrike, 1.0, False );


        tickerCur = tickerName + ExpiryInfo + StrkPr + OptType;
        OptPr = LastValue( Ref( Foreign( tickerCur, "C" ), i + 1 - BarCount ) );
        SymPr = LastValue( Ref( Underlying, i + 1 - BarCount ) );
        result[i] = OTW_IV( OptType, SymPr, StrToNum( StrkPr ), DateTimeDiff( ExpiryDate, LastValue( Ref( DateTime(), i + 1 - BarCount ) ) ) / ( 60 * 60 * 24 * 365 ), Interest, OptPr, Dividend );
    }

	return result;
}
1 Like

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