LSMA_Angle
Download scriptThis script is a specialized technical analysis tool designed for the MetaTrader platform. Its primary purpose is to measure the "steepness" or angle of a specific type of moving average known as the Least Squares Moving Average (LSMA). By converting price movements into an angular measurement, it helps traders visualize the momentum and strength of a trend more clearly than a standard line chart might allow.
Core Logic Overview
The indicator works by analyzing the trajectory of the LSMA over a specific period. Instead of just showing you the current price average, it calculates the slope between two points in time to determine how aggressively the market is moving in a particular direction.
Here is the step-by-step breakdown of how the logic processes your data:
1. Calculating the Reference Points The script identifies two distinct points on the LSMA line:
- The End Point: The current position of the moving average.
- The Start Point: A previous position (based on a user-defined shift) to act as a comparison anchor.
2. Angle Conversion By comparing the value of the "End Point" against the "Start Point," the script determines the difference in height over a specific horizontal distance. It then converts this mathematical difference into a geometric angle (degrees). To ensure these angles are visible and meaningful on your chart, the script applies a scaling factor based on the currency pair being traded.
3. Trend Classification & Visual Feedback Once the angle is calculated, the script categorizes the market movement into three distinct states, represented by different colors in a histogram:
- Bullish Momentum (Green): If the calculated angle is positive and exceeds a specific threshold, it indicates a strong upward trend.
- Bearish Momentum (Red): If the angle is negative and falls below the negative threshold, it indicates a strong downward trend.
- Neutral/Consolidation (Yellow): If the angle is very small (near zero) and stays within your defined threshold, it suggests that the market is moving sideways, consolidating, or lacking a clear direction.
Key User Controls
The script allows you to fine-tune how these calculations are performed through several parameters:
- LSMA Period: Determines how many past candles are used to calculate the moving average.
- Angle Threshold: Defines the "sensitivity." You can set how steep a move must be before it is considered a "strong" trend versus "neutral" movement.
- Shifts: These allow you to manually adjust how far back the script looks to measure the slope, giving you more control over the perspective of the momentum calculation.
/*-----------------------------+
| |
| Shared by www.Aptrafx.com |
| |
+------------------------------*/
//+------------------------------------------------------------------+
//| LSMA_Angle.mq4 |
//| MrPip |
//| |
//| You can use this indicator to measure when the LSMA angle is |
//| "near zero". AngleTreshold determines when the angle for the |
//| LSMA is "about zero": This is when the value is between |
//| [-AngleTreshold, AngleTreshold] (or when the histogram is red). |
//| LSMAPeriod: LSMA period |
//| AngleTreshold: The angle value is "about zero" when it is |
//| between the values [-AngleTreshold, AngleTreshold]. |
//| StartLSMAShift: The starting point to calculate the |
//| angle. This is a shift value to the left from the |
//| observation point. Should be StartEMAShift > EndEMAShift. |
//| EndLSMAShift: 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 from EMAAngle by jpkfox | |
//| Red for down |
//| Yellow for near zero |
//| Green for up |
//| |
//+------------------------------------------------------------------+
#property copyright "Robert L. Hill aka MrPip"
//---- 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 LSMAPeriod=34;
extern double AngleTreshold=15.0;
extern int StartLSMAShift=4;
extern int EndLSMAShift=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("LSMAAngle("+LSMAPeriod+","+AngleTreshold+","+StartLSMAShift+","+EndLSMAShift+")");
//---- initialization done
return(0);
}
//+------------------------------------------------------------------------+
//| LSMA - Least Squares Moving Average function calculation |
//| LSMA_In_Color Indicator plots the end of the linear regression line |
//+------------------------------------------------------------------------+
double LSMA(int Rperiod, int shift)
{
int i;
double sum;
int length;
double lengthvar;
double tmp;
double wt;
length = Rperiod;
sum = 0;
for(i = length; i >= 1 ; i--)
{
lengthvar = length + 1;
lengthvar /= 3;
tmp = 0;
tmp = ( i - lengthvar)*Close[length-i+shift];
sum+=tmp;
}
wt = sum*6/(length*(length+1));
return(wt);
}
//+------------------------------------------------------------------+
//| The angle for LSMA |
//+------------------------------------------------------------------+
int start()
{
double fEndMA, fStartMA;
double fAngle, mFactor, dFactor;
int nLimit, i;
int nCountedBars;
double angle;
int ShiftDif;
if(EndLSMAShift >= StartLSMAShift)
{
Print("Error: EndLSMAShift >= StartLSMAShift");
StartLSMAShift = 6;
EndLSMAShift = 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;
if (Symbol() == "USDJPY") mFactor = 100.0;
ShiftDif = StartLSMAShift-EndLSMAShift;
mFactor /= ShiftDif;
//---- main loop
for(i=0; i<nLimit; i++)
{
fEndMA=LSMA(LSMAPeriod,i+EndLSMAShift);
fStartMA=LSMA(LSMAPeriod,i+StartLSMAShift);
// 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;
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;
}
return(0);
}
//+------------------------------------------------------------------+
Comments