Indicators: Center of Gravity J. F. Ehlers

 

Center of Gravity J. F. Ehlers:

Center of Gravity actually has a zero lag and allows to define turning points precisely. This indicator is the result of Ehler's study of adaptive filters.

The indicator Center of Gravity allows to identify main pivot points almost without any lag.

The idea of calculating a center of gravity appeared from the investigation of lags of different filters with the finite impulse response (FIR) in accordance with the relative amplitude of filter coefficients. SMA (Simple Moving Average) is a FIR-filter, in which all coefficients have one and the same value. As a result the center of gravity of SMA is an exact center of the filter. WMA (Weighted Moving Average) is a FIR-filter, in which the last price change is weighted through the filter length, and so on.

The values of weighting are coefficients of filters. Coefficients of WMA filters can be presented as contours of a triangle. The center of gravity is on the 1/3 of the triangle base length. Thus WMA gravity center is shifted to the right with respect to the center of gravitation of SMA of the same length, which gives us a smaller lag. For all examples with FIR filters the sum of productions of coefficients and the price must be divided by the sum of coefficients for preservation of original prices.

Author: Nikolay Kositsin

Fig.1 Center Of Gravity indicator

 
It's useful! ...I'd like to know the criteria for colour change.
 

Hello,

This oscillator is a great one, but I have problems with it.

The signal line's end, wich is calculated by the last candle is not shown, so it always shows red at the present time. When I reset the oscillator it shows good values for a moment with the end of the signal line, but then switches back to red, (even if it has to show green).

What should i do to eliminate this problem?

I've tried to re-debug it but it didn't help.

Greets,

InfiniteDesign 

 
In his opinion this indicator which works with mehor and symbol and timeframe.
What the best values ​​for the parameters of this indicator?
 

Many many thanks for this great indicator.

I found the previous version as interesting and useful as the new one, or in fact even more so..

Could you please port the old one to mq5 as well ? I'd highly appreciate if it can be done.


Cheers, and thanks in advance !!

 
This indicator is non-deterministic. Ask for the same datapoints twice, at different times, and you'll get different answers. The drift is larger closer to the current time. The inconsistency can be as large as 3.7 x 10^-04.
 
InfiniteDesign:

Hello,

This oscillator is a great one, but I have problems with it.

The signal line's end, wich is calculated by the last candle is not shown, so it always shows red at the present time. When I reset the oscillator it shows good values for a moment with the end of the signal line, but then switches back to red, (even if it has to show green).

What should i do to eliminate this problem?

I've tried to re-debug it but it didn't help.

Greets,

InfiniteDesign 

I have the same problem...
 

I tried to create a Signal from this indicator, but I was unable...

Any help?


//+------------------------------------------------------------------+
//|                                                          COG.mqh |
//|                                                        Bruno Pio |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Bruno Pio"
#property link      "http://www.mql5.com"
#property version   "1.00"
#include "..\ExpertSignal.mqh"   // CExpertSignal is in the file ExpertSignal
#property tester_indicator "CenterOfGravity.ex5"
// wizard description start
//+------------------------------------------------------------------+
//| Description of the class                                         |
//| Title=Signals of Center of Gravity                               |
//| Type=SignalAdvanced                                              |
//| Name=My_COG                                                      |
//| ShortName=CG                                                     |
//| Class=COG                                                        |
//| Page=Not needed                                                  |
//| Parameter=Period_,int,10,Indicator averaging period              |
//| Parameter=SmoothPeriod,int,3,Signal line smoothing period        |
//| Parameter=MA_Method_,ENUM_MA_METHOD,MODE_EMA,Signal Method       |
//| Parameter=AppliedPrice,int,1,Price constant                      |
//+------------------------------------------------------------------+
// wizard description end
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
class COG : public CExpertSignal
  {
private:
CiCustom             m_COG;               // The indicator as an object
//--- Configurable module parameters
   int               m_Period_;           // Indicator averaging period
   int               m_SmoothPeriod;      // Signal line smoothing period 
   ENUM_MA_METHOD    m_MA_Method_;        // Signal line averaging method
   int               m_AppliedPrice;      // Price constant
public:
                     COG(void);
                    ~COG(void);
//--- Checking correctness of input data
   bool              ValidationSettings();
//--- Creating indicators and timeseries for the module of signals
   bool              InitIndicators(CIndicators *indicators);
//--- Access to indicator data
   double            CG(const int index)                 const { return(m_COG.GetData(0,index)); }
   double            Signal(const int index)             const { return(m_COG.GetData(1,index)); }   
//--- Checking buy and sell conditions
   virtual int       LongCondition();
   virtual int       ShortCondition();
//--- Methods for setting
   void              Period_(int value)               { m_Period_=value;        }
   void              SmoothPeriod(int value)          { m_SmoothPeriod=value;   }
   void              MA_Method_(ENUM_MA_METHOD value) { m_MA_Method_=value;     }
   void              AppliedPrice(int value)          { m_AppliedPrice=value;   }
protected:
   //--- Creating indicator
   bool              CreateCOG(CIndicators *indicators);



  };
//+------------------------------------------------------------------+
//| Constructor                                                      |
//+------------------------------------------------------------------+
COG::COG(void) :           m_Period_(10),                // Indicator averaging period
                           m_SmoothPeriod(3),            // Signal line smoothing period 
                           m_MA_Method_(MODE_EMA),       // Signal line averaging method
                           m_AppliedPrice(1)             // Price constant
  {
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
COG::~COG()
  {
  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Checks input parameters and returns true if everything is OK     |
//+------------------------------------------------------------------+
bool COG:: ValidationSettings()
  {
   //--- Call the base class method
   if(!CExpertSignal::ValidationSettings())  return(false);
   //--- Check periods, number of bars for the calculation of the MA >=1
   if(m_Period_<1)
     {
      PrintFormat("Incorrect value set for one of the period! Period_=%d",
                  m_Period_);
      return false;
     }
//--- Check periods, number of bars for the calculation of the MA >=1
   if(m_SmoothPeriod<1)
     {
      PrintFormat("Incorrect value set for one of the period! m_SmoothPeriod=%d",
                  m_SmoothPeriod);
      return false;
     }
//--- Fast MA smoothing type must be one of the four values of the enumeration
   if(m_MA_Method_!=MODE_SMA && m_MA_Method_!=MODE_EMA && m_MA_Method_!=MODE_SMMA && m_MA_Method_!=MODE_LWMA)
     {
      PrintFormat("Invalid type of smoothing of the fast MA!");
      return false;
     }
//--- m_AppliedPrice must be validy
   if(m_AppliedPrice<1 || m_AppliedPrice>11) 
     {
      PrintFormat("Invalid type of Price!");
      return false;
     }
//--- All checks are completed, everything is ok
   return true;
  }
//+------------------------------------------------------------------+
//| Creates indicators                                               |
//| Input:  a pointer to a collection of indicators                  |
//| Output: true if successful, otherwise false                      |
//+------------------------------------------------------------------+
bool COG::InitIndicators(CIndicators *indicators)
  {
//--- Standard check of the collection of indicators for NULL
   if(indicators==NULL) return(false);
//--- Initializing indicators and timeseries in additional filters
   if(!CExpertSignal::InitIndicators(indicators)) return(false);
//--- Creating our indicators
   if(!CreateCOG(indicators))                  return(false);   
//--- Reached this part, so the function was successful, return true
   return(true);
  }
//+------------------------------------------------------------------+
//| Creates the "COG" indicator                                      |
//+------------------------------------------------------------------+
bool COG::CreateCOG(CIndicators *indicators)
  {
//--- Checking the pointer
   if(indicators==NULL) return(false);
//--- Adding an object to the collection
   if(!indicators.Add(GetPointer(m_COG)))
     {
      printf(__FUNCTION__+": Error adding an object of the COG");
      return(false);
     }
//--- Setting parameters of the COG
   MqlParam parameters[5];
//---
   parameters[0].type=TYPE_STRING;
   parameters[0].string_value="CenterOfGravity.ex5";
   parameters[1].type=TYPE_INT;
   parameters[1].integer_value=m_Period_;                 // Period
   parameters[2].type=TYPE_INT;
   parameters[2].integer_value=m_SmoothPeriod;            // Signal line smoothing period
   parameters[3].type=TYPE_INT;
   parameters[3].integer_value=m_MA_Method_;              // Signal line averaging method
   parameters[4].type=TYPE_INT;
   parameters[4].integer_value=m_AppliedPrice;            // Price constant
//--- Object initialization  
   if(!m_COG.Create(m_symbol.Name(),0,IND_CUSTOM,5,parameters))
     {
      printf(__FUNCTION__+": Error initializing the object of the COG");
      return(false);
     }
//--- Number of buffers
   if(!m_COG.NumBuffers(2)) return(false);
//--- Reached this part, so the function was successful, return true
   return(true);
  }
//+------------------------------------------------------------------+
//| Returns the strength of the buy signal                           |
//+------------------------------------------------------------------+
int COG::LongCondition()
  {
   int signal=0;
//--- For operation with ticks idx=0, for operation with formed bars idx=1
   int idx=StartIndex();
//--- Values of COGs at the last formed bar
   double last_fast_value=CG(idx);
   double last_slow_value=Signal(idx);
//--- Values of COGs at the last but one formed bar
   double prev_fast_value=CG(idx+1);
   double prev_slow_value=Signal(idx+1);   
//---If CG > Signal && CG-1 < Signal-1
   if((last_fast_value>last_slow_value) && (prev_fast_value<prev_slow_value))
     {
      signal=100; // There is a signal to buy
     }
//--- Return the signal value
   return(signal);
  }
//+------------------------------------------------------------------+
//| Returns the strength of the sell signal                          |
//+------------------------------------------------------------------+
int COG::ShortCondition()
  {
   int signal=0;
//--- For operation with ticks idx=0, for operation with formed bars idx=1
   int idx=StartIndex();
//--- Values of COGs at the last formed bar
   double last_fast_value=CG(idx);
   double last_slow_value=Signal(idx);
//--- Values of COGs at the last but one formed bar
   double prev_fast_value=CG(idx+1);
   double prev_slow_value=Signal(idx+1);   
//---If CG < Signal && CG-1 > Signal-1
   if((last_fast_value<last_slow_value) && (prev_fast_value>prev_slow_value))
     {
      signal=100; // There is a signal to sell
     }
//--- Return the signal value
   return(signal);
  }
 
interesting thing... need to determine the algorithm in combat conditions
 

The indicator is compiled with no error:

and it works -

------------------

Just need to use the fixed smoothalgorithms.mqh file - look at post


Indicators: T3Taotra_HTF
Indicators: T3Taotra_HTF
  • 2016.06.30
  • www.mql5.com
T3Taotra_HTF: Author: Nikolay Kositsin...
 
Bruno Pio #:
Ugyanez a problémám van...
I have received so many valuable things from the community that I want to give something back.

This is a brilliant indicator (if you also consider the higher trends) I am sending the improved version, the end of which is no longer just red, and it updates properly on the live chart.

Use the CODE button (Alt-S) when inserting code.

A moderator corrected the formatting this time. Please format code properly in future; posts with improperly formatted code may be removed.

/*
 * The SmoothAlgorithms.mqh file must be placed 
 * to terminal_data_folder\MQL5\Include
 */
//+------------------------------------------------------------------+
//|                                            Center of Gravity.mq4 |
//|                      Copyright © 2007, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"
//---- indicator version
#property version   "1.00"
//---- drawing the indicator in a separate window
#property indicator_separate_window
//---- three buffers are used for calculation and drawing the indicator
#property indicator_buffers 3
//---- two plots are used
#property indicator_plots   2
//+----------------------------------------------+
//|  Indicator drawing parameters                |
//+----------------------------------------------+
//---- drawing indicator 1 as a three-colored line
#property indicator_type1 DRAW_COLOR_LINE
//---- used as a three-colored line colors
#property indicator_color1 Gray,Lime,Red
//---- line of the indicator 1 is a continuous curve
#property indicator_style1  STYLE_SOLID
//---- thickness of line of the indicator 1 is equal to 1
#property indicator_width1  1
//---- displaying of the indicator label 1
#property indicator_label1  "Center of Gravity"
//+----------------------------------------------+
//|  Indicator drawing parameters                |
//+----------------------------------------------+
//---- drawing indicator 2 as a line
#property indicator_type2   DRAW_LINE
//---- blue color is used as the color of the indicator line
#property indicator_color2  Blue
//---- line of the indicator 2 is a continuous curve
#property indicator_style2  STYLE_SOLID
//---- thickness of line of the indicator 2 is equal to 1
#property indicator_width2  1
//---- displaying of the indicator line label 2
#property indicator_label2  "Signal Line"
//---- line style is a dot-dash curve
#property indicator_style2 STYLE_DASHDOTDOT
//+-----------------------------------+
//|  Indicators input parameters      |
//+-----------------------------------+
enum Applied_price_ //Type of constant
  {
   PRICE_CLOSE_ = 1,     //PRICE_CLOSE
   PRICE_OPEN_,          //PRICE_OPEN
   PRICE_HIGH_,          //PRICE_HIGH
   PRICE_LOW_,           //PRICE_LOW
   PRICE_MEDIAN_,        //PRICE_MEDIAN
   PRICE_TYPICAL_,       //PRICE_TYPICAL
   PRICE_WEIGHTED_,      //PRICE_WEIGHTED
   PRICE_SIMPLE_,//PRICE_SIMPLE_
   PRICE_QUARTER_,       //PRICE_QUARTER_
   PRICE_TRENDFOLLOW0_,  //PRICE_TRENDFOLLOW0_
   PRICE_TRENDFOLLOW1_   //PRICE_TRENDFOLLOW1_
  };
input int Period_=10;                          // Indicator averaging period
input int SmoothPeriod=3;                      // Signal line smoothing period
input ENUM_MA_METHOD MA_Method_=MODE_SMA;      // Signal line averaging method
input Applied_price_ AppliedPrice=PRICE_CLOSE_;// Price constant
/* , used for calculation of the indicator ( 1-CLOSE, 2-OPEN, 3-HIGH, 4-LOW, 
  5-MEDIAN, 6-TYPICAL, 7-WEIGHTED, 8-SIMPLE, 9-QUARTER, 10-TRENDFOLLOW, 11-0.5 * TRENDFOLLOW.) */
//+-----------------------------------+
//---- declaration of dynamic arrays that further
//---- will be used as indicator buffers
double Ext1Buffer[];
double Ext2Buffer[];
double ColorExt2Buffer[];
//---- Declaration of the integer variables for the start of data calculation
int StartBar;
//+------------------------------------------------------------------+
//| iPriceSeries function description                                |
//| Moving_Average class description                                 | 
//+------------------------------------------------------------------+ 
#include <SmoothAlgorithms.mqh> 
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+  
void OnInit()
  {
//---- initialization of constants
   StartBar=Period_;
//---- set MAMABuffer dynamic array as indicator buffer
   SetIndexBuffer(0,Ext1Buffer,INDICATOR_DATA);
//---- creating label to display in DataWindow
   PlotIndexSetString(0,PLOT_LABEL,"Center of Gravity");
//---- shifting the start of drawing of the indicator
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,StartBar);
//---- turning a dynamic array into a color index buffer   
   SetIndexBuffer(1,ColorExt2Buffer,INDICATOR_COLOR_INDEX);
//---- shifting the start of drawing of the indicator
   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,StartBar+1);

//---- set FAMABuffer dynamic array as indicator array
   SetIndexBuffer(2,Ext2Buffer,INDICATOR_DATA);
//---- creating label to display in DataWindow
   PlotIndexSetString(2,PLOT_LABEL,"Signal Line");
//---- shifting the start of drawing of the indicator
   PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,StartBar+1);

//---- initializations of variable for indicator short name
   string shortname;
   StringConcatenate(shortname,"Center of Gravity(",Period_,")");
//---- creating name for displaying in a separate sub-window and in a tooltip
   IndicatorSetString(INDICATOR_SHORTNAME,shortname);
//---- set accuracy of displaying of the indicator values
   IndicatorSetInteger(INDICATOR_DIGITS,0);
//----
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,    // number of bars in history at the current tick
                const int prev_calculated,// number of bars calculated at previous call
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---- checking the number of bars to be enough for the calculation
   if(rates_total<StartBar) return(0);

//---- Declaration of variables with a floating point  
   double price_,sma,lwma;
//---- Declaration of integer variables
   int first1,first2,first3,bar;

//---- Initialization of the indicator in the OnCalculate() block
   if(prev_calculated>rates_total || prev_calculated<=0)// checking for the first start of calculation of an indicator
     {
      first1=0; // starting number for calculation of all first loop bars
      first2=Period_+1; // starting number for calculation of all signal line bars
      first3=Period_+SmoothPeriod+3; // starting number for calculation of all coloring loop bars
     }
   else // starting number for calculation of new bars
     {
      first1=prev_calculated-1;
      first3=first1;
     }

//---- FIX (additive): a jelvonal (Signal Line) MASeries hívásának "begin" paramétere mindig
//---- ugyanaz az ÁLLANDÓ megbízhatósági küszöb (Period_+1) kell, hogy legyen - eredetileg élő
//---- ticknél tévesen a mindig változó "first1"-re (magára az aktuális gyertyaindexre) volt
//---- állítva, ami minden élő ticknél megegyezett a feldolgozott gyertyával, összezavarva a
//---- belső simító-osztály "meddig megbízható az adat" logikáját - emiatt volt instabil/hibás
//---- a jelvonal élő értéke, és emiatt "javult meg" csak TF-váltás (teljes újraszámolás) után.
   first2=Period_+1;

//---- declaration of variables of the Moving_Average class
   static CMoving_Average MA,LWMA,SIGN;

//---- Main cycle of calculation of the channel center line
   for(bar=first1; bar<rates_total; bar++)
     {
      //---- Call of the PriceSeries function to get the input price 'Series'
      price_=PriceSeries(AppliedPrice,bar,open,low,high,close);

      sma=MA.MASeries(0,prev_calculated,rates_total,Period_,MODE_SMA,price_,bar,false);
      lwma=LWMA.MASeries(0,prev_calculated,rates_total,Period_,MODE_LWMA,price_,bar,false);

      Ext1Buffer[bar]=sma*lwma/_Point;
      Ext2Buffer[bar]=SIGN.MASeries(first2,prev_calculated,rates_total,SmoothPeriod,MA_Method_,Ext1Buffer[bar],bar,false);
     }

//---- Main loop of the signal line coloring
   for(bar=first3; bar<rates_total; bar++)
     {
      ColorExt2Buffer[bar]=0;
      if(Ext1Buffer[bar]<Ext2Buffer[bar])
         ColorExt2Buffer[bar]=2;
      else ColorExt2Buffer[bar]=1;
     }
//----    
   return(rates_total);
  }
//+------------------------------------------------------------------+