ABTrend3.0
Download scriptThis technical indicator is designed to analyze and visualize market trends by filtering out "noise" from price movements. It uses a sophisticated mathematical approach known as an Alpha-beta filter to smooth out fluctuations, allowing for a clearer view of the underlying direction of an asset's price.
How the Logic Works
1. Signal Smoothing (The Alpha-Beta Filter) Rather than simply looking at the current price, the script employs a two-order filter. This means it looks at both the current position of the price and its "velocity" (the speed and direction at which it is moving).
- It calculates a Residual: The difference between the actual current price and where the filter expected the price to be.
- It updates the Position and Velocity: It then adjusts its internal tracking based on this residual, creating a smoothed "path" that follows the trend without being overly distracted by every minor tick or spike.
2. Normalization (The ATR Component) A key feature of this specific version is that it is "Normalized by ATR." In plain terms, raw trend data can be difficult to read because different assets (like Forex pairs vs. Stocks) have different price scales and volatility levels.
- The script calculates the Average True Range (ATR), which measures how much an asset typically moves over a set period.
- By dividing the trend velocity by this volatility measure, the indicator converts the data into a relative value. This allows you to see the strength of a trend in a standardized way, making it easier to compare different markets or recognize when a move is unusually strong for that specific asset.
3. Visual Output The final result is plotted as a line in a separate window on your chart.
- When the line moves upward, it indicates positive momentum/trend strength.
- When the line moves downward, it indicates negative momentum.
- Because of the normalization, you can easily identify periods of high-intensity movement versus periods of consolidation or low-activity "sideways" markets.
Summary for the Trader
This tool doesn't just tell you if the price is going up or down; it tells you how consistently and forcefully the price is moving in that direction, adjusted for the current market volatility. It filters out the "jitter" of the market to provide a cleaner picture of the prevailing trend.
//+------------------------------------------------------------------+
//| Alpha-beta trend |
//| (C) 2020, Eugene Shell |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
// v.3.0 Normalized by ATR
#property copyright "(C) 2020, Eugene Shell"
#property link "https://www.mql5.com"
#property version "3.0"
#property strict
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots 1
//--- plot Trend
#property indicator_label1 "Trend"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrRed
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
#property indicator_level1 0
#property indicator_level2 0.25
#property indicator_level3 -0.25
//#property indicator_color2 clrGreen
//--- input parameters
input uint FilterPeriod=5;
input double q=0.3;
input uint ATRPeriod=21;
//--- indicator buffers
double Trend[];
double X0[];
double X1[];
//--- global vars
double a,b; // fast filter gain
double dT; // time discrete
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
IndicatorBuffers(4);
//--- indicator buffers mapping
SetIndexBuffer(0,Trend);
SetIndexBuffer(1,X0);
SetIndexBuffer(2,X1);
SetIndexStyle(0,DRAW_LINE);
SetIndexStyle(1,DRAW_NONE);
SetIndexStyle(2,DRAW_NONE);
SetIndexDrawBegin(0,ATRPeriod);
dT=1;//PeriodSeconds()/60;
a = 2.0/(FilterPeriod +1);
b = a*q;
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
//---
double X0e; // extrapolatios
double e; // residual
double ATR;
int i,pos;
ArraySetAsSeries(Trend,false);
ArraySetAsSeries(X0,false);
ArraySetAsSeries(X1,false);
ArraySetAsSeries(close,false);
// --- initialize filter ---
if(rates_total<2)
return(0);
if(prev_calculated>1)
pos=prev_calculated-1;
else
{
X0[0]=0;
X1[0]=0;
pos=1;
}
//--- main cycle
for(i=pos; i<rates_total && !IsStopped(); i++)
{
// 2-order filter
X0e=X0[i-1]+X1[i-1]*dT; // extrapolated value
e=close[i]-X0e; // residual
X0[i]=X0e+a*e;
X1[i]=X1[i-1]+b*e/dT; // velocity estimation
if(rates_total>int(ATRPeriod))
{
ATR=iATR(NULL,0,ATRPeriod,rates_total-i-1);
if(ATR>0)
Trend[i]=X1[i]/ATR;
else
Trend[i]=0;
}
else
Trend[i]=0;
} //for
//---- OnCalculate done. Return new prev_calculated.
return(rates_total);
}
//+------------------------------------------------------------------+
Comments