CSVtoTKSConverter

Author: Scriptong
Miscellaneous
It issuies visual alerts to the screenUses files from the file systemIt reads information from a file
0 Views
0 Downloads
0 Favorites
CSVtoTKSConverter
ÿþ#property copyright "Scriptong"

#property link      "https://www.advancetools.net"

#property description "English: Converting tick CSV files to TKS format\nRussian: >=25@B0F8O B8:>2KE D09;>2 D>@<0B0 CSV 2 D>@<0B TKS"

#define VERSION "190.328"

#property version VERSION  

#property strict

#property show_inputs



input string         i_sSrcFileName       = "EURUSD_tick_UTC+2_00.csv";                            // Source file

input string         i_sDstFileName       = "EURUSD.tks";                                          // Destination file





struct TickStruct                                                                                  

{

   datetime time;

   double   bid;

   double   ask;   

};





//+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

//| Script program start function                                                                                                                                                            |

//+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

void OnStart()

{

   Print("Version: ", VERSION);



   int hSrcFile = INVALID_HANDLE;

   if (!IsFileOpen(hSrcFile))

      return;



   datetime dtFirstTick = 0;

   TickStruct stLastTick;

   int nTickCnt = 0;

   if (!IsSrcFileRead(hSrcFile, dtFirstTick, stLastTick, nTickCnt))

      return;

      

   Comment("");

   Alert("Converting is complete! Ticks: ", nTickCnt, " from ", TimeToString(dtFirstTick), " to ", TimeToString(stLastTick.time));   

}

//+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

//| Opening the source file                                                                                                                                                                  |

//+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

bool IsFileOpen(int &hFile)

{

   hFile = FileOpen(i_sSrcFileName, FILE_CSV | FILE_READ | FILE_SHARE_READ | FILE_SHARE_WRITE, ",");

   if (hFile != INVALID_HANDLE)

      return true;

      

   Alert("Unable to open file ", i_sSrcFileName, ". Error !", GetLastError());

   return false;

}

//+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

//| Reading the file                                                                                                                                                                         |

//+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

bool IsSrcFileRead(const int hFile, datetime &dtFirstTick, TickStruct &stLastTick, int &nTicksCnt)

{

   int hDstFile = INVALID_HANDLE;



   while (!FileIsEnding(hFile))

   {

      if (IsStopped())

      {

         Alert("Terminated by user");

         Comment("");

         return false;

      }



      // Datetime reading   

      string sDateTime = FileReadString(hFile);

      stLastTick.time = StringToTime(sDateTime); 



      // Bid price reading

      string sBid = FileReadString(hFile);

      stLastTick.bid = StringToDouble(sBid); 



      // Ask price reading

      string sAsk = FileReadString(hFile);

      stLastTick.ask = StringToDouble(sAsk); 

      

      // Adding the tick to dest file

      if (!IsTickAdded(hDstFile, stLastTick))

      {

         FileClose(hFile);

         return false;

      }



      nTicksCnt++;

      if ((nTicksCnt % 10000) == 0)

         Comment("Ticks recorded !", nTicksCnt, ", date: ", sDateTime);

         

      if (dtFirstTick == 0)

         dtFirstTick = stLastTick.time;



      string sTemp = FileReadString(hFile);

      sTemp = FileReadString(hFile);

   }

   

   FileClose(hDstFile);

   FileClose(hFile);

   return true;

}

//+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

//| Writing the next tick to array                                                                                                                                                           |

//+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

bool IsTickAdded(int &hDstFile, const TickStruct &stTick)

{

   if (hDstFile == INVALID_HANDLE)

   {

      hDstFile = FileOpen(i_sDstFileName, FILE_BIN | FILE_WRITE);

      if (hDstFile == INVALID_HANDLE)

      {

         Alert("Unable to create the destination file. Error !", GetLastError());

         return false;

      }

   }

   

   if (FileWriteStruct(hDstFile, stTick) != sizeof(stTick))

   {

      Alert("Unable to save the next tick to destination file. Error !", GetLastError());

      return false;

   }



   return true;

}

Comments