nonlagdotMS_001
Download scriptThis script is designed to provide a visual representation of market trends while attempting to minimize the "lag" typically associated with standard moving averages. Instead of simply averaging past prices, it uses a sophisticated mathematical approach to identify direction and momentum more fluidly on a trading chart.
Core Logic Overview
1. Advanced Trend Smoothing The primary logic relies on a customized calculation that weights historical price data differently based on a trigonometric cycle. Rather than treating all past data points equally, the script applies a "weighting" system using cosine waves and specific coefficients. This allows the indicator to prioritize price movements that are most relevant to the current trend, helping to create a smoother line that reacts more intelligently to market shifts.
2. Multi-Timeframe Analysis To provide a more stable perspective, the logic incorporates data from a different time frame (specifically a 30-minute chart) to help inform the calculations of the current view. This helps filter out minor "market noise" and ensures that the trend shown is supported by broader price action rather than just localized fluctuations.
3. Trend Filtering and Detection The script features several layers of logic to ensure clarity for the user:
- Volatility Filtering: It can be configured to ignore very small price movements. If a change is smaller than a specified threshold, the indicator remains steady, preventing "choppy" signals in sideways markets.
- Trend Identification: The logic continuously compares the current calculated value against the previous one. If the movement exceeds a specific deviation, it registers a change in trend.
- Directional Buffers: Based on these changes, the script categorizes movements into "Up" or "Down" trends. This is represented visually by distinct colors (Yellow, Royal Blue, and Red) to make it easy to see at a glance whether the momentum is bullish or bearish.
4. Visual Signals and Alerts The script is designed to act as an active monitoring tool:
- Directional Dots: It places specific markers on the chart to highlight where trend shifts occur.
- Historical Continuity: It has a "Backfill" logic that can color previous bars based on the trend detected at that time, providing a cohesive visual history of the market's direction.
- Audio Notifications: The script includes an automated alert system. When a trend reversal is confirmed (switching from up to down, or vice versa), it can trigger an audio file to notify the trader immediately.
Summary for the User
In simple terms, this tool looks at price history through a mathematical "lens" that smooths out random wobbles while staying sensitive to real trend changes. It tells you which way the market is leaning by color-coding the movement and providing audio/visual cues the moment the momentum shifts direction.
//+------------------------------------------------------------------+
//| NonLagDOT.mq4 |
//| Copyright © 2006, TrendLaboratory |
//| http://finance.groups.yahoo.com/group/TrendLaboratory |
//| E-mail: igorad2003@yahoo.co.uk |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, TrendLaboratory"
#property link "http://finance.groups.yahoo.com/group/TrendLaboratory"
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Yellow
#property indicator_width1 2
#property indicator_color2 RoyalBlue
#property indicator_width2 2
#property indicator_color3 Red
#property indicator_width3 2
//---- input parameters
extern int Price = 0;
extern int Length = 16;//20
extern int Displace = 0;
extern int Filter = 0;
extern int Color = 1;
extern int ColorBarBack = 0;
extern double Deviation = 0;
extern int SoundAlertMode = 1;
double Cycle = 4;
//---- indicator buffers
double MABuffer[];
double UpBuffer[];
double DnBuffer[];
double price[];
double trend[];
bool UpTrendAlert=false, DownTrendAlert=false;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
int ft=0;
string short_name;
//---- indicator line
IndicatorBuffers(5);
SetIndexStyle(0,DRAW_ARROW);
SetIndexBuffer(0,MABuffer);
SetIndexStyle(1,DRAW_ARROW);
SetIndexBuffer(1,UpBuffer);
SetIndexStyle(2,DRAW_ARROW);
SetIndexBuffer(2,DnBuffer);
SetIndexBuffer(3,price);
SetIndexBuffer(4,trend);
IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
SetIndexArrow(0,159);
SetIndexArrow(1,159);
SetIndexArrow(2,159);
SetIndexArrow(3,159);
//---- name for DataWindow and indicator subwindow label
short_name="NonLagDot("+Length+")";
IndicatorShortName(short_name);
SetIndexLabel(0,"NLD");
SetIndexLabel(1,"Up");
SetIndexLabel(2,"Dn");
//----
SetIndexShift(0,Displace);
SetIndexShift(1,Displace);
SetIndexShift(2,Displace);
SetIndexDrawBegin(0,Length*Cycle+Length);
SetIndexDrawBegin(1,Length*Cycle+Length);
SetIndexDrawBegin(2,Length*Cycle+Length);
//----
return(0);
}
//+------------------------------------------------------------------+
//| NonLagMA_v4 |
//+------------------------------------------------------------------+
int start()
{
int i,shift, counted_bars=IndicatorCounted(),limit;
double alfa, beta, t, Sum, Weight, step,g;
double pi = 3.1415926535;
double Coeff = 3*pi;
int Phase = Length-1;
double Len = Length*Cycle + Phase;
if ( counted_bars > 0 ) limit=Bars-counted_bars;
if ( counted_bars < 0 ) return(0);
if ( counted_bars ==0 ) limit=Bars-Len-1;
if ( counted_bars < 1 )
for(i=1;i<Length*Cycle+Length;i++)
{
MABuffer[Bars-i]=0;
UpBuffer[Bars-i]=0;
DnBuffer[Bars-i]=0;
}
for(shift=limit;shift>=0;shift--)
{
Weight=0; Sum=0; t=0;
for (i=0;i<=Len-1;i++)
{
g = 1.0/(Coeff*t+1);
if (t <= 0.5 ) g = 1;
beta = MathCos(pi*t);
alfa = g * beta;
//if (shift>=1) price[i] = iMA(NULL,0,Per,Displace,Mode,Price,shift+i);
//else
price[i] = iMA(NULL,30,1,0,MODE_SMA,Price,shift+i); //0 tf
Sum += alfa*price[i];
Weight += alfa;
if ( t < 1 ) t += 1.0/(Phase-1);
else if ( t < Len-1 ) t += (2*Cycle-1)/(Cycle*Length-1);
}
if (Weight > 0) MABuffer[shift] = (1.0+Deviation/100)*Sum/Weight;
if (Filter>0)
{
if( MathAbs(MABuffer[shift]-MABuffer[shift+1]) < Filter*Point ) MABuffer[shift]=MABuffer[shift+1];
}
if (Color>0)
{
trend[shift]=trend[shift+1];
if (MABuffer[shift]-MABuffer[shift+1] > Filter*Point) trend[shift]= 1;
if (MABuffer[shift+1]-MABuffer[shift] > Filter*Point) trend[shift]=-1;
if (trend[shift]>0)
{
UpBuffer[shift] = MABuffer[shift];
if (trend[shift+ColorBarBack]<0) UpBuffer[shift+ColorBarBack]=MABuffer[shift+ColorBarBack];
DnBuffer[shift] = EMPTY_VALUE;
if (SoundAlertMode>0 && trend[shift+1]<0 && shift==0) PlaySound("alert2.wav");
}
if (trend[shift]<0)
{
DnBuffer[shift] = MABuffer[shift];
if (trend[shift+ColorBarBack]>0) DnBuffer[shift+ColorBarBack]=MABuffer[shift+ColorBarBack];
UpBuffer[shift] = EMPTY_VALUE;
if (SoundAlertMode>0 && trend[shift+1]>0 && shift==0) PlaySound("alert2.wav");
}
}
}
//----------
/*
string Message;
if ( trend[2]<0 && trend[1]>0 && Volume[0]>1 && !UpTrendAlert)
{
Message = " "+Symbol()+" M"+Period()+": NAIK ATAS LA CEPAT";
if ( SoundAlertMode>0 ) Alert (Message);
UpTrendAlert=true; DownTrendAlert=false;
}
if ( trend[2]>0 && trend[1]<0 && Volume[0]>1 && !DownTrendAlert)
{
Message = " "+Symbol()+" M"+Period()+": TURUN LA PULOLK";
if ( SoundAlertMode>0 ) Alert (Message);
DownTrendAlert=true; UpTrendAlert=false;
}
*/
//----
return(0);
}
Comments