//------------------------------------------------------------------
//
//------------------------------------------------------------------
#property copyright  ""
#property link       ""

#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 LimeGreen
#property indicator_color2 Red
#property indicator_width1 3
#property indicator_width2 3
#property indicator_width3 3
#import "user32.dll"
   int GetDC(int hwnd);
   int ReleaseDC(int hwnd,int hdc);
#import "gdi32.dll"
   color GetPixel(int hdc,int x,int y);

extern int PERIOD    =  10;
extern int AVERAGE   =  5;

double BullHBuffer[];
double BearHBuffer[];
double ZeroBuffer[];
double pa[];
double pa5[];
double rsv[];
double pak[];
double pad[];

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//

int init()
{
   if(PERIOD  < 2) PERIOD  = 2;
   if(AVERAGE < 2) AVERAGE = 2;
   
   IndicatorBuffers(8);
   SetIndexBuffer(0, BullHBuffer); SetIndexStyle(0,DRAW_HISTOGRAM); SetIndexLabel(0,"Bull High"); SetIndexEmptyValue(0,0);
   SetIndexBuffer(1, BearHBuffer); SetIndexStyle(1,DRAW_HISTOGRAM); SetIndexLabel(1,"Bear High"); SetIndexEmptyValue(1,0);       
   SetIndexBuffer(2, ZeroBuffer);  SetIndexStyle(2,DRAW_HISTOGRAM); SetIndexLabel(2,"Low");       SetIndexEmptyValue(2,0);
   SetIndexBuffer(3, pa);
   SetIndexBuffer(4, pa5);
   SetIndexBuffer(5, rsv);
   SetIndexBuffer(6, pak);
   SetIndexBuffer(7, pad);
   IndicatorDigits(2);
   IndicatorShortName("Winner (" + PERIOD + ", " + AVERAGE + ") ");
   return(0);
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//

int start()
{
   int k,counted_bars=IndicatorCounted();
      if(counted_bars<0) return(-1);
      if(counted_bars>0) counted_bars--;
         int limit = MathMin(Bars-counted_bars,Bars-1);
         int hwnd = WindowHandle(Symbol(),Period());
         int hdc  = GetDC(hwnd);
         color back_ground = GetPixel(hdc,1,2); ReleaseDC(hwnd,hdc);
            SetIndexStyle(2,DRAW_HISTOGRAM,0,EMPTY,back_ground);

   //
   //
   //
   //
   //
   
   for(int i=limit; i>=0; i--)
   {
      pa[i] = (2*Close[i]+High[i]+Low[i])/4;
         double scost5=0;   for(k=0; k < AVERAGE; k++) scost5   += Volume[i+k]*pa[i+k];
         double svolume5=0; for(k=0; k < AVERAGE; k++) svolume5 += Volume[i+k];
            if (svolume5==0) continue;   
      
         pa5[i] = scost5/svolume5;

            double min = pa5[ArrayMinimum(pa5,PERIOD,i)];
            double max = pa5[ArrayMaximum(pa5,PERIOD,i)];
      
         rsv[i] = ((pa5[i] - min)/MathMax(max-min,Point))*100;
   }
   for(i=limit; i>=0; i--)  pak[i] = iMAOnArray(rsv,0,AVERAGE,0,MODE_LWMA,i);
   for(i=limit; i>=0; i--)
   {
      pad[i] = iMAOnArray(pak,0,AVERAGE,0,MODE_LWMA,i);
         if(pak[i]>pad[i])
               { BullHBuffer[i] = pak[i]; ZeroBuffer[i]  = pad[i]; BearHBuffer[i] = 0; }
         else  { BearHBuffer[i] = pad[i]; ZeroBuffer[i]  = pak[i]; BullHBuffer[i] = 0; }
   }
   return(0);
}