ma_Step_EMA_LK

Author: Copyright � 2009, Leif Kalrsson
ma_Step_EMA_LK

This script is a specialized technical analysis tool designed to visualize market trends through a "stepping" movement. Unlike standard moving averages that provide smooth, continuous curves, this indicator creates a stair-step effect to highlight significant price movements while filtering out minor market noise.

Core Logic and Behavior

The tool operates by processing three distinct layers of data to determine how the trend line should behave:

1. Price Foundation The script first establishes a baseline by calculating a standard moving average of the market price. This provides the primary "anchor" from which all other calculations are derived.

2. Dynamic Range Detection To understand the "volatility" or the "size" of market moves, the script looks at a specific window of recent highs and lows. It calculates the distance between these points to determine a dynamic range. This ensures that the "steps" the indicator takes are proportionate to current market conditions—larger steps during high volatility and smaller steps during quiet markets.

3. The Stepping Mechanism This is where the script differs from standard indicators. It calculates a target value based on the previous trend line, but it applies a strict rule:

  • The new position of the trend line cannot jump more than the "Dynamic Range" calculated in the previous step.
  • If the price moves significantly enough to break this threshold, the indicator "steps" up or down to the nearest allowable level.

4. Trend Direction (Up vs. Down) The script manages two separate visual paths: an upward trend and a downward trend.

  • When the calculated value moves higher than the previous point, it activates the Upward Buffer.
  • When the calculated value moves lower, it activates the Downward Buffer.
  • By switching between these two, the script creates a clean, jagged line that clearly shows when a trend has shifted direction, essentially "locking" into a horizontal position until enough price movement occurs to force it to the next step.

Summary for the User

In plain terms, this tool is designed for traders who want to see a "cleaner" version of market momentum. It ignores small fluctuations and only moves when the price action is significant enough to justify a trend change. The result is a visual guide that shows you the current "plateau" of the market, making it easier to identify support and resistance levels based on actual price steps rather than smooth averages.

Indicators Used
Moving average indicator
Miscellaneous
Implements a curve of type %1
6 Views
0 Downloads
0 Favorites
ma_Step_EMA_LK
//+------------------------------------------------------------------+
//|                                                  Step_EMA_LK.mq4 |
//|                                 Copyright © 2009, Leif Karlsson. |
//|                                        Leffemannen1973@telia.com |
//+------------------------------------------------------------------+
//| Please feel free to copy, modify and / or redistribute this      |
//| software / source code in any way you see fit.                   |
//+------------------------------------------------------------------+
//+ ********************* Shameless Ad. **************************** +
//+ * I do custom programing jobs in Java, C, X86 Assembler & MQL4 * +
//+ ***** Pleace do not hesitate to get in contact if you nead ***** +
//+ ***** something special: EA, indicator or somthing else. ******* +
//+ ****************** Leffemannen1973@telia.com ******************* +
//+ **************************************************************** +
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, Leif Kalrsson"
#property link      "mailto://Leffemannen1973@telia.com"
//+------------------------------------------------------------------+
#property  indicator_chart_window
#property  indicator_buffers 2
#property  indicator_color1  Yellow
#property  indicator_color2  Red
#property  indicator_width1  1
//+------------------------------------------------------------------+
extern int EMAPeriod = 30;
extern int ERangePeriod = 14;
extern int ERangeWindow = 8;		// Must be less then ERangePeriod, think of it as custom TF
extern string AppliedPriceText1 = "Close: 0, Open: 1, High: 2, Low: 3";
extern string AppliedPriceText2 = "Median: 4, Typical: 5, Weighted: 6";
extern int AppliedPrice = 4;
extern int PriceShift = 0;
extern int MaxBars = 4000;
//+------------------------------------------------------------------+
double UpBuffer[];
double DwBuffer[];
double Price[];
double ERange[];
double EmaAlpha = 0.0;
double ERangeAlpha = 0.0;
//+------------------------------------------------------------------+
int init() {

   	IndicatorDigits(Digits+1);
   	
   	IndicatorBuffers(4);
   	
   	SetIndexStyle(0, DRAW_LINE);
   	SetIndexStyle(1, DRAW_LINE);
   	SetIndexBuffer(0, UpBuffer);
   	SetIndexBuffer(1, DwBuffer);
   	SetIndexBuffer(2, Price);
   	SetIndexBuffer(3, ERange);
   	   	   	
   	IndicatorShortName("Step_EMA, EMAPeriod: " + EMAPeriod + ", ERangePeriod: " + ERangePeriod + " ");
   	
   	EmaAlpha = 2.0/(EMAPeriod + 1.0);
   	ERangeAlpha = 2.0/(ERangePeriod + 1.0);
   	
   	return(0);
}
//+------------------------------------------------------------------+
int start() {
	int j = 0;
	int i = IndicatorCounted();
	if(i < 0) return(-1);
	i = Bars - i;
	if(i > MaxBars) {
		i = MaxBars;
		ArrayInitialize(UpBuffer, EMPTY_VALUE);
		ArrayInitialize(DwBuffer, EMPTY_VALUE);
		ArrayInitialize(ERange, High[i]-Low[i]);
	}
	
	j = i;
	while(j >= 0) {
		Price[j] = iMA(NULL, 0, 1, 0, 0, AppliedPrice, j+PriceShift);
		j--;
	}
		
	while(i >= 0) {
		double Range = 0.0;
		double StepSize = 0.0;
		double SEma = 0.0;
		double OldSEma = 0.0;
		
		Range = High[ArrayMaximum(High, ERangeWindow, i+PriceShift+1)] - Low[ArrayMinimum(Low, ERangeWindow, i+PriceShift+1)];
		ERange[i] = (1.0-ERangeAlpha)*ERange[i+1] + ERangeAlpha*Range;
		StepSize = ERange[i];
		
		if(UpBuffer[i+1] != EMPTY_VALUE) SEma = (1.0-EmaAlpha)*UpBuffer[i+1] + EmaAlpha*Price[i];
		if(DwBuffer[i+1] != EMPTY_VALUE) SEma = (1.0-EmaAlpha)*DwBuffer[i+1] + EmaAlpha*Price[i];
		if(SEma == 0.0) SEma = Price[i];
		
		if(SEma < Price[i]-StepSize) SEma = Price[i]-StepSize;
		if(SEma > Price[i]+StepSize) SEma = Price[i]+StepSize;
		
		if(UpBuffer[i+1] == EMPTY_VALUE) OldSEma = DwBuffer[i+1];
		else OldSEma = UpBuffer[i+1];
		
		if(SEma > OldSEma) {
			UpBuffer[i] = SEma;
			DwBuffer[i] = EMPTY_VALUE;
			if(UpBuffer[i+1] == EMPTY_VALUE) UpBuffer[i+1] = OldSEma;
		}
		else {
			DwBuffer[i] = SEma;
			UpBuffer[i] = EMPTY_VALUE;
			if(DwBuffer[i+1] == EMPTY_VALUE) DwBuffer[i+1] = OldSEma;
		}
		i--;
	}	

   	return(0);
}

Comments