#include #include #include #include "xmim_api.h" #define usage fprintf(stderr, "Usage: [-h] [-l mm/dd/yyyy] -q host -p port -s symbol \n"); int compare_func(const void *elem1, const void *elem2 ) { return strcmp(*(char**)elem1, *(char**)elem2); } int main (int argc, char** argv) { int numContracts; int numPeriods; int num_children; int found = 0; int numRecords; int usedates = 1; int decimalp = 3; int port, i, ii, ci = 1; float* values; XmimDate* expirationDates; XmimDate* contractDates; XmimDate expDate; XmimDate firstDate; XmimDateTime* dates; XmimString* children; XmimClientHandle handle; char* cols = "Close"; char future[256]; char lastdataday[32]; char attr[1024]; char cond[1024]; char cond2[1024]; char buff[4096]; char tmp[10]; char c; char* host = NULL; char* ptr; char tempname[64]; strcpy(lastdataday, "-1"); if (argc < 6) { usage; exit (0); } while (ci < argc) { c = argv[ci][1]; switch (c) { case 'h': usage; break; case 'l': strcpy(lastdataday, argv[ci +1]); ci += 2; break; case 'p': port = atoi(argv[ci +1]); ci += 2; break; case 'q': host = argv[ci +1]; ci += 2; break; case 's': strcpy(future, argv[ci +1]); ci += 2; break; default: usage; break; } } strcpy(tmp,"%0.3f"); if (XmimConnect (host, port, &handle) != XMIM_SUCCESS) { fprintf(stderr, "XmimConnect Failed\n"); exit(-1); } if (XmimGetRelChildren(handle, future, &num_children, &children) != XMIM_SUCCESS) { fprintf(stderr, "XmimGetRelChildren Failded\n"); exit(-1); } if (num_children <= 0) { fprintf(stderr, "No children\n"); exit (-1); } if (children) qsort (children, num_children, sizeof(XmimString*), compare_func); else { fprintf(stderr, "Problem with the Children\n"); exit (-1); } if (XmimGetRolloverDates(handle, (char *)future, 1, &cols, 1, XMIM_DAYS, "expiration day", "actual prices", &numContracts, &numPeriods, &expirationDates, &contractDates) != XMIM_SUCCESS) { fprintf(stderr, "Failed getting rollover dates\n"); exit (-1); } /* Get aroud RolloverDates differences */ sprintf(attr, "Close of %s", future); if (strcmp(lastdataday, "-1") == 0) sprintf(cond, "Date is %s last_data_day", future); else sprintf(cond, "Date is %s", lastdataday); i = XMIM_SHOW_ATTR_LIST; if (XmimVaSelectRecords(XMIM_CLIENT_HANDLE, handle, XMIM_CONDITION_LIST, &cond, NULL, XMIM_SHOW_ATTR_LIST, &attr, NULL, XMIM_SHOW_BEFORE_REPEAT, 0, XMIM_SHOW_AFTER_REPEAT, 0, XMIM_UNITS, 1, XMIM_DAYS, XMIM_FILL_OPTION, XMIM_FILL_NAN, XMIM_FILL_NAN, XMIM_SKIP_NONE, XMIM_NUM_RECORDS, &numRecords, XMIM_DATE_TIMES, &dates, XMIM_VALUES, &values, XMIM_END_ARGS) == XMIM_SUCCESS) { if (numRecords > 0) { memcpy(&expirationDates[numPeriods-1], &dates[0], sizeof(dates[0])); fprintf(stdout, "%s\n", XmimStringDate(expirationDates[numPeriods-1])); } } else exit (-1); for (ii = 0; ii < num_children; ii++) { if (XmimVaGetRelation(XMIM_CLIENT_HANDLE, handle, XMIM_RELATION, children[ii], XMIM_EXPIRATION_DATE, &expDate, XMIM_END_ARGS) == XMIM_SUCCESS) { if (XmimValidDate(expDate)) { if (!found && XmimDateCmp(expDate, expirationDates[numPeriods-1]) >= 0) { found = 1; firstDate = expDate; } if (found) { sprintf(attr, "Close of %s", children[ii]); sprintf(cond2, "%s is defined", children[ii]); if (XmimVaSelectRecords(XMIM_CLIENT_HANDLE, handle, XMIM_CONDITION_LIST, cond, cond2, NULL, XMIM_SHOW_ATTR_LIST, attr, NULL, XMIM_SHOW_BEFORE_REPEAT, 0, XMIM_SHOW_AFTER_REPEAT, 0, XMIM_UNITS, 1, XMIM_DAYS, XMIM_FILL_OPTION, XMIM_FILL_NAN, XMIM_FILL_NAN, XMIM_SKIP_NONE, XMIM_NUM_RECORDS, &numRecords, XMIM_DATE_TIMES, &dates, XMIM_VALUES, &values, XMIM_END_ARGS) == XMIM_SUCCESS) { if (numRecords > 0) { fprintf(stdout, "%s ", children[ii]); if (usedates == 1 ) fprintf(stdout, "%s ", XmimStringDate(expDate)); else { ptr = strchr(children[ii],(int)'_'); if (ptr) { strcpy(tempname, ptr+1); fprintf(stdout, "%s ", tempname); } else fprintf(stdout, "%s ", XmimStringDate(expDate)); } if (decimalp >= 0 && decimalp < 10) sprintf(tmp,"%%0.%df",decimalp); sprintf(buff, tmp, values[0]); fprintf(stdout, "%s\n", buff); } } } } } } XmimDisconnect (handle); }