EMAAngleZeroAlert
Download scriptThis tool is designed to help traders visualize the "momentum" or "tilt" of an Exponential Moving Average (EMA) by calculating its angle. Instead of just looking at the price line, this indicator focuses on how sharply that line is moving up or down at any given moment.
How It Works
The script looks at two specific points on the EMA: a starting point and an ending point. By measuring the difference in height between these two points over a set horizontal distance, it determines the angle of the trend.
To make this data readable for humans (and to ensure the movement is significant enough to matter), the script applies a multiplier. This scaling factor automatically adjusts based on the currency pair you are trading—specifically ensuring that "JPY" pairs are handled differently than standard pairs to keep the visual measurements consistent across different markets.
Visual Feedback
The indicator displays the results in a separate window as a histogram, using a color-coded system to tell you what the trend is doing:
- Green: The EMA is angled upward significantly, suggesting a sustained bullish (upward) trend.
- Red: The EMA is angled downward significantly, suggesting a sustained bearish (downward) trend.
- Yellow: The angle is "near zero." This indicates that the market issidling or consolidating, and there is no strong directional momentum in either direction.
Key Features for Traders
- Threshold Filtering: You can set a specific sensitivity level. If the angle is smaller than your chosen threshold (the "near zero" zone), it will show as yellow. This helps filter out "noise" and tells you when the market is moving sideways.
- Trend Change Alerts: The script tracks the direction of the trend. If the momentum flips from up to down (or vice versa), it triggers a sound alert to notify you immediately of a potential shift in market dynamics.
- Dynamic Scaling: Because different assets have different price values, the logic includes an automatic adjustment factor. This ensures that the "steepness" of the angle looks similar whether you are trading highly volatile pairs or more stable ones.
//+------------------------------------------------------------------+
//| 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 |
//| 10/23/05 Added other JPY crosses |
//| |
//+------------------------------------------------------------------+
#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 bool DoAlert = true;
extern int EMAPeriod=34;
extern double AngleTreshold=0.2;
extern int StartEMAShift=6;
extern int EndEMAShift=0;
//---- indicator buffers
double UpBuffer[];
double DownBuffer[];
double ZeroBuffer[];
int trend, old_trend = 0;
//+------------------------------------------------------------------+
//| 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;
string Sym;
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 = 2*3.14159/180.0;
mFactor = 10000.0;
Sym = StringSubstr(Symbol(),3,3);
if (Sym == "JPY") mFactor = 100.0;
ShiftDif = StartEMAShift-EndEMAShift;
mFactor /= ShiftDif;
//---- main loop
for(i=0; i<nLimit; i++)
{
fEndMA=iMA(NULL,0,EMAPeriod,0,MODE_EMA,PRICE_MEDIAN,i+EndEMAShift);
fStartMA=iMA(NULL,0,EMAPeriod,0,MODE_EMA,PRICE_MEDIAN,i+StartEMAShift);
// 10000.0 : Multiply by 10000 so that the fAngle is not too small
// for the indicator Window.
fAngle = mFactor * (fEndMA - fStartMA)/2.0;
// fAngle = MathArctan(fAngle)/dFactor;
DownBuffer[i] = 0.0;
UpBuffer[i] = 0.0;
ZeroBuffer[i] = 0.0;
if(fAngle > AngleTreshold)
{
UpBuffer[i] = fAngle;
trend = 1;
if (trend != old_trend) PlaySound("Disconnect.wav");
old_trend = trend;
}
else if (fAngle < -AngleTreshold)
{
DownBuffer[i] = fAngle;
trend = -1;
if (trend != old_trend)PlaySound("Alert.wav");
old_trend = trend;
}
else ZeroBuffer[i] = fAngle;
}
return(0);
}
//+------------------------------------------------------------------+
Comments