/* nag_barrier_std_price (s30fac) Example Program. * * Copyright 2009, Numerical Algorithms Group. * * Mark 9, 2009. */ /* Pre-processor includes */ #include #include #include #include #include #include #include int main(int argc, char *argv[]) { FILE *fpin, *fpout; /*Integer scalar and array declarations */ Integer exit_status = 0; Integer i, j, m, n; NagError fail; Nag_CallPut putnum; Nag_Barrier typenum; /*Double scalar and array declarations */ double h, k, q, r, s, sigma; double *p = 0, *t = 0, *x = 0; /*Character scalar and array declarations */ char put[8+1]; char type[14+1]; Nag_OrderType order; INIT_FAIL(fail); /* Check for command-line IO options */ fpin = nag_example_file_io(argc, argv, "-data", NULL); fpout = nag_example_file_io(argc, argv, "-results", NULL); fprintf(fpout, "nag_barrier_std_price (s30fac) Example Program Results\n"); fprintf(fpout, "Standard Barrier Option\n\n"); /* Skip heading in data file*/ fscanf(fpin, "%*[^\n] "); /* Read put, type*/ fscanf(fpin, "%s%s%*[^\n] ", put, type); /* * nag_enum_name_to_value (x04nac). * Converts NAG enum member name to value */ putnum = (Nag_CallPut) nag_enum_name_to_value(put); typenum = (Nag_Barrier) nag_enum_name_to_value(type); /* Read s, h, k, sigma, r, q*/ fscanf(fpin, "%lf%lf%lf%lf%lf%lf%*[^\n] ", &s, &h, &k, &sigma, &r, &q); /* Read m, n*/ fscanf(fpin, "%ld%ld%*[^\n] ", &m, &n); #ifdef NAG_COLUMN_MAJOR #define P(I, J) p[(J-1)*m + I-1] order = Nag_ColMajor; #else #define P(I, J) p[(I-1)*n + J-1] order = Nag_RowMajor; #endif if ( !(p = NAG_ALLOC(m*n, double)) || !(t = NAG_ALLOC(n, double)) || !(x = NAG_ALLOC(m, double))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } /* Read array of strike/exercise prices, X*/ for (i = 0; i < m; i++) fscanf(fpin, "%lf ", &x[i]); fscanf(fpin, "%*[^\n] "); /* Read array of times to expiry*/ for (i = 0; i < n; i++) fscanf(fpin, "%lf ", &t[i]); fscanf(fpin, "%*[^\n] "); /* * nag_barrier_std_price (s30fac) * Standard Barrier option pricing formula */ nag_barrier_std_price(order, putnum, typenum, m, n, x, s, h, k, t, sigma, r, q, p, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_barrier_std_price (s30fac).\n%s\n", fail.message); exit_status = 1; goto END; } if (putnum == Nag_Call) { fprintf(fpout, "%s\n", "Call :"); } else if (putnum == Nag_Put) { fprintf(fpout, "%s\n", "Put :"); } if (typenum == Nag_DownandIn) { fprintf(fpout, "%s\n\n", "Down-and-In"); } else if (typenum == Nag_DownandOut) { fprintf(fpout, "%s\n\n", "Down-and-Out"); } else if (typenum == Nag_UpandIn) { fprintf(fpout, "%s\n\n", "Up-and-In"); } else if (typenum == Nag_UpandOut) { fprintf(fpout, "%s\n\n", "Up-and-Out"); } fprintf(fpout, "%s%8.4f\n", " Spot = ", s); fprintf(fpout, "%s%8.4f\n", " Barrier = ", h); fprintf(fpout, "%s%8.4f\n", " Rebate = ", k); fprintf(fpout, "%s%8.4f\n", " Volatility = ", sigma); fprintf(fpout, "%s%8.4f\n", " Rate = ", r); fprintf(fpout, "%s%8.4f\n", " Dividend = ", q); fprintf(fpout, "\n"); fprintf(fpout, "%s\n", " Strike Expiry Option Price"); for (i = 1; i <= m; i++) { for (j = 1; j <= n; j++) fprintf(fpout, "%9.4f%9.4f %11.4f\n", x[i-1], t[j-1], P(i, j)); } END: if (fpin != stdin) fclose(fpin); if (fpout != stdout) fclose(fpout); if (p) NAG_FREE(p); if (t) NAG_FREE(t); if (x) NAG_FREE(x); return exit_status; }