EMAAngleZeroDT
Download scriptThis tool is designed to help traders visualize the momentum and "steepness" of a moving average line on a trading chart. Rather than just showing you the price of a moving average, this script analyzes how sharply that average is tilting upward or downward at any given moment.
Core Logic and Functionality
The primary objective of this indicator is to identify periods of high momentum versus periods of consolidation (sideways movement). It achieves this by calculating the "angle" of an Exponential Moving Average (EMA) based on specific historical points.
1. Measuring the Tilt (Angle Calculation) The script looks at two different points in time: a starting point (further back in history) and an ending point (closer to the current price). By comparing the values of the EMA at these two positions, it determines the slope.
- If the value is rising sharply, the angle is positive.
- If the value is falling sharply, the angle is negative.
- If the value is relatively flat, the angle is near zero.
2. Dynamic Scaling To ensure the visual data is readable on your screen, the script includes a "magnification" factor. This ensures that even small changes in the moving average's tilt are exaggerated enough to be seen clearly in the indicator window, rather than appearing as a flat line. It also includes specific adjustments for different currency pairs (like USDJPY) to ensure accuracy across various markets.
3. Visual Classification The tool translates these mathematical angles into a color-coded histogram for easy interpretation:
- Green (Upward Momentum): When the angle exceeds a specific positive threshold, the histogram turns green. This signifies a strong upward trend.
- Red (Downward Momentum): When the angle falls below a specific negative threshold, the histogram turns red. This signifies a strong downward trend.
- Yellow (Neutral/Consolidation): When the angle stays within a small range around zero (the "Threshold"), the histogram appears in yellow. This indicates that the market is currently ranging or lack sufficient momentum to establish a clear direction.
Summary for the Trader
Essentially, this indicator acts as a momentum filter. It allows you to quickly distinguish between a "trending" market (Green/Red) and a "choppy" or "sideways" market (Yellow). By adjusting the threshold and the distance between the start and end points, you can customize how sensitive the tool is to these changes in market direction.
//+------------------------------------------------------------------+
//| EMAAngle.mq4 |
//| jpkfox |
//| |
//| You can use this indicator to measure when the EMA angle is |
//| "near zero". AngleTreshold determines when the angle for the |
//| EMA is "about zero": This is when the value is between |
//| [-AngleTreshold, AngleTreshold] (or when the histogram is red). |
//| EMAPeriod: EMA period |
//| AngleTreshold: The angle value is "about zero" when it is |
//| between the values [-AngleTreshold, AngleTreshold]. |
//| StartEMAShift: The starting point to calculate the |
//| angle. This is a shift value to the left from the |
//| observation point. Should be StartEMAShift > EndEMAShift. |
//| StartEMAShift: The ending point to calculate the |
//| angle. This is a shift value to the left from the |
//| observation point. Should be StartEMAShift > EndEMAShift. |
//| |
//| Modified by MrPip |
//| Red for down |
//| Yellow for near zero |
//| Green for up |
//| 10/15/05 MrPip |
//| Corrected problem with USDJPY and optimized code |
//| |
//+------------------------------------------------------------------+
#property copyright "jpkfox"
#property link "http://www.strategybuilderfx.com/forums/showthread.php?t=15274&page=1&pp=8"
//---- indicator settings
#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 LimeGreen
#property indicator_color2 Yellow
#property indicator_color3 FireBrick
//---- indicator parameters
extern int EMAPeriod=34;
extern double AngleTreshold=0.2;
extern int StartEMAShift=6;
extern int EndEMAShift=0;
extern int MaMethod=1; //0-Simple, 1-Exponential, 2-Smoothed, 3-Linear Weighed
extern int MaPrice=5;
//---- indicator buffers
double UpBuffer[];
double DownBuffer[];
double ZeroBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- 2 additional buffers are used for counting.
IndicatorBuffers(3);
//---- drawing settings
SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,2);
SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID,2);
SetIndexStyle(2,DRAW_HISTOGRAM,STYLE_SOLID,2);
IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+2);
//---- 3 indicator buffers mapping
if(!SetIndexBuffer(0,UpBuffer) &&
!SetIndexBuffer(1,DownBuffer) &&
!SetIndexBuffer(2,ZeroBuffer))
Print("cannot set indicator buffers!");
//---- name for DataWindow and indicator subwindow label
IndicatorShortName("EMAAngleZero("+EMAPeriod+","+AngleTreshold+","+StartEMAShift+","+EndEMAShift+")");
//---- initialization done
return(0);
}
//+------------------------------------------------------------------+
//| The angle for EMA |
//+------------------------------------------------------------------+
int start()
{
double fEndMA, fStartMA;
double fAngle, mFactor, dFactor;
int nLimit, i;
int nCountedBars;
double angle;
int ShiftDif;
if(EndEMAShift >= StartEMAShift)
{
Print("Error: EndEMAShift >= StartEMAShift");
StartEMAShift = 6;
EndEMAShift = 0;
}
nCountedBars = IndicatorCounted();
//---- check for possible errors
if(nCountedBars<0)
return(-1);
//---- last counted bar will be recounted
if(nCountedBars>0)
nCountedBars--;
nLimit = Bars-nCountedBars;
dFactor = 3.14159/180.0;
mFactor = 10000.0;
if (Symbol() == "USDJPY") mFactor = 100.0;
ShiftDif = StartEMAShift-EndEMAShift;
mFactor /= ShiftDif;
//---- main loop
for(i=0; i<nLimit; i++)
{
fEndMA=iMA(NULL,0,EMAPeriod,0,MaMethod,MaPrice,i+EndEMAShift);
fStartMA=iMA(NULL,0,EMAPeriod,0,MaMethod,MaPrice,i+StartEMAShift);
// 10000.0 : Multiply by 10000 so that the fAngle is not too small
// for the indicator Window.
fAngle = mFactor * (fEndMA - fStartMA);
// fAngle = MathArctan(fAngle)/dFactor;
DownBuffer[i] = 0.0;
UpBuffer[i] = 0.0;
ZeroBuffer[i] = 0.0;
if(fAngle > AngleTreshold)
UpBuffer[i] = fAngle;
else if (fAngle < -AngleTreshold)
DownBuffer[i] = fAngle;
else ZeroBuffer[i] = fAngle;
}
return(0);
}
//+------------------------------------------------------------------+
Comments