/*
*********************************************************************
          
                      Bollinger Squeeze v8
                  Original code by Nick Bilak
                   Modifications by Akuma99
                   Copyright © 2006-07  Akuma99
                  http://www.beginnertrader.com  

       For help on this indicator, tutorials and information 
               visit http://www.beginnertrader.com 
                  
*********************************************************************
*/

#property copyright "Copyright © 2006-07, Akuma99"
#property link      "http://www.beginnertrader.com "

#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 Green //uptrend
#property indicator_width1 2  
#property indicator_color2 Red  //downtrend
#property indicator_width2 2  
#property indicator_color3 Yellow  //flat
#property indicator_width3 2  
  


extern string ma0 = "Moving Average Modes";
extern string ma1 = " 0=sma, 1=ema, 2=smma";
extern string ma2 = " 3=lwma, 4=LSMA";
extern int FastMA_Mode = 1;          //0=sma, 1=ema, 2=smma, 3=lwma, 4=LSMA
extern int FastMA_Period =   15;
extern string  p = "--Applied Price Types--";
extern string  p0 = " 0 = close";
extern string  p1 = " 1 = open";
extern string  p2 = " 2 = high";
extern string  p3 = " 3 = low";
extern string  p4 = " 4 = median(high+low)/2";
extern string  p5 = " 5 = typical(high+low+close)/3";
extern string  p6 = " 6 = weighted(high+low+close+close)/4";
extern int FastMA_AppliedPrice = 0;  // 0=close, 1=open, 2=high, 3=low, 4=median((h+l/2)), 5=typical((h+l+c)/3), 6=weighted((h+l+c+c)/4)
extern int SlowMA_Mode = 1;           //0=sma, 1=ema, 2=smma, 3=lwma, 4=LSMA
extern int SlowMA_Period =   45;
extern int SlowMA_AppliedPrice = 0;  // 0=close, 1=open, 2=high, 3=low, 4=median((h+l/2)), 5=typical((h+l+c)/3), 6=weighted((h+l+c+c)/4)
extern string sp0 = "How many pips separation between MAs";
extern string sp1 = " for entry signal";
extern int Fast_SlowSpreadEntry = 9;        // How many pips separation between MAs for signal

double upB[];
double loB[];
double flatB[];

double   myPoint;

//+------------------------------------------------------------------+
int init() {

//   IndicatorBuffers(3);
   
   SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,indicator_width1,indicator_color1);
   SetIndexBuffer(0,upB);
   SetIndexEmptyValue(0,EMPTY_VALUE);
   
   SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID,indicator_width2,indicator_color2); //downtrend
   SetIndexBuffer(1,loB);
   SetIndexEmptyValue(1,EMPTY_VALUE);
   
   SetIndexStyle(2,DRAW_HISTOGRAM,STYLE_SOLID,indicator_width3,indicator_color3); //flat
   SetIndexBuffer(2,flatB);
   SetIndexEmptyValue(2,EMPTY_VALUE);

   IndicatorShortName("MA_Dif" + "(" + FastMA_Period + "," + SlowMA_Period +  ") Histogram" );
   myPoint = SetPoint(Symbol());

   return(0);
}
//+------------------------------------------------------------------+
int deinit() {
   
   
}

double SetPoint(string mySymbol)
{
   double mPoint, myDigits;
   
   myDigits = MarketInfo (mySymbol, MODE_DIGITS);
   if (myDigits < 4)
      mPoint = 0.01;
   else
      mPoint = 0.0001;
   
   return(mPoint);
}

double LSMA(int TimeFrame, int LSMAPeriod, int LSMAPrice,int shift)
{
   double wt;
   
   double ma1=iMA(NULL,TimeFrame,LSMAPeriod,0,MODE_SMA ,LSMAPrice,shift);
   double ma2=iMA(NULL,TimeFrame,LSMAPeriod,0,MODE_LWMA,LSMAPrice,shift);
   wt = MathFloor((3.0*ma2-2.0*ma1)/myPoint)*myPoint;
   return(wt);
}  

double GetMA_Dif(int pos)
{
   double fMA, sMA;
   double dif;
   
   if (FastMA_Mode == 4)
   {
     fMA = LSMA(0,FastMA_Period,FastMA_AppliedPrice,pos);
   }
   else
   {
     fMA = iMA(NULL, 0, FastMA_Period, 0, FastMA_Mode, FastMA_AppliedPrice, pos);
   }
   if (SlowMA_Mode == 4)
   {
     sMA = LSMA(0,SlowMA_Period,SlowMA_AppliedPrice,pos);
   }
   else
   {
     sMA = iMA(NULL, 0, SlowMA_Period, 0, SlowMA_Mode, SlowMA_AppliedPrice, pos);
   }
   
   dif = fMA-sMA;
   
   return(dif);
}
   
//+------------------------------------------------------------------+
int start() {

   int counted_bars=IndicatorCounted();
   int shift,limit;
   double dif;
   
   if (counted_bars<0) return(-1);
   if (counted_bars>0) counted_bars--;
  
   limit=Bars-counted_bars;

   for (shift=limit;shift>=0;shift--)
   {
      
     dif = GetMA_Dif(shift)/myPoint;
   
     if (dif > Fast_SlowSpreadEntry) {
         
         upB[shift]=dif;
         
      } else if (dif < -1.0 * Fast_SlowSpreadEntry) {
      
         loB[shift]=dif;
         
      }
      else
         flatB[shift]= dif;
   }
   
   return(0);

}
//+------------------------------------------------------------------+

