TrendEnvelopes_v5_MTF

Download script
Author: Copyright � 2007, TrendLaboratory Ltd.
TrendEnvelopes_v5_MTF

The TrendEnvelopes tool is designed to help traders visually identify the current direction of the market and pinpoint potential entry moments by tracking price momentum. It operates by analyzing the relationship between the current market price and a moving average of historical highs and lows.

Core Logic and Market Analysis

The script functions by constantly calculating two boundaries: a "ceiling" based on recent price highs and a "floor" based on recent price lows. By comparing where the current price sits relative to these boundaries, the tool determines the prevailing trend.

  • Trend Detection: The script identifies an upward trend when the market price closes above the previous ceiling. Conversely, it recognizes a downward trend when the price falls below the previous floor.
  • Dynamic Envelopes: Instead of just showing a static line, the tool creates "envelopes." When the market is in an uptrend, it tracks the lowest point reached during that move. When in a downtrend, it tracks the highest point reached. These lines act as visual boundaries that show how far the trend has extended or retracted.

Visual Signals and Alerts

Beyond just showing the trend, the tool provides specific visual cues to help with decision-making:

  • Trend Indicators: The primary yellow lines on your chart represent these envelopes, providing a clear visual of the current market structure.
  • Entry Signals: The script includes an optional signal feature. This identifies specific moments where the trend flips—for example, when a previous downtrend ends and the price breaks into an uptrend. These are marked with specific icons on the chart to highlight potential "Buy" or "Sell" opportunities.
  • Alert System: To ensure you don't have to watch the screen constantly, the tool can be configured to provide real-time notifications. If a trend reversal occurs (moving from bearish to bullish, or vice versa) and there is active trading volume, the script can trigger a pop-up alert or play an audio warning.

Customization Options

The logic is flexible enough to be adjusted based on your trading style:

  • Timeframe Control: You can choose to have the indicator calculate data based on different periods (like hourly or daily) regardless of what chart you are currently viewing.
  • Sensitivity: By adjusting the "MA Period," you can make the tool more sensitive to small, quick price movements or keep it smoothed out to capture only major, long-term trends.
  • Alert Management: Users can toggle warnings and alerts on or off depending on whether they prefer manual observation or automated notifications.
Indicators Used
Moving average indicator
Miscellaneous
Implements a curve of type %1It plays sound alerts
15 Views
0 Downloads
0 Favorites
TrendEnvelopes_v5_MTF
//+------------------------------------------------------------------+
//|                                            TrendEnvelopes_v2.mq4 |
//|                           Copyright © 2007, TrendLaboratory Ltd. |
//|            http://finance.groups.yahoo.com/group/TrendLaboratory |
//|                                   E-mail: igorad2003@yahoo.co.uk |
//|         MTF edit.  Thank you igorad for all of your public work. |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, TrendLaboratory Ltd."
#property link      "http://finance.groups.yahoo.com/group/TrendLaboratory"
//---- indicator settings
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 Yellow
#property indicator_color2 Yellow
#property indicator_width1 2
#property indicator_width2 2
#property indicator_color3 Yellow
#property indicator_color4 Yellow
#property indicator_width3 1
#property indicator_width4 1
//---- indicator parameters
extern string WhatIsTimeFrame = "0=Current period, in minutes. 240=H4, 1440=D1.";
extern int     TimeFrame      = 0;
extern int     MA_Period      = 3;
extern int     MA_Method      =  1;
extern int     UseSignal      =  0; 
extern int     AlertMode      =  0;
extern int     WarningMode    =  0;

//---- indicator buffers
double UpBuffer[];
double DnBuffer[];
double UpSignal[];
double DnSignal[]; 
double smax[];
double smin[];
double trend[];

//----
int ExtCountedBars=0;
bool UpTrendAlert=false, DownTrendAlert=false;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   int    draw_begin;
   string short_name;
//---- drawing settings
   IndicatorBuffers(7);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexStyle(2,DRAW_ARROW);
   SetIndexArrow(2,108);
   SetIndexStyle(3,DRAW_ARROW);
   SetIndexArrow(3,108);
   
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
   if(MA_Period<2) MA_Period=10;
   draw_begin=MA_Period-1;
//---- indicator short name
   IndicatorShortName("TrendEnvelopes("+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);
//---- 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] = iMA(NULL,TimeFrame,MA_Period,0,MA_Method,PRICE_HIGH,i);
      smin[i] = iMA(NULL,TimeFrame,MA_Period,0,MA_Method,PRICE_LOW,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)
	   {
	   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] = smin[i];
	        if (WarningMode>0 && i==0) PlaySound("alert2.wav");
	        }
	        else UpSignal[i] = EMPTY_VALUE;
	     }
	   DnBuffer[i]=EMPTY_VALUE;
	   DnSignal[i]=EMPTY_VALUE; 
	   }
	   else
	   {
	   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] = smax[i];
	        if (WarningMode>0 && i==0) PlaySound("alert2.wav");
	        }
	        else DnSignal[i] = EMPTY_VALUE;
	     }
	   UpBuffer[i]=EMPTY_VALUE;
	   UpSignal[i]=EMPTY_VALUE; 
	   }
   }
//----------   
   string Message;
   
   if ( trend[2]<0 && trend[1]>0 && Volume[0]>1 && !UpTrendAlert)
	  {
	  Message = " "+Symbol()+" M"+Period()+": Signal for BUY";
	  if ( AlertMode>0 ) Alert (Message); 
	  UpTrendAlert=true; DownTrendAlert=false;
	  } 
	 	  
	  if ( trend[2]>0 && trend[1]<0 && Volume[0]>1 && !DownTrendAlert)
	  {
	  Message = " "+Symbol()+" M"+Period()+": Signal for SELL";
	  if ( AlertMode>0 ) Alert (Message); 
	  DownTrendAlert=true; UpTrendAlert=false;
	  } 	         
//---- done
   return(0);
  }
//+------------------------------------------------------------------+

Comments