//+------------------------------------------------------------------+
//|                                                       Trendi.mq4 |
//|                                                          Kalenzo |
//+------------------------------------------------------------------+
#property copyright "Kalenzo"
#property link      "bartlomiej.gorski@gmail.com"

#property indicator_separate_window
#property indicator_buffers 6
#property indicator_color1 Blue
#property indicator_color2 Green
#property indicator_color3 DodgerBlue
#property indicator_color4 BlueViolet
#property indicator_color5 SteelBlue
#property indicator_color6 DarkGray

//---- input parameters
extern int       T_Period=20;
double    Deviation=2.0;

double bolinger[];
double bolingerMA[];
double bolingerSMA[];
double trendi[];
double superTrendi[];
double unTrendi[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   SetIndexStyle(0, DRAW_LINE, EMPTY, 1);
   SetIndexStyle(1, DRAW_LINE, EMPTY, 1);
   SetIndexStyle(4, DRAW_LINE, STYLE_DOT, 1);
   
   SetIndexStyle(2, DRAW_ARROW, EMPTY, 0);
   SetIndexStyle(3, DRAW_ARROW, EMPTY, 0);
   SetIndexStyle(5, DRAW_ARROW, EMPTY, 0);
   
   
   SetIndexArrow(2,108);
   SetIndexArrow(3,108);
   SetIndexArrow(5,108);
   
   SetIndexLabel(0,"BBandWidthRatio");
   SetIndexLabel(0,"BB Moving average");
   
   SetIndexBuffer(0, bolinger);
   SetIndexBuffer(1,bolingerMA);   
   
   SetIndexBuffer(2, trendi);
   SetIndexBuffer(3, superTrendi);   
   SetIndexBuffer(5, unTrendi);   
   
   SetIndexBuffer(4, bolingerSMA);   
   
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int i, j;
   double ave, sko, sum;
   int counted_bars=IndicatorCounted();
   double MA, Up, Dn;
   
   if(Bars<=T_Period) return(0);
      
   i=Bars-T_Period;
   if(counted_bars>T_Period) i=Bars-counted_bars-1;
   

   if (Bars<=T_Period) return;
   
   for (i=Bars-T_Period; i>=0; i--) 
   {

    MA = iMA(NULL,0,T_Period,0,MODE_SMA,PRICE_CLOSE,i);
    sum = 0;

    for (j=0; j<T_Period; j++) sum+=Close[i+j];

    ave = sum / T_Period;
    sum = 0;

    for (j=0; j<T_Period; j++) sum+=(Close[i+j]-ave)*(Close[i+j]-ave);

    sko = MathSqrt(sum / T_Period);
    
    Up = MA+(Deviation*sko);
    Dn = MA-(Deviation*sko);
   
    bolinger[i] = (2*(Deviation*sko)/MA)-(Deviation*sko);
      
   
  
   }
   
   for(int bma = Bars-T_Period;bma>=0; bma--) bolingerMA[bma] = iMAOnArray(bolinger,0,10,0,MODE_EMA,bma);   
   
   for(int sma = Bars-T_Period;sma>=0; sma--) bolingerSMA[sma] = iMAOnArray(bolinger,0,144,0,MODE_EMA,sma);   
   
  
   for(int t = Bars-T_Period; t>=0; t--)
   {
      if(bolinger[t] >= bolingerMA[t] && bolinger[t] < bolingerSMA[t])
      {
         trendi[t] = 0;
         superTrendi[t] = EMPTY_VALUE;
      }
      else if(bolinger[t] >= bolingerMA[t] && bolinger[t] >= bolingerSMA[t])
      {
         trendi[t] = EMPTY_VALUE;
         superTrendi[t] = 0;
      }
      else
      {
         trendi[t] = EMPTY_VALUE;
         superTrendi[t] = EMPTY_VALUE;  
         unTrendi[t] = 0;
      }
      
   } 
  
   return(0);
  
  }
//+------------------------------------------------------------------+