ATR_Chart_Labeled
Download scriptThis script is designed for the MetaTrader 4 platform to provide traders with a clear visual representation of market volatility. Specifically, it calculates and displays the Average True Range (ATR), which is a standard technical analysis tool used to measure how much an asset's price typically moves over a specific period.
Core Functionality
The primary purpose of this script is to simplify how you view volatility data. Instead of making you manually calculate averages or look at complex sub-windows, the script automatically processes market price history and displays two key pieces of information directly on your main trading chart:
- Current ATR: The average volatility of the most recent completed price movement.
- Previous ATR: The average volatility of the preceding period.
How the Logic Works
The script follows a systematic process to turn raw market data into readable text on your screen:
- Data Collection: It scans the "High," "Low," and "Close" prices of the candles. It looks at the distance between the current candle's range and the previous candle's closing price to determine the "True Range."
- Smoothing (Moving Average): Once the individual ranges are identified, the script applies a Moving Average calculation (based on a user-defined period, defaulting to 14). This smooths out random spikes and provides a more reliable average of market activity.
- Normalization: The script automatically adjusts these values based on the specific currency pair or asset you are trading (identifying whether it should use 2 or 4 decimal places), ensuring the numbers displayed are accurate to the market's requirements.
Visual Interface and User Experience
To ensure a clean and professional trading environment, the script includes several automated management features:
- Dynamic Labeling: It creates two distinct text labels on the chart. These labels are positioned automatically in the corner of your screen so they do not obstruct the price candles.
- Automatic Cleanup: The script is designed to be "tidy." When you switch between different charts or symbols, it automatically detects and deletes old labels from previous assets. This prevents your screen from becoming cluttered with outdated information.
- Real-Time Updates: As new candles form, the script continuously recalculates the data, ensuring that the "Current ATR" value is always up to date with the latest market movement.
Summary for the Trader
Essentially, this tool acts as an automated assistant. It monitors the "heartbeat" of the market volatility in the background and highlights the current and previous average ranges in a readable format. This helps you quickly gauge whether the market is currently trending calmly or experiencing high-intensity fluctuations.
//+------------------------------------------------------------------+
//| ATR_Chart_Labeled.mq4 |
//| Copyright © 2008, Robert Hill |
//| |
//| Display the Average True Range on the chart for past 2 periods |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, Robert Hill"
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 DodgerBlue
//---- input parameters
extern int AtrPeriod=14;
extern string to="---Text Object Settings---";
extern int Text_X_Offset = 20;
extern int CommentTxtSize = 10;
extern color CommentColor = White;
//---- buffers
double ChartBuffer[];
double AtrBuffer[];
double TempBuffer[];
int Comment1Y, Comment2Y;
string Comment1Label, Comment2Label;
string Object_ID = "ATRC_";
string short_name;
double myPoint;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- 1 additional buffer used for counting.
IndicatorBuffers(3);
//---- indicator line
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0, ChartBuffer);
SetIndexBuffer(1,AtrBuffer);
SetIndexBuffer(2,TempBuffer);
//---- name for DataWindow and indicator subwindow label
short_name="ATR("+AtrPeriod+")";
IndicatorShortName(short_name);
SetIndexLabel(0,short_name);
//----
SetIndexDrawBegin(0,AtrPeriod);
DeleteBadLabels();
DeleteExistingLabels();
SetupLabels();
myPoint = SetPoint(Symbol());
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
ClearLabels();
DeleteExistingLabels();
//----
return(0);
}
//+------------------------------------------------------------------+
//| Average True Range |
//+------------------------------------------------------------------+
int start()
{
int i,counted_bars=IndicatorCounted();
//----
if(Bars<=AtrPeriod) return(0);
if (Comment1Y < 10)
{
SetupLabels();
ClearLabels();
DeleteExistingLabels();
SetupLabels();// Make sure label settings are OK
}
else
{
ClearLabels();
}
//---- initial zero
if(counted_bars<1)
for(i=1;i<=AtrPeriod;i++) AtrBuffer[Bars-i]=0.0;
//----
i=Bars-counted_bars-1;
while(i>=0)
{
double high=High[i];
double low =Low[i];
if(i==Bars-1) TempBuffer[i]=high-low;
else
{
double prevclose=Close[i+1];
TempBuffer[i]=MathMax(high,prevclose)-MathMin(low,prevclose);
}
i--;
}
//----
if(counted_bars>0) counted_bars--;
int limit=Bars-counted_bars;
for(i=0; i<limit; i++)
AtrBuffer[i]=NormalizeDouble(iMAOnArray(TempBuffer,Bars,AtrPeriod,0,MODE_SMA,i), Digits) / myPoint;
//----
OutputComment1ToChart("Curr ATR : " + DoubleToStr(AtrBuffer[0], 0));
OutputComment2ToChart("Prev ATR : " + DoubleToStr(AtrBuffer[1], 0));
return(0);
}
double SetPoint(string mySymbol)
{
double mPoint, myDigits;
myDigits = MarketInfo (mySymbol, MODE_DIGITS);
if (myDigits < 4)
mPoint = 0.01;
else
mPoint = 0.0001;
return(mPoint);
}
void ClearLabels()
{
string mComment = " ";
OutputLabelToChart(Comment1Label, Comment1Y, CommentTxtSize, CommentColor, mComment);
OutputLabelToChart(Comment2Label, Comment2Y, CommentTxtSize, CommentColor, mComment);
}
void DeleteBadLabels()
{
int objLabels = ObjectsTotal(OBJ_LABEL);
string objName;
if (objLabels > 0)
{
for (int i = objLabels; i >= 0;i--)
{
objName = ObjectName(i);
if (StringFind(objName,Object_ID, 0) >= 0)
{
// Found 2 Play object, now check for wrong Symbol
if (StringFind(objName,Symbol(), 0) < 0)
{
ObjectDelete(objName);
}
}
}
}
}
void DeleteExistingLabels()
{
int objLabels = ObjectsTotal(OBJ_LABEL);
string objName;
if (objLabels > 0)
{
for (int i = objLabels; i >= 0;i--)
{
objName = ObjectName(i);
if (StringFind(objName,Object_ID, 0) >= 0)
{
// Found 2 Play object, now check for Symbol
if (StringFind(objName,Symbol(), 0) >= 0)
{
ObjectDelete(objName);
}
}
}
}
}
void SetupLabels()
{
Comment1Y = 12;
Comment2Y = Comment1Y + CommentTxtSize + 4;
Comment1Label = Object_ID + Symbol() + "_Comment1";
Comment2Label = Object_ID + Symbol() + "_Comment2";
}
void OutputLabelToChart(string LabelName, int LabelY, int LabelTxtSize, color LabelColor, string LabelStr)
{
if(ObjectFind(LabelName) != 0)
{
ObjectCreate(LabelName, OBJ_LABEL, 0, 0, 0);
ObjectSet(LabelName, OBJPROP_CORNER, 0);
ObjectSet(LabelName, OBJPROP_XDISTANCE, Text_X_Offset);
ObjectSet(LabelName, OBJPROP_YDISTANCE, LabelY);
}
ObjectSetText(LabelName, LabelStr, LabelTxtSize, "Arial Bold", LabelColor);
}
void OutputComment1ToChart(string mComment)
{
OutputLabelToChart(Comment1Label, Comment1Y, CommentTxtSize, CommentColor, mComment);
}
void OutputComment2ToChart(string mComment)
{
OutputLabelToChart(Comment2Label, Comment2Y, CommentTxtSize, CommentColor, mComment);
}
//+------------------------------------------------------------------+
Comments