COG THV

The "Center of Gravity" (COG) indicator is a sophisticated technical analysis tool designed to identify the underlying trend of a financial asset by calculating a mathematical "equilibrium" point of price action. Rather than simply following the most recent price, it looks at historical data to determine the core momentum of the market.

How the Logic Works

The script operates through a multi-step mathematical process to filter out "noise" and reveal the true direction of the market:

1. Data Collection and Weighting The tool examines a specific window of historical price data (defined by the bars_back setting). It doesn't treat every past price with equal importance; instead, it creates a weighted sum of closing prices. This allows the script to understand how price has behaved over time relative to its position in the recent history.

2. Polynomial Regression Analysis The core of the logic relies on a process called polynomial regression. The script organizes the data into a mathematical matrix to find the best-fitting curve for the price movement. By using these calculations, the tool attempts to "smooth out" erratic jumps and minor fluctuations, leaving behind a curved line that represents the most likely path of the price.

3. Calculating the "Center of Gravity" Once the regression curve is established, the script identifies the primary trendline (the Center of Gravity). This line acts as the "anchor" for the price; if the actual market price moves significantly away from this line, it suggests a potential reversal or an overextended move.

4. Dynamic Volatility Bands To help traders visualize the strength and stability of the trend, the script calculates two different types of boundaries:

  • Standard Deviation Bands: These use a traditional volatility measurement to show how much the price normally fluctuates from the average.
  • Calculated Standard Deviation: The script also generates its own unique variance levels based on the specific curvature of the regression line.

What This Means for the User

Instead of seeing a jagged, messy chart of every tiny tick in price, this tool provides a smoothed "spine" for the market. When you see the price interacting with the Center of Gravity line and its surrounding bands, you are looking at a visualization of:

  • Trend Direction: Where the "weight" of the price is actually heading.
  • Overextended Movements: Identifying when the price has moved too far from its mathematical center, which may indicate it is becoming overbought or oversold.
  • Stability: Providing a clearer picture of market equilibrium by filtering out temporary emotional spikes in trading volume.
14 Views
0 Downloads
0 Favorites
COG THV
//+------------------------------------------------------------------+
//|                                            Center of Gravity.mq4 |
//| Original Code from NG3110@latchess.com                           |                                    
//| Modified for THV system by Cobraforex, www.cobraforex.com        |
//+------------------------------------------------------------------+
#property  copyright "ANG3110@latchess.com"
//---------ang_PR (Din)--------------------
#property indicator_chart_window
#property indicator_buffers 5
#property indicator_color1 Black
#property indicator_color2 FireBrick
#property indicator_color3 DarkGreen
#property indicator_color4 Black
#property indicator_color5 Black
//-----------------------------------
extern int bars_back = 120;
extern int m = 4;
extern int i = 0;
extern double kstd = 2.0;
extern int sName = 720;
//-----------------------
double fx[], sqh[], sql[], stdh[], stdl[];
double ai[10,10], b[10], x[10], sx[20];
double sum;
int    ip, p, n, f;
double qq, mm, tt;
int    ii, jj, kk, ll, nn;
double sq, std;
//*******************************************
int init()
{
   IndicatorShortName("COG THV");
   SetIndexStyle(0, DRAW_LINE);
   SetIndexBuffer(0, fx);
   SetIndexBuffer(1, sqh);
   SetIndexBuffer(2, sql);
   SetIndexBuffer(3, stdh);
   SetIndexBuffer(4, stdl);
   p = MathRound(bars_back);
   nn = m + 1;
   ObjectCreate("pr" + sName, 22, 0, Time[p], fx[p]);
   ObjectSet("pr" + sName, 14, 159);
   return(0);
}
//----------------------------------------------------------
int deinit()
{
   ObjectDelete("pr" + sName);
}
//**********************************************************************************************
int start()
{
   int mi;
//-------------------------------------------------------------------------------------------
   ip = iBarShift(Symbol(), Period(), ObjectGet("pr" + sName, OBJPROP_TIME1));
   p = bars_back; 
   sx[1] = p + 1;
   SetIndexDrawBegin(0, Bars - p - 1);
   SetIndexDrawBegin(1, Bars - p - 1);
   SetIndexDrawBegin(2, Bars - p - 1);
   SetIndexDrawBegin(3, Bars - p - 1);
   SetIndexDrawBegin(4, Bars - p - 1); 
//----------------------sx-------------------------------------------------------------------
   for(mi = 1; mi <= nn * 2 - 2; mi++)
   {
      sum = 0;
      for(n = i; n <= i + p; n++)
      {
         sum += MathPow(n, mi);
      }
      sx[mi + 1] = sum;
   }  
//----------------------syx-----------
   for(mi = 1; mi <= nn; mi++)
   {
      sum = 0.00000;
      for(n = i; n <= i + p; n++)
      {
         if(mi == 1)
            sum += Close[n];
         else
            sum += Close[n] * MathPow(n, mi - 1);
      }
      b[mi] = sum;
   } 
//===============Matrix=======================================================================================================
   for(jj = 1; jj <= nn; jj++)
   {
      for(ii = 1; ii <= nn; ii++)
      {
         kk = ii + jj - 1;
         ai[ii, jj] = sx[kk];
      }
   }  
//===============Gauss========================================================================================================
   for(kk = 1; kk <= nn - 1; kk++)
   {
      ll = 0; mm = 0;
      for(ii = kk; ii <= nn; ii++)
      {
         if(MathAbs(ai[ii, kk]) > mm)
         {
            mm = MathAbs(ai[ii, kk]);
            ll = ii;
         }
      }
      if(ll == 0)
         return(0);   

      if(ll != kk)
      {
         for(jj = 1; jj <= nn; jj++)
         {
            tt = ai[kk, jj];
            ai[kk, jj] = ai[ll, jj];
            ai[ll, jj] = tt;
         }
         tt = b[kk]; b[kk] = b[ll]; b[ll] = tt;
      }  
      for(ii = kk + 1; ii <= nn; ii++)
      {
         qq = ai[ii, kk] / ai[kk, kk];
         for(jj = 1; jj <= nn; jj++)
         {
            if(jj == kk)
               ai[ii, jj] = 0;
            else
               ai[ii, jj] = ai[ii, jj] - qq * ai[kk, jj];
         }
         b[ii] = b[ii] - qq * b[kk];
      }
   }  
   x[nn] = b[nn] / ai[nn, nn];
   for(ii = nn - 1; ii >= 1; ii--)
   {
      tt = 0;
      for(jj = 1; jj <= nn - ii; jj++)
      {
         tt = tt + ai[ii, ii + jj] * x[ii + jj];
         x[ii] = (1 / ai[ii, ii]) * (b[ii] - tt);
      }
   } 
//===========================================================================================================================
   for(n = i; n <= i + p; n++)
   {
      sum = 0;
      for(kk = 1; kk <= m; kk++)
      {
         sum += x[kk + 1] * MathPow(n, kk);
      }
      fx[n] = x[1] + sum;
   } 
//-----------------------------------Std-----------------------------------------------------------------------------------
   sq = 0.0;
   for(n = i; n <= i + p; n++)
   {
      sq += MathPow(Close[n] - fx[n], 2);
   }
   sq = MathSqrt(sq / (p + 1)) * kstd;
   std = iStdDev(NULL, 0, p, MODE_SMA, 0, PRICE_CLOSE, i) * kstd;
   for(n = i; n <= i + p; n++)
   {
      sqh[n] = fx[n] + sq;
      sql[n] = fx[n] - sq;
      stdh[n] = fx[n] + std;
      stdl[n] = fx[n] - std;
   } 
//-------------------------------------------------------------------------------
   ObjectMove("pr" + sName, 0, Time[p], fx[p]);
//----------------------------------------------------------------------------------------------------------------------------
   return(0);
}
//==========================================================================================================================   

Comments