//+------------------------------------------------------------------+
//|                                                          Sys.mq4 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      ""
#property version   "1.30"
#property strict
#property indicator_chart_window
#property indicator_buffers 6

// ---- buffers ----
double BuffLoCh[];
double BuffHiCh[];
double BuffDelta[];
double BuffExt[];
double BuffOrd[];
double BuffOpen[];

extern int    History = 700;  
extern double Tprice = 20;
extern double Kvolume = 0.2;
static datetime prevtime = 0;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void OnInit(void)
  {
   IndicatorDigits(Digits);

   SetIndexBuffer(0, BuffLoCh);
   SetIndexBuffer(1, BuffHiCh);
   SetIndexBuffer(2, BuffDelta);
   SetIndexBuffer(3, BuffExt);
   SetIndexBuffer(4, BuffOrd);
   SetIndexBuffer(5, BuffOpen);
   
   SetIndexStyle(0,DRAW_NONE);
   SetIndexStyle(1,DRAW_NONE);
   SetIndexStyle(2,DRAW_NONE);
   SetIndexStyle(3,DRAW_NONE);
   SetIndexStyle(4,DRAW_NONE);
   SetIndexStyle(5,DRAW_LINE,STYLE_SOLID,2,clrBlue);
  }
//+------------------------------------------------------------------+
//| 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[])
  {
//--------------------------------------------------------------------
   if(prevtime == Time[0]) return(0);
   prevtime = Time[0];
    
   BuffLoCh[rates_total-1] = Close[rates_total-1];
   BuffHiCh[rates_total-1] = Close[rates_total-1];
   BuffDelta[rates_total-1] = 0;
   BuffExt[History + 1] = 0;
   
   int i;
   if((rates_total - prev_calculated) == 1) i = 1;
   else i = rates_total - 2;
   
   for(int n = i; n >=1; n--)
   {
     BuffLoCh[n] = NormalizeDouble(BuffLoCh[n+1] + BuffDelta[n+1]/Tprice + MathMin(0,(close[n]-BuffLoCh[n+1])), Digits);
     BuffHiCh[n] = NormalizeDouble(BuffHiCh[n+1] - BuffDelta[n+1]/Tprice + MathMax(0,(close[n]-BuffHiCh[n+1])), Digits);
     BuffDelta[n] = NormalizeDouble(BuffHiCh[n] - BuffLoCh[n], Digits);
     if (n <= History)
     {
         if (MathMin(BuffLoCh[n],MathMin(BuffLoCh[n+1],BuffLoCh[n+2])) == BuffLoCh[n+1]) BuffExt[n] = 1;
         else if (MathMax(BuffHiCh[n],MathMax(BuffHiCh[n+1],BuffHiCh[n+2])) == BuffHiCh[n+1]) BuffExt[n] = -1;
              else BuffExt[n] = BuffExt[n+1];
     
         double Average = 0;
         double S = 0;
         double S2 = 0;
         double SO = 0;
         
         for(int x = n + History - 1; x >= n; x--)S = S + tick_volume[x]; 
         Average = NormalizeDouble(S/History, 0);
         for(int x = n + History - 1; x >= n; x--)S2 = S2 + MathPow(tick_volume[x]-Average,2); 
         SO = NormalizeDouble(MathPow(S2/History,0.5), 0);
         
         if(tick_volume[n] >= (Average+SO)*Kvolume) BuffOrd[n] = BuffExt[n];
         else BuffOrd[n] = BuffOrd[n+1];
         if (BuffOrd[n] == BuffOrd[n+1]) BuffOpen[n] = BuffOpen[n+1];
         else BuffOpen[n] = close[n];
     }
    }
   return(rates_total);
  }
//+------------------------------------------------------------------+
