Author: Copyright � 2014, andresbarale@gmail.com
garch

This tool is a specialized financial indicator designed to analyze and visualize market volatility. It utilizes a mathematical model known as GARCH (Generalized Autoregressive Conditional Heteroskedasticity), which is primarily used to estimate and forecast the "variance" or "noise" in price movements over time.

In simpler terms, instead of just looking at whether a price is going up or down, this script focuses on how much the price is shaking or fluctuating. Here is a breakdown of the logic used:

Data Selection

The script allows you to choose which specific price point the analysis should be based on—such as the Closing price, Opening price, High, or Low. Once selected, it gathers this historical data to begin its calculations.

The Calculation Process

The indicator processes the market data using a multi-step logic:

  1. Identifying Movement (Residuals): The script first calculates the difference between the current price and the previous price. This tells the tool how much the market moved in a single step.
  2. Measuring Intensity: It then squares that difference. Squaring the movement ensures that the tool looks at the "magnitude" of the change regardless of direction (it treats a sharp drop and a sharp rise with equal importance).
  3. Volatility Weighting: This is where the core logic resides. The script takes two specific user-defined factors (Alpha and Beta) to weight the results:
    • It looks at the most recent "shocks" or sudden price changes.
    • It combines those with a running memory of previous volatility.
  4. Smoothing the Output: Finally, the tool calculates the square root of this weighted result to produce a smooth, readable value.

Visual Result

The final output is plotted as a continuous line on your chart window. This line represents the current "volatility state" of the market. When the line rises, it indicates that price fluctuations are becoming more intense or erratic; when the line falls, it suggests the market is entering a period of relative stability or calmer price action.

This makes it a valuable tool for traders who need to understand the underlying "energy" of the market rather than just the direction of the trend.

Miscellaneous
It issuies visual alerts to the screenImplements a curve of type %1
2 Views
0 Downloads
0 Favorites
garch
//+------------------------------------------------------------------+
//|                                                        GARCH.mq4 |
//|                                              Andres Barale Sarti |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Andres Barale Sarti"
#property link      "http://www.mql5.com"
#property version   "1.20"
#property strict
#property indicator_chart_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//|                                                       GARCH.mq4  |
//|                      Copyright © 2014, andresbarale@gmail.com    |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2014, andresbarale@gmail.com  "
#property link      "http://www.mql5.com"

#property indicator_separate_window
#property indicator_color1 Blue
#property indicator_width1 2
#property indicator_buffers 1
//************************************************************
// Input parameters
//************************************************************
extern int    e_type_data   = PRICE_CLOSE;
extern int    e_reverse_data   = 0;
extern double e_alpha = 0.010; 
extern double e_beta = 0.080;
//************************************************************
// Constant
//************************************************************
string INDICATOR_NAME="GARCH";
string FILENAME      ="___GARCH.mq4";
double LOG_2;
//************************************************************
// Private vars
//************************************************************
double ExtOutputBuffer[];
double ExtInputBuffer[];
int g_period_minus_1;
//+-----------------------------------------------------------------------+
//| FUNCTION : init                                                       |                                                                                                                                                                                                                                                      
//| Initialization function                                               |                                   
//| Check the user input parameters and convert them in appropriate types.|                                                                                                    
//+-----------------------------------------------------------------------+
int init() {
   if(e_type_data < PRICE_CLOSE || e_type_data > PRICE_WEIGHTED ) {
      Alert( "[ 20-ERROR  " + FILENAME + " ] input parameter \"e_type_data\" unknown (" + e_type_data + ")" );
      return( -1 );
   }
   IndicatorBuffers( 1 );
   SetIndexBuffer( 0, ExtOutputBuffer );
   SetIndexStyle( 0, DRAW_LINE, STYLE_SOLID, 2 );
//----
   return( 0 );
 }
//+------------------------------------------------------------------+
//| FUNCTION : deinit                                                |
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit() {
   return(0);
  }
//+------------------------------------------------------------------+
//| FUNCTION : start                                                 |
//| This callback is fired by metatrader for each tick               |
//+------------------------------------------------------------------+
int start(){
   int counted_bars = IndicatorCounted();
   
   _computeLastNbBars();
//----
   return(0);
  }
//+================================================================================================================+
//+=== FUNCTION : _computeLastNbBars                                                                            ===+
//+===                                                                                                          ===+
//+===                                                                                                          ===+
//+=== This callback is fired by metatrader for each tick                                                       ===+
//+===                                                                                                          ===+
//+===                                                                                                          ===+
//+================================================================================================================+
//+------------------------------------------------------------------+
//| FUNCTION : _computeLastNbBars                                    |
//| This callback is fired by metatrader for each tick               |
//| In : - lastBars : these "n" last bars must be repainted          | 
//+------------------------------------------------------------------+
double tmpArray[];

void _computeLastNbBars( ) {
  int pos;
   switch( e_type_data ){
      case PRICE_CLOSE    : ArrayCopy(tmpArray,Close,0,0,WHOLE_ARRAY); _GARCH( tmpArray); break;
      case PRICE_OPEN     : ArrayCopy(tmpArray,Open,0,0,WHOLE_ARRAY);  _GARCH( tmpArray); break;
      case PRICE_HIGH     : ArrayCopy(tmpArray,High,0,0,WHOLE_ARRAY);  _GARCH( tmpArray); break;
      case PRICE_LOW      : ArrayCopy(tmpArray,Low,0,0,WHOLE_ARRAY);   _GARCH( tmpArray); break;      
      default :
         Alert( "[ 20-ERROR  " + FILENAME + " ] the imput parameter e_type_data <" + e_type_data + "> is unknown" );
     }
    
  }
//+------------------------------------------------------------------+
//| FUNCTION : _GARCH                                                |
//| Compute GARCH ARMA indicator                                     |
//| In :                                                             |                      
//|    - inputData : data array on which the computation is applied  | 
//+------------------------------------------------------------------+
void _GARCH(double &inputData[] ) {
   int    pos;
   double GarchNum;
   double residual;
   double residualSq;
   double GarchNumVar = 0.000001;
   int posData = ArraySize(inputData)-1;
//----
   for( pos=1; pos<(ArraySize(inputData)); pos++ ) {
      if(e_reverse_data == 0){
         posData = pos;
      }
      residual = inputData[posData] - inputData[posData - 1];
      residualSq = MathPow(residual, 2.0);
      GarchNumVar = e_alpha * residualSq + e_beta * GarchNumVar;
      GarchNum = MathSqrt(GarchNumVar);
      ExtOutputBuffer[posData]=GarchNum;
      posData--;
   } 
}

Comments