Added -h and long options
This commit is contained in:
@@ -6,13 +6,70 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
#include <getopt.h>
|
||||||
|
|
||||||
|
|
||||||
|
struct helptext {
|
||||||
|
const char *opt;
|
||||||
|
const char *desc;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
static struct helptext options[] = {
|
||||||
|
{"-s, --spot",
|
||||||
|
"Spot price"},
|
||||||
|
{"-k, --strike",
|
||||||
|
"Strike price"},
|
||||||
|
{"-r, --rfr",
|
||||||
|
"Risk-free rate"},
|
||||||
|
{"-v, --implied-volatility",
|
||||||
|
"Implied volatility"},
|
||||||
|
{"-d, --effective-date",
|
||||||
|
"Effective date [YYYY-MM-DD]"},
|
||||||
|
{"-e, --expiry-date",
|
||||||
|
"Expiry date [YYYY-MM-DD]"},
|
||||||
|
{"-N",
|
||||||
|
"Number of MC simulations"},
|
||||||
|
{"-c, --call",
|
||||||
|
"Call flag"},
|
||||||
|
{"-p, --put",
|
||||||
|
"Put or call flag"},
|
||||||
|
{"-h",
|
||||||
|
"This help text"},
|
||||||
|
{ NULL , NULL }
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
static struct option long_options[] = {
|
||||||
|
{"spot", required_argument, 0, 's'},
|
||||||
|
{"strike", required_argument, 0, 'k'},
|
||||||
|
{"rfr", required_argument, 0, 'r'},
|
||||||
|
{"implied-volatility", required_argument, 0, 'v'},
|
||||||
|
{"effective-date", required_argument, 0, 'd'},
|
||||||
|
{"expiry-date", required_argument, 0, 'e'},
|
||||||
|
{"call", no_argument, 0, 'c'},
|
||||||
|
{"put", no_argument, 0, 'p'},
|
||||||
|
{0, 0, 0, 0}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
int print_help(void)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
printf("Usage: opt-pricer [options...]\n");
|
||||||
|
for(i=0; options[i].opt; i++) {
|
||||||
|
printf(" %-25s %s\n", options[i].opt, options[i].desc);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
double spot = 0, strike = 0, rfr = 0, vol = 0, sims = 1000;
|
double spot = 0, strike = 0, rfr = 0, vol = 0, sims = 1000;
|
||||||
double bs_price, mc_price;
|
double bs_price, mc_price;
|
||||||
char expiry_date[11], buffer[512];
|
char expiry_date[11], buffer[512];
|
||||||
int opt, type = 0;
|
int opt, option_index = 0, type = 0;
|
||||||
struct tm expiry, value;
|
struct tm expiry, value;
|
||||||
|
|
||||||
extern char *optarg;
|
extern char *optarg;
|
||||||
@@ -21,9 +78,9 @@ int main(int argc, char *argv[])
|
|||||||
memset(&expiry, 0, sizeof(expiry));
|
memset(&expiry, 0, sizeof(expiry));
|
||||||
memset(&value, 0, sizeof(value));
|
memset(&value, 0, sizeof(value));
|
||||||
|
|
||||||
while ((opt = getopt(argc, argv, "s:k:r:v:d:e:n:cp")) != -1) {
|
while ((opt = getopt_long(argc, argv, "s:k:r:v:d:e:N:cp", long_options, &option_index)) != -1) {
|
||||||
switch (opt) {
|
switch (opt) {
|
||||||
case 's': /* spot */
|
case 's':
|
||||||
if (sscanf(optarg, "%lf", &spot) == EOF) {
|
if (sscanf(optarg, "%lf", &spot) == EOF) {
|
||||||
return 1;
|
return 1;
|
||||||
};
|
};
|
||||||
@@ -49,7 +106,7 @@ int main(int argc, char *argv[])
|
|||||||
case 'd': /* valuation date, must be YYYY-MM-DD */
|
case 'd': /* valuation date, must be YYYY-MM-DD */
|
||||||
strptime(optarg, "%Y-%m-%d", &value);
|
strptime(optarg, "%Y-%m-%d", &value);
|
||||||
break;
|
break;
|
||||||
case 'n': /* number of simulations */
|
case 'N': /* number of simulations */
|
||||||
if (sscanf(optarg, "%lf", &sims) == EOF) {
|
if (sscanf(optarg, "%lf", &sims) == EOF) {
|
||||||
return 1;
|
return 1;
|
||||||
};
|
};
|
||||||
@@ -60,8 +117,15 @@ int main(int argc, char *argv[])
|
|||||||
case 'p': /* set as put */
|
case 'p': /* set as put */
|
||||||
type = -1;
|
type = -1;
|
||||||
break;
|
break;
|
||||||
|
case 'h': /* print help*/
|
||||||
|
print_help();
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (spot == 0 || strike == 0 || rfr == 0 || vol == 0) {
|
||||||
|
print_help();
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
strftime(expiry_date, 11, "%Y-%m-%d", &expiry);
|
strftime(expiry_date, 11, "%Y-%m-%d", &expiry);
|
||||||
bs_price = bsm(spot, rfr, vol, strike, expiry, value, type);
|
bs_price = bsm(spot, rfr, vol, strike, expiry, value, type);
|
||||||
|
|||||||
Reference in New Issue
Block a user