Search code examples
csvmql4metatrader4

How to plot CSV values as a Custom Indicator?


I am new to MQL4 and MetaTrader4. I have a CSV file in a following format -

2017.2.1 0:00, 120
2017.2.1 0:05, 123
2017.2.1 0:10, 125    

The date format is YYYY.M.D H:MM. I searched other forums, but couldn't get help. I want this to be plotted as an indicator.


Solution

  • about reading data: need to open data, then read its content:

    bool ReadFile(const string fileName, string &data){
        const int handle=FileOpen(fileName,FILE_READ|FILE_TXT);
        if (handle==INVALID_HANDLE) return false;
        string collector = "";
        int SIZE = (int)FileSize(handle);
        int size=StringLen(collector);
        while(size < SIZE && !IsStopped()){
          collector = StringConcatenate(collector, "\n", FileReadString(handle, SIZE - size));
          size = StringLen(collector);
        }
        FileClose(handle);
        if (!FileDelete(fileName))
           Print("FileDelete(", fileName, ") FAILED"); // to delete this file after it is read
        data = collector;
        return true;
        }
    

    about parsing each line of the above obtained text:

      MqlTime mql;
      int st_pos=0,end_pos=0;
      int year = 0;
      end_pos = StringFind(line, ".", st_pos);
      int year = StrToInteger(StringSubStr(line,st_pos+1,end_pos-st_pos-1));
      mql.year = year;
      // same with month, day, hour and minute
      datetime time = StructToTime(mql); - this is your date
    

    after that - find index using iBarShift() that corresponds to your date and Buffer[i] = value that is parsed from the same line