//+------------------------------------------------------------------+
//|                                           Plot External Data.mq4 |
//+------------------------------------------------------------------+

#property  indicator_separate_window

//#include <hanover --- function header (np).mqh>

#property  indicator_buffers 3
#property  indicator_color1  Red
#property  indicator_width1  1

extern   string   FileNamePrefix         = "indicator_";
extern   string   DateFormat       = "YYYY.MM.DD HH:II";
//extern   string   DateDelimiters   = ". :";
extern   string   FieldDelimiter   = ",";
extern   color    LineColorBuy        = Green;
extern   color    LineColorSell        = Red;
extern   color    LineColor        = Orange;
extern   int      LineWidth        = 1;
extern   int      LineStyle        = STYLE_SOLID;
extern   double   ScalingFactor    = 1.0;

double     buffer0[], buffer1[], buffer2[];

datetime   dt[];
double     val1[], val2[], val3[];
int calculate_count = -1;

//+------------------------------------------------------------------+
int init()  {
//+------------------------------------------------------------------+
  IndicatorShortName("External: "+FileNamePrefix);
  
  SetIndexBuffer(0,buffer0);
  SetIndexStyle(0,DRAW_LINE,LineStyle,LineWidth,LineColorBuy);
  SetIndexDrawBegin(0,0);
  
  SetIndexBuffer(1,buffer1);
  SetIndexStyle(1,DRAW_LINE,LineStyle,LineWidth,LineColorSell);
  SetIndexDrawBegin(1,0);
  
  SetIndexBuffer(2,buffer2);
  SetIndexStyle(2,DRAW_LINE,LineStyle,LineWidth,LineColor);
  SetIndexDrawBegin(2,0);
  
  SetLevelValue(1, 0);
  SetLevelStyle(STYLE_SOLID, 1);
  SetLevelStyle(DRAW_NONE, 1, clrGray);
   
  return(0);
}

//+------------------------------------------------------------------+
int start()  {
//+------------------------------------------------------------------+
  if (calculate_count == -1) { // only execute on first tick - modify if you want it to calculate on every tick, or every x minutes
     int h = FileOpen("CSV/" + FileNamePrefix + Symbol() + "_temp.csv", FILE_CSV|FILE_READ,FieldDelimiter);
     if (GetLastError() != 0) {
       Alert("File " + "CSV/" + FileNamePrefix + Symbol() + "_temp.csv" + " error: " + GetLastError());
       return(0);
     }
   
     // First pass loads data from file into arrays dt, val   (i.e. date/time, and value)
     int c = -1;
     ArrayResize(dt, 0);
     ArrayResize(val1, 0);
     ArrayResize(val2, 0);
     ArrayResize(val3, 0);
     while(FileTell(h)<FileSize(h)){
       c++;
       string tmp = FileReadString(h); // timestamp, as string
       if (StringLen(StringTrimRight(tmp)) < 1)   continue;    // ignore blank lines
       ArrayResize(dt, ArraySize(dt) + 1);
       ArrayResize(val1, ArraySize(val1) + 1);
       ArrayResize(val2, ArraySize(val2) + 1);
       ArrayResize(val3, ArraySize(val3) + 1);
       dt[c]  = StringToTime(tmp);
       val1[c] = StringToDouble(FileReadString(h)); // val1
       val2[c] = StringToDouble(FileReadString(h)); // val2
       val3[c] = StringToDouble(FileReadString(h)); // val3
     };
     FileClose(h);
      
     /*
     for (int c=0; !FileIsEnding(h) && c<9999; c++)  {
       string tmp = FileReadString(h);
       if (FileIsEnding(h))  break;
       if (StringLen(StringTrimRight(tmp)) < 1)   continue;    // ignore blank lines
       StrToStringArray(tmp,arr,FieldDelimiter);
       dt[c]  = StrToDate(arr[0],DateFormat,DateDelimiters);
       val[c] = StrToNumber(arr[1]);
   //    if (c>0)  log(c,DateToStr(dt[c]),NumberToStr(val[c],"RT-3.5"));    // debugging only
     }
     FileClose(h);
     */
     
     // Second pass re-synchs data from dt, val arrays onto chart timeframe, and plots them accordingly
     for (int i=0; i<=Bars; i++)  {
       while(Time[i]<dt[c]) c--;
       buffer0[i] = val1[c] * ScalingFactor;
       buffer1[i] = val2[c] * ScalingFactor;
       buffer2[i] = val3[c] * ScalingFactor;
   //    if (i<30) log(i,DateToStr(Time[i]),c,DateToStr(dt[c]),val[c],buffer0[i]);    // debugging only
     }
  };
  
  calculate_count++;
  
  return(0);
}

//+------------------------------------------------------------------+
//#include <hanover --- extensible functions (np).mqh>

