//+------------------------------------------------------------------+
//|                                                 Juice_mod_v1.mq4 |
//|                           Copyright © 2006, TrendLaboratory Ltd. |
//|            http://finance.groups.yahoo.com/group/TrendLaboratory |
//|                                       E-mail: igorad2004@list.ru |
//|                                            Thanks Perky for Idea.|
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, TrendLaboratory Ltd."
#property link      "http://finance.groups.yahoo.com/group/TrendLaboratory"
//---- indicator settings
#property  indicator_separate_window
#property  indicator_buffers 2
#property  indicator_color1  LimeGreen
#property  indicator_color2  FireBrick
//---- indicator parameters
extern int    Length=7;
extern double Ks=0.618;
extern int    CalcBars=144;

//---- indicator buffers

double UpBuffer[];
double DnBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {

//---- drawing settings
   SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,2);
   SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID,1);

   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+2);
//---- 3 indicator buffers mapping
   SetIndexBuffer(0,UpBuffer);
   SetIndexBuffer(1,DnBuffer);
   
//---- name for DataWindow and indicator subwindow label
   IndicatorShortName("Juice mod v1 ("+Length+","+Ks+")");
//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//| Juice_mod_v1                                                     |
//+------------------------------------------------------------------+
int start()
{

   int limit,i;
   int counted_bars=IndicatorCounted();
//---- check for possible errors
   if(counted_bars<0) return(-1);
//---- last counted bar will be recounted
   if(counted_bars<1)
   for(i=1;i<=Length;i++) 
   {UpBuffer[Bars-i]=0.0;DnBuffer[Bars-i]=0.0;}
     
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars-Length;
 
//---- main loop
   double sum=0;
   if (CalcBars==0) int NBars=Bars-Length; else NBars=CalcBars;
   for(i=0; i<=NBars-1; i++) sum+=iStdDev(NULL,0,Length,MODE_EMA,0,PRICE_CLOSE,i);
   
   double avg=sum/NBars;
   
   for(i=0; i<=limit; i++)
   {
        
      double Juice=iStdDev(NULL,0,Length,MODE_EMA,0,PRICE_CLOSE,i)-Ks*avg;
      
      if(Juice > 0)
      {
         UpBuffer[i]=Juice;
         DnBuffer[i]=0;
      }
      else if(Juice<0) 
      {
         DnBuffer[i]=Juice;
         UpBuffer[i]=0;
      } else
      {
         DnBuffer[i]=0;
         UpBuffer[i]=0;
      }
   }
//---- done
   return(0);
  }
//+------------------------------------------------------------------+

