NRTR WATR1
Download scriptThis script is designed as a custom technical indicator for the MetaTrader trading platform. Its primary purpose is to identify and visualize market trends by calculating specific price "buffer zones" based on historical volatility.
Core Logic Overview
The logic functions by analyzing the relationship between current price movements and the average range of price candles over a specific period. Instead of simply looking at whether the price went up or down, the script calculates a dynamic value (referred to as WATR) that represents the "strength" or "stretch" of the market's movement.
Key Components of the Calculation
- Volatility Measurement: The script looks back at a user-defined number of candles (the Average Period). It measures the difference between the high and low of these candles to determine how much the market typically fluctuates.
- Weighting System: It applies a weighted calculation to these fluctuations. This means that more recent price movements are given different importance than older ones, allowing the indicator to react more fluidly to current market conditions.
- Currency-Specific Adjustment: The logic includes a specialized rule for certain Japanese Yen pairs (USDJPY, GBPJPY, EURJPY). It automatically scales the calculation for these specific currencies to ensure the visual data remains consistent across different asset classes.
Trend Identification and Visualization
The script identifies the trend direction based on how price reacts to its own recent highs and lows:
- Trend Tracking: It keeps track of whether the market is currently in an upward or downward state.
- Dynamic Thresholds:
- In an uptrend, the script monitors if the price drops below a certain "safety buffer" (the current high minus the volatility value). If it breaches this, the trend flips to downward.
- In a downtrend, the script monitors if the price rises above a "ceiling" (the current low plus the volatility value). If it breaches this, the trend flips back to upward.
- Visual Output: The results are displayed on the chart as distinct arrows or markers. One set of colors represents the active upward trend, while the other signifies the downward trend.
User Customization
The logic allows for three different "Variants," which change how the historical data is sampled (comparing current prices against specific historical windows). Users can also adjust the length of the average period and the number of bars displayed to fine-tune how sensitive the indicator is to market noise versus significant trend shifts.
//+------------------------------------------------------------------+
//| NRTR WATR.mq4 |
//| |
//| Ramdass - Conversion only |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Blue
#property indicator_color2 Red
//---- input parameters
extern int AveragePeriod=10;
extern int Variant=2;
extern int CountBars=300;
//---- buffers
double value1[];
double value2[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicator line
IndicatorBuffers(2);
SetIndexStyle(0,DRAW_ARROW);
SetIndexArrow(0,167);
SetIndexStyle(1,DRAW_ARROW);
SetIndexArrow(1,167);
SetIndexBuffer(0,value1);
SetIndexBuffer(1,value2);
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| NRTR WATR |
//+------------------------------------------------------------------+
int start()
{
if (CountBars>=Bars) CountBars=Bars - AveragePeriod - 2;
// CountBars = CountBars - AveragePeriod - 3;
SetIndexDrawBegin(0,Bars-CountBars+1);
SetIndexDrawBegin(1,Bars-CountBars+1);
int i,i2,bar,counted_bars=IndicatorCounted();
double value,WATR;
double trend=1,dK,AvgRange,price,AveragePeriod_D;
AveragePeriod_D=AveragePeriod;
//----
if(Bars<=AveragePeriod) return(0);
//---- initial zero
if(counted_bars<1)
{
for(i=1;i<=AveragePeriod;i++) value1[Bars-i]=0.0;
for(i=1;i<=AveragePeriod;i++) value2[Bars-i]=0.0;
}
AvgRange=0;
if ((Variant == 2) || (Variant == 3))
{
for (i=AveragePeriod; i>=1; i--)
{
dK = 1+(AveragePeriod_D-i)/AveragePeriod_D;
AvgRange=AvgRange + dK*MathAbs(High[i]-Low[i]);
}
if (Symbol()=="USDJPY" || Symbol()=="GBPJPY" || Symbol()=="EURJPY")
{WATR = AvgRange/AveragePeriod_D/100;}
else
{WATR = AvgRange/AveragePeriod_D;}
}
if (Variant == 1)
{
for (i=1; i<=AveragePeriod; i++)
{
dK = 1+(AveragePeriod_D-i)/AveragePeriod_D;
AvgRange=AvgRange + dK*MathAbs(High[CountBars + i]-Low[CountBars + i]);
}
WATR = AvgRange/AveragePeriod_D;
}
if (Close[CountBars-1] > Open[CountBars-1])
{
value1[CountBars - 1] = Close[CountBars - 1] * (1 - WATR);
trend = 1; value2[CountBars - 1] = 0.0;
}
if (Close[CountBars-1] < Open[CountBars-1])
{
value2[CountBars - 1] = Close[CountBars - 1] * (1 + WATR);
trend = -1; value1[CountBars - 1] = 0.0;
}
//----
bar=CountBars;
while(bar>=0)
{
value1[bar]=0.0; value2[bar]=0.0;
if (Variant == 3)
{
AvgRange=0;
for (i=1; i<=AveragePeriod; i++)
{
dK = 1+(AveragePeriod_D-i)/AveragePeriod_D;
AvgRange=AvgRange+ dK*MathAbs(High[bar + i]-Low[bar + i]);
}
WATR = AvgRange/AveragePeriod_D;
}
if (trend == 1)
{
if (Close[bar] > price) price = Close[bar];
value = price - WATR;
if (Close[bar] < value)
{
price = Close[bar];
value = price + WATR;
trend = -1;
}
}
if (trend == -1)
{
if (Close[bar] < price) price = Close[bar];
value = price + WATR;
if (Close[bar] > value)
{
price = Close[bar];
value = price - WATR;
trend = 1;
}
}
if (trend == 1) {value1[bar]=value; value2[bar]=0.0;}
if (trend == -1) {value2[bar]=value; value1[bar]=0.0;}
bar--;
}
return(0);
}
//+------------------------------------------------------------------+
Comments