Envelopes
Download scriptThis tool is designed to provide visual guidance on a trading chart by creating a "channel" or "envelope" around the current price action. It helps traders identify the standard range of price movement based on historical data.
How it Works
The logic functions by calculating a Moving Average, which is a smoothed line representing the average price over a specific number of previous bars. Once this central baseline is established, the script generates two outer boundaries (an Upper and a Lower envelope) to create a visible corridor.
Key Components of the Logic:
- The Baseline: The tool looks back at a set period of historical data (user-definable) to determine the current average price. This acts as the "center" of your trading zone.
- The Deviation: You can choose how wide you want the corridor to be. The script takes this percentage and applies it both upwards and downwards from the baseline.
- The Upper Envelope is calculated by adding a percentage of the average price to the baseline.
- The Lower Envelope is calculated by subtracting that same percentage from the baseline.
- Visual Representation: On your chart, this appears as two distinct lines (typically Blue and Red). When the market is moving within these lines, it suggests the price is following its normal trend. When the price breaks outside of these envelopes, it may signal a significant shift in momentum or an overextended move.
User Customization
The logic allows you to fine-tune how the tool behaves to suit different trading styles:
- Period: Adjusts how much historical data is used (longer periods result in smoother, slower-moving lines; shorter periods make the tool more responsive to sudden price changes).
- Shift: Allows you to move the visual lines forward or backward on the chart.
- Deviation: Controls the "width" of the envelope, allowing you to create a tight corridor for precise trading or a wide one to capture broader market swings.
//+------------------------------------------------------------------+
//| Envelopes.mq4 |
//| Copyright © 2005, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"
//---- indicator settings
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Blue
#property indicator_color2 Red
//---- indicator parameters
extern int MA_Period=14;
extern int MA_Shift=0;
extern int MA_Method=0;
extern int Applied_Price=0;
extern double Deviation=0.1;
//---- indicator buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
//----
int ExtCountedBars=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
int draw_begin;
string short_name;
//---- drawing settings
SetIndexStyle(0,DRAW_LINE);
SetIndexStyle(1,DRAW_LINE);
SetIndexShift(0,MA_Shift);
SetIndexShift(1,MA_Shift);
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,"Env("+MA_Period+")Upper");
SetIndexLabel(1,"Env("+MA_Period+")Lower");
SetIndexDrawBegin(0,draw_begin);
SetIndexDrawBegin(1,draw_begin);
//---- indicator buffers mapping
SetIndexBuffer(0,ExtMapBuffer1);
SetIndexBuffer(1,ExtMapBuffer2);
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=0; i<limit; i++)
{
ExtMapBuffer1[i] = (1+Deviation/100)*iMA(NULL,0,MA_Period,0,MA_Method,Applied_Price,i);
ExtMapBuffer2[i] = (1-Deviation/100)*iMA(NULL,0,MA_Period,0,MA_Method,Applied_Price,i);
}
//---- done
return(0);
}
//+------------------------------------------------------------------+
Comments