Thursday, March 17, 2011

c++ Code for pricing Binary option

The code calculate the bet pricing using the very simplistic Black Scholes model.
The statement for bet is " I want to win X amount if the price of underlying in between Up or Down, While the current price is A"
/*---------------------------------------------------------
#
# Function: Price bets according to BlackScholes framework
# Arguments: Bet amountHigh range, low range, current exchange price,
# time for which bet is valid and implied volatility
# (Can be calculated using current trading options on
# exchange rate )
# Returns: Price for bet in Base currency
#---------------------------------------------------------
*/
#include
#include
#include
using namespace std;
#ifndef Pi
#define Pi 3.141592653589793238462643
#endif
#define interestRate 0.05 // interest rate for the time period
#define maxTries 4 // Number of wrong tries allowed for each entry
double const a0 = 1.26551223, a1 = 1.00002368, a2 = 0.37409196,a3=0.09678418;
double const a4 = 0.18628806, a5 = 0.27886807,a6=1.13520398, a7=1.48851587,a8=0.82215223,a9=0.17087277;
double getCDF(double);
double getArg();

int main()
{
double principle,low_strike,high_strike,time,currentPrice,volatility,bet_price;
cout<<"Enter money to be made out of bet"<<'\n';
principle=getArg();
cout<<"Enter Low range followed by High range and time for bet in days"<<'\n';
low_strike=getArg();high_strike=getArg();time=getArg();
cout<<"Enter the current trading rate and implied volatility(%) for exchange rate(To be determined by fetching data"
"from third party software, currently entered mannually)"<<'\n';
currentPrice=getArg();volatility=getArg();
volatility = volatility/100; // convert to decimal
double years,d_low,d_high;
years= time/365; // covert to years
// Long position
d_low=(log(currentPrice/low_strike) +(interestRate+(volatility*volatility)/2)*years)/(volatility*sqrt(years))-volatility*sqrt(years);
// Short position
d_high=(log(currentPrice/high_strike) +(interestRate+(volatility*volatility)/2)*years)/(volatility*sqrt(years))-volatility*sqrt(years);
bet_price = principle*exp(-1*interestRate*years)*(getCDF(d_low)-getCDF(d_high));
cout< system("PAUSE");
}

double getCDF(double D)
{
double t,erf,z;
z = fabs(D);
z = z/sqrt(2.0);
t = 1/(1+(z*0.5));
erf =t*exp(-z*z-a0+t*(a1+t*(a2+t*(a3+t*(-a4+t*(a5+t*(-a6+t*(a7+
t*(-a8+t*a9)))))))));
erf = erf >= 0.0 ? erf : 2.0-erf;
D = erf/2;
if (D < 0 )
{
D= 1.0 - D;
}
return D;
}
double getArg()
{
double l_arg,exit_c=1;
cin>>l_arg;
while(exit_c {
cin.clear();
cin.ignore(numeric_limits::max(), '\n');
cout << "ERROR: You must enter valid value (no negatives and no alphabets)"<<'\n'<<"<<";
cin >> l_arg;
exit_c+=1;
}
if(exit_c==maxTries)
exit(1);
else
return l_arg;
}

No comments:

Post a Comment