LWMA_ATR_S
Download scriptThis tool is designed to provide traders with a clear, real-time visual representation of market volatility. By analyzing price movements directly on your trading chart, it helps you understand how much the market is currently "breathing" or moving in terms of pip value.
Core Functionality
The primary purpose of this script is to calculate the Average True Range (ATR) using a Linear Weighted Moving Average (LWMA). Unlike a standard average that treats all past data points equally, a linear weight gives more importance to recent price action. This ensures that the indicator reacts more quickly to current market trends while still maintaining a smooth, readable line on your chart.
How the Logic Works
The script processes data through three main stages:
- Volatility Measurement: The tool looks at the distance between the highest and lowest prices reached during a specific period. It also accounts for "gaps" (the difference between the previous day's close and the current day's open) to ensure that any sudden jumps in price are captured accurately.
- Smoothing the Data: Once the raw volatility is measured, the script applies a Linear Weighted Moving Average. This filters out "noise" from the market, resulting in a smoother line that represents the prevailing trend of price fluctuations rather than erratic spikes.
- Dynamic Calculations: The script continuously updates two key pieces of information on your screen:
- Current Volatility: It displays the current average movement in pips based on the timeframe you are viewing.
- Relative Stop-Distance: Based on a user-defined multiplier, it calculates a suggested distance for trade management. For example, if the market is moving 20 pips on average and your multiplier is set to 3, it will inform you that a 60-pip movement is considered a significant deviation from the norm.
Key Benefits for Traders
- Real-Time Awareness: You can instantly see at a glance whether the market is in a high-activity state or a quiet, consolidating phase.
- Smart Stop-Loss Planning: By providing a calculated percentage of the ATR, it helps you set stop-loss orders that are mathematically aligned with current market conditions—neither too tight to be hit by normal "noise" nor too wide to be inefficient.
- Visual Clarity: The indicator automatically places text labels on your chart, providing you with specific pip values so you don't have to perform manual calculations during live trading sessions.
//+------------------------------------------------------------------+
//| LWMA_ATR_S.mq4 |
//| Copyright © Sovpel Alexander, 2009 |
//+------------------------------------------------------------------+
#property copyright "Copyright © Sovpel Alexander, 2009"
#property link "E-mail: Sovpel-Alexander@yandex.ru"
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 DodgerBlue
//---- input parameters
extern int AtrPeriod =15;
extern int Multiplier =3;
extern int Text_Corner =0;
extern color Text_Color =DeepSkyBlue;
//---- buffers
double AtrBuffer[];
double TempBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
string short_name;
//---- 1 additional buffer used for counting.
IndicatorBuffers(2);
//---- indicator line
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,AtrBuffer);
SetIndexBuffer(1,TempBuffer);
//---- name for DataWindow and indicator subwindow label
short_name="LWMA_ATR("+AtrPeriod+")";
IndicatorShortName(short_name);
SetIndexLabel(0,short_name);
//----
SetIndexDrawBegin(0,AtrPeriod);
//----
ObjectCreate("ATR_0",OBJ_LABEL,0,TimeCurrent(),0,0);
ObjectSet("ATR_0", OBJPROP_XDISTANCE, 5);
ObjectSet("ATR_0", OBJPROP_YDISTANCE, 15);
ObjectCreate("ATR_1",OBJ_LABEL,0,TimeCurrent(),0,0);
ObjectSet("ATR_1", OBJPROP_XDISTANCE, 5);
ObjectSet("ATR_1", OBJPROP_YDISTANCE, 33);
return(0);
}
//+------------------------------------------------------------------+
//| Average True Range |
//+------------------------------------------------------------------+
int start()
{
int i,counted_bars=IndicatorCounted();
//----
if(Bars<=AtrPeriod) return(0);
//---- 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))/Point;
}
i--;
}
//----
if(counted_bars>0) counted_bars--;
int limit=Bars-counted_bars;
for(i=0; i<limit; i++)
AtrBuffer[i]=iMAOnArray(TempBuffer,Bars,AtrPeriod,0,MODE_LWMA,i);
//----
if(Multiplier != 1) int Percentage = Multiplier * 100;
//----
ObjectSet("ATR_0", OBJPROP_CORNER, Text_Corner);
ObjectSetText("ATR_0","Ëèíåéíî-âçâåøåííàÿ âîëàòèëüíîñòü ðûíêà "+DoubleToStr(AtrBuffer[0],0)+" pips", 10, "Tahoma", Text_Color);
//----
ObjectSet("ATR_1", OBJPROP_CORNER, Text_Corner);
ObjectSetText("ATR_1","Àäàïòèâíûé ñòîï-ïðèêàç ( "+Percentage+"% of ATR ) "+DoubleToStr(AtrBuffer[0] * Multiplier,0)+" pips", 10, "Tahoma", Text_Color);
//----
return(0);
}
//----
int deinit()
{
ObjectDelete("ATR_0");
ObjectDelete("ATR_1");
return;
}
//+------------------------------------------------------------------+
Comments