#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <limits.h>
#include "option.h"
extern clock_t clock();
 /* Computes European/American-style call/put options for a
  * general additive binomial tree valuation *
  * 
  */


int main(int argc, char **argv)
{
 double K, S,r,u,d,sigma;
 int    T,N;
 double result;
 double rst, rc;
 double t1,t2;
 double An,Adt,Av,Adxu, Adxd,Apu,Apd,Adiscount,barrier;

 if (argc !=2 ) {
  fprintf(stderr,"%s horizon datafile-from-stdinput\n",argv[0]);
  return(1);
 }
 scanf("%lf",&K);
 scanf("%d",&T);
 scanf("%lf",&S);
 scanf("%lf",&r);
 scanf("%d",&N);
 scanf("%lf",&sigma);
 N=atoi(argv[1]);
 barrier= (double)atoi(argv[1]);

 Adt = (double) T / (double) N;
 An = (double) N;
 Av = r - 0.5*sigma*sigma;
 Adxu = sqrt(sigma*sigma*Adt+Av*Adt*Av*Adt);
 Adxd = -Adxu;
 Apu  = 0.5 + 0.5 * (Av*Adt /Adxu);
 Apd  = 1-Apu;
 Adiscount = exp(-r*Adt);

 fprintf(stdout, "K=%f T=%d S=%f r=%f N=%d, sigma=%f dt=%f v=%f dxu=%f dxd=%f pu=%f pd=%f discount=%f barrier=%f\n",K,T,S,r,N,sigma,Adt,Av,Adxu,Adxd,Apu,Apd,Adiscount,barrier); 
 fflush(stdout);

 t1=clock();
 result=option(K,T,S,r,N,sigma,barrier,&rst,&rc,EUROPEANCALL);
/*
*/
 t2=clock();
 fprintf(stdout,"EuropeanCallOption = %8.4f (S= %f) (Time=%f)\n",rc,rst,(t2-t1)/((double)CLOCKS_PER_SEC));
 fflush(stdout);

 t1=clock();
 result=option(K,T,S,r,N,sigma,barrier,&rst,&rc,EUROPEANPUT);
/*
*/
 t2=clock();
 fprintf(stdout,"EuropeanPutOption  = %8.4f (S= %f) (Time=%f)\n",rc,rst,(t2-t1)/((double)CLOCKS_PER_SEC));
 fflush(stdout);

 t1=clock();
/*
*/
 result=option(K,T,S,r,N,sigma,barrier,&rst,&rc,AMERICANCALL);
 t2=clock();
 fprintf(stdout,"AmericanCallOption = %8.4f (S= %f) (Time=%f)\n",rc,rst,(t2-t1)/((double)CLOCKS_PER_SEC));
 fflush(stdout);

 t1=clock();
 result=option(K,T,S,r,N,sigma,barrier,&rst,&rc,AMERICANPUT);
/*
*/
 t2=clock();
 fprintf(stdout,"AmericanPutOption  = %8.4f (S= %f) (Time=%f)\n",rc,rst,(t2-t1)/((double)CLOCKS_PER_SEC));
 fflush(stdout);


}
