Advertisement
Guest User

Untitled

a guest
May 26th, 2016
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.01 KB | None | 0 0
  1. //#define _POSIX_C_SOURCE
  2. #define SPREAD
  3. #include <default.c>
  4. #include <stdio.h>
  5.  
  6. // already should contain ticks in reversed order
  7. string base_path = "CsvHistory\\";
  8. string smbl = "USDJPY";
  9. int start = 2004;
  10. int end = 2016;
  11.  
  12. typedef struct _SYSTEMTIME {
  13.     WORD wYear;
  14.     WORD wMonth;
  15.     WORD wDayOfWeek;
  16.     WORD wDay;
  17.     WORD wHour;
  18.     WORD wMinute;
  19.     WORD wSecond;
  20. } SYSTEMTIME;
  21.  
  22. API(SystemTimeToVariantTime,oleaut32)
  23. int _stdcall SystemTimeToVariantTime(SYSTEMTIME* lpSystemTime, double* pvtime);
  24.  
  25. DATE ConvertTime(int Year,int Month,int Day1,int Hour,int Minute,int Second,int Milliseconds)
  26. {
  27.     SYSTEMTIME Time;
  28.     memset(&Time,0,sizeof(SYSTEMTIME));
  29.     if(Year < 50) Year += 2000;
  30.     else if(Year < 100) year += 1900;
  31.     Time.wYear = Year;
  32.     Time.wMonth = Month;
  33.     Time.wDay = Day1;
  34.     Time.wHour = Hour;
  35.     Time.wMinute = Minute;
  36.     Time.wSecond = Second;
  37.    
  38.     DATE vTime;
  39.     SystemTimeToVariantTime(&Time,&vTime);
  40.     // Add milliseconds
  41.     vTime += (double) Milliseconds / 1000 / 60 / 60 / 24;
  42.     return vTime;
  43. }
  44.  
  45. string readT1(string content, T1* t1, T1* spread)
  46. {
  47.     // tokenize a single line  
  48.     char* line = strtok(content,"\n");
  49.     int Year, Month, Day1, Hour = 0, Minute = 0, Second = 0, Milliseconds = 0;
  50.     float fBid = 0, fAsk = 0, fBidVol = 0, fAskVol = 0;
  51.    // DUKAS line format "2010.01.03 17:00:00.123,1.430100,1.430400,20.12;28.73"
  52.  
  53.     if(11 != sscanf(line,"%4d.%2d.%2d %2d:%2d:%2d.%3d,%f,%f,%f,%f",
  54.         &Year, &Month, &Day1, &Hour, &Minute, &Second, &Milliseconds,
  55.         &fBid, &fAsk, &fBidVol, &fAskVol)) {
  56.         printf("\nWrong line format!");
  57.         return 0;
  58.     }
  59.     t1->fVal = fAsk;
  60.     t1->time = ConvertTime(Year,Month,Day1,Hour,Minute,Second,Milliseconds);
  61.     if(spread != NULL)
  62.     {
  63.         spread->fVal = fAsk - fBid;
  64.         spread->time = t1->time;
  65.     }
  66.     return line+strlen(line)+1;
  67. }
  68.  
  69. function main()
  70. {
  71.     FILE *fr;
  72.     int y1;
  73.     for(y1 = start; y1 <= end; y1++)
  74.     {
  75.         printf("\nProcessing year %d...", y1);
  76.         char yStr[4];
  77.         char InName[256];
  78.         char OutName[256];
  79.         char OutName2[256];
  80.        
  81.         sprintf(yStr, "%d", y1);
  82.         strcpy(InName, base_path);
  83.         strcat(InName, smbl);
  84.         strcat(InName, "_");
  85.         strcat(InName, yStr);
  86.         strcpy(OutName, InName);
  87.         strcat(InName, ".csv");
  88.         strcat(OutName, ".t1");
  89.         strcpy(OutName2, base_path);
  90.         strcat(OutName2, smbl);
  91.         strcat(OutName2, "s_");
  92.         strcat(OutName2, yStr);
  93.         strcat(OutName2, ".t1");
  94.        
  95.         printf("\nInFile: %s", InName);
  96.         printf("\nOutFile: %s", OutName);
  97.         printf("\nOutFile2: %s", OutName2);
  98.        
  99.         if(!file_length(InName)) {
  100.             quit("Data file not found!");
  101.             return;
  102.         }
  103.  
  104.         //string content = file_content(InName);
  105.         int i = 0;
  106.         int maxticks = 60*24*365;
  107.         T1* ticks = malloc(maxticks * sizeof(T1));
  108.         T1* tick = ticks;
  109. #ifdef SPREAD
  110.         T1* spreads = malloc(maxticks * sizeof(T1));
  111.         T1* spread = spreads;
  112. #endif
  113.  
  114.         fr = fopen(InName, "r");   
  115.         char line[128];
  116.  
  117.         while(fgets(line, 128, fr))
  118.         {
  119.             //printf("\n%s", line);
  120.             ++i;
  121. #ifdef SPREAD
  122.             readT1(line, tick++, spread++);
  123. #else
  124.             readT1(line, tick++, NULL);
  125. #endif
  126.  
  127.             if(i == maxticks)
  128.             {  
  129.                 printf("\nSplit: %.5f / %.5f", (var)(tick-1)->time, (var)(tick-1)->fVal);          
  130.                 int size = (int)(tick)-(int)(ticks);
  131.                 file_append(OutName,ticks,size);
  132.                 free(ticks);
  133.                 ticks = malloc(maxticks*sizeof(T1));
  134.                 tick = ticks;
  135. #ifdef SPREAD
  136.                 file_append(OutName2,spreads,size);
  137.                 free(spreads);
  138.                 spreads = malloc(maxticks*sizeof(T1));
  139.                 spread = spreads;
  140. #endif
  141.                 i = 0;
  142.             }
  143.         } // end while
  144.    
  145.         if(i > 0)
  146.         {
  147.             int size = (int)(tick)-(int)(ticks);
  148.             if(i >= 2)
  149.             {
  150.                 // in case there is an empty line at the end of the file
  151.                 printf("\nLast: %.5f / %.5f (%d / %d)", (var)(tick-2)->time, (var)(tick-2)->fVal, i, size);
  152.                 printf("\nLast: %.5f / %.5f (%d / %d)", (var)(tick-1)->time, (var)(tick-1)->fVal, i, size);
  153.             }
  154.             else
  155.             {
  156.                 printf("\ni = %d", i);
  157.             }  
  158. #ifdef SPREAD
  159.             file_append(OutName2,spreads,size);
  160.             free(spreads);
  161. #endif
  162.             file_append(OutName,ticks,size);
  163.             free(ticks);
  164.         } // end if(i > 0)
  165.         fclose(fr);
  166.     } // end for
  167.    
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement