Download MetaTrader 5

Help needed , how to import a cvs file data as an indicator

To add comments, please log in or register
twoci
53
twoci  

Hi,

I got a csv file with a list of data: Datetime, and values.

These are the values of an indicator at certain moments, calculated in excel/mathlab.
How can I import these data to appear on my screen (either oin the main window or separate one) ?

Thnx in advance for you help !
Twoci
onewithzachy
2269
onewithzachy  
twoci:

Hi,

I got a csv file with a list of data: Datetime, and values.

These are the values of an indicator at certain moments, calculated in excel/mathlab.
How can I import these data to appear on my screen (either oin the main window or separate one) ?

Thnx in advance for you help !
Twoci

Create CI, use file functions  https://www.mql5.com/en/docs/files. You may want to experiment with  https://www.mql5.com/en/docs/files/filereadarray

However this is not fun, much more easy in MT4, coz it has offline chart, filereadarray can read any string. 

Documentation on MQL5: File Functions
  • www.mql5.com
File Functions - Documentation on MQL5
meisme
39
meisme  

Hello,

Please try this:

//+------------------------------------------------------------------+
//|                                                   csvdisplay.mq5 |
//|                                                           meisme |
//+------------------------------------------------------------------+
#property copyright "meisme"
#property version   "1.00"

class CCsvDisplay {
   public:
   //Constructor, pass the filename here
   CCsvDisplay(string strFilename) {
      m_strFilename = strFilename;
   }
   ~CCsvDisplay(){/*Destructor*/};
   //Read the csv file
   void ReadDisplayFile() {
      if (FileIsExist(m_strFilename) == true) {
         int iFile = FileOpen(m_strFilename, FILE_TXT | FILE_READ | FILE_SHARE_READ | FILE_ANSI);
         //Is file open succeed?
         if (iFile != -1) {
         //Succeed
            Print("Open file ", m_strFilename);
            string strFormatDisplayString;
            //Parse and display file content until file reach the end
            while (FileIsEnding(iFile) == false) {
               //Read file line per line
               string strContentLine = FileReadString(iFile);
               string strParsedColumn[];
               //Parse string column by column (based on separator)
               int iContentCount = StringSplit(strContentLine, StringGetCharacter(",",0), strParsedColumn);
               if (iContentCount > 0) {
                  for (int i = 0; i < iContentCount; i++) {
                     strFormatDisplayString += strParsedColumn[i];
                     if (i != (iContentCount - 1)) strFormatDisplayString += " -> ";
                  }
                  //Add return and new line for display
                  strFormatDisplayString += "\n";
               }
            } 
            //Close file after read
            FileClose(iFile);
            //Plot string as comment on chart
            Comment(strFormatDisplayString);
         }
         else {
         //Failed
            Comment("Cannot open ", m_strFilename, " !");
         }
      }
      else {
      //File doesn't exist?
         Comment("File ", m_strFilename, " doesn't exist!");
      }
   }
   
   private:
      string m_strFilename;
};

//============

CCsvDisplay *m_pcCsvDisp;
int OnInit()
  {
   m_pcCsvDisp = new CCsvDisplay("myFile.csv");
   m_pcCsvDisp.ReadDisplayFile();
   
   Print("Load Csv display");
   return(0);
  }
  
  void OnDeinit(const int r)
  {
   if (m_pcCsvDisp != NULL) delete m_pcCsvDisp;
  }

The csv file have to be placed inside folder: C:\Program Files\MetaTrader 5\MQL5\Files

with name: myFile.csv

Good luck!


Edit: Add file close code







onewithzachy
2269
onewithzachy  
meisme:

Hello,

Please try this:

The csv file have to be placed inside folder: C:\Program Files\MetaTrader 5\MQL5\Files

with name: myFile.csv

Good luck!

Hi meisme,

Thanks for the code. However if I may ...

The flag in file open should be FILE_CSV not FILE_TXT. That's OK though, I made the same mistake lately, but the file still can be opened and read.

And there's no need for StringSplit(), because FileReadString(), will read CSV file before delimiter, and we also can directly pass the value of CSV columns into an array of double type by directly convert the string into double (and datetime) by using FileReadNumber(). So, FileReadString() is not even needed.

Here's my CSV file reading which anyone can modify  https://www.mql5.com/en/forum/6909#comment_209726. There, the CSV data is passed to string array but not using StringSplit() (and not converted into Double or Integer).

However the real thing that need attention is, the number of read data should not exceed the number of Bars on chart :(.

:D 

 

meisme
39
meisme  

Hi onewithzachy,

Thanks to pointed that up.  You are right, using FILE_CSV may made the code much shorter.

I wrote above code with mindset that OP could possibly extend the csv content for more than 2 columns, with unknown data type.  So imo string is the best option as the purpose is only for display.

It's quite good link you have, also i am quite agree people here are too serious :)

Edit: Forget to tell that at above code, it's better to close the file after the file content already read.  I missed that part. I am going to fix it


Regards,

twoci
53
twoci  
meisme:

Hi onewithzachy,

Thanks to pointed that up.  You are right, using FILE_CSV may made the code much shorter.

I wrote above code with mindset that OP could possibly extend the csv content for more than 2 columns, with unknown data type.  So imo string is the best option as the purpose is only for display.

It's quite good link you have, also i am quite agree people here are too serious :)

Edit: Forget to tell that at above code, it's better to close the file after the file content already read.  I missed that part. I am going to fix it


Regards,

Dear onewithzachy, Dear meisme,


Thank your for your replies

Regards

To add comments, please log in or register