MACD-4H+
Download scriptThis technical tool is designed to provide traders with a "multi-timeframe" perspective on market momentum. While most standard indicators only show you what is happening on the current chart you are looking at, this script allows you to visualize high-level trends from a larger timeframe while staying on a smaller, more detailed one.
Core Logic Overview
The primary purpose of this script is to synchronize data from a higher time period (such as a 4-hour chart) and display it alongside your current lower-timeframe price action. This helps traders identify whether the broader market trend supports the immediate price movements they are seeing on their screen.
How It Works
- Timeframe Synchronization: The script identifies a specific "higher" timeframe (defined by a repetition factor). It calculates where the bars of that larger timeframe align with your current chart's timeline.
- Data Extraction: Instead of simply calculating the Moving Average Convergence Divergence (MACD) based on the candles you see in front of you, it reaches back into the data of the higher timeframe. It finds the corresponding point in time for a large "parent" bar and extracts its specific MACD values.
- Visual Representation: The script then maps those larger trend values onto your current chart. You see a histogram and signal line that represent the momentum of the 4-hour (or other specified) period, even if you are currently trading on a 5-minute or 1-hour chart.
Key Benefits for the User
By using this logic, you can avoid "getting lost in the noise" of small price fluctuations. If the higher timeframe MACD indicates a strong upward trend, but your current chart shows a small dip, the script helps you see that the larger trend is still intact. It essentially provides a "big picture" overlay, ensuring that your short-term trades are aligned with the long-term direction of the market.
//+------------------------------------------------------------------+
//| MACD+ 4H.mq4 |
//| Copyright © 2006, ETV1 |
//| MACD from old timeframe |
//| v.10 enable for visual mode |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, ETV1"
//---- indicator settings
#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 Green
#property indicator_color2 Red
#property indicator_color3 Sienna
#property indicator_width1 2
#property indicator_width2 2
#property indicator_width3 0
//---- indicator parameters
extern int FastEMA=9;
extern int SlowEMA=26;
extern int SignalSMA=9;
extern int AppliedPrice = 0; // 0=PRICE_CLOSE; 1=PRICE_OPEN; 5=PRICE_TYPICAL
extern int VarPeriod=240; //old timeframe
//PERIOD_M5 5 5 minutes
//PERIOD_M15 15 15 minutes
//PERIOD_M30 30 30 minutes
//PERIOD_H1 60 1 hour
//PERIOD_H4 240 4 hours
//PERIOD_D1 1440 1 day
//PERIOD_W1 10080 1 week
//PERIOD_MN1 43200 1 month
//---- indicator buffers
double MacdBuffer[],SignalBuffer[];
int p; // current period
int k; // repetition factor old timeframe
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- drawing settings
IndicatorBuffers(4);
SetIndexStyle(0,DRAW_HISTOGRAM);
SetIndexStyle(1,DRAW_HISTOGRAM);
SetIndexStyle(2,DRAW_LINE);
SetIndexStyle(2, 12);
SetIndexStyle(3, 12);
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 + "min)");
SetIndexLabel(0, "MACD 4H(" + VarPeriod + ")");
SetIndexLabel(1, "Signal 4H(" + VarPeriod + ")");
//---- initialization done
return(0);
}
//+------------------------------------------------------------------+
//| Moving Averages Convergence/Divergence (MACD) for old timeframe |
//+------------------------------------------------------------------+
int start()
{
int i, limit;
int counted_bars = IndicatorCounted();
//---- check for possible errors
if(counted_bars < 0) return(-1);
if(counted_bars <= SlowEMA)
limit = Bars - SlowEMA ;
else
limit = Bars - counted_bars;
p = Period();
k = VarPeriod/p; // repetition factor old timeframe
i=limit-1;
while (i>=0)
{
int bb=HighBar(Symbol(), 0, i, VarPeriod); // definition of HighBar, having symbol, lowTF,
// and bar's number in lowTF, calculated highTF;
// (symbol, lowTF, bar's number, highTF)
if (bb==-1) continue;
MacdBuffer[i] =iMACD(Symbol(), VarPeriod, FastEMA, SlowEMA, SignalSMA, AppliedPrice, 0, bb);
SignalBuffer[i]=iMACD(Symbol(),VarPeriod, FastEMA, SlowEMA, SignalSMA, AppliedPrice, 1, bb);
i--;
}
//---- done
return(0);
}
//+------------------------------------------------------------------+
int HighBar(string symbol,int TFLow,int BarLow,int TFHigh)
// definition of HighBar, having symbol, lowTF,
// and bar's number in lowTF, calculated highTF;
// (symbol, lowTF, bar's number, highTF)
{
datetime BarTimeLow=iTime(symbol, TFLow, BarLow); // time's value of bar means in BarLow
int error=GetLastError();
if(error==4066) return (-1);
int res=iBarShift(symbol, TFHigh, BarTimeLow, false);// displacement of bar means in BarTimeLow
error=GetLastError();
if(error==4066) return (-1);
return (res);
}
//+------------------------------------------------------------------+
Comments