Overview of the Center of Gravity Indicator
The Center of Gravity (CoG) script is a technical analysis tool designed to help traders identify the "balance point" of price action within a specific timeframe. Instead of simply looking at where the price currently sits, this indicator calculates the weighted average of price movements, providing a clearer picture of where the primary concentration of trading activity is occurring.
How the Logic Works
The script processes market data by analyzing the relationship between the highest and lowest prices reached during each trading session (the "High" and "Low"). It operates through the following logical steps:
1. Data Collection Period The user can define a specific lookback period (set by default as 10 units). The script focuses on this window of time to determine the current balance of the market. By concentrating on a specific number of bars, it filters out older, irrelevant data to focus on recent momentum.
2. Calculating the Midpoint For every individual bar within the selected period, the script first calculates the midpoint between the high and low prices. This represents the "center" of that specific candle's movement.
3. Weighted Average Distribution This is where the "Center of Gravity" logic distinguishes itself from a simple moving average. The script doesn't treat every bar in the period equally. Instead, it applies a weighted distribution:
- It calculates the sum of all midpoints within the period.
- It then calculates a secondary sum where each midpoint is multiplied by its position in the sequence.
- By dividing the first sum by the second, the script identifies the mathematical "balance point."
4. Visual Representation The result is plotted as two distinct lines on a separate window below your main chart. These lines represent the center of gravity over time.
Practical Application
Because this indicator accounts for the distribution of price rather than just the final closing price, it acts as a stabilizer. When the market is in a state of equilibrium, the price will tend to gravitate toward these lines. Deviations from these levels can signal potential exhaustion or a shift in momentum, helping traders visualize whether the current price is "heavy" (overextended) or finding its natural resting point.
//+------------------------------------------------------------------+
//| Center of Gravity.mq4
//|
//+------------------------------------------------------------------+
#property copyright "Copyright 2002, Finware.ru Ltd."
#property link "http://www.finware.ru/"
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Blue
#property indicator_color2 Red
//Inputs : Per(10);
//Variable : shift(0),StartBar(600);
//Variable : value1(0), sum(0),sum1(0), cnt(0);
extern int Per=10;
extern int CountBars=20000;
//---- buffers
double val1[];
double val2[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
string short_name;
//---- indicator line
IndicatorBuffers(2);
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,val1);
SetIndexStyle(1,DRAW_LINE);
SetIndexBuffer(1,val2);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Center of Gravity |
//+------------------------------------------------------------------+
int start()
{
if (CountBars>=Bars) CountBars=Bars;
SetIndexDrawBegin(0,Bars-CountBars+Per+1);
SetIndexDrawBegin(1,Bars-CountBars+Per+1);
int i,cnt,counted_bars=IndicatorCounted();
double value1,sum,sum1;
//----
if(Bars<=38) return(0);
//---- initial zero
if(counted_bars<Per)
{
for(i=1;i<=0;i++) val1[CountBars-i]=0.0;
for(i=1;i<=0;i++) val2[CountBars-i]=0.0;
}
//----
i=CountBars-Per-1;
// if(counted_bars>=39) i=Bars-counted_bars-1;
while(i>=0)
{
sum = 0.0;
for (cnt=0; cnt<=Per-1; cnt++)
{
sum = sum + (High[i+cnt]+Low[i+cnt])/2;
}
sum1=0.0;
for (cnt=0; cnt<=Per-1; cnt++)
{
sum1=sum1+((High[i+cnt]+Low[i+cnt])*(cnt+1)/2);
}
value1=sum/sum1;
//SetIndexValue(i,value1);
//SetIndexValue2(i-1,value1);
val1[i]=value1;
if (i>0) val2[i-1]=value1;
i--;
}
return(0);
} //+------------------------------------------------------------------+
Comments