//+------------------------------------------------------------------+
//|                                           Plot External Data.mq4 |
//+------------------------------------------------------------------+

#property  indicator_separate_window

#include <hanover --- function header (np).mqh>

#property  indicator_buffers 8
#property  indicator_color1  Green
#property  indicator_width1  2
#property  indicator_color2  SaddleBrown
#property  indicator_width2  2
#property  indicator_color3  Gold
#property  indicator_width3  2
#property  indicator_color4  HotPink
#property  indicator_width4  2
#property  indicator_color5  Red
#property  indicator_width5  2
#property  indicator_color6  DarkOrchid
#property  indicator_width6  2
#property  indicator_color7  RoyalBlue
#property  indicator_width7  2
#property  indicator_color8  White
#property  indicator_width8  2

extern string   FileName            = "PSA\PSA_CROSSTAB.CSV";
extern string   FieldDelimiter      = ",";
extern color    Color_AUD           = Green;
extern color    Color_CAD           = SaddleBrown;
extern color    Color_CHF           = Gold;
extern color    Color_EUR           = HotPink;
extern color    Color_GBP           = Red;
extern color    Color_JPY           = DarkOrchid;
extern color    Color_NZD           = RoyalBlue;
extern color    Color_USD           = White;
extern int      LineWidth           = 2;
extern int      LineStyle           = 0;
extern bool     DisplayLegend       = true;

double     buffer0[],buffer1[],buffer2[],buffer3[],buffer4[],buffer5[],buffer6[],buffer7[];
int        wno;
datetime   dt[9999];
double     valu[8][9999];
string     arr[20], IndiName;    // added to accomplish format conversion

//+------------------------------------------------------------------+
int init()  {
//+------------------------------------------------------------------+
  SetIndexBuffer(0,buffer0);
  SetIndexStyle(0,DRAW_LINE,LineStyle,LineWidth,Color_AUD);
  SetIndexDrawBegin(0,0);

  SetIndexBuffer(1,buffer1);
  SetIndexStyle(1,DRAW_LINE,LineStyle,LineWidth,Color_CAD);
  SetIndexDrawBegin(1,0);

  SetIndexBuffer(2,buffer2);
  SetIndexStyle(2,DRAW_LINE,LineStyle,LineWidth,Color_CHF);
  SetIndexDrawBegin(2,0);

  SetIndexBuffer(3,buffer3);
  SetIndexStyle(3,DRAW_LINE,LineStyle,LineWidth,Color_EUR);
  SetIndexDrawBegin(3,0);

  SetIndexBuffer(4,buffer4);
  SetIndexStyle(4,DRAW_LINE,LineStyle,LineWidth,Color_GBP);
  SetIndexDrawBegin(4,0);

  SetIndexBuffer(5,buffer5);
  SetIndexStyle(5,DRAW_LINE,LineStyle,LineWidth,Color_JPY);
  SetIndexDrawBegin(5,0);

  SetIndexBuffer(6,buffer6);
  SetIndexStyle(6,DRAW_LINE,LineStyle,LineWidth,Color_NZD);
  SetIndexDrawBegin(6,0);

  SetIndexBuffer(7,buffer7);
  SetIndexStyle(7,DRAW_LINE,LineStyle,LineWidth,Color_USD);
  SetIndexDrawBegin(7,0);

  IndiName = "PSA_Crosstab";
  IndicatorShortName(IndiName);
  wno = WindowFind(IndiName);
  display_legend();

  return(0);
}

//+------------------------------------------------------------------+
int deinit()  {
//+------------------------------------------------------------------+
  del_obj();
  return(0);
}

//+------------------------------------------------------------------+
void del_obj()  {
//+------------------------------------------------------------------+
  int k=0;
  while (k<ObjectsTotal())   {
    string objname = ObjectName(k);
    if (StringSubstr(objname,0,StringLen(IndiName)) == IndiName)  
      ObjectDelete(objname);
    else
      k++;
  }    
  return(0);
}

//+------------------------------------------------------------------+
void display_legend()  {
//+------------------------------------------------------------------+
  // Display legend.......
  del_obj();
  if (DisplayLegend)   {
    string symb = "";
    color  colr = CLR_NONE;
    for (int i=0; i<8; i++)  {
      switch (i)  {
        case 0 :  symb = "AUD";  colr = Color_AUD;  break;
        case 1 :  symb = "CAD";  colr = Color_CAD;  break;
        case 2 :  symb = "CHF";  colr = Color_CHF;  break;
        case 3 :  symb = "EUR";  colr = Color_EUR;  break;
        case 4 :  symb = "GBP";  colr = Color_GBP;  break;
        case 5 :  symb = "JPY";  colr = Color_JPY;  break;
        case 6 :  symb = "NZD";  colr = Color_NZD;  break;
        case 7 :  symb = "USD";  colr = Color_USD;  break;
      }
      PlotLabel (IndiName+NumberToStr(i,"'-'Z6"), true, wno, 1, 350-i*45, 2, symb, colr, 11, "Arial Black",  0.0, false, 0);       // Plot text label
  } }
  return(0);
}

//+------------------------------------------------------------------+
int start()  {
//+------------------------------------------------------------------+
  for (int i=0; i<8; i++)  
    for (int j=0; j<9999; j++)
      valu[i][j] = 0;

  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);
    for (i=0; i<8; i++) 
      valu[i][c] = StrToNumber(arr[2*i+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
  for (i=0; i<9999; i++)  {
    int z=c-i-1;
    if (z<0)  break;
    buffer0[z] = valu[0][i];
    buffer1[z] = valu[1][i];
    buffer2[z] = valu[2][i];
    buffer3[z] = valu[3][i];
    buffer4[z] = valu[4][i];
    buffer5[z] = valu[5][i];
    buffer6[z] = valu[6][i];
    buffer7[z] = valu[7][i];
  }
  if (wno<=0)  {
    wno = WindowFind(IndiName);
    display_legend();
  }  
        
  return(0);
}

//+------------------------------------------------------------------+
#include <hanover --- extensible functions (np).mqh>

