//+------------------------------------------------------------------+
//|                                       Flatliner (by hanover).mq4 |
//+------------------------------------------------------------------+

#property indicator_separate_window
#property indicator_minimum -1
#property indicator_maximum 1
#property indicator_level1 0
#property indicator_levelcolor Silver
#property indicator_levelstyle STYLE_DOT

#property indicator_buffers 1
#property indicator_color1  DodgerBlue
#property indicator_width1  1


//:::::::::::::::::::::::::::::::::::::::::::::
#include <mt4accountinfo.mqh>
#include <mt4string.mqh>
#include <mt4datetime.mqh>
#include <mt4objects_1.mqh>
#include <mt4timeseries_2.mqh>
//Etc.
//:::::::::::::::::::::::::::::::::::::::::::::::

input string  _Info                    = "Flatliner using Zigzag";
input int     _PeriodLast_Backstep     = 5;
input int     _PeriodPips_Deviation    = 14;
input int     _PeriodBars_Depth        = 100;
input int     _barcount                = 3000;

double     ind_buffer1[];

//+------------------------------------------------------------------+
int OnInit()  {
  //:::::::::::::::::::::::::::::::::::::::::::::
  double Ask=SymbolInfoDouble(Symbol(),SYMBOL_ASK);
  double Bid=SymbolInfoDouble(Symbol(),SYMBOL_BID);
  int Bars=Bars(Symbol(),PERIOD_CURRENT);
  double Point=Point();
  //Etc.
  //:::::::::::::::::::::::::::::::::::::::::::::::

//+------------------------------------------------------------------+
  IndicatorShortName("Flatliner (using Zigzag)");
  SetIndexStyle(0,DRAW_HISTOGRAM,1,DodgerBlue);
  SetIndexBuffer(0,ind_buffer1);
  SetIndexDrawBegin(0,0);
  ArrayInitialize(ind_buffer1,EMPTY_VALUE);
  return(0);

}

//+------------------------------------------------------------------+
int OnDeinit()  {
  //:::::::::::::::::::::::::::::::::::::::::::::
  double Ask=SymbolInfoDouble(Symbol(),SYMBOL_ASK);
  double Bid=SymbolInfoDouble(Symbol(),SYMBOL_BID);
  int Bars=Bars(Symbol(),PERIOD_CURRENT);
  double Point=Point();
  //Etc.
  //:::::::::::::::::::::::::::::::::::::::::::::::

//+------------------------------------------------------------------+
  return(0);
}

//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime& time[],
                const double& open[],
                const double& high[],
                const double& low[],
                const double& close[],
                const long& tick_volume[],
                const long& volume[],
                const int& spread[])   {
  //:::::::::::::::::::::::::::::::::::::::::::::
  double Ask=SymbolInfoDouble(Symbol(),SYMBOL_ASK);
  double Bid=SymbolInfoDouble(Symbol(),SYMBOL_BID);
  int Bars=Bars(Symbol(),PERIOD_CURRENT);
  double Point=Point();
  //Etc.
  //:::::::::::::::::::::::::::::::::::::::::::::::

//+------------------------------------------------------------------+
  int flat_value = 0;
  double prev_zz = 0;
  for (int i=MathMin(Bars-10,_barcount); i>=0; i--)   {
    double zz = iCustom(Symbol(),Period(),"ZigZag",_PeriodBars_Depth,_PeriodPips_Deviation,_PeriodLast_Backstep,0,i);
    if (zz==0 || zz==EMPTY || zz==EMPTY_VALUE)  
      ind_buffer1[i] = flat_value;
    else {
      flat_value = MathSign(prev_zz-zz);
      prev_zz = zz;
      ind_buffer1[i] = flat_value;
    }
  }  
   return(rates_total);
}

//+------------------------------------------------------------------+
int MathSign(double n)  {
//+------------------------------------------------------------------+
  if (n > 0) return(1);
  else if (n < 0) return (-1);
  else return(0);
}  
//+------------------------------------------------------------------+

