AutoEnvelope

Author: Copyright � 2011, Leandro Farias (Dr. Alexander Elder AutoEnvelope based)
AutoEnvelope

This script is designed to analyze price movements in a financial market and display potential trading ranges on a chart. It calculates these ranges using statistical methods based on historical price data. Here's a breakdown of what the script does:

  1. Overall Goal: The script aims to create a visual representation of dynamic support and resistance levels on a price chart. These levels are shown as bands around the price, potentially indicating areas where the price might reverse or find temporary support or resistance.

  2. Key Components:

    • Moving Averages (MME and MME2): The script calculates two exponential moving averages (EMAs) of the closing price. EMAs are used to smooth out price fluctuations and identify trends. The mme_periodo variable determines the period for the slow EMA, while mme2_periodo determines the period for the fast EMA.
    • Standard Deviation: It calculates the standard deviation of the closing prices over a defined period (stdev_periodo). Standard deviation measures how much the price typically deviates from its average. In essence, it shows how volatile the price has been recently.
    • Dynamic Bands: The script calculates bands above and below the slower EMA. These bands are not fixed; they dynamically adjust based on the standard deviation calculated. The width of the bands represents the degree of volatility. Higher volatility results in wider bands and vice versa.
    • Smoothing: It smoothes the standard deviation using another moving average-like calculation controlled by the band_mme parameter. This helps to dampen the effect of extreme volatility spikes on the band width.
  3. How It Works (In Detail):

    • It starts by looking at the historical price data available on the chart.
    • It calculates the standard deviation of the closing prices for each point in time.
    • Then, it calculates a smoothed version of the standard deviation, this serves as an volatility index.
    • It calculates the two EMAs.
    • Finally, it uses the volatility index to create the upper and lower bands. The bands are a percentage away from the slower EMA based on the current volatility index.
    • The script then draws these lines on the chart: the upper band, the lower band, the slow EMA, and the fast EMA.
  4. What it shows on the chart: The script plots these values as lines on the price chart:

    • Upper Band: A dotted line above the price.
    • Lower Band: A dotted line below the price.
    • Slow EMA: A solid line, representing the longer-term trend.
    • Fast EMA: A solid line, representing the shorter-term trend.
  5. Hiding: The hide variable, when set to true, will prevent the indicator from plotting any lines on the chart.

In summary, this script analyzes price data, calculates smoothed volatility, and displays potential trading ranges based on that volatility around a slow moving average. Traders might use this indicator to identify overbought/oversold conditions, potential support and resistance levels, and trend direction.

Indicators Used
Standard Deviation indicatorMoving average indicator
Miscellaneous
Implements a curve of type %1
14 Views
2 Downloads
0 Favorites
AutoEnvelope
//+------------------------------------------------------------------+
//|                                                 AutoEnvelope.mq4 |
//|                                Copyright © 2011, Leandro Farias. |
//|                           Dr. Alexander Elder AutoEnvelope based |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, Leandro Farias (Dr. Alexander Elder AutoEnvelope based)"
#property link      "http://www.metaquotes.net"

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 Blue
#property indicator_color2 Red
#property indicator_color3 Black
#property indicator_color4 Green

//--- input parameters
extern int       mme_periodo=26;          // Slow MME
extern int       mme2_periodo=13;         // Fast MME
extern int       stdev_periodo=60;        // Period standard seviation (60 recommended)
extern int       band_mme=200;            // Smoothing channel bands MME (200 recommended)
extern bool       hide=false;             // Show/hide

double emapb1[];                          // Upper band
double emapb2[];                          // Lower band
double emapb3[];                          // Slow MME
double emapb4[];                          // Fast MME

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators

      SetIndexBuffer(0,emapb1);
      SetIndexStyle(0,DRAW_LINE,STYLE_DOT,1);

      SetIndexBuffer(1,emapb2);
      SetIndexStyle(1,DRAW_LINE,STYLE_DOT,1);
   
      SetIndexBuffer(2,emapb3);
      SetIndexStyle(2,DRAW_LINE,STYLE_SOLID,2);

      SetIndexBuffer(3,emapb4);
      SetIndexStyle(3,DRAW_LINE,STYLE_SOLID,2);
      

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
   int i,a=0;
   int nLimit;
   double stdv;
   double mme, mme_ontem, k;
   
   if(hide == true) return(0);

   nLimit = Bars - mme_periodo;
   
   k = 2.0 / (band_mme+1);

   for(i = nLimit; i > -1; i--,a++)
   {
         stdv = iStdDev(NULL, 0, stdev_periodo, 0, MODE_EMA, PRICE_CLOSE, i);

         if(a == 0) mme_ontem = stdv;
         else mme_ontem = mme;

         mme = (stdv * k) + (mme_ontem * (1 - k));

         emapb3[i] = iMA(NULL, 0, mme_periodo, 0, MODE_EMA, PRICE_CLOSE, i);
         
         if(mme2_periodo > 0)
         {
            emapb4[i] = iMA(NULL, 0, mme2_periodo, 0, MODE_EMA, PRICE_CLOSE, i);
         }
         
         emapb1[i] = emapb3[i]*(1+(mme / emapb3[i]));
         emapb2[i] = emapb3[i]*(1-(mme / emapb3[i]));
         
   }
   

//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+

Comments