Market_Statistics_v7_0

Download script
Author: Copyright � 2010, Akif TOKUZ
Market_Statistics_v7_0

This tool is designed to provide traders with a comprehensive visual overview of market sentiment and volatility by analyzing price action in relation to trading volume. Rather than just looking at where the price is moving, this script interprets how much "effort" (volume) is behind those movements, helping to identify the true strength of a trend.

Core Analytical Logic

The logic operates on three primary components: Volume Weighting, Standard Deviation, and Multi-Layered Bands.

1. Volume-Weighted Moving Average (VWMA) Standard moving averages treat every price point with equal importance. This script, however, uses a Volume-Weighted approach. It calculates the average price by giving more "weight" to periods where trading activity was highest. If a price move happens on low volume, the indicator reacts less; if it happens on massive volume, the indicator reflects that significance more heavily. This provides a clearer picture of where the "real" money is moving.

2. Dynamic Volatility Measurement (Standard Deviation) To understand how far the price is stretching away from its average, the script calculates the Standard Deviation. It measures the mathematical variance between the current price and the VWMA over a specific period. By calculating this, the tool can determine if the current market movement is "normal" or if it has entered an extreme state of volatility.

3. Multi-Layered Statistical Bands The script projects three distinct sets of "bands" (Upper and Lower limits) based on the Standard Deviation. These act as boundaries for market behavior:

  • Layer 1: Represents a standard deviation threshold (e.g., 1.0).
  • Layer 2: Represents a wider, more significant deviation (e.g., 2.0).
  • Layer 3: Represents an extreme volatility threshold (e.g., 3.0).

Practical Application for the User

By observing where the price sits in relation to these bands, users can gain insights into market mechanics:

  • Trend Identification: When the price stays consistently within the inner bands while moving in one direction, it suggests a healthy, supported trend.
  • Overextended Markets: When the price pushes toward or outside the outer bands (Layer 2 or 3), it signals that the market may be overextended or "overbought/oversold," potentially indicating a need for caution or a coming reversal.
  • Customization: The logic allows users to choose which specific price types to analyze (such as High, Low, Close, or various averages) and to toggle the visibility of different bands to suit their specific trading style.
Miscellaneous
Implements a curve of type %1
8 Views
0 Downloads
0 Favorites
Market_Statistics_v7_0
//+------------------------------------------------------------------+
//|                                       Market_Statistics_v7_0.mq4 |
//|                                     Copyright © 2010, Akif TOKUZ |
//|                                             akifusenet@gmail.com |
//| Some concepts are taken from Igorad's AllAverages_v2_2           |
//| inspired from Jperl's `Trading With Market Statistics` thread on |
//| Traders Laboratory.                                              |
//|                                                                  |
//| History:                                                         |
//| 23.05.2010: v7.0=>Realization of 6.2 is a reinvention of the     |
//|               => the wheel. This is much MORE faster.
//| 17.03.2010: v6.2=>Bug-fix of resetting at week start             |
//| 04.03.2010: v6.1=>Record MTF calculated stat values for every bar|
//| 17.01.2010: v5.0=>Sliding window for days back instead of fixed  |
//|               =>starting date(calculate starting from x day      |
//|               =>back, starting date is updated automatically in  |
//|               =>real-time)                                       |
//| 17.01.2010: v4.2=>Small Bugfix [zero division possibility ]      |
//|               =>When time period change call deinit and init     |
//| 09.09.2009: v4.1=>Small bugfix [delete startDate correct prefix] |
//| 09.09.2009: v4=>Instead of fixed start date now we have a        |
//|               =>relative startdate like 2 days back at 22:00     |
//| 02.09.2009: v3=>Calculation done only at the start of a new bar  |
//|               =>implementation is corrected                      |
//|               =>histogram defaulted 50 bar sd3show made true     |
//| 10.08.2009: v2=>End date added.SD2Show defaulted to true         |
//|               =>Show/disable options added for every line        |
//| 06.08.2009: v1=>Initial release                                  |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, Akif TOKUZ"
#property link      "akifusenet@gmail.com"

#property indicator_chart_window
#property indicator_buffers 8

#property indicator_color1 Aqua        //VWAP
#property indicator_width1 2

#property indicator_color2 Green       //SD1Pos
#property indicator_width2 1
#property indicator_style2 2

#property indicator_color3 Red         //SD1Neg
#property indicator_width3 1
#property indicator_style3 2

#property indicator_color4 DarkOliveGreen//SD2Pos
#property indicator_width4 1
#property indicator_style4 2

#property indicator_color5 Crimson     //SD2Neg
#property indicator_width5 1
#property indicator_style5 2

#property indicator_color6 DarkGreen   //SD3Pos
#property indicator_width6 1
#property indicator_style6 2

#property indicator_color7 FireBrick   //SD3Neg
#property indicator_width7 1
#property indicator_style7 2

//---- input parameters
extern int Periods=20;
extern int AppliedPrice=5;

extern double  SD1_Deviation  = 1.0;
extern double  SD2_Deviation  = 2.0;
extern double  SD3_Deviation  = 3.0;

extern bool    Show_VWMA      = true;
extern bool    Show_SD1       = true;
extern bool    Show_SD2       = true;
extern bool    Show_SD3       = true;

//---- buffers
double VWMA[];
double SD1Pos[];
double SD1Neg[];
double SD2Pos[];
double SD2Neg[];
double SD3Pos[];
double SD3Neg[];
double UsedPrice[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   IndicatorBuffers(8);
 
   if (Show_VWMA==true) SetIndexStyle(0,DRAW_LINE);
   else  SetIndexStyle(0,DRAW_NONE);
   SetIndexLabel(0,"VWMA");      
   SetIndexBuffer(0,VWMA);   
   
   if (Show_SD1==true) SetIndexStyle(1,DRAW_LINE);
   else  SetIndexStyle(1,DRAW_NONE);
   SetIndexLabel(1,"SD1Pos");      
   SetIndexBuffer(1,SD1Pos);
   
   if (Show_SD1==true) SetIndexStyle(2,DRAW_LINE);
   else  SetIndexStyle(2,DRAW_NONE);
   SetIndexLabel(2,"SD1Neg");      
   SetIndexBuffer(2,SD1Neg);
   
   if (Show_SD2==true) SetIndexStyle(3,DRAW_LINE);
   else  SetIndexStyle(3,DRAW_NONE);
   SetIndexLabel(3,"SD2Pos");      
   SetIndexBuffer(3,SD2Pos);
   
   if (Show_SD2==true) SetIndexStyle(4,DRAW_LINE);
   else  SetIndexStyle(4,DRAW_NONE);
   SetIndexLabel(4,"SD2Neg");      
   SetIndexBuffer(4,SD2Neg);
   
   if (Show_SD3==true) SetIndexStyle(5,DRAW_LINE);
   else  SetIndexStyle(5,DRAW_NONE);
   SetIndexLabel(5,"SD3Pos");      
   SetIndexBuffer(5,SD3Pos);
   
   if (Show_SD3==true) SetIndexStyle(6,DRAW_LINE);
   else  SetIndexStyle(6,DRAW_NONE);
   SetIndexLabel(6,"SD3Neg");      
   SetIndexBuffer(6,SD3Neg);
   
   SetIndexStyle(7,DRAW_NONE);
   SetIndexBuffer(7,UsedPrice);

   string short_name="VWMA_Bands";
   IndicatorShortName(short_name);     

//---- first values aren't drawn
   SetIndexDrawBegin(0,Periods);
//----
   return(0);
  }
  

// MA_Method=19: VWMA - Volume Weighted Moving Average 
double VWMA(int per,int bar)
{
   double Sum = 0;
   double Weight = 0;
   
      for(int i = 0;i < per;i++)
      { 
      Weight+= Volume[bar+i];
      Sum += UsedPrice[bar+i]*Volume[bar+i];
      }
   if(Weight>0) double vwma = Sum/Weight;
   else vwma = 0; 
   return(vwma);
} 
  
  
//+------------------------------------------------------------------+
//| VWMA Bands                                               |
//+------------------------------------------------------------------+
int start()
  {
   int    i,j,nLimit,nCountedBars;
   double dAPrice,dAmount,dMovingAverage,SD;
   double totalVolume;
     
//---- insufficient data
   if(Bars<=Periods) return(0);
//---- bars count that does not changed after last indicator launch.
   nCountedBars=IndicatorCounted();

   i=Bars-Periods-1;
   if(nCountedBars>Periods) 
      i=Bars-nCountedBars;  
  
   while(i>=0)
     {
      UsedPrice[i] = GetAppliedPrice(AppliedPrice,i);
      dAmount=0.0;
      //dMovingAverage=iCustom(NULL,0,"AllAverages_v2.2",0,AppliedPrice,Periods,0,19,0,0,i);
      dMovingAverage=VWMA(Periods,i);
      
      totalVolume=0;
      for(j=0; j<Periods; j++)
        {
         dAPrice=GetAppliedPrice(AppliedPrice,i+j);
         dAmount+=Volume[i+j]*(dAPrice-dMovingAverage)*(dAPrice-dMovingAverage);
         totalVolume+=Volume[i+j];
        }
      SD=MathSqrt(dAmount/totalVolume);
      VWMA[i]=dMovingAverage;
      SD1Pos[i]=dMovingAverage+SD*SD1_Deviation;
      SD1Neg[i]=dMovingAverage-SD*SD1_Deviation;
      SD2Pos[i]=dMovingAverage+SD*SD2_Deviation;
      SD2Neg[i]=dMovingAverage-SD*SD2_Deviation;
      SD3Pos[i]=dMovingAverage+SD*SD3_Deviation;
      SD3Neg[i]=dMovingAverage-SD*SD3_Deviation;         
      
      i--;
     }
//----
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double GetAppliedPrice(int nAppliedPrice, int nIndex)
  {
   double dPrice;
//----
   switch(nAppliedPrice)
     {
      case 0:  dPrice=Close[nIndex];                                  break;
      case 1:  dPrice=Open[nIndex];                                   break;
      case 2:  dPrice=High[nIndex];                                   break;
      case 3:  dPrice=Low[nIndex];                                    break;
      case 4:  dPrice=(High[nIndex]+Low[nIndex])/2.0;                 break;
      case 5:  dPrice=(High[nIndex]+Low[nIndex]+Close[nIndex])/3.0;   break;
      case 6:  dPrice=(High[nIndex]+Low[nIndex]+2*Close[nIndex])/4.0; break;
      default: dPrice=0.0;
     }
//----
   return(dPrice);
  }
//+------------------------------------------------------------------+

Comments