Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#define _POSIX_C_SOURCE
- #define SPREAD
- #include <default.c>
- #include <stdio.h>
- // already should contain ticks in reversed order
- string base_path = "CsvHistory\\";
- string smbl = "USDJPY";
- int start = 2004;
- int end = 2016;
- typedef struct _SYSTEMTIME {
- WORD wYear;
- WORD wMonth;
- WORD wDayOfWeek;
- WORD wDay;
- WORD wHour;
- WORD wMinute;
- WORD wSecond;
- } SYSTEMTIME;
- API(SystemTimeToVariantTime,oleaut32)
- int _stdcall SystemTimeToVariantTime(SYSTEMTIME* lpSystemTime, double* pvtime);
- DATE ConvertTime(int Year,int Month,int Day1,int Hour,int Minute,int Second,int Milliseconds)
- {
- SYSTEMTIME Time;
- memset(&Time,0,sizeof(SYSTEMTIME));
- if(Year < 50) Year += 2000;
- else if(Year < 100) year += 1900;
- Time.wYear = Year;
- Time.wMonth = Month;
- Time.wDay = Day1;
- Time.wHour = Hour;
- Time.wMinute = Minute;
- Time.wSecond = Second;
- DATE vTime;
- SystemTimeToVariantTime(&Time,&vTime);
- // Add milliseconds
- vTime += (double) Milliseconds / 1000 / 60 / 60 / 24;
- return vTime;
- }
- string readT1(string content, T1* t1, T1* spread)
- {
- // tokenize a single line
- char* line = strtok(content,"\n");
- int Year, Month, Day1, Hour = 0, Minute = 0, Second = 0, Milliseconds = 0;
- float fBid = 0, fAsk = 0, fBidVol = 0, fAskVol = 0;
- // DUKAS line format "2010.01.03 17:00:00.123,1.430100,1.430400,20.12;28.73"
- if(11 != sscanf(line,"%4d.%2d.%2d %2d:%2d:%2d.%3d,%f,%f,%f,%f",
- &Year, &Month, &Day1, &Hour, &Minute, &Second, &Milliseconds,
- &fBid, &fAsk, &fBidVol, &fAskVol)) {
- printf("\nWrong line format!");
- return 0;
- }
- t1->fVal = fAsk;
- t1->time = ConvertTime(Year,Month,Day1,Hour,Minute,Second,Milliseconds);
- if(spread != NULL)
- {
- spread->fVal = fAsk - fBid;
- spread->time = t1->time;
- }
- return line+strlen(line)+1;
- }
- function main()
- {
- FILE *fr;
- int y1;
- for(y1 = start; y1 <= end; y1++)
- {
- printf("\nProcessing year %d...", y1);
- char yStr[4];
- char InName[256];
- char OutName[256];
- char OutName2[256];
- sprintf(yStr, "%d", y1);
- strcpy(InName, base_path);
- strcat(InName, smbl);
- strcat(InName, "_");
- strcat(InName, yStr);
- strcpy(OutName, InName);
- strcat(InName, ".csv");
- strcat(OutName, ".t1");
- strcpy(OutName2, base_path);
- strcat(OutName2, smbl);
- strcat(OutName2, "s_");
- strcat(OutName2, yStr);
- strcat(OutName2, ".t1");
- printf("\nInFile: %s", InName);
- printf("\nOutFile: %s", OutName);
- printf("\nOutFile2: %s", OutName2);
- if(!file_length(InName)) {
- quit("Data file not found!");
- return;
- }
- //string content = file_content(InName);
- int i = 0;
- int maxticks = 60*24*365;
- T1* ticks = malloc(maxticks * sizeof(T1));
- T1* tick = ticks;
- #ifdef SPREAD
- T1* spreads = malloc(maxticks * sizeof(T1));
- T1* spread = spreads;
- #endif
- fr = fopen(InName, "r");
- char line[128];
- while(fgets(line, 128, fr))
- {
- //printf("\n%s", line);
- ++i;
- #ifdef SPREAD
- readT1(line, tick++, spread++);
- #else
- readT1(line, tick++, NULL);
- #endif
- if(i == maxticks)
- {
- printf("\nSplit: %.5f / %.5f", (var)(tick-1)->time, (var)(tick-1)->fVal);
- int size = (int)(tick)-(int)(ticks);
- file_append(OutName,ticks,size);
- free(ticks);
- ticks = malloc(maxticks*sizeof(T1));
- tick = ticks;
- #ifdef SPREAD
- file_append(OutName2,spreads,size);
- free(spreads);
- spreads = malloc(maxticks*sizeof(T1));
- spread = spreads;
- #endif
- i = 0;
- }
- } // end while
- if(i > 0)
- {
- int size = (int)(tick)-(int)(ticks);
- if(i >= 2)
- {
- // in case there is an empty line at the end of the file
- printf("\nLast: %.5f / %.5f (%d / %d)", (var)(tick-2)->time, (var)(tick-2)->fVal, i, size);
- printf("\nLast: %.5f / %.5f (%d / %d)", (var)(tick-1)->time, (var)(tick-1)->fVal, i, size);
- }
- else
- {
- printf("\ni = %d", i);
- }
- #ifdef SPREAD
- file_append(OutName2,spreads,size);
- free(spreads);
- #endif
- file_append(OutName,ticks,size);
- free(ticks);
- } // end if(i > 0)
- fclose(fr);
- } // end for
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement