TrendEnvelopes_v1_Alert

Author: Copyright � 2006, TrendLaboratory Ltd.
TrendEnvelopes_v1_Alert

The TrendEnvelopes tool is designed to help traders identify the prevailing direction of the market by creating dynamic boundaries around price action. It essentially acts as a visual guide that "wraps" around the price, helping to distinguish between sustained trends and temporary fluctuations.

How the Logic Works

1. Establishing the Core Trend The indicator starts by calculating a Moving Average (MA), which serves as the baseline for the market's current momentum. From this baseline, it calculates two specific "envelopes" or boundaries: an upper limit and a lower limit. These limits are determined by a user-defined percentage (the Deviation), which creates a corridor of price movement.

2. Trend Identification The script constantly monitors where the closing price sits in relation to these envelopes:

  • Uptrend: If the price closes above the previous upper boundary, the logic recognizes an upward trend.
  • Downtrend: If the price closes below the previous lower boundary, the logic recognizes a downward trend.

3. Dynamic Boundary Tracking Rather than letting the boundaries move freely with every minor tick, the script uses a "memory" feature for the envelopes:

  • In an uptrend, the lower boundary (the support level) only moves upward; it will never drop lower than its previous position.
  • In a downtrend, the upper boundary (the resistance level) only moves downward; it will never rise higher than its previous position. This ensures that the "floor" and "ceiling" of the trend remain structurally sound as long as the trend persists.

4. Visual Signals and Alerts To make the data actionable for a trader, the script provides several visual and auditory cues:

  • Visual Envelopes: It draws lines on the chart representing the current support and resistance levels based on the active trend.
  • Signal Arrows: When a trend shift occurs (e.g., moving from a down-trend to an up-trend), the script places specific markers (arrows) on the chart to highlight the potential entry point.
  • Notifications: The logic includes a built-in alert system. You can configure it to trigger a sound or send an email the moment a trend reversal is detected, providing real-time information without needing to constantly stare at the screen.

Summary for the User

In simple terms, this script filters out market "noise" by creating a structural corridor. It tells you: "The market is currently moving up; here is the floor it shouldn't break." When the price finally breaks through that structural floor or ceiling, the script alerts you that the trend has likely changed, providing a clear signal for potential trading opportunities.

Price Data Components
Series array that contains open time of each bar
Indicators Used
Moving average indicatorIndicator of the average true range
Miscellaneous
Implements a curve of type %1It issuies visual alerts to the screenIt sends emails
14 Views
0 Downloads
0 Favorites
TrendEnvelopes_v1_Alert
//+------------------------------------------------------------------+
//|                                            TrendEnvelopes_v1.mq4 |
//|                           Copyright © 2006, TrendLaboratory Ltd. |
//|            http://finance.groups.yahoo.com/group/TrendLaboratory |
//|                                   E-mail: igorad2003@yahoo.co.uk |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, TrendLaboratory Ltd."
#property link      "http://finance.groups.yahoo.com/group/TrendLaboratory"
//---- indicator settings
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 White
#property indicator_color2 Red
#property indicator_width1 3
#property indicator_width2 3
#property indicator_color3 White
#property indicator_color4 Red
#property indicator_width3 3
#property indicator_width4 3

// inputs for showing alerts
extern bool SoundON=true;
extern bool EmailON=false;

//---- indicator parameters
extern int     MA_Period      = 14;
extern int     MA_Shift       =  0;
extern int     MA_Method      =  3;
extern int     Applied_Price  =  0;
extern double  Deviation      =0.2;
extern int     UseSignal      =  1; 
//---- indicator buffers
double UpBuffer[];
double DnBuffer[];
double UpSignal[];
double DnSignal[]; 
double smax[];
double smin[];
double trend[];

//----
int ExtCountedBars=0;
// declare variables used by Alert
int flagval1 = 0;
int flagval2 = 0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   int    draw_begin;
   string short_name;
//---- drawing settings
   IndicatorBuffers(7);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexShift(0,MA_Shift);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexShift(1,MA_Shift);
   
   SetIndexStyle(2,DRAW_ARROW);
   SetIndexShift(2,MA_Shift);
   SetIndexArrow(2,233);
   SetIndexStyle(3,DRAW_ARROW);
   SetIndexShift(3,MA_Shift);
   SetIndexArrow(3,234);
   
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
   if(MA_Period<2) MA_Period=14;
   draw_begin=MA_Period-1;
//---- indicator short name
   IndicatorShortName("Env("+MA_Period+")");
   SetIndexLabel(0,"UpTrendEnv");
   SetIndexLabel(1,"DnTrendEnv");
   SetIndexLabel(2,"UpSignal");
   SetIndexLabel(3,"DnSignal");
   SetIndexDrawBegin(0,draw_begin);
   SetIndexDrawBegin(1,draw_begin);
   SetIndexDrawBegin(2,draw_begin);
   SetIndexDrawBegin(3,draw_begin);
   //---- indicator buffers mapping
   SetIndexBuffer(0,UpBuffer);
   SetIndexBuffer(1,DnBuffer);
   SetIndexBuffer(2,UpSignal);
   SetIndexBuffer(3,DnSignal);
   SetIndexBuffer(4,smax);
   SetIndexBuffer(5,smin);
   SetIndexBuffer(6,trend);
   if(Deviation<0.1) Deviation=0.1;
   if(Deviation>100.0) Deviation=100.0;
//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int limit;
   if(Bars<=MA_Period) return(0);
   ExtCountedBars=IndicatorCounted();
//---- check for possible errors
   if (ExtCountedBars<0) return(-1);
//---- last counted bar will be recounted
   if (ExtCountedBars>0) ExtCountedBars--;
   limit=Bars-ExtCountedBars;
//---- EnvelopesM counted in the buffers
   for(int i=limit; i>=0; i--)
     { 
      smax[i] = (1+Deviation/100)*iMA(NULL,0,MA_Period,0,MA_Method,Applied_Price,i);
      smin[i] = (1-Deviation/100)*iMA(NULL,0,MA_Period,0,MA_Method,Applied_Price,i);
   
      trend[i]=trend[i+1]; 
	   
	   if (Close[i]>smax[i+1])  trend[i]=1; 
	   if (Close[i]<smin[i+1])  trend[i]=-1;

	   if(trend[i]>0)
	   {
// Add Buy Alert
         if (i == 1 && flagval1==0)
         {
           flagval1=1;
           flagval2=0;
           if (SoundON) Alert("BUY signal at Ask=",Ask,"\n Bid=",Bid,"\n Time=",TimeToStr(CurTime(),TIME_DATE)," ",TimeHour(CurTime()),":",TimeMinute(CurTime()),"\n Symbol=",Symbol()," Period=",Period());
           if (EmailON) SendMail("BUY signal alert","BUY signal at Ask="+DoubleToStr(Ask,4)+", Bid="+DoubleToStr(Bid,4)+", Date="+TimeToStr(CurTime(),TIME_DATE)+" "+TimeHour(CurTime())+":"+TimeMinute(CurTime())+" Symbol="+Symbol()+" Period="+Period());
         }
	   if (smin[i]<smin[i+1]) smin[i]=smin[i+1];
	   UpBuffer[i]=smin[i];
	   if (UseSignal>0) 
	   if (trend[i+1]<0) UpSignal[i] = Low[i]-0.5*iATR(NULL,0,MA_Period,i);
	   DnBuffer[i]=EMPTY_VALUE;
	   }
	   else
	   {
// Add Sell Alert
         if (i == 1 && flagval2==0)
         {
           flagval2=1;
           flagval1=0;
           if (SoundON) Alert("SELL signal at Ask=",Ask,"\n Bid=",Bid,"\n Date=",TimeToStr(CurTime(),TIME_DATE)," ",TimeHour(CurTime()),":",TimeMinute(CurTime()),"\n Symbol=",Symbol()," Period=",Period());
           if (EmailON) SendMail("SELL signal alert","SELL signal at Ask="+DoubleToStr(Ask,4)+", Bid="+DoubleToStr(Bid,4)+", Date="+TimeToStr(CurTime(),TIME_DATE)+" "+TimeHour(CurTime())+":"+TimeMinute(CurTime())+" Symbol="+Symbol()+" Period="+Period());
         }
	   if(smax[i]>smax[i+1]) smax[i]=smax[i+1];
	   DnBuffer[i]=smax[i];
	   if (UseSignal>0) 
	   if (trend[i+1]>0) DnSignal[i] = High[i]+0.5*iATR(NULL,0,MA_Period,i);
	   UpBuffer[i]=EMPTY_VALUE;
	   }
   }	         
//---- done
   return(0);
  }
//+------------------------------------------------------------------+

Comments