//+------------------------------------------------------------------+
//|                                                 tc_envelopes.mq4 |
//|                                   Copyright © 2010, Thomas Liles |
//|                                       http://www.trendchaser.org |
//|                        need something coded? tomhliles@yahoo.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, Thomas Liles"
#property link      "http://www.trendchaser.org"
#property indicator_chart_window
#property indicator_buffers 6
#property indicator_color1 DeepSkyBlue
#property indicator_color2 DeepSkyBlue
#property indicator_color3 Orange
#property indicator_color4 Orange
#property indicator_color5 Gray
#property indicator_color6 Gray
//+------------------------------------------------------------------+
extern int    TimeFrame           = 0;
extern int    Envelopes_Period    = 14;
extern int    Envelopes_Method    = 0;
extern int    Envelopes_Price     = 0;    
extern int    Envelopes_Shift     = 0; 
extern double Envelopes_Deviation = 0.10;
extern bool   play_sound    = false; 
extern bool   popup    = false;
extern bool   mail    = false;
extern bool   show_lines    = false; 
extern color  buy_color    = DodgerBlue; 
extern color  sell_color    = Orange; 
bool long=true,short=true;
//+------------------------------------------------------------------+
static double EnvUpper[];
static double EnvLower[];
static double EnvUpper2[];
static double EnvLower2[];
static double EnvUpper3[];
static double EnvLower3[];
//-------------------------------------------------------------------+
int init()
  {
   IndicatorShortName("envelopes");
   SetIndexStyle(0,DRAW_LINE);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexStyle(2,DRAW_LINE);
   SetIndexStyle(3,DRAW_LINE);
   SetIndexStyle(4,DRAW_LINE);
   SetIndexStyle(5,DRAW_LINE);
   SetIndexBuffer(0,EnvUpper);
   SetIndexBuffer(1,EnvLower);
   SetIndexBuffer(2,EnvUpper2);
   SetIndexBuffer(3,EnvLower2);
   SetIndexBuffer(4,EnvUpper3);
   SetIndexBuffer(5,EnvLower3);
   SetIndexLabel(0,"EnvUpper");
   SetIndexLabel(1,"EnvLower");
   SetIndexLabel(2,"EnvUpper2");
   SetIndexLabel(3,"EnvLower2");
   SetIndexLabel(4,"EnvUpper3");
   SetIndexLabel(5,"EnvLower3");
   SetIndexDrawBegin(0,Envelopes_Period);
   SetIndexDrawBegin(1,Envelopes_Period);
   SetIndexDrawBegin(2,Envelopes_Period);
   SetIndexDrawBegin(3,Envelopes_Period);
   SetIndexDrawBegin(4,Envelopes_Period);
   SetIndexDrawBegin(5,Envelopes_Period);
   return(0);
  }
//-------------------------------------------------------------------+
int deinit()
  {
for(int i=Bars; i>=0; i--)
     {
     ObjectDelete(TimeFrame+"envelopes"+Time[i]);
     }
   return(0);
  }
//-------------------------------------------------------------------+
int start()
  {
   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; i>=0; i--)
     {
      int bar_shift = iBarShift(NULL,TimeFrame,Time[i]);
      if(short
      &&iClose(NULL,TimeFrame,bar_shift+1)>iEnvelopes(NULL,TimeFrame,Envelopes_Period,Envelopes_Method,Envelopes_Shift,Envelopes_Price,Envelopes_Deviation,MODE_UPPER,bar_shift+1)
      ){
      long=true;
      short=false;
     if (play_sound&&i==0)PlaySound("alert.wav");
     if (popup&&i==0)Alert("envelopes signal", Close[0],"!!!");
     if (mail&&i==0)SendMail("envelopes signal", "Price "+DoubleToStr(Bid,Digits));
     if (ObjectFind(TimeFrame+"envelopes"+Time[i]) == -1&&show_lines ){
          ObjectCreate(TimeFrame+"envelopes"+Time[i], OBJ_VLINE, 0, 0, 0);
          ObjectSet(TimeFrame+"envelopes"+Time[i], OBJPROP_TIME1, Time[i]);
          ObjectSet(TimeFrame+"envelopes"+Time[i], OBJPROP_BACK, 1);
          ObjectSet(TimeFrame+"envelopes"+Time[i], OBJPROP_COLOR, buy_color);
          ObjectSet(TimeFrame+"envelopes"+Time[i], OBJPROP_STYLE, 3);}
}
     if(long
     &&iClose(NULL,TimeFrame,bar_shift+1)<iEnvelopes(NULL,TimeFrame,Envelopes_Period,Envelopes_Method,Envelopes_Shift,Envelopes_Price,Envelopes_Deviation,MODE_LOWER,bar_shift+1)
     ){
     long=false;
     short=true;
     if (play_sound&&i==0)PlaySound("alert.wav");
     if (popup&&i==0)Alert("envelopes signal", Close[0],"!!!");
     if (mail&&i==0)SendMail("envelopes signal", "Price "+DoubleToStr(Bid,Digits));
     if (ObjectFind(TimeFrame+"envelopes"+Time[i]) == -1 &&show_lines){
            ObjectCreate(TimeFrame+"envelopes"+Time[i], OBJ_VLINE, 0, 0, 0);
            ObjectSet(TimeFrame+"envelopes"+Time[i], OBJPROP_TIME1, Time[i]);
            ObjectSet(TimeFrame+"envelopes"+Time[i], OBJPROP_BACK, 1);
            ObjectSet(TimeFrame+"envelopes"+Time[i], OBJPROP_COLOR, sell_color);
            ObjectSet(TimeFrame+"envelopes"+Time[i], OBJPROP_STYLE, 3);}
} 
  EnvUpper3[i]  = EMPTY_VALUE;
  EnvLower[i] = EMPTY_VALUE;
    EnvUpper2[i]  = EMPTY_VALUE;
  EnvLower3[i] = EMPTY_VALUE;
      if(long){
      EnvUpper3[i] = iEnvelopes(NULL,TimeFrame,Envelopes_Period,Envelopes_Method,Envelopes_Shift,Envelopes_Price,Envelopes_Deviation,MODE_UPPER,bar_shift);
      EnvLower[i] = iEnvelopes(NULL,TimeFrame,Envelopes_Period,Envelopes_Method,Envelopes_Shift,Envelopes_Price,Envelopes_Deviation,MODE_LOWER,bar_shift);
     }
       if(short){
       EnvUpper2[i] = iEnvelopes(NULL,TimeFrame,Envelopes_Period,Envelopes_Method,Envelopes_Shift,Envelopes_Price,Envelopes_Deviation,MODE_UPPER,bar_shift);
       EnvLower3[i] = iEnvelopes(NULL,TimeFrame,Envelopes_Period,Envelopes_Method,Envelopes_Shift,Envelopes_Price,Envelopes_Deviation,MODE_LOWER,bar_shift);
     }
     }
   return(0);
  }
  
//-------------------------------------------------------------------+

