//+------------------------------------------------------------------+
//|                                     Plot External Data matto.mq4 |
//+------------------------------------------------------------------+

#property  indicator_separate_window

#include <hanover --- function header (np).mqh>

#property  indicator_buffers 2
#property  indicator_color1  Red
#property  indicator_width1  1
#property  indicator_color2  DodgerBlue
#property  indicator_width2  1

extern   string   FileName         = "livorno.csv";
extern   string   DateFormat       = "YYYY-MM-DDTHH";
extern   string   DateDelimiters   = "-T";
extern   string   FieldDelimiter   = ",";
extern   color    Line1Color       = Red;
extern   int      Line1Width       = 1;
extern   int      Line1Style       = STYLE_SOLID;
extern   color    Line2Color       = DodgerBlue;
extern   int      Line2Width       = 1;
extern   int      Line2Style       = STYLE_SOLID;
extern   double   ScalingFactor    = 1.0;

double     buffer0[], buffer1[];

datetime   dt[9999];
double     val1[9999],val2[9999];
string     arr[10];    // added to accomplish format conversion


//+------------------------------------------------------------------+
int init()  {
//+------------------------------------------------------------------+
  SetIndexBuffer(0,buffer0);
  SetIndexStyle(0,DRAW_LINE,Line1Style,Line1Width,Line1Color);
  SetIndexDrawBegin(0,0);
  SetIndexBuffer(1,buffer1);
  SetIndexStyle(1,DRAW_LINE,Line2Style,Line2Width,Line2Color);
  SetIndexDrawBegin(1,0);
  IndicatorShortName("External: "+FileName);
  return(0);
}

//+------------------------------------------------------------------+
int start()  {
//+------------------------------------------------------------------+
  int h = FileOpen(FileName, FILE_CSV|FILE_READ,'~');
  if (h==0) {
    Comment("File "+FileName+" not found.");
    return(0);
  }  

  // First pass loads data from file into arrays dt, val   (i.e. date/time, and value)
  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);
    val1[c] = StrToNumber(arr[1]);
    val2[c] = StrToNumber(arr[2]);
//    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
  c--;
  for (int i=0; i<=Bars; i++)  {
    while(Time[i]<dt[c]) c--;
    if (c >= 0) {
      buffer0[i] = val1[c]*ScalingFactor;
      buffer1[i] = val2[c]*ScalingFactor;
  } }    
//    if (i<30) log(i,DateToStr(Time[i]),c,DateToStr(dt[c]),val[c],buffer0[i]);    // debugging only
  
  return(0);
}

//+------------------------------------------------------------------+
#include <hanover --- extensible functions (np).mqh>

