#include "xmim_api.h" #include #include #include #include #include #define usage \ fprintf (stderr, "Usage: %s\n \t%s\n \t%s\n \t%s\n \t%s\n \t%s\n \t%s\n \t%s\n \t%s\n \t%s\n \t%s\n \t%s\n \t%s\n \t%s\n \t%s\n \t%s\n \t%s\n \t%s\n \t%s\n \n\t%s%s%s\n", argv[0], \ "-q host : MIM Host Name", \ "-r numOfRels rel1 rel2 ... : Number of Relations and the Relation Names", \ "<-c numOfCols col1 col2 ...> : Number of Columns and the Column Name", \ " If not present all columns will be returned", \ "<-p port> : MIM Port Number", \ "<-n num_exe_units units> : Number of Units and Units, ex 1 4 = everday", \ " num_exe_units ex 10 4 = every 10th day", \ " 1 = seconds, 2 = minutes, 3 = hours, 4 = days ...", \ "<-d from_date> : From Date, ex 01/01/2001", \ "<-e to_date> : To Date, ex 01/01/2001", \ "<-g from_exp_date to_exp_date> : The From and To Expiration Date", \ "<-m from_strike_price to_strike_price> : The From and To Strike Price to consider", \ "<-t from_time> : From Time ex 12:00", \ "<-u to_time> : To Time ex 15:00>", \ "<-o option_type> : c = Call, p = Put", \ "<-v> : Use Relative Api Call", \ "<-s strike_weight> : The Strike Weight to consider", \ "<-N> : Show contract names with the resulting data series", \ "<-h> : Help\t", \ "(example: ", argv[0], " -q hostname -p port# -r 1 IBM_OPTIONS)") #define EXIT \ XmimPrintError(NULL); XmimDisconnect(handle); return 1 char gCal[12][4] = { "F", "G", "H", "J", "K", "M", "N", "Q", "U", "V", "X", "Z" }; XmimDate pass_date(char* s) { int i = 0; int y, m, d; XmimDate rslt = XMIM_INVALID_DATE; char *t = s, *p = s; while (*t != '/' && *t != '\0') t++; if (*t != '/') return rslt; *t = '\0'; m = atoi(p); t++; p = t; while (*t != '/' && *t != '\0') t++; if (*t != '/') return rslt; *t = '\0'; d = atoi(p); t++; p = t; while (*t != '/' && *t != '\0') t++; if (*t != '\0') return rslt; y = atoi(p); if (y < 100) y += 1900; rslt = XmimSetDate(y, m, d); return rslt; } XmimTime pass_time(char* s) { int i = 0; int h = 0, m = 0, ss = 0; XmimTime rslt = XMIM_INVALID_TIME; char *t = s, *p = s; while (*t != ':' && *t != '\0') { if (!isdigit(*t)) return rslt; else t++; } if (*t == '\0') h = atoi(p); else { *t = '\0'; h = atoi(p); t++; p = t; while (*t != ':' && *t != '\0') { if (!isdigit(*t)) return rslt; else t++; } if (*t == '\0') m = atoi(p); else { *t = '\0'; m = atoi(p); t++; p = t; while (*t != '\0') { if (!isdigit(*t)) return rslt; else t++; } ss = atoi(p); } } rslt = XmimSetTime(h, m, ss); return rslt; } main (int argc, char **argv) { //Prog int i, j, width; XmimBoolean relative = FALSE; char *host = NULL; int port = 0; char c; XmimBoolean all_cols = TRUE; XmimBoolean debug = TRUE; bool show_contract = false; //Connection XmimClientHandle handle; //Rels and Cols int numOfRels = 0, numOfCols = 0; XmimString *rels = NULL; XmimString *cols = NULL; //Date and Time XmimDate from_date = XMIM_INVALID_DATE; XmimDate to_date = XMIM_INVALID_DATE; XmimTime from_time = XMIM_INVALID_TIME; XmimTime to_time = XMIM_INVALID_TIME; XmimDate from_exp_date = XMIM_INVALID_DATE; XmimDate to_exp_date = XMIM_INVALID_DATE; //Expiration int num_exp_units = 1; XmimUnits exp_units = XMIM_MONTHS; //Strike float from_strike = -1.0; float to_strike = -1.0; float strike_weight = 100.0; //Units int num_exe_units = 1; XmimUnits units = XMIM_DAYS; //Other XmimBoolean select_once = FALSE; XmimOptionType option_type = XMIM_OPTION_ALL; //Returns int numRecords; XmimDateTime *dates; XmimDate *expDates; XmimOptionType *optTypes; float *strikes; float *values; XmimDateFormat expDateFormat = XMIM_DATE_mmsddsyyyy; //Need at least x args if (argc < 6) { if (debug) { fprintf (stdout, "Number of Args %d\n", argc ) ; fflush (stdout); } usage; exit (0); } i = 1; //Process the command line while (i < argc) { c = argv[i][1]; switch (c) { case 'c': //Columns all_cols = FALSE; numOfCols = atoi(argv[i+1]); i += 2; cols = (char**)malloc((numOfCols)*sizeof(char*)); for (j = 0; j < numOfCols; j++) cols[j] = argv[i+j]; i += numOfCols; break; case 'd': //From Date from_date = pass_date(argv[i+1]); i += 2; break; case 'e': //To Date to_date = pass_date(argv[i+1]); i += 2; break; case 'g': //From To Experation Date from_exp_date = pass_date(argv[i+1]); to_exp_date = pass_date(argv[i+2]); i += 3; break; case 'h': //Help usage; break; case 'm': //From To Strike Price from_strike = atof(argv[i+1]); to_strike = atof(argv[i+2]); i += 3; break; case 'n': //Number of Units and ExecUnits num_exe_units = atoi(argv[i+1]); i += 2; units = (XmimUnits)atoi(argv[i]); i++; break; case 'o': //Option Type if (argv[i+1][0] == 'c') option_type = XMIM_OPTION_CALL; else if (argv[i+1][0] == 'p') option_type = XMIM_OPTION_PUT; i += 2; break; case 'p': //MIM Port Number port = atoi(argv[i+1]); i += 2; break; case 'q': //MIM Host Name host = argv[i+1]; i += 2; break; case 'r': //Relations numOfRels = atoi(argv[i+1]); i += 2; rels = (char**)malloc((numOfRels)*sizeof(char*)); for (j = 0; j < numOfRels; j++) rels[j] = argv[i+j]; i += numOfRels; break; case 's': //Strike Weight strike_weight = atof(argv[i]); i += 2; break; case 't': //From Time from_time = pass_time(argv[i+1]); i +=2; break; case 'u': //To Time to_time = pass_time(argv[i+1]); i +=2; break; case 'v': //Relative or not relative = TRUE; i +=1; break; case 'x': //Expiration num_exp_units = atoi(argv[i+1]); i += 2; exp_units = (XmimUnits)atoi(argv[i]); i++; break; case 'N': //Show contract name show_contract = true; i++; break; default: //Help usage; break; } } if (XmimConnect (host, port, &handle) != XMIM_SUCCESS) { XmimPrintError("XmimConnect"); return 1; } if (relative) { if (!all_cols) { if (debug) { fprintf (stdout, "Relative") ; fflush (stdout); } if (XmimVaGetRecordsOptionRelative ( XMIM_CLIENT_HANDLE, handle, XMIM_RELATION_ARRAY, numOfRels, rels, XMIM_COLUMN_ARRAY, numOfCols, cols, XMIM_FROM_DATE, from_date, XMIM_TO_DATE, to_date, XMIM_FROM_TIME, from_time, XMIM_TO_TIME, to_time, XMIM_UNITS_TO_EXPIR, num_exp_units, exp_units, XMIM_STRIKE_WEIGHT, strike_weight, XMIM_OPTION_TYPE, option_type, XMIM_SELECT_ONCE, select_once, XMIM_UNITS, num_exe_units, units, XMIM_FILL_OPTION, XMIM_FILL_NAN, XMIM_FILL_NAN, XMIM_SKIP_NONE, XMIM_LIMIT, 0, XMIM_LIMIT_BY_RECORDS, XMIM_NUM_RECORDS, &numRecords, XMIM_DATE_TIMES, &dates, XMIM_EXPIRATION_DATES, &expDates, XMIM_OPTION_TYPES, &optTypes, XMIM_STRIKE_PRICES, &strikes, XMIM_VALUES, &values, XMIM_END_ARGS) != XMIM_SUCCESS) { EXIT; } } else { if (debug) { fprintf (stdout, "Relative AllCols") ; fflush (stdout); } if (XmimVaGetRecordsOptionRelativeAllColumns ( XMIM_CLIENT_HANDLE, handle, XMIM_RELATION_ARRAY, numOfRels, rels, XMIM_COLUMN_ARRAY, &numOfCols, &cols, XMIM_FROM_DATE, from_date, XMIM_TO_DATE, to_date, XMIM_FROM_TIME, from_time, XMIM_TO_TIME, to_time, XMIM_UNITS_TO_EXPIR, num_exp_units, exp_units, XMIM_STRIKE_WEIGHT, strike_weight, XMIM_OPTION_TYPE, option_type, XMIM_SELECT_ONCE, select_once, XMIM_UNITS, num_exe_units, units, XMIM_FILL_OPTION, XMIM_FILL_NAN, XMIM_FILL_NAN, XMIM_SKIP_NONE, XMIM_LIMIT, 0, XMIM_LIMIT_BY_RECORDS, XMIM_NUM_RECORDS, &numRecords, XMIM_DATE_TIMES, &dates, XMIM_EXPIRATION_DATES, &expDates, XMIM_OPTION_TYPES, &optTypes, XMIM_STRIKE_PRICES, &strikes, XMIM_VALUES, &values, XMIM_END_ARGS) != XMIM_SUCCESS) { EXIT; } } } else { if (!all_cols) { if (XmimVaGetRecordsOption ( XMIM_CLIENT_HANDLE, handle, XMIM_RELATION_ARRAY, numOfRels, rels, XMIM_COLUMN_ARRAY, numOfCols, cols, XMIM_FROM_DATE, from_date, XMIM_TO_DATE, to_date, XMIM_FROM_TIME, from_time, XMIM_TO_TIME, to_time, XMIM_FROM_EXP_DATE, from_exp_date, XMIM_TO_EXP_DATE, to_exp_date, XMIM_FROM_STRIKE, from_strike, XMIM_TO_STRIKE, to_strike, XMIM_OPTION_TYPE, option_type, XMIM_FILL_OPTION, XMIM_FILL_NAN, XMIM_FILL_NAN, XMIM_SKIP_NONE, XMIM_LIMIT, 0, XMIM_LIMIT_BY_RECORDS, XMIM_NUM_RECORDS, &numRecords, XMIM_DATE_TIMES, &dates, XMIM_EXPIRATION_DATES, &expDates, XMIM_OPTION_TYPES, &optTypes, XMIM_STRIKE_PRICES, &strikes, XMIM_VALUES, &values, XMIM_END_ARGS) != XMIM_SUCCESS) { EXIT; } } else { if (XmimVaGetRecordsOptionAllColumns ( XMIM_CLIENT_HANDLE, handle, XMIM_RELATION_ARRAY, numOfRels, rels, XMIM_COLUMN_ARRAY, &numOfCols, &cols, XMIM_FROM_DATE, from_date, XMIM_TO_DATE, to_date, XMIM_FROM_TIME, from_time, XMIM_TO_TIME, to_time, XMIM_FROM_EXP_DATE, from_exp_date, XMIM_TO_EXP_DATE, to_exp_date, XMIM_FROM_STRIKE, from_strike, XMIM_TO_STRIKE, to_strike, XMIM_OPTION_TYPE, option_type, XMIM_FILL_OPTION, XMIM_FILL_NAN, XMIM_FILL_NAN, XMIM_SKIP_NONE, XMIM_LIMIT, 0, XMIM_LIMIT_BY_RECORDS, XMIM_NUM_RECORDS, &numRecords, XMIM_DATE_TIMES, &dates, XMIM_EXPIRATION_DATES, &expDates, XMIM_OPTION_TYPES, &optTypes, XMIM_STRIKE_PRICES, &strikes, XMIM_VALUES, &values, XMIM_END_ARGS) != XMIM_SUCCESS) { EXIT; } } } //Print out headings using the column names if ( show_contract ) { printf("Contract "); printf("\t\t\t\t\t "); } else { width = 10 + 12 + 6 + 10; printf("%*s", width, ""); } for (i = 0; i < numOfCols; i++) printf("%10s", cols[i]); printf("\n\n"); //Print out data records char sym[64]; char* reltok; int theYear = 0; int item = 0; for (i = 0; i < numRecords; i ++) { if ( show_contract ) { for (item = 0; item < numOfRels*numOfCols; item++) { if (! XmimTestNaN (handle, values[i*numOfRels*numOfCols+item])) break; } item /= numOfCols; theYear = expDates[i].year; strcpy(sym, rels[item]); reltok = strtok(sym,"_"); printf ("%s_%d%s.%s%.0f\t", reltok, expDates[i].year, gCal[expDates[i].month - 1], optTypes[i] == XMIM_OPTION_CALL ? "C" : "P", strikes[i]); } XmimPrintDateTime (dates[i], False, False); printf (", "); XmimPrintExpDateWithFormat (expDates[i], XMIM_DATE_mmsddsyyyy); printf (", %4s", ( optTypes[i] == XMIM_OPTION_CALL ? "Call" : "Put " )); printf (", %8.3f", strikes[i]); for (j = 0; j < numOfRels*numOfCols; j++) { if (XmimTestNaN (handle, values[i*numOfRels*numOfCols+j])) { if ( show_contract ) continue; else printf (", %8s", "NaN"); } else printf (", %8.3f", values[i*numOfRels*numOfCols+j]); } printf("\n"); } printf("\nTotal number of records is: %10d\n", numRecords); XmimDisconnect (handle); return 0; }