//+---------------------------------+
//BS LSMA Daily MTF                 |
//Copyright © KG 2009               | 
//Opreked by wiedstone@yahoo.com    |
//+---------------------------------+

#property indicator_chart_window
#property indicator_buffers 7
#property indicator_color1 Lime
#property indicator_width1 1
#property indicator_color2 OliveDrab
#property indicator_color3 OliveDrab
#property indicator_color4 OliveDrab
#property indicator_color5 OliveDrab
#property indicator_color6 OliveDrab
#property indicator_color7 OliveDrab

extern int    MA_Period      = 24;
extern int    Applied_Price  = PRICE_CLOSE;
extern int    shift          = 0;
extern double SD1            = 0.25;
extern double SD2            = 0.5;
extern double SD3            = 0.75;
extern bool Show_SD          = true;
extern bool Show_Price_LSMA  = false;
extern bool Show_Price_Level = false;
string line_name             = "BS_LSMA_Daily";
double buf_0[],buf_1[],buf_2[],buf_3[],buf_4[],buf_5[],buf_6[];

int TimeFrame = PERIOD_D1;

double line_SD1_upper,line_SD1_lower,line_SD2_upper,line_SD2_lower,line_SD3_upper,line_SD3_lower;

int init() {
   getPeriod();
   
   SetIndexBuffer(0, buf_0);
   SetIndexBuffer(1, buf_1);
   SetIndexBuffer(2, buf_2);
   SetIndexBuffer(3, buf_3);
   SetIndexBuffer(4, buf_4);
   SetIndexBuffer(5, buf_5);
   SetIndexBuffer(6, buf_6);
   
   SetIndexStyle(0, DRAW_LINE);
    if(!Show_SD)
    {  SetIndexStyle(1, DRAW_NONE);
       SetIndexStyle(2, DRAW_NONE);
       SetIndexStyle(3, DRAW_NONE);
       SetIndexStyle(4, DRAW_NONE);
       SetIndexStyle(5, DRAW_NONE);
       SetIndexStyle(6, DRAW_NONE);
    }
    if(Show_SD)
    {  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_SOLID);
       SetIndexStyle(6, DRAW_LINE, STYLE_SOLID);
    }
   return (0);
}

int deinit() {
//----
   for(int i = ObjectsTotal() - 1; i >= 0; i--)
     {
       string label = ObjectName(i);
       if(StringFind(label, line_name, 0) < 0)
           continue;
       ObjectDelete(label);
     }   
//----
   return (0);
}

int start()
{
   int shift;
   double L;
   double H;
   double avg;
   double StdDev1, StdDev2, StdDev3;
   double HL;
   int counted_bars= IndicatorCounted();
   if (counted_bars< 0) return (-1);
   if (counted_bars> 0) counted_bars--;
   int limit = Bars - counted_bars;
   for (int i = limit - 1; i >= 0; i--) 
   {
      shift = iBarShift(Symbol(), TimeFrame, Time[i], FALSE);
      L = iLow(NULL, TimeFrame, shift+1);
      H = iHigh(NULL, TimeFrame, shift+1);
      avg= MathPow(L - (L + H) / 2.0, 2) + MathPow(H - (L + H) / 2.0, 2);
      StdDev1= SD1* MathSqrt(avg/ 2.0);
      StdDev2= SD2* MathSqrt(avg/ 2.0);
      StdDev3= SD3* MathSqrt(avg/ 2.0);
      HL= 3.0*iMA(NULL,0,MA_Period,0,MODE_LWMA,Applied_Price,i)-2.0*iMA(NULL,0,MA_Period,0,MODE_SMA,Applied_Price,i);
      line_SD1_upper = HL+ StdDev1;
      line_SD1_lower = HL- StdDev1;
      line_SD2_upper = HL+ StdDev2;
      line_SD2_lower = HL- StdDev2;
      line_SD3_upper = HL+ StdDev3;
      line_SD3_lower = HL- StdDev3;
      
      buf_0[i] = HL;
      if(Show_Price_LSMA) { SetPrice(line_name+"  Prc", Time[i], HL, indicator_color1);
                            SetText(line_name+"  Txt", "LSMA Daily Level", Time[i], HL, indicator_color1);
                          }
      buf_1[i] = line_SD1_upper;
      if(Show_Price_Level) { SetPrice(line_name+"  SD+1  Prc", Time[i], line_SD1_upper, indicator_color2);
                             SetText(line_name+"  SD+1 Txt", "SD+0.5", Time[i], line_SD1_upper, indicator_color2);
                           }
      buf_2[i] = line_SD1_lower ;
      if(Show_Price_Level) { SetPrice(line_name+"  SD-1 Prc", Time[i], line_SD1_lower, indicator_color3);
                            SetText(line_name+"  SD-1 Txt", "SD-0.5", Time[i], line_SD1_lower, indicator_color3);
                           }
      buf_3[i] = line_SD2_upper ;
      if(Show_Price_Level) { SetPrice(line_name+"  SD+2 Prc", Time[i], line_SD2_upper, indicator_color4);
                             SetText(line_name+"  SD+2 Txt", "SD+1.0", Time[i], line_SD2_upper, indicator_color4);
                           }
      buf_4[i] =line_SD2_lower;
      if(Show_Price_Level) { SetPrice(line_name+"  SD-2 Prc", Time[i], line_SD2_lower, indicator_color5);
                             SetText(line_name+"  SD-2 Txt", "SD-1.0", Time[i], line_SD2_lower , indicator_color5);
                           }
      buf_5[i] = line_SD3_upper ;
      if(Show_Price_Level) { SetPrice(line_name+"  SD+3 Prc", Time[i], line_SD3_upper, indicator_color6);
                             SetText(line_name+"  SD+3 Txt", "SD+1.5", Time[i], line_SD3_upper, indicator_color6);
                           }
      buf_6[i] =line_SD3_lower;
      if(Show_Price_Level) { SetPrice(line_name+"  SD-3 Prc", Time[i], line_SD3_lower, indicator_color7);
                             SetText(line_name+"  SD-3 Txt", "SD-1.5", Time[i], line_SD3_lower , indicator_color7);
                           }
   }
   return (0);
}

void SetPrice(string name, int datetime_0, double price_0, color color_0) {
   if (ObjectFind(name) == -1) {
      ObjectCreate(name, OBJ_ARROW, 0, datetime_0, price_0);
      ObjectSet(name, OBJPROP_COLOR, color_0);
      ObjectSet(name, OBJPROP_WIDTH, 1);
      ObjectSet(name, OBJPROP_ARROWCODE, SYMBOL_RIGHTPRICE);
      return;
   }
   ObjectSet(name, OBJPROP_TIME1, datetime_0);
   ObjectSet(name, OBJPROP_PRICE1, price_0);
   ObjectSet(name, OBJPROP_COLOR, color_0);
   ObjectSet(name, OBJPROP_WIDTH, 1);
   ObjectSet(name, OBJPROP_ARROWCODE, SYMBOL_RIGHTPRICE);
}

void SetText(string name, string text, int datetime_1, double price_1, color color_1) {
   if (ObjectFind(name) == -1) {
      ObjectCreate(name, OBJ_TEXT, 0, datetime_1, price_1);
      ObjectSetText(name, text, 7, " Tahoma", color_1);
      return;
   }
   ObjectSet(name, OBJPROP_TIME1, datetime_1);
   ObjectSet(name, OBJPROP_PRICE1, price_1);
   ObjectSetText(name, text, 7, "Tahoma", color_1);
}
void getPeriod() {
   switch (Period()) {
   case PERIOD_M1:  MA_Period = 1440;return;
   case PERIOD_M5:  MA_Period = 288;return;
   case PERIOD_M15: MA_Period = 96;return;
   case PERIOD_M30: MA_Period = 48;return;
   case PERIOD_H1:  MA_Period = 24;return;
   case PERIOD_H4:  MA_Period = 6;return;
   case PERIOD_D1:  MA_Period = 0;return;
   case PERIOD_W1:  MA_Period = 0;return;
   case PERIOD_MN1: MA_Period = 0;return;
   }
}