//+------------------------------------------------------------------+
//|                                               KG Daily Level.mq4 |
//|                                                  Copyright © 2008|
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008"
#property link      "www.freedom.net"
//----
#property indicator_chart_window
#property indicator_buffers 8
#property indicator_color1 Lime
#property indicator_color2 Lime
#property indicator_color3 Lime
#property indicator_color4 Lime
#property indicator_color5 Lime
#property indicator_color6 Lime
#property indicator_color7 Lime
#property indicator_color8 Lime

//---- buffers
double P1Buffer[];
double P2Buffer[];
double P3Buffer[];
double P4Buffer[];
double P5Buffer[];
double P6Buffer[];
double P7Buffer[];
double P8Buffer[];
//----
extern int myPeriod = PERIOD_D1;
extern double SD_1 = 1;
extern double SD_2 = 2;
extern double SD_3 = 3;
extern double SD_4 = 4;
//----
double H1, L1, H2, L2, H3, L3, H4, L4, Q;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   SetIndexBuffer(0, P1Buffer);
   SetIndexBuffer(1, P2Buffer);
   SetIndexBuffer(2, P3Buffer);
   SetIndexBuffer(3, P4Buffer);
   SetIndexBuffer(4, P5Buffer);
   SetIndexBuffer(5, P6Buffer);
   SetIndexBuffer(6, P7Buffer);
   SetIndexBuffer(7, P8Buffer);
//----
   SetIndexStyle(0, DRAW_LINE, STYLE_DOT);
   SetIndexStyle(1, DRAW_LINE, STYLE_DOT);
   SetIndexStyle(2, DRAW_LINE, STYLE_DOT);
   SetIndexStyle(3, DRAW_LINE, STYLE_DOT);
   SetIndexStyle(4, DRAW_LINE, STYLE_DOT);
   SetIndexStyle(5, DRAW_LINE, STYLE_DOT);
   SetIndexStyle(6, DRAW_LINE, STYLE_DOT);
   SetIndexStyle(7, DRAW_LINE, STYLE_DOT);
//----
   
   return(0);
  }
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
   ObjectDelete("HSD_0.5");
   ObjectDelete("HSD_1.0");
   ObjectDelete("HSD_1.5");
   ObjectDelete("HSD_2.0");

   ObjectDelete("LSD_0.5");
   ObjectDelete("LSD_1.0");
   ObjectDelete("LSD_1.5");
   ObjectDelete("LSD_2.0");

   ObjectDelete("txtLSD_0.5");
   ObjectDelete("txtLSD_1.0");
   ObjectDelete("txtLSD_1.5");
   ObjectDelete("txtLSD_2.0");

   ObjectDelete("txtHSD_0.5");
   ObjectDelete("txtHSD_1.0");
   ObjectDelete("txtHSD_1.5");
   ObjectDelete("txtHSD_2.0");
   Comment("Kg Standard Deviation Level");
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int i, dayi, counted_bars = IndicatorCounted();
   double L,H,O,C,S,P2,P3,P4,P5,PA;
//---- check for possible errors
   if(counted_bars < 0) 
       return(-1);
//---- last counted bar will be recounted
   if(counted_bars > 0) 
       counted_bars--;  
   int limit = Bars - counted_bars;
//----   
   for(i = limit - 1; i >= 0; i--)
     {
       dayi = iBarShift(Symbol(), myPeriod, Time[i], false);
       Q = (iHigh(Symbol(), myPeriod,dayi+1) - iLow(Symbol(),myPeriod,dayi+1));
            
         L = iLow(NULL,myPeriod,dayi+1);
         H = iHigh(NULL,myPeriod,dayi+1);
         O = iOpen(NULL,myPeriod,dayi+1);
         C = iClose(NULL,myPeriod,dayi+1);
            
         // Deviations
         S = MathPow(C-((C+C+L+H)/4),2)+MathPow(C-((C+C+L+H)/4),2)+MathPow(L-((C+C+L+H)/4),2)+MathPow(H-((C+C+L+H)/4),2);
         //sum+=S*S;
            P5=SD_4*MathSqrt(S/3);
            P4=SD_3*MathSqrt(S/3);
            P3=SD_2*MathSqrt(S/3);
            P2=SD_1*MathSqrt(S/3);

         PA = (C+C+L+H)/4;
	        
            
       //----
       H1 = PA + P2;
       L1 = PA - P2;
       //----
       H2 = PA + P3;
       L2 = PA - P3;
       //----
       H3 = PA + P4;
       L3 = PA - P4;
       //----
       H4 = PA + P5;
       L4 = PA - P5;
       //----
       P1Buffer[i] = H1;
       SetPrice("HSD_0.5", Time[i], H1, Lime);
       SetText("txtHSD_0.5", DoubleToStr(SD_1,1), Time[i], H1, Lime);
       //----
       P2Buffer[i] = L1;
       SetPrice("LSD_0.5", Time[i], L1, Lime);
       SetText("txtLSD_0.5", DoubleToStr(SD_1,1), Time[i], L1, Lime);
       //----
       P3Buffer[i] = H2;
       SetPrice("HSD_1.0", Time[i], H2, Lime);
       SetText("txtHSD_1.0", DoubleToStr(SD_2,1), Time[i], H2, Lime);
       //----
       P4Buffer[i] = L2;
       SetPrice("LSD_1.0",Time[i],L2,Lime);
       SetText("txtLSD_1.0",DoubleToStr(SD_2,1),Time[i],L2,Lime);
       //----
       P5Buffer[i] = H3;
       SetPrice("HSD_1.5", Time[i], H3, Lime);
       SetText("txtHSD_1.5", DoubleToStr(SD_3,1), Time[i], H3, Lime);
       //----
       P6Buffer[i] = L3;
       SetPrice("LSD_1.5", Time[i], L3, Lime);
       SetText("txtLSD_1.5", DoubleToStr(SD_3,1), Time[i], L3, Lime);
       //----
       P7Buffer[i] = H4;
       SetPrice("HSD_2.0", Time[i], H4, Lime);
       SetText("txtHSD_2.0", DoubleToStr(SD_4,1), Time[i], H4, Lime);
       //----
       P8Buffer[i] = L4;
       SetPrice("LSD_2.0", Time[i], L4, Lime);
       SetText("txtLSD_2.0", DoubleToStr(SD_4,1), Time[i], L4, Lime);

    }
//----
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void SetPrice(string name, datetime Tm, double Prc, color clr)
  {
   if(ObjectFind(name) == -1)
     {
       ObjectCreate(name, OBJ_ARROW, 0, Tm, Prc);
       ObjectSet(name, OBJPROP_COLOR, clr);
       ObjectSet(name, OBJPROP_WIDTH, 1);
       ObjectSet(name, OBJPROP_ARROWCODE, SYMBOL_RIGHTPRICE);
     }
   else
     {
       ObjectSet(name, OBJPROP_TIME1, Tm);
       ObjectSet(name, OBJPROP_PRICE1, Prc);
       ObjectSet(name, OBJPROP_COLOR, clr);
       ObjectSet(name, OBJPROP_WIDTH, 1);
       ObjectSet(name, OBJPROP_ARROWCODE, SYMBOL_RIGHTPRICE);
     } 
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void SetText(string name, string txt, datetime Tm, double Prc, color clr)
  {
   if(ObjectFind(name) == -1)
     {
       ObjectCreate(name, OBJ_TEXT, 0, Tm, Prc);
       ObjectSetText(name, txt, 7, "Times New Roman", clr);
     }
   else
     {
       ObjectSet(name, OBJPROP_TIME1, Tm);
       ObjectSet(name, OBJPROP_PRICE1, Prc);
       ObjectSetText(name, txt, 7, "Times New Roman", clr);
     } 
  }
//+------------------------------------------------------------------+