TrendEnvelopes_v1[1].1
Download scriptThis tool is designed to help traders visually identify and navigate market trends by creating dynamic "envelopes" based on moving averages. It simplifies the complexities of price action into clear visual boundaries, helping users distinguish between bullish (upward) and bearish (downward) momentum.
How the Logic Works
The core functionality of the script relies on three primary layers of analysis:
1. The Dynamic Envelope Calculation The tool starts by calculating a standard Moving Average to establish a "baseline" for price. It then creates a percentage-based deviation (an envelope) above and below this baseline. Think of this as a corridor that expands or contracts based on the recent price history, providing a defined zone where the price is expected to trade.
2. Trend Detection & Trapping The script constantly monitors whether the current market price is breaking out of these envelopes:
- Bullish Shift: If the price closes above the upper boundary, the logic identifies a potential upward trend. It then "locks" onto that trend by tracking the lowest point reached since the break.
- Bearish Shift: If the price closes below the lower boundary, it identifies a downward trend and tracks the highest point reached since that breach.
By doing this, the tool doesn't just show you where the price is now; it keeps track of the "extreme" boundaries of the current move, helping to visualize how far the market has stretched in its current direction.
3. Visual Signals and Alerts To make the data actionable for a trader, the script translates these calculations into visual cues:
- Trend Lines: It draws distinct lines on your chart that represent the current active trend boundary. When the trend switches, the line "flips" to reflect the new direction.
- Entry Signals: If enabled, the tool places specific icons (arrows) on the chart at the exact moment the trend flips from bearish to bullish (or vice versa).
- Audio and Pop-up Alerts: The logic includes a notification system that can trigger a sound or a pop-up message on your screen specifically when a trend reversal is confirmed, ensuring you don't miss a potential shift in market sentiment.
Summary for the User
Essentially, this script acts as a filter. It ignores minor price fluctuations and only reacts when price action strongly breaks out of its established "envelope." It provides you with a clear visual of the current trend's reach and alerts you immediately when the market shifts from one direction to another.
//+------------------------------------------------------------------+
//| 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 LightBlue
#property indicator_color2 Orange
#property indicator_width1 2
#property indicator_width2 2
#property indicator_color3 LightBlue
#property indicator_color4 Orange
#property indicator_width3 2
#property indicator_width4 2
//---- 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 = 0;
extern int SoundAlertMode=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);
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)
{
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);
if (SoundAlertMode>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] = High[i]+0.5*iATR(NULL,0,MA_Period,i);
if (SoundAlertMode>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 ( SoundAlertMode>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 ( SoundAlertMode>0 ) Alert (Message);
DownTrendAlert=true; UpTrendAlert=false;
}
//---- done
return(0);
}
//+------------------------------------------------------------------+
Comments