!_MTF_ATR
Download scriptThis tool is designed to provide traders with a visual representation of market volatility across different time horizons simultaneously. It specifically calculates the Average True Range (ATR)—a common technical analysis metric that measures how much an asset typically moves in a given period—and displays it on your chart in a way that allows for "Multi-Timeframe" analysis.
How the Logic Works
1. Cross-Timeframe Data Synchronization The primary purpose of this script is to let you view volatility data from a higher timeframe while staying on your current, smaller chart. For example, if you are actively trading on a 1-minute chart but want to keep an eye on the overall volatility of the 1-hour or Daily periods, this tool "fetches" that larger data and aligns it with your current screen.
2. Dynamic Period Selection The logic is built to be flexible. It allows you to define exactly which timeframe you want to track by selecting from common trading intervals (such as M1, H1, D1, etc.). The script then identifies the specific price points for those larger periods and pulls the corresponding volatility calculations into the current view.
3. Smoothing and Visual Continuity When viewing data from a large timeframe (like a Weekly chart) on a small timeframe (like a 5-minute chart), the data can often appear "choppy" or jump erratically because one candle on a large timeframe represents many candles on a small one. To solve this, the script includes a specific correction logic:
- It identifies the relationship between your current chart and the target timeframe.
- It "smears" or carries over the value from the larger timeframe across the smaller bars.
- This ensures that the resulting line remains smooth and continuous, providing a steady visual signal rather than a jagged set of disconnected points.
4. User-Defined Parameters The logic relies on two main user inputs to customize the experience:
- TimeFrame: Determines which higher-level period you want to observe.
- Periods: Sets the look-back window for the ATR calculation (e.g., looking at the average movement over the last 12 candles of that chosen timeframe).
Summary for the User
Essentially, this script acts as a bridge. It looks "ahead" or "higher up" at the market's broader movements, calculates how much the price is swinging on those larger scales, and then paints that information onto your current screen in a smooth, readable line. This helps you understand if the current market environment is volatile or calm based on the bigger picture, without having to constantly switch back and forth between different tabs or charts.
//+------------------------------------------------------------------+
//| #MTF_ATR.mq4 |
//|------------------------------------------------------------------+
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 HotPink
//---- 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.
---------------------------------------*/
extern int TimeFrame = 0;
extern int Periods=12;
double ExtMapBuffer1[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicator line
SetIndexStyle(0,DRAW_LINE,EMPTY,1);
SetIndexBuffer(0,ExtMapBuffer1);
//---- name for DataWindow and indicator subwindow label
switch(TimeFrame)
{
case 1 : string TimeFrameStr="Period_M1"; break;
case 5 : TimeFrameStr="Period_M5"; break;
case 15 : TimeFrameStr="Period_M15"; break;
case 30 : TimeFrameStr="Period_M30"; break;
case 60 : TimeFrameStr="Period_H1"; break;
case 240 : TimeFrameStr="Period_H4"; break;
case 1440 : TimeFrameStr="Period_D1"; break;
case 10080 : TimeFrameStr="Period_W1"; break;
case 43200 : TimeFrameStr="Period_MN1"; break;
default : TimeFrameStr="Current Timeframe";
}
IndicatorShortName(" #MTF_ATR ( "+Periods+" ) ( "+TimeFrameStr+" ) ");
}
//----
return(0);
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start()
{
datetime TimeArray[];
int i,limit,y=0,counted_bars=IndicatorCounted();
// Plot defined time frame on to current time frame
ArrayCopySeries(TimeArray,MODE_TIME,Symbol(),TimeFrame);
limit= Bars-1;
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 time frame
Rule 3: Use 'y' for your indicator's shift value
**********************************************************/
ExtMapBuffer1[i]=iATR(Symbol(),TimeFrame,Periods,y);
}
//FIX for display
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++
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];
********************************************************/
ExtMapBuffer1[i]=ExtMapBuffer1[0];
//----
} } }
//+++++++++++++++++++++++++++++++++++++++++++++++++++
//END of fix
return(0);
}
//+------------------------------------------------------------------+
Comments