EMAAngleZeroAlert_v1
Download scriptThis tool is designed to help traders visualize the momentum and direction of an Exponential Moving Average (EMA) by calculating its "angle" relative to the price chart. Instead of just looking at whether a moving average is going up or down, this script quantifies the steepness of that movement to help identify periods of strong trends or areas where the market may be stalling.
How the Logic Works
The core functionality of the script relies on measuring the difference between two points on an Exponential Moving Average:
- Comparison Points: The script looks at two specific spots on the EMA. It takes a starting point (set further back in time) and compares it to the current price point.
- Angle Calculation: By calculating the distance between these two points over a specific timeframe, the script determines the "angle" of the moving average.
- Scaling for Visibility: Because market prices can be very small or very large, the script automatically adjusts the calculation based on the currency pair being traded (for example, it uses different scaling factors for Japanese Yen pairs versus other currencies) to ensure the visual data remains readable on your screen.
Visual Feedback and Alerts
The script translates these mathematical calculations into a color-coded histogram in a separate window on your platform:
- Green (Upward Momentum): When the EMA is angled upward more sharply than your defined threshold, the histogram turns green. This signifies a positive trend with strength.
- Red (Downward Momentum): When the EMA is angled downward more sharply than your defined threshold, the histogram turns red. This signifies a negative trend with strength.
- Yellow (Neutral/Sideways): When the angle of the EMA is "near zero"—meaning it is relatively flat—the histogram turns yellow. This indicates that the market may be moving sideways or consolidating, and the previous trend has lost its momentum.
Customization Options
The script includes several key parameters that allow you to tailor the logic to your specific trading style:
- EMA Period: Choose how many past candles the average should consider.
- Angle Threshold: Define what you consider "flat." A lower number makes the yellow zone more sensitive, while a higher number requires a more significant move before it registers as a clear trend.
- Shift Values: These determine the distance between the two points used to measure the angle, allowing you to look at short-term changes or longer-term movements.
In summary, this script acts as a momentum filter, helping you distinguish between a market that is moving with purpose and one that is simply drifting without a clear 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 |
//| 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[];
//+------------------------------------------------------------------+
//| 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;
PlaySound("Disconnect.wav");
}
else if (fAngle < -AngleTreshold)
{
DownBuffer[i] = fAngle;
PlaySound("Alert.wav");
}
else ZeroBuffer[i] = fAngle;
}
return(0);
}
//+------------------------------------------------------------------+
Comments