//+------------------------------------------------------------------+
//|                                                GRAALUn-HISTO.mq4 |
//|                        Copyright 2016, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_style1 STYLE_SOLID
#property indicator_style2 STYLE_SOLID
#property indicator_color1 Yellow
#property indicator_color2 Red


#property indicator_level1 0
//#property indicator_level2 -2
//#property indicator_level3 2
//#property indicator_level4 -1
//#property indicator_level5 1
//#property indicator_minimum -3
#property indicator_maximum 3






extern int period=9;
extern int MA_Period1=5;
extern int MA_Shift1=0;
extern int MA_Method1=3; 
double ExtBuffer0[];
double ExtBuffer1[];
int init()
{
   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(0,ExtBuffer0);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,ExtBuffer1);
   IndicatorShortName("un");
return(0);
}


int start()
{
int limit;
int counted_bars;
double prev,current,old;
double Value=0,Value1=0,Value2=0,Fish=0,Fish1=0,Fish2=0;
double price;
double MinL=0;
double MaxH=0;
   
   counted_bars=IndicatorCounted();

   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;


   for(int i=0; i<limit; i++)
   {
      MaxH = High[iHighest(NULL,0,MODE_HIGH,period,i)];                       // Ищем максимальную цену в количестве баров period
      MinL = Low[iLowest(NULL,0,MODE_LOW,period,i)];                          // Ищем миимальную цену в количестве баров period
      price = (High[i]+Low[i])/2;                                             // Получаем среднюю цену последнего бара

      if(MaxH-MinL == 0) Value = 0.33*2*(0-0.5) + 0.67*Value1;
      else Value = 0.33*2*((price-MaxH)/(MinL-MaxH)-0.5) + 0.67*Value1;

      Value=MathMin(MathMax(Value,-0.999),0.999);

      if(1-Value == 0) ExtBuffer0[i]=0.5+0.5*Fish1;
      else ExtBuffer0[i]=-0.5*MathLog((1+Value)/(1-Value))+0.5*Fish1;

      Value1=Value;
      Fish1=ExtBuffer0[i];
   }
   for( i=Bars;i>=0;i--){
          ExtBuffer1[i]=iMAOnArray(ExtBuffer0,0,MA_Period1,MA_Shift1,MA_Method1,i);
           }

return(0);
}
