MCOV

Open Source Monte Carlo Option Valuer
Home
Contact
Download
Releases
Installation
Read-Me
License

Monte Carlo
Strategy Objects
C++ Classes

C++ Files
mcovaluer.h
mcovaluer.cxx
mcov.cxx

mcov.cxx

Tiny C++ Main Program using MCOV Software

This is the mcov.cxx file that comes with a MCOV distribution (without the license section)...

// ================================================================
//  Use a Monte Carlo technique to estimate an option's value.
//
//  See copyright notice and license at end of this file.
//
//  Notes:
//
//    - This file illustrates two ways of creating an mcoValuer
//      object: 
//         - using default strategies and limits
//         - using your own strategies and limits
//
//    - Program arguments are not checked here because the
//      mcoValuer does thorough argument checking.
//
// ----------------------------------------------------------------

#include <cstdlib>
#include <iostream>
#include "mcovaluer.h"

using namespace std;

// ----------------------------------------------------------------
//  Estimate an option value using an mcoValuer object.

int main (int argc, char *argv[]) {

  if (argc < 5 || argc > 6) {
    cout << "\n"
         << "Usage:\n"
         << "    mcov  history_file  put-call  strike_price  "
         <<           "days_to_expiry  [tries]\n"
         << "  Examples:\n"
         << "    mcov mydata1.opd  C  47.50  11\n" 
         << "    mcov xiu2003.opd  P  45.00  19  1000\n"
         << "  History-File: one line/trading-day, \n"
         << "    Earliest first; may contain gaps;\n"
         << "    Lines:  daynum closing-price \n"
         << "    Lines beginning with # are ignored.\n\n";
    return 1;
  }

  // Note that checking of the arguments is done
  // by mcoValuer and its associated classes.

  const char * history_file = argv[1];
  char         put_call     = *(argv[2]);
  double       strike       = atof(argv[3]);
  long         days         = atol(argv[4]);
  long         tries        = (argc>=6) ? atol(argv[5]) : 0;

  mcoErr *e = mcoErr::instance();  // Singleton

    // If we wanted to use our own strategies, factories,
    // and limits, we could declare them like this...
  mcoSellStrategy1  sell_strategy;
  mcoPDayStrategy1  proto_strategy;
  mcoDayFactory     day_factory;
  long              max_history_days = 2000;
  long              max_projected_days = 100;

  bool Use_Default_Algorithms_and_Limits = true;

  mcoValuer *mcov;    // Will point to our option-valuer.

  if (Use_Default_Algorithms_and_Limits) {
    mcov = new mcoValuer (history_file);
  } else {
    mcov = new mcoValuer (history_file, 
                          &sell_strategy, &proto_strategy,
                          &day_factory,
                          max_history_days, max_projected_days);
  }

  if (e->is_err()) {                 // Did an error occur?
    cout << endl << e->msg() << endl;
    return 1;
  } else if (!mcov) {
    cout << "\nERROR creating mcoValuer object\n\n";
    return 1;
  }

  mcoSentry mcov_sentry(mcov);   // ensure that mcov gets deleted

  mcoOption opt (put_call, strike, days);  // declare the option

  double val = 0.0;
  if (tries) {
    val = mcov->option_value (&opt, tries);
  } else {
    val = mcov->option_value (&opt);
  }

  if (e->is_err()) {                 // Did an error occur?
    cout << endl << e->msg() << endl;
    return 1;
  }

  cout << val << endl;
 
  // Don't have to delete mcov; the sentry does it.

  return 0;

}

// ================================================================