SMA_Angle
Download scriptOverview of the SMA Angle Indicator
This tool is designed to help traders visualize the momentum and direction of a Simple Moving Average (SMA) by calculating its "angle." Instead of just looking at the price, this indicator focuses on how sharply the moving average is sloping up or down, helping to identify periods of strong trends versus periods where the market is moving sideways or consolidating.
Core Logic and Functionality
The indicator works by comparing two different points on the Moving Average to determine its current trajectory:
- Comparison Points: The script looks at the current value of the SMA and compares it to a previous value (based on a user-defined "shift"). By measuring the difference between these two points over a specific distance, the tool calculates the steepness of the line.
- Dynamic Scaling: To ensure the data is readable and consistent across different currencies—specifically handling variations like Japanese Yen (JPY) pairs correctly—the script applies a mathematical scaling factor. This ensures that the "angle" remains proportionate and visually meaningful regardless of the asset being traded.
- Momentum Detection: The primary goal is to distinguish between active movement and stagnation. It calculates the intensity of the slope and categorizes it into different visual signals:
- Upward Movement: When the calculated angle meets specific upward criteria, the indicator highlights this momentum.
- Near-Zero/Neutral Movement: If the angle falls within a defined "threshold" (meaning the line is relatively flat), it identifies the market as being in a state of equilibrium or low volatility.
- Downward Movement: While the core logic focuses on identifying trends, the visual output differentiates between positive and negative slopes to give the trader a clear sense of direction.
Visual Representation
The indicator displays its findings in a separate window below the main price chart using a histogram format:
- Green Color: Indicates a confirmed upward trend or significant positive momentum.
- Yellow Color: Signifies that the angle is "near zero," suggesting that the market is currently flat, consolidating, or lacking a clear direction.
- Red Color: Represents a downward trend or negative momentum.
How it Helps the Trader
By providing a clear visual of the SMA's slope, this tool allows traders to quickly identify when a trend is gaining strength or when the market is entering a "choppy" phase. It helps filter out "noise," making it easier to see whether the price action is moving with purpose or simply drifting horizontally.
//+------------------------------------------------------------------+
//| SMA_Angle.mq4 |
//| jpkfox |
//| |
//| You can use this indicator to measure when the SMA angle is |
//| "near zero". AngleTreshold determines when the angle for the |
//| SMA is "about zero": This is when the value is between |
//| [-AngleTreshold, AngleTreshold] (or when the histogram is red). |
//| SMAPeriod: SMA period |
//| AngleTreshold: The angle value is "about zero" when it is |
//| between the values [-AngleTreshold, AngleTreshold]. |
//| StartSMAShift: The starting point to calculate the |
//| angle. This is a shift value to the left from the |
//| observation point. Should be StartSMAShift > EndSMAShift. |
//| StartSMAShift: The ending point to calculate the |
//| angle. This is a shift value to the left from the |
//| observation point. Should be StartSMAShift > EndSMAShift. |
//| |
//| 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 |
//| 01/02/07 tradeIgel: changed for "5Min Intraday System" by |
//| Phillip Nel http://www.forexfactory.com/forexforum/showthread.php?t=11854&page=1&pp=15 |
//| |
//+------------------------------------------------------------------+
#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 SMAPeriod=34;
extern double AngleTreshold=0.2;
extern int StartSMAShift=6;
extern int EndSMAShift=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("SMA_Angle("+SMAPeriod+","+AngleTreshold+","+StartSMAShift+","+EndSMAShift+")");
//---- initialization done
return(0);
}
//+------------------------------------------------------------------+
//| The angle for SMA |
//+------------------------------------------------------------------+
int start()
{
double fEndMA, fStartMA;
double fAngle, mFactor, dFactor;
int nLimit, i;
int nCountedBars;
double angle;
int ShiftDif;
string Sym;
if(EndSMAShift >= StartSMAShift)
{
Print("Error: EndSMAShift >= StartSMAShift");
StartSMAShift = 6;
EndSMAShift = 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 = StartSMAShift-EndSMAShift;
mFactor /= ShiftDif;
//---- main loop
for(i=0; i<nLimit; i++)
{
fEndMA=iMA(NULL,0,SMAPeriod,0,MODE_SMA,PRICE_MEDIAN,i+EndSMAShift);
fStartMA=iMA(NULL,0,SMAPeriod,0,MODE_SMA,PRICE_MEDIAN,i+StartSMAShift);
// 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(MathAbs(fAngle) >= 0.349 && MathAbs(fAngle) <= 0.524)
{
UpBuffer[i] = fAngle;
}
else
{
ZeroBuffer[i] = fAngle;
}
}
return(0);
}
//+------------------------------------------------------------------+
Comments