#property copyright "Copyright 2020, RepublicOfMars"
#property link      "https://www.mql5.com/en/users/lorio"
#property version   "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_plots   3
input int MaPeriod=20;//MA Period
input ENUM_MA_METHOD MaMethod=MODE_SMA;//MA Method 
input int MaShift=0;//MA Shift
input ENUM_APPLIED_PRICE MaPrice=PRICE_TYPICAL;//MA Price

//--- indicator buffers
double         MovingAverageBuffer[];
double         HistaboveBuffer[];
double         HistobelowBuffer[];
int BarDeflekt=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,HistaboveBuffer);
   SetIndexBuffer(1,HistobelowBuffer);
   SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,1,clrYellowGreen);
   SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID,1,clrOrangeRed);
   SetIndexBuffer(2,MovingAverageBuffer);
   SetIndexStyle(2,DRAW_LINE,STYLE_SOLID,1,clrWhite);
   BarDeflekt=MaPeriod;
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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[])
  {
//---
  int total=rates_total-prev_calculated;
  int toscan=rates_total-BarDeflekt-1;
  if(prev_calculated==rates_total){toscan=0;}
  if(rates_total>prev_calculated&&prev_calculated>0&&total==1){toscan=1;}
  for(int i=toscan;i>=0;i--)
  {
  double ma=iMA(_Symbol,_Period,MaPeriod,MaShift,MaMethod,MaPrice,i);
  MovingAverageBuffer[i]=ma;
  //non zero
    if(i>0)
    {
    double h=MathMax(High[i],ma),l=MathMin(Low[i],ma);
    if(Close[i]>=ma){HistaboveBuffer[i]=h;HistobelowBuffer[i]=l;}
    if(Close[i]<ma){HistobelowBuffer[i]=h;HistaboveBuffer[i]=l;}
    }
  //zero
    if(i==0)
    {
    double h=MathMax(High[i],ma),l=MathMin(Low[i],ma);
    if(Bid>=ma){HistaboveBuffer[i]=h;HistobelowBuffer[i]=l;}
    if(Bid<ma){HistobelowBuffer[i]=h;HistaboveBuffer[i]=l;}
    }
  }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---
   
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---
   
  }
//+------------------------------------------------------------------+
