Hello,
i am new to afl coding
I Some how coded Implied volatility of options ,and i am able to get implied volatility ,but now i want the out come i.e implied volatility as symbol in amibroker so that i can add oscillating indicators ,like ATR(which accepts only range)etc. But i am not able to do so please help me out how could i use result of an code as symbol ,so that i can add indictors to it
Following is the code
N( Symbol1= ParamStr("IV OPTION 1", "NIFTY15SEP7800CE") );
SetForeign( Symbol1 );
C1 = C;
H1 = H;
L1 = L;
O1 = O;
V1 = V;
RestorePriceArrays();
_N( Symbol2= ParamStr("IV OPTION 2", "NIFTY-I") );
SetForeign( Symbol2 );
C2 = C;
H2 = H;
L2 = L;
O2 = O;
V2 = V;
RestorePriceArrays();
function callprice(Stp ,stk,tim,inte,vk)//(stock price,time
{
StockPrice = Stp;
Timedays = tim; //Time to expiry ( days to
StrikePrice =stk; //strike Price of Option to
InterestRate= inte; //prevailing interest
VKnown =vk;//You can insert Known
//BLACK SHCOLES OPTION PRIFCING FORMULA
//coded by Anthony Faragasso
//1-01-03
//////////////////////////////////////////////////////
time=timedays/365;// days to expire conversion formula
//Formula variables below
/*************************************************/
// Solves for ( X )
x = (ln(stockPrice/strikePrice) + (interestrate +
Vknown*Vknown/2)*time)/(Vknown*sqrt(time));
/*************************************************/
P = 0.2316419;
bb1 = 0.31938153;
bb2 = -0.3565638;
bb3 = 1.78147794;
bb4 = -1.821256;
bb5 = 1.33027443;
pi = 3.141592654; // PI
A2 = 1/sqrt(2*pi);
A3 = exp(-(x^2)/2);
y= a2*a3;
A4 = exp(-interestrate*time);
t1 = 1/(1+ P*x);
A5=(bb1*t1)+(bb2*t1^2) +( bb3*t1^3)+(bb4*t1^4)+(bb5*t1^5);
/************************************************************/
//Standard Normal Distribution Function of ( x )
N = 1- y *A5 ;
/************************************************************/
// Solves for ( X1 )
X1=x-Vknown*sqrt(TIME);
y1=1/sqrt(2*pi);
N0=exp(-(x1^2)/2);
T2=1/(1+ P*X1);
A6=(bb1*t2)+(bb2*t2^2) +( bb3*t2^3)+(bb4*t2^4)+(bb5*t2^5);
A7=exp(-interestrate*time);
y2=y1*n0;
/************************************************************/
/* Standard Normal Distribution Function OF ( x1 )*/
/***********************************************************/
N2= 1-y2 * A6;
/************ CALL OPTION FAIR VALUE************/
Call = stockPrice * N - strikePrice * A4 * N2;
/************ PUT OPTION FAIR VALUE*************/
Put = Call - stockprice + strikeprice*A7;
kall=Call;
return kall;
}
Stp=C2;
stk=7800;
uy=day() ;
tim=(24-uy);
inte=0;
OptionValue=C1;
function impliedcall(Stp ,stk,tim,inte,OptionValue)
{
dVol = 0.00001;
epsilon = 0.00001;
maxIter = 100;
vol_1 = 0.25;
i =0; // starting point
do
{
Value_1 =callprice(Stp ,stk,tim,inte,vol_1);
vol_2 = vol_1 - dVol;
Value_2 = callprice(Stp ,stk,tim,inte,vol_2);
dx = (Value_2 - Value_1) / dVol;
if( i > maxIter )
{
break;
}
vol_1 = vol_1 - (OptionValue - Value_1) / dx;
i = i + 1;
}
while( ++i < BarCount );
ImpliedVolatility = vol_1;
return ImpliedVolatility ;
}
calliv=impliedcall(Stp ,stk,tim,inte,OptionValue)/0.01 ;
cimp=impliedcall(C2 ,stk,tim,inte,C1)/0.01;