Ehlers COG

This tool is designed to provide traders with a visual representation of the "Center of Gravity" (COG) for a specific price range on a chart. Instead of simply looking at the current price, this logic aims to identify the weighted average price over a designated period, helping to highlight where the "balance of power" sits in the market.

Core Logic and Functionality

The script operates by analyzing a user-defined number of candles (the period). It treats each candle within that window not equally, but rather assigns a weight based on its recency. The primary goal is to determine the mathematical center of price action during that timeframe.

How the calculation works:

  1. Price Identification: For every candle in the selected period, the script identifies the midpoint between the highest and lowest prices reached during that specific time frame.
  2. Weighted Accumulation: The script then creates two separate totals:
    • A "Numerator" sum, which multiplies each midpoint by its position in the sequence (giving more importance to more recent data).
    • A "Denominator" sum, which is the simple total of all midpoints in that period.
  3. The Final Result: By dividing the weighted sum by the total sum and applying a directional multiplier, the script generates a single value representing the Center of Gravity.

Visual Representation on the Chart

To make this data easy for a trader to interpret, the script produces two distinct lines:

  • Primary COG Line: This represents the calculated center of gravity based on the current price action. It allows you to see where the "true" average is trending relative to the current market price.
  • Lagging/Comparison Line: The script also plots a secondary line that follows the primary calculation, providing a visual reference point to help identify shifts in momentum or trend reversals as the Center of Gravity moves.

Practical Application for Traders

By using this indicator, a visitor can look beyond the standard "highs" and "lows" of a candle. Rather than reacting to every small price fluctuation, the logic smooths out the noise to show the underlying equilibrium of the market. When the price moves significantly away from this Center of Gravity line, it may indicate overextended conditions; when the price gravitates back toward it, it suggests a return to the "weighted" norm of the current trend.

Miscellaneous
Implements a curve of type %1
7 Views
0 Downloads
0 Favorites
Ehlers COG
//+------------------------------------------------------------------+
//|                                                   Ehlers COG.mq4 |
//+------------------------------------------------------------------+ 
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1  Red
#property indicator_color2  Blue

extern int COGPeriod = 10;

double COG[], COG1[], wrkArray[];
//+------------------------------------------------------------------+
int init()
{
   IndicatorBuffers(2);
   IndicatorDigits( MarketInfo( Symbol(), MODE_DIGITS ));
   SetIndexStyle(0, DRAW_LINE);
   SetIndexStyle(1, DRAW_LINE); 

   if( !SetIndexBuffer( 0, COG ) &&
       !SetIndexBuffer( 1, COG1 ))
      Print( "cannot set indicator buffers!" );

   SetIndexDrawBegin( 0, COGPeriod );
   SetIndexDrawBegin( 1, COGPeriod ); 

   IndicatorShortName( "Ehlers COG" );

   return(0);
}
//+------------------------------------------------------------------+
// Calculating Ehlers Center of Gravity
//+------------------------------------------------------------------+ 
// COG = -1 * (NUM / DENOM)
// NUM = the sum of [PRICE[i] * (i + 1)] from 0 to N
// DENOM = the sum of PRICE[i] from 0 to N
//   where N equals the number of periods (COGPeriod)
//         PRICE[0] equals the current bar 
//         PRICE[1] equals the previous bar
//         PRICE[n] equals the price from n bars previous
//+------------------------------------------------------------------+
int start()
{
   int i, j, limit, iWrk00; 
   int countedBars = IndicatorCounted();
   double dWrk00, num, denom;

   limit = Bars - countedBars;
   for( i = 0; i < limit; i++ )
   {
      if((i >= COGPeriod) || (limit < 10))
      { 
         num = 0;
         denom = 0;
         for( j = 0; j < COGPeriod; j++)
         {
            if(limit < 10)
               iWrk00 = i + j;
            else
               iWrk00 = i - COGPeriod + j; 
            dWrk00 = (High[iWrk00] + Low[iWrk00]) / 2;
            num = num + (1 + j) * dWrk00;
            denom = denom + dWrk00;
         }
         if(limit < 10)
            iWrk00 = i;
         else 
            iWrk00 = i - COGPeriod;
         COG[iWrk00] = -num / denom;
         if(iWrk00 > 0)
            COG1[iWrk00 - 1] = COG[iWrk00];
      }
   }

   return(0);
}
//+------------------------------------------------------------------+ It doesn't seem to be charting out for me 

Comments