Author: Copyright � 2005, TrendLaboratory Ltd.
StepMA_v3

This tool is designed to act as a visual trend-following indicator on your trading charts. Its primary purpose is to smooth out market noise and provide a clear, "stepped" representation of price movement, helping traders identify the current direction of the market without being distracted by minor fluctuations.

How the Logic Works

1. Dynamic Volatility Measurement The script begins by analyzing a specific window of recent price data (defined by the period setting). Instead of looking at just one price point, it calculates an "Average Range." It takes into account the difference between the highs and lows of previous bars, applying a weighted calculation to ensure that more recent price action has a significant impact on the current result. This allows the tool to adapt to different market conditions—expanding its awareness during high-volatility periods and tightening during calmer markets.

2. Defining the "Step" Size Once the average range is established, the script calculates a specific "StepSize." You can think of this as the "grid" or the minimum distance the indicator is allowed to move at one time. By establishing this consistent step, the tool prevents the line from moving in jagged, erratic patterns; it only moves when price action is strong enough to cross these established thresholds.

3. Trend Detection and Movement The core of the script works by comparing current closing prices against boundaries created by the StepSize:

  • Upward Trend: If the price climbs above the upper boundary, the tool identifies an upward trend. It then plots a line that follows the price from below, moving up in fixed steps.
  • Downward Trend: If the price drops below the lower boundary, the tool identifies a downward trend and plots a line that follows the price from above, moving down in fixed steps.

4. Smart Locking Mechanism To ensure the line remains smooth and continuous, the logic includes a "locking" feature. When the market is trending upward, the indicator "holds" its position to prevent it from jumping backward unnecessarily. It will only update its level when the price pushes it further in the direction of the trend. This creates that distinct "staircase" effect on your chart.

5. Customizable Behavior The script offers two ways to behave based on your preferences:

  • High/Low Mode: The boundaries are calculated based on the extreme highs and lows of the candles, which is useful for capturing the full range of market volatility.
  • Close Price Mode: The boundaries are calculated strictly based on where the candles close, which can be preferred by traders who prioritize settlement prices over "wick" movements.
13 Views
0 Downloads
0 Favorites
StepMA_v3
//+------------------------------------------------------------------+
//|                                                    StepMA_v2.mq4 |
//|                           Copyright © 2005, TrendLaboratory Ltd. |
//|                                       E-mail: igorad2004@list.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, TrendLaboratory Ltd."
#property link      "E-mail: igorad2004@list.ru"

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Red
//---- input parameters
extern int PeriodWATR=100;
extern double Kwatr=1.0000;
extern int Advance=0;
extern int HighLow=0;
//---- indicator buffers
double LineBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
  int init()
  {
   string short_name;
//---- indicator line
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,1);
   SetIndexShift(0,Advance);
   SetIndexBuffer(0,LineBuffer);
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
//---- name for DataWindow and indicator subwindow label
   short_name="StepMA("+PeriodWATR+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,"StepMA");
//----
   SetIndexDrawBegin(0,PeriodWATR);
//----
   return(0);
  }

//+------------------------------------------------------------------+
//| StepMA_v2                                                         |
//+------------------------------------------------------------------+
int start()
  {
   int i,shift,trend;
   double smin0,smax0,smin1,smax1,AvgRange,dK,WATR;

   AvgRange=0;
	for (i=PeriodWATR-1;i>=0;i--)
	    { 
       dK = 1+1.0*(PeriodWATR-i)/PeriodWATR;
       AvgRange=AvgRange+ dK*MathAbs(High[i]-Low[i]);
       }
	WATR = AvgRange/PeriodWATR;
	int StepSize=Kwatr*WATR/Point;
	Comment(" StepSize = ", StepSize);
	
	
   for(shift=Bars-1;shift>=0;shift--)
   {	
	if (HighLow>0)
	  {
	  smax0=Low[shift]+2*StepSize*Point;
	  smin0=High[shift]-2*StepSize*Point;
	
	  if(Close[shift]>smax1) trend=1; 
	  if(Close[shift]<smin1) trend=-1;
	  }
	 
	if (HighLow == 0)
	  {
	  smax0=Close[shift]+2*StepSize*Point;
	  smin0=Close[shift]-2*StepSize*Point;
	
	  if (Close[shift]>smax1)  trend=1; 
	  if (Close[shift]<smin1)  trend=-1;
	  }
	 	
	  if(trend>0 && smin0<smin1) smin0=smin1;
	  if(trend<0 && smax0>smax1) smax0=smax1;
		
	  if (trend>0) LineBuffer[shift]=smin0+StepSize*Point;
	  if (trend<0) LineBuffer[shift]=smax0-StepSize*Point;
	  
	  smin1=smin0;
	  smax1=smax0;
	 }
	return(0);	
 }

Comments