//+------------------------------------------------------------------+
//|                                             KoliErBands_Indi.mq4 |
//|                                       Copyright 2010, KoliEr Li. |
//|                                                 http://kolier.li |
//+------------------------------------------------------------------+
#property copyright "Copyright 2010, KoliEr Li."
#property link      "http://kolier.li"

/*
 * I here get paid to program for you. Just $15 for all scripts.
 *
 * I am a bachelor major in Financial-Mathematics.
 * I am good at programming in MQL for Meta Trader 4 platform. The ability is between medium and top level.
 * No matter what it is, create or modify any indicators, expert advisors and scripts.
 * I will ask these jobs which are not too large, price from $15, surely refundable if you are not appreciate mine.
 * All products will deliver in 3 days.
 * Also, I am providing EA, Indicator and Trade System Improvement Consultant services, contact me for the detail.
 * If you need to have it done, don't hesitate to contact me at: kolier.li@gmail.com
 */
 
#property indicator_chart_window
#property indicator_buffers 7
#property indicator_color1 Lime
#property indicator_width1 2
#property indicator_color2 Green
#property indicator_width2 1
#property indicator_style2 STYLE_DOT
#property indicator_color3 Green
#property indicator_width3 1
#property indicator_style3 STYLE_DOT
#property indicator_color4 Green
#property indicator_width4 1
#property indicator_style4 STYLE_DOT
#property indicator_color5 Green
#property indicator_width5 1
#property indicator_style5 STYLE_DOT
#property indicator_color6 Green
#property indicator_width6 1
#property indicator_style6 STYLE_DOT
#property indicator_color7 Green
#property indicator_width7 1
#property indicator_style7 STYLE_DOT

//+------------------------------------------------------------------+
//| Universal Constants                                              |
//+------------------------------------------------------------------+
 
//+------------------------------------------------------------------+
//| User input variables                                             |
//+------------------------------------------------------------------+
extern string IndicatorName = "KoliErBands";  
extern int      BarsToCount = 0;    // Set to 0 to count all bars
int        KB_Period = 96;   // Peroid of KoliEr Bands
extern int     KB_Deviation1 = 1;    // Deviation Level
extern int     KB_Deviation2 = 2;
extern int     KB_Deviation3 = 3;

//+------------------------------------------------------------------+
//| Universal variables                                              |
//+------------------------------------------------------------------+
double buffer_kb[], buffer_upper1[], buffer_lower1[], buffer_upper2[], buffer_lower2[],
       buffer_upper3[], buffer_lower3[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   IndicatorShortName(IndicatorName);
   SetIndexBuffer(0, buffer_kb);
   SetIndexStyle(0, DRAW_LINE);
   SetIndexLabel(0, "SB");
   SetIndexDrawBegin(0, KB_Period);
   SetIndexBuffer(1, buffer_upper1);
   SetIndexStyle(1, DRAW_LINE);
   SetIndexLabel(1, "Upper");
   SetIndexDrawBegin(1, KB_Period);
   SetIndexBuffer(2, buffer_lower1);
   SetIndexStyle(2, DRAW_LINE);
   SetIndexLabel(2, "Lower");
   SetIndexDrawBegin(2, KB_Period);
      SetIndexBuffer(3, buffer_upper2);
   SetIndexStyle(3, DRAW_LINE);
   SetIndexLabel(3, "Upper2");
   SetIndexDrawBegin(3, KB_Period);
      SetIndexBuffer(4, buffer_lower2);
   SetIndexStyle(4, DRAW_LINE);
   SetIndexLabel(4, "Lower2");
   SetIndexDrawBegin(4, KB_Period);
         SetIndexBuffer(5, buffer_upper3);
   SetIndexStyle(5, DRAW_LINE);
   SetIndexLabel(5, "Upper2");
   SetIndexDrawBegin(5, KB_Period);
      SetIndexBuffer(6, buffer_lower3);
   SetIndexStyle(6, DRAW_LINE);
   SetIndexLabel(6, "Lower2");
   SetIndexDrawBegin(6, KB_Period);
   
   

   
   
   return(0);
  }
  
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }
  
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int bars_counted = IndicatorCounted();
    int ActivePer=Period();
   double Per24H=1440/ActivePer;
   if(bars_counted < 0) {
      return(1);
   }
   else if(bars_counted > 0) {
      bars_counted--;
   }
   int limit = Bars - bars_counted;
   if(BarsToCount>0 && limit>BarsToCount) {
      limit = BarsToCount;
   }
   
   int i, k;
   double sum, val_old, val_new, deviation1,deviation2,deviation3;
   for(i=limit; i>=0; i--) {
     // buffer_kb[i] = (High[iHighest(Symbol(),0,MODE_HIGH,Per24H,i)] + Low[iLowest(Symbol(),0,MODE_LOW,Per24H,i)])/2;
      buffer_kb[i] = ((High[iHighest(Symbol(),0,MODE_HIGH,Per24H,i)]+ iMA(NULL, 0, Per24H, 0, MODE_SMA, PRICE_CLOSE, i))/2+
    (Low[iLowest(Symbol(),0,MODE_LOW,Per24H,i)]+ iMA(NULL, 0, Per24H, 0, MODE_SMA, PRICE_CLOSE, i))/2)/2;
   }
   
   for(i=limit; i>=0; i--) {
      sum = 0;
      k = i + Per24H - 1;
      val_old = buffer_kb[i];
      while(k>=i) {
         val_new = Close[k] - val_old;
         sum += val_new*val_new;
         k--;
      }
      deviation1 = KB_Deviation1 * MathSqrt(sum/Per24H);
      deviation2 = KB_Deviation2 * MathSqrt(sum/Per24H);
      deviation3 = KB_Deviation3 * MathSqrt(sum/Per24H);
      buffer_upper1[i] = buffer_kb[i] + deviation1;
      buffer_lower1[i] = buffer_kb[i] - deviation1;
      
      buffer_upper2[i] = buffer_kb[i] + deviation2;
      buffer_lower2[i] = buffer_kb[i] - deviation2;
      
      buffer_upper3[i] = buffer_kb[i] + deviation3;
      buffer_lower3[i] = buffer_kb[i] - deviation3;
   }
   
   return(0);
  }
  

