NAG Library Manual, Mark 30.2
Interfaces:  FL   CL   CPP   AD 

NAG CL Interface Introduction
Example description
/* nag_specfun_opt_barrier_std_price (s30fac) Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 *
 * Mark 30.2, 2024.
 */
#include <math.h>
#include <nag.h>
#include <stdio.h>
#include <string.h>

int main(void) {
  /* 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);

  printf(
      "nag_specfun_opt_barrier_std_price (s30fac) Example Program Results\n");
  printf("Standard Barrier Option\n\n");
  /* Skip heading in data file */
  scanf("%*[^\n] ");
  /* Read put, type */
  scanf("%8s%14s%*[^\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 */
  scanf("%lf%lf%lf%lf%lf%lf%*[^\n] ", &s, &h, &k, &sigma, &r, &q);
  /* Read m, n */
  scanf("%" NAG_IFMT "%" NAG_IFMT "%*[^\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))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  /* Read array of strike/exercise prices, X */
  for (i = 0; i < m; i++)
    scanf("%lf ", &x[i]);
  scanf("%*[^\n] ");
  /* Read array of times to expiry */
  for (i = 0; i < n; i++)
    scanf("%lf ", &t[i]);
  scanf("%*[^\n] ");
  /*
   * nag_specfun_opt_barrier_std_price (s30fac)
   * Standard Barrier option pricing formula
   */
  nag_specfun_opt_barrier_std_price(order, putnum, typenum, m, n, x, s, h, k, t,
                                    sigma, r, q, p, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_specfun_opt_barrier_std_price (s30fac).\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }
  if (putnum == Nag_Call)
    printf("%s\n", "Call :");
  else if (putnum == Nag_Put)
    printf("%s\n", "Put :");
  if (typenum == Nag_DownandIn)
    printf("%s\n\n", "Down-and-In");
  else if (typenum == Nag_DownandOut)
    printf("%s\n\n", "Down-and-Out");
  else if (typenum == Nag_UpandIn)
    printf("%s\n\n", "Up-and-In");
  else if (typenum == Nag_UpandOut)
    printf("%s\n\n", "Up-and-Out");
  printf("%s%8.4f\n", "  Spot       = ", s);
  printf("%s%8.4f\n", "  Barrier    = ", h);
  printf("%s%8.4f\n", "  Rebate     = ", k);
  printf("%s%8.4f\n", "  Volatility = ", sigma);
  printf("%s%8.4f\n", "  Rate       = ", r);
  printf("%s%8.4f\n", "  Dividend   = ", q);
  printf("\n");
  printf("%s\n", "   Strike   Expiry   Option Price");
  for (i = 1; i <= m; i++)
    for (j = 1; j <= n; j++)
      printf("%9.4f%9.4f %11.4f\n", x[i - 1], t[j - 1], P(i, j));

END:
  NAG_FREE(p);
  NAG_FREE(t);
  NAG_FREE(x);

  return exit_status;
}