center of gravity_v1

Download script
center of gravity_v1

This script is a specialized technical analysis tool designed to calculate and visualize the Center of Gravity (CoG) for price action on a trading chart. Unlike standard moving averages that simply track price trends, this indicator aims to identify the "weighted" balance point of price movement over a specific period.

How the Logic Works

The core objective of this script is to determine where the majority of the price activity is concentrated within a set timeframe. It achieves this through three primary logical steps:

1. Identifying the Midpoint For every individual candle on your chart, the tool first identifies the midpoint between the highest and lowest price reached during that specific time interval. This provides a consistent baseline for what happened during that "bar."

2. Calculating Weighted Averages Instead of treating every bar in a sequence as equal, the script applies a weighting system. It looks back at a user-defined number of periods (the "Per" setting). As it moves backward through time, it assigns more significance to the most recent price data. By multiplying the midpoints by their relative position in the sequence, the script creates a "weighted sum."

3. Determining the Center of Gravity The final value shown on your chart is the result of dividing the simple average of these midpoints by the weighted sum calculated in the previous step. This mathematical ratio reveals the Center of Gravity—the point where the price momentum is most balanced.

Visual Representation

The script outputs two distinct lines on a separate window below your main trading chart:

  • Blue Line: Represents the primary Center of Gravity calculation.
  • Red Line: Provides a secondary visual reference based on the same logic.

By observing these lines, traders can better visualize where the "weight" of the market is currently leaning, helping to distinguish between actual trend shifts and minor price fluctuations.

Miscellaneous
Implements a curve of type %1
14 Views
0 Downloads
0 Favorites
center of gravity_v1
/*-----------------------------+
|			       |
| Shared by www.Aptrafx.com    |
|			       |
+------------------------------*/

//+------------------------------------------------------------------+

//| Center of Gravity.mq4

//+------------------------------------------------------------------+



#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=3000;

//---- 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