MTF_MA-4H_v1
Download scriptThis tool is designed for traders who need to see "the big picture" without constantly switching between different charts. Its primary purpose is to project moving averages from a higher, longer timeframe onto your current, lower timeframe chart.
How the Logic Works
Multi-Timeframe Integration Most standard indicators only look at the data available on your current screen (e.g., if you are looking at a 5-minute chart, the indicator only sees 5-minute price movements). This script changes that behavior by allowing you to pull data from a different "parent" timeframe—such as the 4-hour or Daily charts—and overlay it directly on your active screen.
Time Synchronization Because the data is coming from a different timeframe, the script performs a specific mathematical conversion to ensure the lines align correctly. It identifies the exact time of the current candle on your small chart, looks up what the corresponding candle was on the larger timeframe, and then calculates the moving average based on that specific point in time. This ensures that the lines you see are accurate reflections of the higher timeframe's trend.
Dual-Line Analysis The script is capable of displaying two different moving averages simultaneously:
- A Fast Moving Average: This reacts more quickly to recent price changes.
- A Slow Moving Average: This provides a broader view of the long-term trend.
By viewing these two lines together, you can better identify "crossovers"—moments where the short-term momentum shifts against the long-term trend, which is a common signal used in professional trading strategies.
Key Features for the User
- Customizable Calculation Methods: You can choose how the average is calculated (e.g., Simple, Exponential, Smoothed, or Linear Weighted) to suit your preferred trading style.
- Flexible Timeframes: You can define exactly which timeframe you want to "borrow" data from (such as 4-hour, Daily, Weekly, etc.).
- Visual Clarity: The tool draws these averages as distinct lines on your chart, clearly colored and styled to differentiate between the fast and slow data points.
In summary, this script acts as a "bridge," allowing you to monitor major market trends while still executing trades on smaller, more detailed price movements.
//+------------------------------------------------------------------+
//| Ma_4H_all_timeframe.mq4 |
//| Copyright © 2006, lukas1 |
//| MA from all timeframe |
//| enable for visual mode |
//+------------------------------------------------------------------+
#property copyright "Copyright © february 2007, lukas1"
//---- indicator settings
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 MediumSeaGreen
#property indicator_width1 2
#property indicator_width2 2
//---- indicator parameters
extern int Ma=8;
extern int SecondMa=21;
extern int Mode=1; /*MODE_SMA 0 Ïðîñòîå ñêîëüçÿùåå ñðåäíåå
MODE_EMA 1 Ýêñïîíåíöèàëüíîå ñêîëüçÿùåå ñðåäíåå
MODE_SMMA 2 Ñãëàæåííîå ñêîëüçÿùåå ñðåäíåå
MODE_LWMA 3 Ëèíåéíî-âçâåøåííîå ñêîëüçÿùåå ñðåäíåå */
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
extern bool second_Ma=true;
double MaFBuffer[], MaSBuffer[];
int p; // current period
int k; // repetition factor old timeframe
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- drawing settings
SetIndexStyle(0, DRAW_LINE);
SetIndexStyle(1, DRAW_LINE);
SetIndexDrawBegin(0, SecondMa);
SetIndexDrawBegin(1, SecondMa);
IndicatorDigits(Digits + 1);
//---- indicator buffers mapping
SetIndexBuffer(0, MaFBuffer);
SetIndexBuffer(1, MaSBuffer);
//---- name for DataWindow and indicator subwindow label
IndicatorShortName("MA 4H (" + Ma + ", " + SecondMa +
", " + VarPeriod + " min)");
SetIndexLabel(0, "MA 4H fast(" + Ma + ", " + VarPeriod + ")");
SetIndexLabel(1, "MA 4H slow(" + SecondMa + ", " + VarPeriod + ")");
//----
Comment("Ma_4H_all_timeframe(" + Ma + ", " + SecondMa +
", " + VarPeriod + " min)");
//---- initialization done
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
Comment("");
//----
return(0);
}
//+-------------------------------------------------------------+
//| Moving Averages (MA) for all timeframe |
//+-------------------------------------------------------------+
int start()
{
int i, limit;
int counted_bars=IndicatorCounted();
//---- check for possible errors
if(counted_bars < 0)
return(-1);
if(counted_bars<=SecondMa)
limit=Bars - SecondMa ;
else
limit=Bars - counted_bars;
p=Period();
k=VarPeriod/p; // repetition factor old timeframe
i=limit - 1;
while(i>=0)
{
// definition of HighBar, having symbol, lowTF,
int bb=HighBar(Symbol(), 0, i, VarPeriod);
// and bar's number in lowTF, calculated highTF;
// (symbol, lowTF, bar's number, highTF)
if(bb==-1)
continue;
MaFBuffer[i]=iMA(Symbol(), VarPeriod, Ma, 0, Mode, AppliedPrice, bb);
if(second_Ma)
MaSBuffer[i]=iMA(Symbol(), VarPeriod, SecondMa, 0, Mode, AppliedPrice, bb);
i--;
}
//---- done
return(0);
}
//+------------------------------------------------------------------+
//| Definition of HighBar, having symbol, lowTF, and bar's number |
//| in lowTF, calculated highTF; |
//| (symbol, lowTF, bar's number, highTF) |
//+------------------------------------------------------------------+
int HighBar(string symbol,int TFLow,int BarLow,int TFHigh)
{
// time's value of bar means in BarLow
datetime BarTimeLow=iTime(symbol, TFLow, BarLow);
int error=GetLastError();
if(error==4066)
return(-1);
// displacement of bar means in BarTimeLow
int res=iBarShift(symbol, TFHigh, BarTimeLow, false);
error=GetLastError();
if(error==4066)
return(-1);
return(res);
}
//+------------------------------------------------------------------+
Comments