//+------------------------------------------------------------------+

#property indicator_chart_window

#include <hanover --- function header (np).mqh>

extern  string    CSVfilename                     = "pivots.csv";
extern  string    CSVdelimiter                    = ",";
extern  string    CSVformat                       = "Daily ,S3,S2,S1,PP,R1,R2,R3";
extern  string    LineColorWidthStyleStartLength  = "Green,1,0,-5,0";
extern  string    LabelFontSizeColorHposVposMask  = "Arial,11,Green,-8,0,' = 'TR-5' pips'";
extern  string    SymbolSuffix                    = "";
extern  string    Visibility                      = "M1,M5,M15,M30,H1,H4,D1,W1,MN";
//extern  string    RefreshPeriod                   = "+0";

string     ccy, IndiName, arr[15], label[15], LabelFont, LabelMask;
int        dig, tf, tmf, vis, wno, LineWidth, LineStyle, LineStartCandle, LineLength, LabelSize, LabelHpos, LabelVpos, RefreshEveryXMins;
double     spr, pnt, tickval;
color      LineColor, LabelColor;
datetime   prev_time;
bool       FirstTime;

//+------------------------------------------------------------------+
int init()  {
//+------------------------------------------------------------------+
  IndiName = "AFP-" + CSVfilename;
  IndicatorShortName(IndiName);

  ccy     = Symbol();
  tmf     = Period();
  pnt     = MarketInfo(ccy,MODE_POINT);
  dig     = MarketInfo(ccy,MODE_DIGITS);
  spr     = MarketInfo(ccy,MODE_SPREAD);
  tickval = MarketInfo(ccy,MODE_TICKVALUE);
  if (dig == 3 || dig == 5) {
    pnt     *= 10;
    spr     /= 10;
    tickval *= 10;
  } 

  wno = 0;  
  vis = GetVisibility(Visibility);
  
  StrToStringArray(LineColorWidthStyleStartLength,arr);
  LineColor       = StrToColor(arr[0]);
  LineWidth       = StrToInteger(arr[1]);  
  LineStyle       = StrToInteger(arr[2]);  
  LineStartCandle = StrToNumber(arr[3]);  
  LineLength      = StrToInteger(arr[4]);  

  StrToStringArray(LabelFontSizeColorHposVposMask,arr);
  LabelFont       = arr[0];
  LabelSize       = StrToInteger(arr[1]);
  if (StringLen(arr[2])<1)
    LabelColor    = LineColor;
  else
    LabelColor    = StrToColor(arr[2]);
  LabelHpos       = StrToNumber(arr[3]);
  LabelVpos       = StrToNumber(arr[4]);
  LabelMask       = arr[5];

//  RefreshEveryXMins = StrToTF(RefreshPeriod);
//  prev_time = -9999;

  del_obj();
  plot_obj();

  return(0);
}

//+------------------------------------------------------------------+
int deinit()  {
//+------------------------------------------------------------------+
  del_obj();
  return(0);
}

//+------------------------------------------------------------------+
int start()  {
//+------------------------------------------------------------------+
/*
  if (RefreshEveryXMins < 0)
    return(0);
  if (RefreshEveryXMins == 0) {
    del_obj();
    plot_obj();    
  }
  else {
    if(prev_time != iTime(Symbol(),RefreshEveryXMins,0))  {
      del_obj();
      plot_obj();
      prev_time = iTime(Symbol(),RefreshEveryXMins,0);
  } }      
*/  
  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 plot_obj()   {
//+------------------------------------------------------------------+
  StrToStringArray(CSVformat,label,CSVdelimiter);
  int h_inp = FileOpen(CSVfilename,FILE_CSV|FILE_READ,'~');
  if (h_inp < 0)    { Comment("Unable to open file '...\\MT4\\Files\\" + CSVfilename + "': " + err_msg(GetLastError()));    return(0); }
  while (!FileIsEnding(h_inp))    {
    string s_inp = FileReadString(h_inp);
    if (FileIsEnding(h_inp))    break;
    StrToStringArray(s_inp,arr,CSVdelimiter);
    if (arr[0]+SymbolSuffix != ccy)  continue;
    for (int i=1; i<=12; i++)   {
      double prc = StrToNumber(arr[i]);
      if (prc<=0)  continue;
      datetime time1 = Time[0]-LineStartCandle*60*Period();
      datetime time2 = Time[0]-(LineStartCandle-MathMax(LineLength,1))*60*Period();
      string objtext = label[0] + label[i] + NumberToStr(DivZero(prc-Bid,pnt),LabelMask);
      PlotTL    (IndiName+NumberToStr(i,"'-'Z4"), false, wno, time1, prc, time2, prc, LineColor, LineWidth, LineStyle, (LineLength<=0), false, vis, "");                         // Plot trendline
      if (LabelSize>0)
        PlotText(IndiName+NumberToStr(i,"'-'Z5"), false, wno, time1-LabelHpos*60*Period(), prc+LabelVpos*pnt, objtext, LabelColor, LabelSize, LabelFont,  0.0, false, vis);      // Plot text
    }
    break;
  }
  FileClose(h_inp);
  return(0);
}

//+------------------------------------------------------------------+
#include <hanover --- extensible functions (np).mqh>

