//+------------------------------------------------------------------+
//|                                                kiosotto SELL.mq4 |
//|                                   Copyright 2014, Masakazu Corp. |
//|                                          http://www.masakazu.com |
//+------------------------------------------------------------------+
#property indicator_separate_window
#property indicator_buffers 5

#property indicator_level1  25
#property indicator_level2 -25

#property indicator_color1 clrSteelBlue
#property indicator_color2 clrBrown
#property indicator_color3 clrLime
#property indicator_color4 clrOrange
#property indicator_color5 clrOrange

extern int       dev_period    = 150;
extern int       bars          = 1000;
extern int       RSIPeriod     = 21;
extern int       BandPeriod    = 84;
extern double    BandDeviation = 2;

double ExtMapBuffer1[];
double ExtMapBuffer2[];
double RSIBuf[],UpZone[],DnZone[];

int init()
  {
   SetIndexStyle(0,DRAW_HISTOGRAM,EMPTY,1);
   SetIndexBuffer(0,ExtMapBuffer1);

   SetIndexStyle(1,DRAW_HISTOGRAM,EMPTY,1);
   SetIndexBuffer(1,ExtMapBuffer2);
   
   SetIndexBuffer(2,RSIBuf);
   SetIndexBuffer(3,UpZone);
   SetIndexBuffer(4,DnZone); 
   
   SetIndexStyle (2,DRAW_LINE);
   SetIndexStyle (3,DRAW_LINE);
   SetIndexStyle (4,DRAW_LINE); 
   
   
   return(0);
  }

int start()
  {
  { 
   int counted_bars=IndicatorCounted();
   int i,j,limit;
   double hpres, lpres, TSBUL, TSBER, sbl, sbr;
   limit=Bars-counted_bars;
   j=0;
   if (limit> bars)  limit=bars;
   for(i = limit; i >= 0; i--) 
   {
      j = 0;
      TSBUL = 0;
      TSBER = 0;
      hpres =0;
      lpres = 9999999;
      RSIBuf[i] = iRSI(NULL,0,RSIPeriod,PRICE_WEIGHTED,i)-50;   
      
   
      while (j<dev_period)   
      {
          sbl = 0;
          sbr = 0;
         {
            int shift=i+j; 
            datetime date = iTime(Symbol(), 0, shift); 
            int bsht = iBarShift(Symbol(), Period(), date, false);
            int kolichestvo = bsht-MathRound(Period()/Period());
            if (kolichestvo < 0 ) {kolichestvo=0;}
            for (int n=bsht;n>=kolichestvo;n--) 
            {
               double ii = iRSI(NULL,0,dev_period,PRICE_CLOSE,n);
               
               double hnw = iHigh(Symbol(), Period(), n);
               double lnw = iLow(Symbol(), Period(), n);
               double cle = iClose(Symbol(), Period(), n);
               
                             
                             
               if (hnw> hpres)
               {
                  hpres = hnw;
                  sbl = sbl + ii*cle;
               }
               if (lpres> lnw)
               {
                  lpres = lnw;
                  sbr = sbr + ii*cle;
               }               
              }
              
             
                          
             }
                 TSBUL = TSBUL + sbl ;
                 TSBER = TSBER + sbr ;
         
               j++;
           }

          if ((TSBER/TSBUL)>(TSBUL/TSBER))  
          ExtMapBuffer1[i] = TSBER/TSBUL;
          if ((TSBER/TSBUL)<(TSBUL/TSBER))
          ExtMapBuffer2[i] = -TSBUL/TSBER;
          
       }
    for(i = limit; i >= 0; i--) 
     {
      double ma  = iMAOnArray(RSIBuf,0,BandPeriod,0,MODE_SMA,i);
      double dev = iStdDevOnArray(RSIBuf,0,BandPeriod,0,MODE_SMA,i);
      UpZone[i] = ma + BandDeviation * dev;
      DnZone[i] = ma - BandDeviation * dev;  
     } 
 }
    return(0);
}