Русский 中文 Español Deutsch 日本語 Português
A Trader's Assistant Based on Extended MACD Analysis

A Trader's Assistant Based on Extended MACD Analysis

MetaTrader 4Examples | 29 August 2008, 09:16
3 935 5
Genkov
Genkov

Introduction

MACD histogram is recognized as one of the best trend-of-prices indicators. As recommended by Dr. A. Elder, the trend of prices is determined by the last two bars on the corresponding timeframe. However, we cannot say anything else using those two bars. Of course, an experienced trader looking at a histogram can imagine a bit more basing him or herself upon intuition. Well, if we take the last three bars for our analysis, the quantity and quality of our information will be extended considerably.


The script algorithm is based on comparison of differences between the increments of the zeroth, the first and the second bar of the MACD histogram. For example, if the value of the difference between the current and the preceding bar exceeds the value of the difference between the values of the preceding and the pre-preceding bar, then the trend is growing with positive acceleration. If the difference is smaller than that, it means that the trend slows its growing (damping trend), and you should think of closing your long position.

A Glimpse of the Script Itself:

You can use the script in at least two directions.

First, it can be used as a trading assistant to define the market enter/exit points, as well as to decide about the amount of traded lots (volume) according to the position, the direction and the positive/negative acceleration of the histogram volume.

Second, you can use the script while analyzing historical data. If you attach the script to the chart in order to analyze the data, the window of properties will appear, in which you can specify (change) the number of the initial bar and any timeframe to be analyzed. If you use the script in real-time trading, you should set the initial bar number to zero (the last current bar) or to one (at user's wish), and you should specify the timeframe that you use to open or hold your positions.

//+------------------------------------------------------------------+
//|                                                 TrendMACD v5.mq4 |
//|MACD histogram analysis by the last three bars             Genkov |
//|                                                     genkov@bk.ru |
//+------------------------------------------------------------------+
#property copyright "Genkov"
#property link      "genkov@bk.ru"
//if you comment on the below line, the properties window will not be displayed
#property show_inputs   
extern int a = 0;            // here you can specify any initial bar to be analyzed
//extern int period = 1;     //  1 minute 
//extern int period = 5;     //  5 minutes 
//extern int period = 15;    // 15 minutes 
extern int period = 30;      //  30 minutes
//extern int period = 60;    // comment on the timeframe you need
//extern int period = 240;   // 4 hours
//extern int period = 1440;  // day
//extern int period = 10080; // week
//extern int period = 43200; // month
//+------------------------------------------------------------------+

We will place the main variable 'a' for the initial bar number and variable 'period' for timeframes in the script's global variables. The internal variable for calculations and comparisons will be placed after the launching function:

int start()
  {
    int b,c;
    b=a+1; c=b+1;
//----
double Macd_h1_a= iMACD(NULL,period,12,26,9,PRICE_CLOSE,MODE_MAIN,a);
double Macd_h1_b= iMACD(NULL,period,12,26,9,PRICE_CLOSE,MODE_MAIN,b);
double Macd_h1_c= iMACD(NULL,period,12,26,9,PRICE_CLOSE,MODE_MAIN,c);

For the visual control of calculations, you can put here the Print() operator that can be useful when analyzing the past events (on history). Multiplier "*1000" (or you may use any other) is used for better readability of the obtained results.

Print("   Macd_h1_c= ",Macd_h1_c*1000,"   Macd_h1_b= ",
          Macd_h1_b*1000,"   Macd_h1_a= ",Macd_h1_a*1000);

If you need to disable messaging (Alerts), you should replace one with zero in the "int Message = 1;" operator. At the same time, to display the information somewhere, you should comment on the operators of "int Message = 1;" and "Message == 1;" and replace all later operators Alert() with operators Print(), then you will be able to see the information in the "Expert Advisors" tab of the client terminal.

The script provides the possibility to calculate the script operating time using the GetTickCount() function. For this purpose, place the following operator at the beginning of the program:

int start=GetTickCount();
//…
//…, and at the end of the program:
Alert("   Calculation time  ", GetTickCount()-start," millisec"); 

The main code of the script consists of blocks comparing the statuses (the position and the size of ratios of differences between the values of the histogram on the analyzed bars).

In the script, the main relevant possible transient states (situations) are analyzed. For example:


In total, there are 6 comparison blocks in the program: blocks 1-2 for MACD below the axis:

// ----------------------for MACD below the axis-------------------------1--&
  if(Macd_h1_c<0.0&&Macd_h1_a<0.0&&Macd_h1_a<0.0) 
   {
     //increment of bar "0" as related to the first one
     double r1=MathAbs(MathAbs(Macd_h1_a)-MathAbs(Macd_h1_b));
     //increment of bar "1" as related to bar "2" 
     double r2=MathAbs(MathAbs(Macd_h1_b)-MathAbs(Macd_h1_c));
// -- MACD is below - the trend is moving down
   if(Macd_h1_c>Macd_h1_b&&Macd_h1_b>Macd_h1_a)
    {
     if(r1>r2) 
     {
     Alert("On bar ",a," MACD<0 = ",Macd_h1_a,
           "  The trend is moving down with acceleration");
     }
    if(r1<r2) 
     {
     Alert("On bar ",a," MACD<0 = ",Macd_h1_a,
           "  The trend is moving down with slowing down");
     }
    if((r1==r2)||MathAbs(r1-r2)<0.000015)
     {
     Alert("On bar ",a," MACD<0 = ",Macd_h1_a,
           "  The trend is moving down with constant acceleration");
     }
    }
// -- MACD is below - the trend is moving up
   if(Macd_h1_c<Macd_h1_b&&Macd_h1_b<Macd_h1_a)
    {
    if(r1>r2) 
     {
     Alert("On bar ",a," MACD<0 =",Macd_h1_a,
           "  The trend is moving up with acceleration");
     }
    if(r1<r2) 
     {
     Alert("On bar ",a," MACD<0 = ",Macd_h1_a,
           "  The trend is moving up with slowing down");
     }
    if((r1==r2)||MathAbs(r1-r2)<0.000015)
     {
     Alert("On bar ",a," MACD<0 = ",Macd_h1_a,
           "  The trend is moving up with constant acceleration");
     }
    }
// --- MACD is below - the trend reverses down    
   if(Macd_h1_c<Macd_h1_b&&Macd_h1_b>Macd_h1_a)
    {
    if(r1>r2) 
     {
     Alert("On bar ",a," MACD<0 = ",Macd_h1_a,
           "  The trend reverses down with acceleration");
     }
    if(r1<r2)
     {
     Alert("On bar ",a," MACD<0 = ",Macd_h1_a,
           "  The trend reverses down with slowing down");
     }
    }
// --- MACD is below - the trend reverses up
   if(Macd_h1_c>Macd_h1_b&&Macd_h1_b<Macd_h1_a)
    {
    if(r1>r2)
     {
     Alert("On bar ",a," MACD<0 = ",Macd_h1_a,
           "  The trend reverses up with acceleration");
     }
     if(r1<r2) 
     {
     Alert("On bar ",a," MACD<0 = ",Macd_h1_a,
           "  The trend reverses up with slowing down");
     }
    }
   if(MathAbs(MathAbs(Macd_h1_a)-MathAbs(Macd_h1_b))<0.0002 && 
      MathAbs(MathAbs(Macd_h1_c)-MathAbs(Macd_h1_b))<0.0002)
    {
    Alert("On bar ",a,", it is Flat! within the range of:  ",Macd_h1_c,
          "   ",Macd_h1_b,"   ",Macd_h1_a);
    }
   }

Block 2-3 is very similar - for MACD above the axis (see the attached files).

Block 3-4 is used for up-crossing the axis:

// ------------for up-crossing the axis------------------3--&
   if(Macd_h1_c<0.0 && Macd_h1_b<0.0 && Macd_h1_a>0&&  // c<0 b<0
      Macd_h1_c<Macd_h1_b&&Macd_h1_b<Macd_h1_a)  
    {
     r1=MathAbs(Macd_h1_a)+MathAbs(Macd_h1_b);
     r2=MathAbs(Macd_h1_c)-MathAbs(Macd_h1_b);
    if(MathAbs(r1)>MathAbs(r2))
     {
     Alert("On bar ",a," the trend is up-crossing axis with acceleration ");
     }
    if(MathAbs(r1)<MathAbs(r2))
     {
     Alert("On bar ",a," the trend is up-crossing axis with slowing down ");
     }
    }
//---     
   if(Macd_h1_c<0.0 && Macd_h1_b>0.0 && Macd_h1_a>0&&  // b>0 a>0
      Macd_h1_c<Macd_h1_b&&Macd_h1_b<Macd_h1_a)
    {
     r1=MathAbs(Macd_h1_a)-MathAbs(Macd_h1_b);
     r2=MathAbs(Macd_h1_c)+MathAbs(Macd_h1_b);
    if(MathAbs(r1)>MathAbs(r2))
     {
     Alert("On bar ",a," the trend is up-crossing axis with acceleration");
     }
    if(MathAbs(r1)<MathAbs(r2))
     {
     Alert("On bar ",a," the trend is up-crossing axis with slowing down");
     }
    }

Similarly, block 4-5 is used to down-cross the axis (see the attached files).

Block 5-6 is used for the reversal under the axis, block 6-7 is for the reversal above the axis. The blocks are commented on and can be used by those who like to analyze historical data (see the attached files).

An example of the script working on history:




If you want to use the script for trading, its heading should look as follows:

//+------------------------------------------------------------------+
//|                                                 TrendMACD v6.mq4 |
//|     for trading                                                  |
//+------------------------------------------------------------------+
 
#property copyright "Genkov"
#property link      "genkov@bk.ru"
extern int a = 0;      // here you can specify the initial bar to be analyzed
extern int period = 30;       // set the value of the period you need
//+------------------------------------------------------------------+
int start()
  { 
   int b,c; b=a+1; c=b+1;
//----
  double Macd_h1_a= iMACD(NULL,period,12,26,9,PRICE_CLOSE,MODE_MAIN,a);
  double Macd_h1_b= iMACD(NULL,period,12,26,9,PRICE_CLOSE,MODE_MAIN,b);
  double Macd_h1_c= iMACD(NULL,period,12,26,9,PRICE_CLOSE,MODE_MAIN,c);
…
….

The other comparison blocks are the same as those given in the basic version.

An example of how the script works for trading:





Please note that the values of variables 'a' and 'period' must be set before you start using the script! Besides, it would be more reasonable in the trading version to set the initial bar for one, since the zero bar may turn out to be incomplete as of the moment of data requesting, for example, at the beginning or in the middle of a period.

As a conclusion, I would like to express my hope for that the usage of this script will help both the beginners in trading and the experienced traders, and that it will also decrease the amount of errors by at least 10 percent. Happy trading to everybody!

Translated from Russian by MetaQuotes Ltd.
Original article: https://www.mql5.com/ru/articles/1519

Attached files |
TrendMACD_v5.mq4 (11.36 KB)
TrendMACD_v6.mq4 (9.63 KB)
Last comments | Go to discussion (5)
Mehmet Bastem
Mehmet Bastem | 30 Aug 2008 at 07:08

Please translate english

if(Macd_h1_c<Macd_h1_b&&Macd_h1_b<Macd_h1_a&&(r1>r2))
{
Alert("Íà ",a," áàğå MACD >0 = ",Macd_h1_a,
" Òğåíä èäåò ââåğõ //''ñ óñêîğåíèåì");

slacktrader
slacktrader | 2 Sep 2008 at 17:02

I tried for myself to implement MACD extension which shows differences of current and previous MACD histogram value as another histogram. These values are small to MACD histogram so I multiplied it with 5

You can see it with blue color. It gives pretty good signals.



MACD Extended

[Deleted] | 5 Sep 2008 at 12:08
Mehmet:

Please translate english

if(Macd_h1_c<Macd_h1_b&&Macd_h1_b<Macd_h1_a&&(r1>r2))
{
Alert("Íà ",a," áàğå MACD >0 = ",Macd_h1_a,
" Òğåíä èäåò ââåğõ //''ñ óñêîğåíèåì");

The comments on the code have been translated. The updated files are attached to this article.
[Deleted] | 24 Jan 2009 at 11:53
navodar:

I tried for myself to implement MACD extension which shows differences of current and previous MACD histogram value as another histogram. These values are small to MACD histogram so I multiplied it with 5

You can see it with blue color. It gives pretty good signals.



MACD Extended

Do you trade with this indicator? What are you success results?

Andrew Thompson
Andrew Thompson | 17 Mar 2013 at 10:00
navodar:

I tried for myself to implement MACD extension which shows differences of current and previous MACD histogram value as another histogram. These values are small to MACD histogram so I multiplied it with 5

You can see it with blue color. It gives pretty good signals.


MACD Extended

Very nice signals - did you develop this any further?
Expert Advisors Based on Popular Trading Systems and Alchemy of Trading Robot Optimization (Part VII) Expert Advisors Based on Popular Trading Systems and Alchemy of Trading Robot Optimization (Part VII)
In this article, the author gives an example Expert Advisor meeting the requirements stated in the Rules of the Automated Trading Championship 2008
Grouped File Operations Grouped File Operations
It is sometimes necessary to perform identical operations with a group of files. If you have a list of files included into a group, then it is no problem. However, if you need to make this list yourself, then a question arises: "How can I do this?" The article proposes doing this using functions FindFirstFile() and FindNextFile() included in kernel32.dll.
Individual Psychology of a Trader Individual Psychology of a Trader
A portrait of a trader's behavior on the financial market. Author's own menu from the book "Как играть и выигрывать на бирже" ("How to speculate on stock exchange and win") by A. Elder.
Method of Determining Errors in Code by Commenting Method of Determining Errors in Code by Commenting
The article describes a method of searching the errors in the MQL4 code that is based on commenting. This method is found to be a useful one in case of problems occuring during the compilation caused by the errors in a reasonably large code.