0

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.

user3666197
  • 1
  • 6
  • 50
  • 92
John
  • 2,820
  • 3
  • 30
  • 50
  • 1
    indicator is updated every tick, so you will have to get a new file after some time. is it okay? anyway, your task should be splitted into two: reading the file and parsing it to get correct time format, and plotting the indicator – Daniel Kniaz Mar 10 '17 at 08:29
  • @Daniel, Yes. I will add data in the file on regular basis. So far I have put a months data in it. The minimum time period of data to be displayed will 5 mins. – John Mar 10 '17 at 09:48

1 Answers1

1

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

Daniel Kniaz
  • 4,603
  • 2
  • 14
  • 20
  • Thanks for the code. I will try and let you know about the success. Honestly speaking this will be my first ever program on mt4. – John Mar 10 '17 at 10:22
  • then it would take you time, this doesnt seem to be the easiest task for the newcomer. welcome to mql4! – Daniel Kniaz Mar 10 '17 at 12:22
  • Messed. Total. Wanted to plot a inclined line and got a vertical line. – John Mar 10 '17 at 12:36
  • @John well, after a few decades in this domain, choosing to start right with a **`CustomIndicator`** type of `MQL4`-code is a double trouble. You might want to read a few examples and re-use the core-logic of how the incremental, batch-sized updates are calculated in ***New*-`MQL4`** first, then trying to modify some trivial examples, to get better grasp of the reversed-step indexing in TimeSeries and finally integrate your case ( adding some smart file-based update of just the head-of-array, whereas the tail remains the same (yes, efficiency matters as all Indicators SHARE A SINGLE THREAD...) – user3666197 Mar 10 '17 at 14:04
  • 1
    @John may enjoy a few samples from >>> http://www.desynced.net/fx/eas/indicators.php – user3666197 Mar 10 '17 at 14:06
  • @DanielKniaz, Can you please put some remarks what each line of code is for? Will help me a little. :) – John Mar 18 '17 at 13:27
  • @DanielKniaz, Also in the second piece of code, about parsing each line, do I need to parse the date & time only? A little explanation why it has to be done will be of great help. Even a link will be sufficient. – John Mar 18 '17 at 13:39
  • about first part - i hope it is clear from the code itself - the function reads all lines to a string and then deletes the used file. about part #2 - if you had standard datetime format (2017.03.18 0:27) then you could read the full block from start of the line till comma, but if you dont - then need to read each part of it by selecting text betweeen previously read - till new "." or ":" or "," to get that values(line-4), and then fill them into a MqlDatetime structure(l-6) after converting to int(l-5), at end - convert structure into regular datetime format. – Daniel Kniaz Mar 18 '17 at 18:19