Lswpr_Angleexp2
Download scriptOverview
This technical tool is designed to help traders visualize the momentum and direction of a moving average (specifically the Exponential Moving Average, or EMA). Instead of just looking at whether a price line is going up or down, this script calculates the specific "angle" or steepness of that movement. It translates these movements into a visual histogram, making it easier to identify periods of strong trends, weak momentum, or complete stagnation.
How the Logic Works
The core functionality of the script relies on measuring the change in the EMA over a specific distance in time. Here is the step-by-step breakdown of the logic:
1. Measuring the Slope The tool looks at two different points in time on the chart: a "starting point" (further back in history) and an "ending point" (closer to the current price). By comparing the value of the EMA at these two points, the script determines how much the average has risen or fallen.
2. Calculating the Angle Once the difference between these two points is identified, the tool converts that numerical difference into a visual angle. It uses a scaling factor to ensure that even small movements in the market are large enough to be clearly visible on your trading screen. This allows you to see at a glance how aggressively the price is moving.
3. Currency Adjustments To ensure accuracy across different types of assets, the logic includes a built-in adjustment for currency pairs. For example, it recognizes Japanese Yen (JPY) pairs and adjusts its calculation factor automatically. This ensures that the visual "steepness" of the angle remains consistent regardless of whether you are trading a high-value pair or a lower-value one.
4. Visual Feedback (The Histogram) The results are displayed as a histogram in a separate window below your main chart:
- Upward Momentum: When the EMA is trending strongly upward, the angle is positive.
- Downward Momentum: When the EMA is trending strongly downward, the angle is negative.
- Neutral Zones: The tool allows you to define a "threshold." If the angle falls within a specific small range (near zero), it identifies that the market is moving sideways or lacks a clear trend, signaling potential consolidation.
Summary for Traders
Think of this tool as a "speedometer" for market trends. Rather than just telling you where the price is, it tells you how fast and in what direction the average price is moving. This helps traders identify when a trend is gaining strength, slowing down, or becoming flat, allowing for more informed entry and exit decisions based on momentum.
//+------------------------------------------------------------------+
//| EMA_Angle.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 int lswprPeriod=14;
extern double AngleTreshold=0.1;
extern int StartEMAShift=6;
extern int EndEMAShift=0;
extern int Bars_to_Use = 300;
//---- 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("LSWPR_Angle("+lswprPeriod+","+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_to_Use;
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=iCustom(NULL,0,"LSwpr_in_color",lswprPeriod,0,i+EndEMAShift);
fStartMA=iCustom(NULL,0,"LSwpr_in_color",lswprPeriod,0,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;
{
UpBuffer[i] = fAngle;
}
}
return(0);
}
//+------------------------------------------------------------------+
Comments