MA_AngleZeroSigMA
Download scriptThis tool is designed to help traders visualize the momentum and trajectory of a Moving Average (MA) by calculating its "angle" or slope. Instead of just looking at the price line itself, this script analyzes how sharply the average is moving up, down, or staying flat.
Core Logic Overview
The indicator works by comparing the value of a Moving Average at two different points in time simultaneously. By looking at the difference between these two points, it determines the "steepness" of the trend.
Here is how the logic breaks down into practical visual signals:
1. Trend Identification (Up and Down) The script calculates the trajectory of the Moving Average over a specific number of bars.
- Bullish Momentum: If the angle is significantly positive (above a user-defined threshold), it is displayed as an Upward signal.
- Bearish Momentum: If the angle is significantly negative (below that same threshold), it is displayed as a Downward signal.
2. The "Near Zero" Detection One of the primary features of this script is its ability to identify "choppy" or sideways markets. It uses a specific threshold to determine when the Moving Average is moving too weakly to be considered a clear trend. When the slope falls within this neutral zone, the indicator identifies it as being "Near Zero." This helps traders spot periods where the market may be consolidating or lacking direction.
3. Smoothing and Filtering To prevent the visual data from becoming too erratic or "noisy," the script includes a secondary smoothing layer. It applies a secondary Moving Average to the calculated angles themselves. This creates a cleaner, more refined line that represents the overall trend of the momentum, making it easier for a human observer to follow the general flow of the market without being distracted by minor fluctuations.
Key Components for the User
- Slope Sensitivity: Users can adjust the "Angle Threshold" to decide how aggressive a move needs to be before it is counted as a clear trend.
- Dynamic Scaling: The logic includes automatic adjustments for different currency pairs (such as USDJPY) to ensure the visual angles remain consistent and readable regardless of the asset's price point.
- Visual Hierarchy: The script uses distinct colors (Green, Red, and Gold/Yellow) to immediately communicate three states: Strong Up, Strong Down, and Neutral/Sideways.
//+------------------------------------------------------------------+
//| 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. |
//| EndEMAShift: 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 |
//| |
//+------------------------------------------------------------------+
//mod2008fxtsd ki
#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 4
#property indicator_color1 Green
#property indicator_color2 Red
#property indicator_color3 Gold
#property indicator_color4 DeepSkyBlue
#property indicator_width1 2
#property indicator_width2 2
#property indicator_width3 2
#property indicator_width4 1
#property indicator_level1 0.2
#property indicator_level2 -0.2
#property indicator_level3 0.5
#property indicator_level4 -0.5
//#property indicator_level3 0
#property indicator_levelcolor RoyalBlue
//---- indicator parameters
extern int MAPeriod=34;
extern int MA_MODE=1;//0 sma; 1 ema; 2 smma; 3 lwma
extern int MA_PRICE=0;
extern double AngleTreshold=0.2;
extern int StartMAShift=6;
extern int EndMAShift=0;
extern int SigMA_Period = 9;
extern int SigMA_mode = 1;//0 sma; 1 ema; 2 smma; 3 lwma
extern string MA_Mode__= "0-sma; 1 ema; 2 smma; 3 lwma";
extern string MA_Price_= "0C 1O 2H 3L 4Md 5Tp 6WghC: Md(HL/2)4,Tp(HLC/3)5,Wgh(HLCC/4)6";
//---- indicator buffers
double UpBuffer[];
double DownBuffer[];
double ZeroBuffer[];
double SigMABuffer[];
double fAngle1[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- 2 additional buffers are used for counting.
IndicatorBuffers(5);
//---- drawing settings
SetIndexStyle(0,DRAW_HISTOGRAM);
SetIndexStyle(1,DRAW_HISTOGRAM);
SetIndexStyle(2,DRAW_HISTOGRAM);
SetIndexStyle(3,DRAW_LINE);
//---- 3 indicator buffers mapping
if(!SetIndexBuffer(0,UpBuffer) &&
!SetIndexBuffer(1,DownBuffer) &&
!SetIndexBuffer(3,SigMABuffer) &&
!SetIndexBuffer(4,fAngle1) &&
!SetIndexBuffer(2,ZeroBuffer))
Print("cannot set indicator buffers!");
//---- name for DataWindow and indicator subwindow label
IndicatorShortName("MA_AngleZero ("+MAPeriod+", "+DoubleToStr(AngleTreshold,2)+", "+SigMA_Period+")");
SetIndexLabel(0,"");
SetIndexLabel(1,"");
SetIndexLabel(2,"");
SetIndexLabel(3,"SigL");
IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+2);
//---- 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(EndMAShift >= StartMAShift)
{
Print("Error: EndEMAShift >= StartEMAShift");
StartMAShift = 6;
EndMAShift = 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 = StartMAShift-EndMAShift;
mFactor /= ShiftDif;
//---- main loop
for(i=0; i<nLimit; i++)
{
fEndMA=iMA(NULL,0,MAPeriod,0,MA_MODE,MA_PRICE,i+EndMAShift);
fStartMA=iMA(NULL,0,MAPeriod,0,MA_MODE,MA_PRICE,i+StartMAShift);
// 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;
fAngle1[i]=fAngle;
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;
}
// limit= MathMax(limit,TimeFrame/Period());
// nLimit = Bars-nCountedBars+SigMA_Period;
for(i=0; i<nLimit; i++)
{
SigMABuffer[i]=iMAOnArray(fAngle1,0,SigMA_Period,0,SigMA_mode,i);
}
return(0);
}
//+------------------------------------------------------------------+
Comments