//+------------------------------------------------------------------+
//|                                              Spaced Lines v3.mq4 |
//+------------------------------------------------------------------+

#property indicator_chart_window

#include <hanover --- function header (np).mqh>

extern string     ParameterFile          = "%SYMBOL%";
extern string     StartPrice             = "C,0";
extern string     PriceLevels            = "-20,-10,0,10,20";
extern bool       AbsolutePrices         = false;
extern string     LineColors             = "DimGray";
extern string     LineStyles             = "10,10,10,10,10";
extern string     Visibility             = "M1, M5, M15, M30, H1, H4, D1, W1, MN";
extern int        CounterLookback        = 0;
/*
extern string     ParameterFile          = "NONE";
extern string     StartPrice             = "C0,100";
extern string     PriceLevels            = "-1000,-950,-900,-850,-800,-750,-700,-650,-600,-550,-500,-450,-400,-350,-300,-250,-200,-150,-100,-50,0,50,100,150,200,250,300,350,400,450,500,550,600,650,700,750,800,850,900,950,1000";
extern bool       AbsolutePrices         = false;
extern string     LineColors             = "DimGray";
extern string     LineStyles             = "10,12,10,12,10,12,10,12,10,12,10,12,10,12,10,12,10,12,10,12,10,12,10,12,10,12,10,12,10,12,10,12,10,12,10,12,10,12,10,12,10";
extern string     Visibility             = "M1, M5, M15, M30, H1, H4, D1, W1, MN";
extern int        CounterLookback        = 0;
*/
string   ccy, sym;
int      dig, tf, tmf, vis;
double   spr, pnt, tickval, bidp, askp, minlot, lswap, sswap;

string   IndiName = "Spaced Lines-";
double   PL[100];
color    LC[100];
int      LS[100];
int      ccPL, ccLC, ccLS;
string   SP[2];

//+------------------------------------------------------------------+
int init()  {
//+------------------------------------------------------------------+
  ccy     = Symbol();
  tmf     = Period();
  bidp    = MarketInfo(ccy,MODE_BID);
  askp    = MarketInfo(ccy,MODE_ASK);
  pnt     = MarketInfo(ccy,MODE_POINT);
  dig     = MarketInfo(ccy,MODE_DIGITS);
  spr     = MarketInfo(ccy,MODE_SPREAD);
  tickval = MarketInfo(ccy,MODE_TICKVALUE);
  minlot  = MarketInfo(ccy,MODE_MINLOT);
  lswap   = MarketInfo(ccy,MODE_SWAPLONG);
  sswap   = MarketInfo(ccy,MODE_SWAPSHORT);
  if (dig == 3 || dig == 5) {
    pnt     *= 10;
    spr     /= 10;
    tickval *= 10;
  }  

  CheckPresets();
  del_obj();
  plot_obj();
  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);
}

//+------------------------------------------------------------------+
int start()  {
//+------------------------------------------------------------------+
  return(0);
}

//+------------------------------------------------------------------+
void plot_obj()  {
//+------------------------------------------------------------------+
  double mult = GetPipValue(Symbol());
       
  double prc = 0;       
  int x = StrToStringArray(StartPrice,SP,",");
  SP[0]   = StringUpper(SP[0]);
  int bar = StrToNumber(SP[0]);
  if (StringFind(SP[0],"C") >= 0)   prc = Close[bar];
  if (StringFind(SP[0],"H") >= 0)   prc = High[bar];
  if (StringFind(SP[0],"L") >= 0)   prc = Low[bar];
  if (StringFind(SP[0],"O") >= 0)   prc = Open[bar];
  if (prc == 0)                     prc = StrToNumber(SP[0]);
  int fix = StrToNumber(SP[1]);
  if (fix>0)
    prc = fix*mult*MathFloor(prc/mult/fix+0.5);

  double counter = CounterLookback;
  if (counter > 1)   counter--;
  if (counter < 0)   counter = WindowFirstVisibleBar()-1;

  for (int i=0; i<ccPL; i++)   {
    string objname = IndiName + "-" + i;
    double price = prc+PL[i]*mult;
    if (AbsolutePrices)
      price = PL[i];
    int bStyle = MathMod(LS[i],10);
    int bWidth = LS[i]/10;
    PlotHL (objname, true, 0, price, LC[i], bWidth, bStyle, false, vis);      // Plot horizontal line

    if (counter < 1)  continue;
    int cnt=0;
    for (int j=0; j<=counter; j++)   {
      if (price >= Low[j] && price <= High[j])  cnt++;
    }
    string objdesc = NumberToStr(price,"'@ 'T5."+NumberToStr(dig,"T2")+"' : '") + NumberToStr(cnt,"T6") + NumberToStr(counter+1,"' / 'T6"); 
    ObjectSetText(objname,objdesc,12,"Arial",White);   
  }
  return(0);
}

//+------------------------------------------------------------------+
int CheckPresets()
//+------------------------------------------------------------------+
{
//---------------------------------------------------------------------------------------------------------------
//    Enter the file name in here
//---------------------------------------------------------------------------------------------------------------
  ParameterFile = StringUpper(ParameterFile);
  string FileName = "Presets---Spaced Lines.TXT";
  if (ParameterFile == "%SYMBOL%") 
    ParameterFile = Symbol();       
  if (ParameterFile > "")
    FileName = "Presets---Spaced Lines." + ParameterFile;
//---------------------------------------------------------------------------------------------------------------
  int handle = FileOpen(FileName, FILE_CSV|FILE_READ,';');
  if (handle > 0)  {
    while(!FileIsEnding(handle))  {
      string text  = FileReadString(handle);
      int t0 = StringFind(text,"//",0);
      if (t0 == 0)       text = "";    
      else if (t0 > 0)   text = StringSubstr(text,0,t0);
      string temp  = "";
      int    quote = 0;
      for(int i=0; i<StringLen(text); i++)   {
        string char = StringSubstr(text,i,1);
        if (char == "\x22")    quote = 1 - quote;  
        else if (quote == 1)    temp  = temp + char;
        else if (char != " " && char != "_") temp  = temp + StringLower(char);  
      }
      if (StringLen(temp) > 0) {
        int equal = StringFind(temp,"=",0);
        int semic = StringFind(temp,";",0);
        string pname = "";
        pname   = StringSubstr(temp,0,equal);
        string pvalue = StringSubstr(temp,equal+1,semic-equal+1);
        if (pvalue != "*")  {
//---------------------------------------------------------------------------------------------------------------
//    Parameter assignment statements go in here
//---------------------------------------------------------------------------------------------------------------
          if (pname == "startprice")               StartPrice               = pvalue;                     else
          if (pname == "pricelevels")              PriceLevels              = pvalue;                     else
          if (pname == "absoluteprices")           AbsolutePrices           = StrToBool(pvalue);          else
          if (pname == "linecolors")               LineColors               = pvalue;                     else
          if (pname == "linestyles")               LineStyles               = pvalue;                     else
          if (pname == "visibility")               Visibility               = pvalue;                     else
          if (pname == "counterlookback")          CounterLookback          = StrToInteger(pvalue);
//          Debug("pname  = " + pname);
//          Debug("pvalue = " + pvalue);
//---------------------------------------------------------------------------------------------------------------
        }
      }  
      temp = FileReadString(handle);
    }
    FileClose(handle);
  }  
  
  ArrayInitialize(PL,0);  
  ArrayInitialize(LC,-1);
  ArrayInitialize(LS,-1);
  ccPL = StrToDoubleArray(PriceLevels,PL,",");
  ccLC = StrToColorArray(LineColors,LC,",");
  ccLS = StrToIntegerArray(LineStyles,LS,",");
  for (i=0; i<ccPL; i++)  {
    if (LC[i] < 0)  LC[i] = LC[i-1];
    if (LS[i] < 0)  LS[i] = LS[i-1];
  }    

  string str = NumberToStr(StartPrice,"2.5") + PriceLevels + LineColors + LineStyles;    // TimeToStr(TimeLocal(),TIME_SECONDS);
  IndiName = "SpacedLines-" + GetHash(str);
  IndicatorShortName(IndiName);

  vis = GetVisibility(Visibility);
  return(0);
}

#include <hanover --- extensible functions (np).mqh>

