Lswpr_Angleexp

Download script
Lswpr_Angleexp

This tool is designed to help traders visualize the momentum and direction of an Exponential Moving Average (EMA) by measuring its "angle" or slope. Instead of just looking at whether a price trend is moving up or down, this script analyzes the intensity of that movement to identify periods of strength, weakness, or stagnation.

How the Logic Works

The script functions by comparing the position of the EMA at two different points in time: a "starting" point (further back in history) and an "ending" point (closer to the current price). By calculating the difference between these two positions, it determines the rate of change—essentially the steepness of the trend.

Key Components of the Analysis

  • Angle Calculation: The logic determines how sharply the EMA is tilting. A steeper incline represents stronger momentum, while a flatter line suggests that the current trend is losing steam.
  • The "Near Zero" Threshold: One of the primary purposes of this script is to identify when the EMA is "flat." The user can set a specific threshold; if the calculated angle falls within this small range (close to zero), it signals that the market may beSIDEWAYS or consolidating, which is often a warning sign for trend followers.
  • Dynamic Scaling: To ensure the data is readable across different types of currency pairs (such as standard forex pairs versus those involving the Japanese Yen), the script automatically adjusts its internal math. This ensures that the "angle" looks consistent and proportional regardless of the asset's price scale.

Visual Interpretation

The tool translates these mathematical calculations into a color-coded histogram to make it easier for traders to read at a glance:

  • Green: Indicates an upward tilt, signaling positive momentum.
  • Red: Indicates a downward tilt, signaling negative momentum.
  • Yellow: Signals that the angle is "near zero," highlighting periods where the trend is neutral or exhausted.

By using this logic, traders can more effectively distinguish between a strong, reliable trend and a weak, unstable market movement where the price is merely drifting without significant direction.

Miscellaneous
Implements a curve of type %1
11 Views
0 Downloads
0 Favorites
Lswpr_Angleexp
//+------------------------------------------------------------------+
//|                                                    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;

//---- 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-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=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