Angulo_de__MAs
Download scriptThis script is designed to analyze the "momentum" or "tilt" of a Moving Average (MA) on a trading chart. Instead of just showing you the price line itself, this tool calculates the steepness of that line and displays it as an angle in degrees.
How the Logic Works
1. Selection of the Moving Average The script starts by allowing you to choose which type of Moving Average you want to track. It supports a wide variety of standard and advanced mathematical models (such as SMA, EMA, Hull Moving Average, Jurik Moving Average, etc.). You can also define the period (how much historical data to look at) and the specific price type (Close, High, Low, etc.) used for the calculation.
2. Calculating the Slope To determine the angle, the script looks at the current value of the chosen Moving Average and compares it to a previous value (based on a "jump" or "gap" you can set in the settings).
- It calculates the difference in price between these two points.
- It then converts that price difference into a rate of change relative to time (minutes).
3. Converting Change to Degrees Once the rate of change is established, the script uses trigonometry to convert that mathematical slope into a visual angle (degrees). This allows you to see how aggressively the trend is moving upward or downward. For example, a very steep rise will result in a high degree value, while a flat, sideways market will result in an angle near zero.
4. Smoothing the Data Because raw angle calculations can sometimes be "jittery" or produce jumpy lines on a chart, the script applies an extra layer of processing called "Smoothing." It uses an Exponential Moving Average (EMA) on the calculated angles to ensure that the final line displayed on your screen is fluid and easier to read.
5. Visual Representation The result is plotted in a separate window below your main price chart. The visual output provides a clear, readable curve representing the "strength" of the trend's incline. You can adjust a "Visual Factor" to tweak how these angles are scaled on your screen to best fit your personal trading style.
//+------------------------------------------------------------------+
//| ProjectName |
//| Copyright 2012, CompanyName |
//| http://www.companyname.net |
//+------------------------------------------------------------------+
/*+------------------------------------------------------------------+
| Angulo de MAs suave.mq5 |
| It calculates the slope of a moving average chart |
| Slope = Change in price / minute |
| Angle = Sexagesimal degrees |
| Smooth line with EMA |
| Copyright © 2013, José Miguel Soriano |
| Almería-Spain, josemiguel1812@gmail.com |
+------------------------------------------------------------------+
| Indicators JMA.mq5, LRMA.mq5, HMA.mq5 |
| Copyright © 2010, Nikolay Kositsin |
| farria@mail.redcom.ru |
| JMA - http://www.mql5.com/en/code/427 |
| LRMA - http://www.mql5.com/en/code/429 |
| HMA - http://www.mql5.com/en/code/549 |
+------------------------------------------------------------------+
| Place you the SmoothAlgorithms.mqh, IndicatorsAlgorithms.mqh |
| and MovingAverages.mqhfiles (http://www.mql5.com/en/code/77)|
| to the directory: terminal_data_folder\\MQL5\Include |
+------------------------------------------------------------------|
*/
#include <MovingAverages.mqh>
#include <SmoothAlgorithms.mqh>
#include <IndicatorsAlgorithms.mqh>
#property indicator_separate_window
#property indicator_buffers 3
#property indicator_plots 1
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrTurquoise
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
#property indicator_label1 "MaAngSuav"
#property indicator_level1 0
#property indicator_level2 15
#property indicator_level3 -15
#property indicator_level4 30
#property indicator_level5 -30
enum mis_MA_METODOS {SMA,EMA,SMMA,LWMA,DEMA,TEMA,LRMA,HMA,FRAMA,VIDYA,AMA,JMA};
//--- input parameters
input mis_MA_METODOS MAmetodo = EMA; //Method MA
input int MAPeriodo = 14; //Period MA
input int perSuavizado = 4; //Smoothing period
input ENUM_APPLIED_PRICE MAPrecio = PRICE_TYPICAL; //Price applied
input int velaSalto = 2; //Jump into candles slope calculation
input double factorVisual = 0.8; //Set visual angle numeric-image
int JMAperiodSuav = 6;
int JMAfase = 100;
int AMAFast = 5;
int AMASlow = 14;
int VIDYAcmoPeriodo = 9;
int maxBarras=-1; //Max bars to calculate
int puntMA;
ENUM_TIMEFRAMES marcoTrab=PERIOD_CURRENT;
//--- indicator buffers
double AngBuffer[],MABuffer[],senalBuffer[];
string simbTrab=Symbol();
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- indicator buffers mapping
string etiqu="MAAngSuav("+etiquetaMA(MAmetodo,MAPeriodo,MAPrecio, AMAFast, AMASlow)+"-"+
IaS(perSuavizado)+"-"+IaS(velaSalto)+")- "+abrevPrecio(MAPrecio);
SetIndexBuffer(0,senalBuffer,INDICATOR_DATA);
SetIndexBuffer(1,MABuffer,INDICATOR_CALCULATIONS);
SetIndexBuffer(2,AngBuffer,INDICATOR_CALCULATIONS);
ArraySetAsSeries(AngBuffer,true);
ArraySetAsSeries(MABuffer,true);
ArraySetAsSeries(senalBuffer,true);
PlotIndexSetString(0,PLOT_LABEL,etiqu);
IndicatorSetInteger(INDICATOR_DIGITS,_Digits);
IndicatorSetString(INDICATOR_SHORTNAME,etiqu);
switch(MAmetodo)
{
case SMA: puntMA=iMA(simbTrab,marcoTrab,MAPeriodo,0,MODE_SMA,MAPrecio); break;
case EMA: puntMA=iMA(simbTrab,marcoTrab,MAPeriodo,0,MODE_EMA,MAPrecio); break;
case SMMA: puntMA=iMA(simbTrab,marcoTrab,MAPeriodo,0,MODE_SMMA,MAPrecio); break;
case LWMA: puntMA=iMA(simbTrab,marcoTrab,MAPeriodo,0,MODE_LWMA,MAPrecio); break;
case AMA: puntMA=iAMA(simbTrab,marcoTrab,MAPeriodo,AMAFast,AMASlow,0,MAPrecio); break;
case DEMA: puntMA=iDEMA(simbTrab,marcoTrab,MAPeriodo,0,MAPrecio); break;
case FRAMA:puntMA=iFrAMA(simbTrab,marcoTrab,MAPeriodo,0,MAPrecio); break;
case TEMA: puntMA=iTEMA(simbTrab,marcoTrab,MAPeriodo,0,MAPrecio); break;
case VIDYA:puntMA=iVIDyA(simbTrab,marcoTrab,VIDYAcmoPeriodo,MAPeriodo,0,MAPrecio); break;
case LRMA: puntMA=iCustom(simbTrab,marcoTrab,"LRMA",MAPeriodo,MAPrecio); break;
case HMA: puntMA= iCustom(simbTrab,marcoTrab,"HMA",MAPeriodo,MAPrecio); break;
case JMA: puntMA= iCustom(simbTrab,marcoTrab,"JMA",JMAperiodSuav,JMAfase,MAPrecio); break;
}
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int OnCalculate(const int rangoTotal,
const int preCalculado,
const int begin,
const double &price[])
{
int k,inicio,limite,nCopiados,nBuffer=0,
contBarras=preCalculado;
if(contBarras<0) return(-1);
else if(contBarras>0) contBarras--;
if(contBarras==0)inicio=0;
else inicio=contBarras;
limite=rangoTotal-contBarras-velaSalto; // - (MAPeriodo+velaSalto);
if(maxBarras>0) limite=MathMin(limite,maxBarras);
nCopiados=CopyBuffer(puntMA,nBuffer,0,rangoTotal-inicio,MABuffer);
if(nCopiados>0)
{
limite=MathMin(limite,nCopiados);
for(k=inicio; k<limite; k++) AngBuffer[k]=anguloMA(MABuffer,k,marcoTrab,velaSalto,factorVisual,simbTrab);
ExponentialMAOnBuffer(rangoTotal,preCalculado,begin,perSuavizado,AngBuffer,senalBuffer);
}
return(rangoTotal);
}
//----------------------------------------- ÁNGULO DE MEDIA MOVIL ---------------------------------------------
double anguloMA(double &arrayValores[],int indVela,ENUM_TIMEFRAMES marcoTmp,int barraSalto=2,double factor_visual=0.80,string simb="")
{ //DEVUELVE EL ÁNGULO FORMADO POR LA LINEA DE UNA MEDIA MÓVIL, expresado en grados
double MAactual=0,MAprevia=0;
double incremento=0,tangente=0,angulo=0;
if(simb=="") simb=Symbol();
MAactual= arrayValores[indVela];
MAprevia= arrayValores[indVela+barraSalto];
incremento= (MAactual - MAprevia) / valorPunto(simb); //Points()
tangente= incremento / (barraSalto*PeriodSeconds(marcoTmp)/60); //tangente= incremento en puntos / minutos
angulo= MathArctan(tangente) / M_PI * 180 * factor_visual; //ángulo en grados sexagesimales
return(angulo);
}
//+------------------------------------- DEFINE ETIQUETA de MA --------------------------------------+
string etiquetaMA(mis_MA_METODOS metodo,int MAPeriodo,ENUM_APPLIED_PRICE MAPrecio,int AMArapida=5,int AMAlenta=14)
{
string etiqueta;
switch(metodo)
{
case SMA:
etiqueta="SMA("+IaS(MAPeriodo)+","+abrevPrecio(MAPrecio)+")";
break;
case EMA:
etiqueta="EMA("+IaS(MAPeriodo)+","+abrevPrecio(MAPrecio)+")";
break;
case SMMA:
etiqueta="SMMA("+IaS(MAPeriodo)+","+abrevPrecio(MAPrecio)+")";
break;
case LWMA:
etiqueta="LWMA("+IaS(MAPeriodo)+","+abrevPrecio(MAPrecio)+")";
break;
case AMA:
etiqueta="AMA("+IaS(MAPeriodo)+","+IaS(AMArapida)+","+IaS(AMAlenta)+","+abrevPrecio(MAPrecio)+")";
break;
case DEMA:
etiqueta="DEMA("+IaS(MAPeriodo)+","+abrevPrecio(MAPrecio)+")";
break;
case FRAMA:
etiqueta="FRAM("+IaS(MAPeriodo)+","+abrevPrecio(MAPrecio)+")";
break;
case TEMA:
etiqueta="TEMA("+IaS(MAPeriodo)+","+abrevPrecio(MAPrecio)+")";
break;
case VIDYA:
etiqueta="VIDYA("+IaS(MAPeriodo)+","+IaS(VIDYAcmoPeriodo)+","+abrevPrecio(MAPrecio)+")";
break;
case HMA:
etiqueta="HMA("+IaS(MAPeriodo)+","+abrevPrecio(MAPrecio)+")";
break;
case LRMA:
etiqueta="LRMA("+IaS(MAPeriodo)+","+abrevPrecio(MAPrecio)+")";
break;
case JMA:
etiqueta="JMA("+IaS(MAPeriodo)+","+abrevPrecio(MAPrecio)+")";
break;
}
return(etiqueta);
}
//+------------------------------ ABREVIA PRECIO ------------------------------------+
string abrevPrecio(ENUM_APPLIED_PRICE precio)
{
string resp="-";
switch(precio)
{
case PRICE_CLOSE: resp= "C"; break;
case PRICE_HIGH: resp= "H"; break;
case PRICE_LOW: resp= "L"; break;
case PRICE_MEDIAN: resp= "M"; break;
case PRICE_OPEN: resp= "O"; break;
case PRICE_TYPICAL: resp= "T"; break;
case PRICE_WEIGHTED:resp= "W";
}
return(resp);
}
//----------------------------------- VALOR DEL PUNTO en precio (Point())---------------------------------
double valorPunto(string simb="")
{
if(simb=="") simb=_Symbol;
double resp=SymbolInfoDouble(simb,SYMBOL_POINT);
return(resp);
}
//--------------------------------- DÍGITOS DEL SÍMBOLO ---------------------------------------
int digitosSimb(string simb)
{
int numDig=SymbolInfoInteger(simb,SYMBOL_DIGITS);
return(numDig);
}
//+------------------------------------ ENTERO A CADENA -----------------------------------------+
string IaS(int valor)
{
string resp;
resp=IntegerToString(valor);
return(resp);
}
//+------------------------------------------------------------------+
Comments