MACD_4H[1]

This custom indicator is designed to provide traders with a unique perspective on market momentum by synchronizing data from different timeframes. While standard indicators only show you the immediate price action of your current chart, this tool allows you to visualize the Moving Average Convergence Divergence (MACD) of a specific, larger timeframe—in this case, optimized for the 4-hour cycle—regardless of what smaller timeframe you are currently viewing.

Core Logic and Functionality

The primary purpose of this script is "Timeframe Scaling." It solves a common challenge in trading where a person might want to see the "big picture" trends (like the 4-hour trend) while executing trades on a much faster chart (like the 15-minute or 1-hour chart).

How it works internally:

  • Multi-Timeframe Alignment: The script automatically detects your current chart's timeframe and compares it against a target period (set to 240 minutes, which represents 4 hours). It then performs the necessary mathematical calculations to "stretch" or "compress" that 4-hour data so it fits perfectly onto your smaller chart.
  • Smart Scaling: The logic includes an automated switch. If you are looking at a timeframe smaller than 4 hours, it scales the data upwards. If you happen to be on a timeframe larger than 4 hours, it scales the data downwards. This ensures that the MACD lines remain smooth and relevant to your current view.
  • Standard MACD Calculation: Even though it pulls from a different timeframe, it maintains the integrity of the standard MACD formula. It uses two moving averages (Fast and Slow) to calculate the "MACD Line" and a third "Signal Line."
  • Visual Output: The script generates two distinct visual elements:
    1. A Histogram: Representing the main MACD value.
    2. A Line: Representing the Signal line.

Practical Application for Traders

Instead of having to switch back and forth between different tabs or windows to see what the 4-hour trend is doing, this tool overlays that information directly onto your active chart. It allows you to monitor long-term momentum and potential trend reversals in real-time while focusing on shorter-term price movements. This helps in ensuring that your smaller trades are always aligned with the broader market direction.

Indicators Used
MACD Histogram
Miscellaneous
Implements a curve of type %1
14 Views
0 Downloads
0 Favorites
MACD_4H[1]
//+------------------------------------------------------------------+
//|                                                      MACD_4H.mq4 |
//|                                         Copyright © 2006, lukas1 |
//|                                        MACD from given timeframe |
//+------------------------------------------------------------------+
#property  copyright "Copyright © 2006, lukas1"
#property  link      "http://www.alpari-idc.ru/"
//---- indicator settings
#property  indicator_separate_window
#property  indicator_buffers 2
#property  indicator_color1  Blue
#property  indicator_color2  Red
// #property  indicator_width1  2
//---- indicator parameters
extern int FastEMA = 12;
extern int SlowEMA = 26;
extern int SignalSMA = 9;
extern int AppliedPrice = 0;
extern int VarPeriod = 240;     // çàäàííûé òàéìôðåéì 
//---- indicator buffers
double     MacdBuffer[], SignalBuffer[];
double     MA1[], MA2[];
int p;  // òåêóùèé ïåðèîä
int k;  // êðàòíîñòü ïåðèîäà
int i, j;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- drawing settings
   p = Period();
   SetIndexStyle(0, DRAW_HISTOGRAM);
   SetIndexStyle(1, DRAW_LINE);
   SetIndexDrawBegin(0, SlowEMA);
   SetIndexDrawBegin(1, SlowEMA);   
   IndicatorDigits(Digits + 1);
//---- indicator buffers mapping
   SetIndexBuffer(0, MacdBuffer);
   SetIndexBuffer(1, SignalBuffer);
//---- name for DataWindow and indicator subwindow label
   IndicatorShortName("MACD_4H (" + FastEMA + "," + SlowEMA + "," + SignalSMA + 
                      "," + VarPeriod + ")");
   SetIndexLabel(0, "MACD_4H(" + VarPeriod + ")");
   SetIndexLabel(1, "Signal_4H(" + VarPeriod + ")");
//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//| Moving Averages Convergence/Divergence (MACD) for given timeframe|
//+------------------------------------------------------------------+
int start()
  {
   int limit;
   int counted_bars = IndicatorCounted();
//---- check for possible errors
   if(counted_bars < 0)
       return(-1);
   if(counted_bars <= SlowEMA)
       limit = Bars - SlowEMA - 1;
   else
       limit = Bars - counted_bars;   
   p = Period();
//----
   if(counted_bars == 0)
       if(p <= VarPeriod)
           k = VarPeriod / p; // êðàòíîñòü ïåðèîäà ââåðõ
       else
           k = p / VarPeriod; // êðàòíîñòü ïåðèîäà âíèç   
//----
   if(p <= VarPeriod)
     {
       for(i = 0; i < limit; i++)
         {
           j = i / k;
           MacdBuffer[i] = iMACD(NULL, VarPeriod, FastEMA, SlowEMA, SignalSMA, 
                                 AppliedPrice, 0, j);
           SignalBuffer[i] = iMACD(NULL, VarPeriod, FastEMA, SlowEMA, SignalSMA, 
                                 AppliedPrice, 1, j);
         }
     } 
   else
     {
       for(i = 0; i < limit; i++)
         {
           MacdBuffer[i] = iMACD(NULL, VarPeriod, FastEMA, SlowEMA, SignalSMA, 
                                 AppliedPrice, 0, i*k);
           SignalBuffer[i] = iMACD(NULL, VarPeriod, FastEMA, SlowEMA, SignalSMA, 
                                 AppliedPrice, 1, i*k);
         } 
     }
//---- done
   return(0);
  }
//+------------------------------------------------------------------+

Comments