MACD-4H-v10
Download scriptThis tool is designed to provide traders with a specialized visual perspective on market momentum by analyzing data from a higher timeframe while viewing it on a lower, more active chart. It essentially allows you to "see the big picture" without having to constantly switch back and forth between different timeframes.
Core Concept: Multi-Timeframe Integration
The primary purpose of this script is to calculate the MACD (Moving Average Convergence Divergence)—a popular momentum indicator—using data from a specific larger timeframe (set by default to 4 hours) and project those results onto your current live chart. This helps traders identify whether the long-term trend supports their current short-term trades.
How the Logic Works
To achieve this, the script performs several automated steps:
- Timeframe Synchronization: The tool takes the time of the candles on your current chart and looks "backward" to find the corresponding point in time on the larger timeframe. This ensures that the data being displayed is perfectly aligned with the present moment.
- Data Retrieval: Instead of just looking at the most recent price, the script identifies the specific historical bar on the higher timeframe (e.g., the 4-hour mark) that corresponds to the current candle you are viewing.
- Dual Indicator Calculation: It calculates two main components for the user:
- The MACD Line: Represents the difference between two moving averages, showing the strength of the momentum.
- The Signal Line: A smoothed version of the MACD line used to identify potential turning points or changes in trend direction.
- Visual Representation: The results are drawn as a histogram and a line in a separate window below your price chart. This allows you to see the relationship between different timeframes at a single glance.
Key Benefits for the User
- Contextual Awareness: It helps prevent "trading against the trend." For example, if the 4-hour MACD is showing strong upward momentum, it provides confirmation for bullish trades on smaller minutes or hour charts.
- Seamless Workflow: By automating the conversion between timeframes, the script removes the manual effort of comparing different windows, reducing the risk of human error and providing a cleaner, more unified visual workspace.
//+------------------------------------------------------------------+
//| MACD 4H.mq4 |
//| Copyright © 2006, lukas1 |
//| MACD from old timeframe |
//| v.10 enable for visual mode |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, lukas1"
//---- indicator settings
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 MediumPurple
#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; // 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
SetIndexStyle(0, DRAW_HISTOGRAM);
SetIndexStyle(1, 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