#MTF_zlagMACD_v3

Download script
Author: Copyright � 2006, Keris2112
#MTF_zlagMACD_v3

This script is a specialized technical analysis tool designed for the MetaTrader 4 platform. It functions as a multi-timeframe "Zero Lag MACD" indicator, which helps traders visualize momentum and trend directions across different time periods simultaneously.

Core Functionality

The primary purpose of this script is to allow a trader to view MACD data from a higher timeframe (such as a Daily or Weekly chart) while they are looking at a lower timeframe (such as a 15-minute or Hourly chart). This provides a "big picture" perspective, helping traders identify whether the broader market trend aligns with their current short-term trade.

Key Logic Components

Multi-Timeframe Synchronization The script automatically fetches data from a specific timeframe defined by the user. It maps that data onto the current chart so that the visual lines correspond correctly to the time periods on your screen, ensuring that the trend information remains accurate and synchronized.

Zero Lag Calculation By utilizing a "Zero Lag" logic (via an underlying zlagmacd call), the script aims to reduce the typical delay found in standard moving averages. This helps in identifying price shifts more quickly than traditional indicators might allow.

Dynamic Visual Scaling (Auto-Multiplier) One of the most distinct features of this script is its ability to automatically manage how large the indicator lines appear on your screen:

  • Automatic Mode: The script monitors the historical highs and lows of the MACD data. If the values are too small to see clearly or too large for the display window, it automatically calculates a "Multiplier." It shrinks or expands the visual lines so they always fit perfectly within your chart's designated area without being cut off or appearing as flat lines.
  • Manual Mode: For users who prefer total control, there is an option to disable the automatic scaling and manually set a specific multiplier to adjust the thickness or height of the lines.

Visual Customization The script is designed for clarity. It simplifies the visual output by:

  • Displaying only the primary trend lines (removing standard histograms).
  • Rounding the data to two decimal places for a cleaner look.
  • Automatically updating the label on the chart to show which timeframe is currently being displayed, making it easy for the trader to keep track of their settings at a glance.

Summary for the Trader

Essentially, this tool acts as a "lens" that lets you see the momentum of a larger time period on your current screen. It handles the complex math of aligning different timeframes and automatically scales the visual lines so they are always easy to read, regardless of how volatile the market is.

Miscellaneous
Implements a curve of type %1
10 Views
0 Downloads
0 Favorites
#MTF_zlagMACD_v3
//+------------------------------------------------------------------+
//|                                             #MTF_zlagMACD_v3.mq4 |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Keris2112"
#property link      "http://www.forex-tsd.com"
#property link      "HOH CHEE YEN"
/* version 3 (HCY)
| Set Auto_Multiplier to true and Auto_Multiplier_LEVEL to 0.2~0.9 for
| automatic multiplier. Or set Auto_Multiplier to false and increase
| Manual_Multiplier for manual adjustment of MACD lines.
*/

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Orange
#property indicator_color2 Orange
#property indicator_level1 0
// Added (HCY)
#property indicator_width1 1
#property indicator_style2 2
// Set window range for proper line display (HCY)
#property indicator_maximum 1
#property indicator_minimum -1

//---- input parameters
/* Settings for different periods (Own preferences): (HCY)
MN,WK,D1:22,11,8 -- D1:11,22,8
WK,D1,H4:30,13,10 -- H4:13,30,10
D1,H4,H1:24,11,8 -- H1:11,24,8
H4,H1,M15:15,7,5 -- M15:7,15,5
H1,M15,M5:12(or 13),6,4 -- M5:6,12,4
M15,M5,M1:15,7,5 -- M1:7,15,5
*/
extern int TimeFrame=0;
extern int FastEMA=12;
extern int SlowEMA=26;
extern int SignalEMA=9;
// For automatic Multiplier (HCY)
extern int CountBars=300;
// Automatic adjust Multiplier for proper line display (HCY)
extern bool Auto_Multiplier=true;
// Level is between 0.2 - 0.9 when indicator maximum level is 1 (HCY)
extern double Auto_Multiplier_LEVEL=0.7;
double AutoMultiplierLEVEL=0.0;
double AutoMultiplier=5.0;
// Manually increase this number to enlarge its lines when Auto_Multiplier is off (HCY)
extern double Manual_Multiplier=5;
double Max_Multiplier_Level=0.9;
double Min_Multiplier_Level=0.2;
double Max_Signal=0.0;
datetime Max_Signal_Time=0;
//---- indicator buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];

//---- input parameters
/*************************************************************************
PERIOD_M1   1
PERIOD_M5   5
PERIOD_M15  15
PERIOD_M30  30 
PERIOD_H1   60
PERIOD_H4   240
PERIOD_D1   1440
PERIOD_W1   10080
PERIOD_MN1  43200
You must use the numeric value of the timeframe that you want to use
when you set the TimeFrame' value with the indicator inputs.
**************************************************************************/
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   string short_name;
//---- indicator line
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexBuffer(1,ExtMapBuffer2);
//   SetIndexStyle(0,DRAW_SECTION,STYLE_SOLID,1);
//   SetIndexStyle(1,DRAW_SECTION,STYLE_SOLID,1);
   // Draw lines only (HCY)
   SetIndexStyle(0,DRAW_LINE);
   SetIndexStyle(1,DRAW_LINE);
/* Original settings (HCY) 
   SetIndexStyle(0,DRAW_HISTOGRAM,EMPTY,2);
   SetIndexStyle(1,DRAW_LINE,EMPTY,2);
*/
   SetIndexDrawBegin(0,SlowEMA);
   SetIndexDrawBegin(1,SlowEMA);
   // No value displayed at left corner (HCY)
   SetIndexLabel(0,NULL);
   SetIndexLabel(1,NULL);
/* Original settings (HCY)
   SetIndexLabel(0,"MACD");
   SetIndexLabel(1,"Signal");
*/
   // Show only 2 decimal points (HCY)
   IndicatorDigits(2);

//---- name for DataWindow and indicator subwindow label
   int TF=TimeFrame;
   if (TimeFrame==0) TF=Period();
   switch(TF)
   {
      case 1 : string TimeFrameStr="M1"; break;
      case 5 : TimeFrameStr="M5"; break;
      case 15 : TimeFrameStr="M15"; break;
      case 30 : TimeFrameStr="M30"; break;
      case 60 : TimeFrameStr="H1"; break;
      case 240 : TimeFrameStr="H4"; break;
      case 1440 : TimeFrameStr="D1"; break;
      case 10080 : TimeFrameStr="W1"; break;
      case 43200 : TimeFrameStr="MN1"; break;
      default : TimeFrameStr="CUR";
   } 

   // Set shorter indicator name (HCY)
//   short_name="MzM";
   // Show only TimeFrame (HCY)
   IndicatorShortName(TimeFrameStr);
/* Original settings (HCY) 
   short_name="MTF ZeroLag MACD";
   IndicatorShortName(short_name+ " " + TimeFrameStr+ " | " +FastEMA + ", " + SlowEMA + ", " + SignalEMA+" |  ");  
*/
   // Initialize variable (HCY)
   AutoMultiplierLEVEL=Auto_Multiplier_LEVEL;
   Max_Multiplier_Level=indicator_maximum*0.9;
   Min_Multiplier_Level=indicator_maximum*0.2;
   Max_Signal_Time=Time[CountBars];
//----
   return(0);
  }

// For automatic multiplier (HCY)
void GetBufferMax(double ExtMapBuffer[],int& Max,double& MaxSignal) {
   int max1,max2;
   double max_1,max_2;
   max1=ArrayMaximum(ExtMapBuffer,CountBars,1);
   max2=ArrayMinimum(ExtMapBuffer,CountBars,1);
   max_1=MathAbs(ExtMapBuffer[max1]);
   max_2=MathAbs(ExtMapBuffer[max2]);
   if (max_1>max_2) {
      Max=max1;
      MaxSignal=max_1;
      }
   else if (max_1==max_2) {
      Max=MathMin(max1,max2);
      MaxSignal=max_1;
      }
   else {
      Max=max2;
      MaxSignal=max_2;
      }
   }

//+------------------------------------------------------------------+
//| MTF Moving Average                                   |
//+------------------------------------------------------------------+
int start()
  {
   datetime TimeArray[];
   int    i,limit,y=0,counted_bars=IndicatorCounted();
   // For automatic multiplier (HCY)
   int old_y=-1,Max1=0,Max2=0;
   double Max_1=0.0,Max_2=0.0;
    
// Plot defined timeframe on to current timeframe   
   ArrayCopySeries(TimeArray,MODE_TIME,Symbol(),TimeFrame); 

   // Modified (HCY)
   limit=Bars-counted_bars+TimeFrame/Period();
//   limit=Bars-counted_bars;

   for(i=0,y=0;i<limit;i++)
   {
   if (Time[i]<TimeArray[y]) y++; 
   
 /***********************************************************   
   Add your main indicator loop below.  You can reference an existing
      indicator with its iName  or iCustom.
   Rule 1:  Add extern inputs above for all neccesary values   
   Rule 2:  Use 'TimeFrame' for the indicator timeframe
   Rule 3:  Use 'y' for the indicator's shift value
 **********************************************************/  

   // For automatic multiplier (HCY)
   if (Auto_Multiplier) {
      if (i==0) {
         if (counted_bars==0) {
            Max_Signal=0.0;
            }
         else if (Time[CountBars]>Max_Signal_Time) {
            GetBufferMax(ExtMapBuffer1,Max1,Max_1);
            GetBufferMax(ExtMapBuffer2,Max2,Max_2);
            if (Max_1>Max_2) {
               Max_Signal_Time=Time[Max1];
               Max_Signal=Max_1;
               }
            else if (Max_1==Max_2) {
               if (Max1<Max2)
                  Max_Signal_Time=Time[Max1];
               else
                  Max_Signal_Time=Time[Max2];
               Max_Signal=Max_1;
               }
            else {
               Max_Signal_Time=Time[Max2];
               Max_Signal=Max_2;
               }
            }
         }
      ExtMapBuffer1[i]=(iCustom(NULL,TimeFrame,"zlagmacd",FastEMA,SlowEMA,SignalEMA,0,y)*AutoMultiplier);
      ExtMapBuffer2[i]=(iCustom(NULL,TimeFrame,"zlagmacd",FastEMA,SlowEMA,SignalEMA,1,y)*AutoMultiplier);
      if (i<=CountBars && old_y!=y) {
         old_y++;
         double Signal=MathMax(MathAbs(ExtMapBuffer1[i]),MathAbs(ExtMapBuffer2[i]));
         if (Signal>Max_Signal) {
            Max_Signal=Signal;
            Max_Signal_Time=Time[i];
            }
         }
      if (i==limit-1) {
       if (Max_Signal>indicator_maximum || Max_Signal<AutoMultiplierLEVEL) {
         if (Auto_Multiplier_LEVEL>Max_Multiplier_Level)
            AutoMultiplierLEVEL=Max_Multiplier_Level;
         else if (Auto_Multiplier_LEVEL<Min_Multiplier_Level)
            AutoMultiplierLEVEL=Min_Multiplier_Level;
         else
            AutoMultiplierLEVEL=Auto_Multiplier_LEVEL;
         double OldAutoMultiplier=AutoMultiplier;
         AutoMultiplier=AutoMultiplierLEVEL/Max_Signal;
         Max_Signal=Max_Signal*AutoMultiplier;
         for(int j=0;j<Bars-1;j++) {
            ExtMapBuffer1[j]=ExtMapBuffer1[j]*AutoMultiplier;
            ExtMapBuffer2[j]=ExtMapBuffer2[j]*AutoMultiplier;
            }
         AutoMultiplier=AutoMultiplier*OldAutoMultiplier;
         }
       }
      }
   else {
      // For proper line display (HCY)
      ExtMapBuffer1[i]=(iCustom(NULL,TimeFrame,"zlagmacd",FastEMA,SlowEMA,SignalEMA,0,y)*Manual_Multiplier);
      ExtMapBuffer2[i]=(iCustom(NULL,TimeFrame,"zlagmacd",FastEMA,SlowEMA,SignalEMA,1,y)*Manual_Multiplier);
/* Original (HCY)
   ExtMapBuffer1[i]=iCustom(NULL,TimeFrame,"zlagmacd",FastEMA,SlowEMA,SignalEMA,0,y);
   ExtMapBuffer2[i]=iCustom(NULL,TimeFrame,"zlagmacd",FastEMA,SlowEMA,SignalEMA,1,y);
*/
      }
   }  
     
/* Disabled (HCY)
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   if (TimeFrame>Period()) {
     int PerINT=TimeFrame/Period()+1;
     datetime TimeArr[]; ArrayResize(TimeArr,PerINT);
     ArrayCopySeries(TimeArr,MODE_TIME,Symbol(),Period()); 
     for(i=0;i<PerINT+1;i++) {if (TimeArr[i]>=TimeArray[0]) {
//----
 /************************************************ by Raff   
    Refresh buffers:         buffer[i] = buffer[0];
 ********************************************************/  

/* Disabled (HCY)
   ExtMapBuffer1[i]=ExtMapBuffer1[0];
   ExtMapBuffer2[i]=ExtMapBuffer2[0];

//----
   } } }
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*/

   return(0);
  }
//+------------------------------------------------------------------+

Comments