MTF_NonLagMA_v7.1
Download scriptThis script is designed to provide traders with a sophisticated trend-following tool that analyzes market price movements across different timeframes simultaneously. Unlike standard moving averages, which often suffer from "lag"—the delay between a price change and the indicator's reaction—this specific logic utilizes a Non-Lagging Moving Average (NonLagMA) to provide a more responsive visual of the current trend.
Core Logic Overview
The primary function of this tool is to calculate and display trend data by smoothing out market "noise" without sacrificing the speed at which the indicator updates. It achieves this through several specific mechanisms:
1. Multi-Timeframe (MTF) Integration One of the most powerful features of this logic is its ability to look at different timeframes at once. For example, while you are looking at a 5-minute chart, the script can calculate and display data from the 1-hour or Daily charts. It synchronizes these larger timeframes onto your current view so you can see the "big picture" trends while making smaller, more immediate decisions.
2. Advanced Weighting System Instead of treating every price point equally, the script applies a complex mathematical weighting system (using trigonometric calculations). This allows the indicator to give different levels of importance to recent price data compared to older data. By doing this, it creates a smoother curve that follows the price more naturally, attempting to eliminate the typical "choppiness" seen in standard indicators.
3. Dynamic Trend Mapping The logic tracks three distinct components:
- The Main Trend: A smooth line representing the core average price direction.
- Upper/Lower Boundaries: The script identifies specific "Up" and "Down" trend buffers. These help traders distinguish between a strong move in one direction and a period of consolidation or indecision.
- Smoothing & Deviation: It includes filters to manage how much "deviation" (volatility) the indicator should ignore, helping to keep the visual output clean during heavy market fluctuations.
4. Data Synchronization and Smoothing To ensure that the data from higher timeframes looks fluid on a smaller chart, the script contains a "refresh" logic. When you are viewing a small timeframe (like 1 minute) but pulling data from a large timeframe (like 1 hour), there are often gaps in the data. The script intelligently fills these gaps by carrying forward the most recent calculated values, ensuring that the lines on your screen appear continuous and unbroken.
Summary for the User
In simple terms, this tool is built to show you where the market is heading with as little delay as possible. It gathers information from higher timeframes to give you context, uses advanced math to keep the lines smooth yet responsive, and ensures that all data aligns perfectly on your screen regardless of which chart timeframe you choose to trade.
//+------------------------------------------------------------------+
//| MTF_NonLagMA_v7.1.mq4 |
//| Copyright © 2007, TrendLaboratory |
//| http://finance.groups.yahoo.com/group/TrendLaboratory |
//| mtf:FXTSD.com E-mail: igorad2003@yahoo.co.uk |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, TrendLaboratory"
#property link "http://finance.groups.yahoo.com/group/TrendLaboratory"
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Orange
#property indicator_width1 2
#property indicator_color2 SkyBlue
#property indicator_width2 2
#property indicator_color3 Tomato
#property indicator_width3 2
//---- input parameters
/*****************************************************************
PERIOD_M1 1
PERIOD_M5 5
PERIOD_M15 15
PERIOD_M30 30
PERIOD_H1 60
PERIOD_H4 240
PERIOD_D1 1440
PERIOD_W1 10080
PERIOD_MN1 43200
You must use the numeric value of the timeframe that you want to use
when you set the TimeFrame' value with the indicator inputs.
********************************************************************/
//----
extern int TimeFrame=0;
extern int Price = 0; //Apply to Price(0-Close;1-Open;2-High;3-Low;4-Median price;5-Typical price;6-Weighted Close)
extern int Length = 9; //Period of NonLagMA
extern int Displace = 0; //DispLace or Shift
extern double PctFilter = 0; //Dynamic filter in decimal
extern int Color = 1; //Switch of Color mode (1-color)
extern int ColorBarBack = 1; //Bar back for color mode
extern double Deviation = 0; //Up/down deviation
extern int AlertMode = 0; //Sound Alert switch (0-off,1-on)
extern int WarningMode = 0; //Sound Warning switch(0-off,1-on)
extern string note1 = "Price(OCHLMTF)Length(period)Displace(shift)Color(0/1)";
//---- indicator buffers
double MABuffer[];
double UpBuffer[];
double DnBuffer[];
double trend[];
double Del[];
double AvgDel[];
double alfa[];
int i, Phase, Len,Cycle=4;
double Coeff, beta, t, Sum, Weight, g;
double pi = 3.1415926535;
bool UpTrendAlert=false, DownTrendAlert=false;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
IndicatorBuffers(6);
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,MABuffer);
SetIndexStyle(1,DRAW_LINE);
SetIndexBuffer(1,UpBuffer);
SetIndexStyle(2,DRAW_LINE);
SetIndexBuffer(2,DnBuffer);
SetIndexBuffer(3,trend);
SetIndexBuffer(4,Del);
SetIndexBuffer(5,AvgDel);
string short_name;
//---- indicator line
IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
//---- name for DataWindow and indicator subwindow label
short_name="NonLagMA["+Length+"]TF("+TimeFrame+")";
IndicatorShortName(short_name);
SetIndexLabel(0,""+short_name+"");
SetIndexLabel(1,""+short_name+"");
SetIndexLabel(2,""+short_name+"");
//----
SetIndexShift(0,Displace*TimeFrame/Period());
SetIndexShift(1,Displace*TimeFrame/Period());
SetIndexShift(2,Displace*TimeFrame/Period());
SetIndexEmptyValue(0,EMPTY_VALUE);
SetIndexEmptyValue(1,EMPTY_VALUE);
SetIndexEmptyValue(2,EMPTY_VALUE);
SetIndexDrawBegin(0,Length*Cycle+Length+1);
SetIndexDrawBegin(1,Length*Cycle+Length+1);
SetIndexDrawBegin(2,Length*Cycle+Length+1);
//----
Coeff = 3*pi;
Phase = Length-1;
Len = Length*4 + Phase;
ArrayResize(alfa,Len);
Weight=0;
for (i=0;i<Len-1;i++)
{
if (i<=Phase-1) t = 1.0*i/(Phase-1);
else t = 1.0 + (i-Phase+1)*(2.0*Cycle-1.0)/(Cycle*Length-1.0);
beta = MathCos(pi*t);
g = 1.0/(Coeff*t+1);
if (t <= 0.5 ) g = 1;
alfa[i] = g * beta;
Weight += alfa[i];
}
//----
switch(TimeFrame)
{
case 1 : string TimeFrameStr="Period_M1"; break;
case 5 : TimeFrameStr="Period_M5"; break;
case 15 : TimeFrameStr="Period_M15"; break;
case 30 : TimeFrameStr="Period_M30"; break;
case 60 : TimeFrameStr="Period_H1"; break;
case 240 : TimeFrameStr="Period_H4"; break;
case 1440 : TimeFrameStr="Period_D1"; break;
case 10080 : TimeFrameStr="Period_W1"; break;
case 43200 : TimeFrameStr="Period_MN1"; break;
default : TimeFrameStr="Current Timeframe";
}
//--
return(0);
}
//+------------------------------------------------------------------+
//| MTF_NonLagMA_v7.1 |
//+------------------------------------------------------------------+
int start()
{
datetime TimeArray[];
int i,shift,limit,y=0,counted_bars=IndicatorCounted();
// Plot defined timeframe on to current timeframe
ArrayCopySeries(TimeArray,MODE_TIME,Symbol(),TimeFrame);
limit=Bars-counted_bars+TimeFrame/Period();
for(i=0,y=0;i<limit;i++)
{
if (Time[i]<TimeArray[y]) y++;
/***********************************************************
Add your main indicator loop below. You can reference an existing
indicator with its iName or iCustom.
Rule 1: Add extern inputs above for all neccesary values
Rule 2: Use 'TimeFrame' for the indicator timeframe
Rule 3: Use 'y' for the indicator's shift value
**********************************************************/
MABuffer[i]=iCustom(NULL,TimeFrame,"NonLagMA_v7.1", Price,Length,Displace,
PctFilter,Color,ColorBarBack,Deviation,AlertMode,WarningMode,0,y);
UpBuffer[i]=iCustom(NULL,TimeFrame,"NonLagMA_v7.1", Price,Length,Displace,
PctFilter,Color,ColorBarBack,Deviation,AlertMode,WarningMode,1,y);
DnBuffer[i]=iCustom(NULL,TimeFrame,"NonLagMA_v7.1", Price,Length,Displace,
PctFilter,Color,ColorBarBack,Deviation,AlertMode,WarningMode,2,y);
}
//---- Refresh buffers +++++++++++++++++++++ upgrade by Raff
if (TimeFrame>Period()) {
int PerINT=TimeFrame/Period()+1;
datetime TimeArr[]; ArrayResize(TimeArr,PerINT);
ArrayCopySeries(TimeArr,MODE_TIME,Symbol(),Period());
for(i=0;i<PerINT+1;i++) {if (TimeArr[i]>=TimeArray[0]) {
//----
/***********************************************************
Refresh buffers: buffer[i] = buffer[0];
************************************************************/
MABuffer[i]= MABuffer[0];
UpBuffer[i]= UpBuffer[0];
DnBuffer[i]= DnBuffer[0];
//----
} } }
//+++++++++++++++++++++++++++++++++++++++++++++ Raff ++++++
return(0);
}
Comments