NT_Trigger_Lines_Big_Recoded

Download script
Author: Copyright 2013, William Kreider (Madhatt30)
NT_Trigger_Lines_Big_Recoded

This tool is designed to help traders visualize market trends and identify specific turning points by analyzing price movements through a multi-layered mathematical approach. Instead of simply looking at raw price action, it processes data into smoother "trend lines" to filter out unnecessary market noise.

The Core Logic

The script functions by creating two primary layers of data:

  1. Linear Regression (The Foundation): The tool first calculates a Linear Regression value. In simple terms, this looks at a specific number of past price bars and attempts to find the "best-fit" straight line through them. This represents the underlying direction of the price over a set period.

  2. Exponential Moving Average (The Trigger): The script then takes that Linear Regression data and applies an Exponential Moving Average (EMA) to it. Because an EMA gives more weight to recent data, this creates a "trigger" line that reacts dynamically to changes in the primary trend.

Visual Interpretation

On your trading chart, these calculations are translated into four distinct visual lines:

  • Trend Identification: The logic constantly compares the position of the Primary Trend line against the Trigger line.
  • Color Coding & State Changes: The script uses these two lines to determine the current "mood" of the market. When the relationship between the trend and the trigger changes—specifically when they cross or shift their orientation—the script updates the visual output.
  • Dynamic Highlighting: By alternating which lines are visible (Green vs. Red), the indicator highlights whether the momentum is currently shifting in favor of an upward move or a downward move.

How it Works for the User

Essentially, the script performs the heavy mathematical lifting of calculating regressions and averages in real-time. It then simplifies that complex math into a visual "signal." By watching the interaction between the green and red lines, you can more easily identify when the market momentum is accelerating or reversing, providing a clearer picture of the trend than raw price candles alone would offer.

Indicators Used
Moving average indicator
Miscellaneous
Implements a curve of type %1
3 Views
0 Downloads
0 Favorites
NT_Trigger_Lines_Big_Recoded
//+------------------------------------------------------------------+
//|                                     NT Trigger Lines Recoded.mq4 |
//|                      Copyright 2013, William Kreider (Madhatt30) |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, William Kreider (Madhatt30)"
#property link      "http://www.metaquotes.net"

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 Green
#property indicator_color2 Green
#property indicator_color3 Red
#property indicator_color4 Red
#property indicator_width1 2
#property indicator_width2 2
#property indicator_width3 2
#property indicator_width4 2
//--- input parameters
extern int       length=80;
extern int       trigAvg=20;
//--- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
double c;
//---- additional calculating buffers
double LRBuffer[],EMABuffer[];
bool bTrigWasRising=false;
bool firstrun=true;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   IndicatorBuffers(6);
//---- indicators
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexStyle(2,DRAW_LINE);
   SetIndexBuffer(2,ExtMapBuffer3);
   SetIndexStyle(3,DRAW_LINE);
   SetIndexBuffer(3,ExtMapBuffer4);
//---- Counting buffers same as DataSeries in NinjaTrader
   SetIndexBuffer(4,LRBuffer);
   SetIndexBuffer(5,EMABuffer);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int i;
   int counted_bars=IndicatorCounted();
   if(counted_bars < 0)  return(-1);
   if(counted_bars>0) counted_bars--;
   int limit=Bars-counted_bars;
   if(counted_bars==0) limit-=1+length;
//----
   if(firstrun)
     {
      for(i=limit;i>=0;i--)
        {
         LRBuffer[i]=linreg(length,i);
        }
      for(i=limit;i>=0;i--)
        {
         EMABuffer[i]=iMAOnArray(LRBuffer,0,trigAvg,0,MODE_EMA,i);
        }
      firstrun=false;
     }
   RefreshRates();
   WindowRedraw();
   for(i=0;i<=limit;i++)
     {
      LRBuffer[i]=linreg(length,i);
      EMABuffer[i]=iMAOnArray(LRBuffer,0,trigAvg,0,MODE_EMA,i);
      if(EMABuffer[i]>=LRBuffer[i])
        {
         if(bTrigWasRising)
           {
            ExtMapBuffer1[i]=LRBuffer[i];
            ExtMapBuffer2[i]=EMABuffer[i];
            ExtMapBuffer3[i]=EMPTY_VALUE;
            ExtMapBuffer4[i]=EMPTY_VALUE;
           }
         else
           {
            ExtMapBuffer3[i]=LRBuffer[i];
            ExtMapBuffer4[i]=EMABuffer[i];
            ExtMapBuffer1[i]=LRBuffer[i];
            ExtMapBuffer2[i]=EMABuffer[i];
           }
         bTrigWasRising=false;
        }
      else
        {
         if(!bTrigWasRising)
           {
            ExtMapBuffer3[i]=LRBuffer[i];
            ExtMapBuffer4[i]=EMABuffer[i];
            ExtMapBuffer1[i]=LRBuffer[i];
            ExtMapBuffer2[i]=EMABuffer[i];
           }
         else
           {
            ExtMapBuffer1[i]=LRBuffer[i];
            ExtMapBuffer2[i]=EMABuffer[i];
            ExtMapBuffer3[i]=EMPTY_VALUE;
            ExtMapBuffer4[i]=EMPTY_VALUE;
           }
         bTrigWasRising=true;
        }
     }
//----
   RefreshRates();
   WindowRedraw();
   return(0);
  }
//+------------------------------------------------------------------+
//| linreg                                                           |
//+------------------------------------------------------------------+
double linreg(int p,int i)
  {
   double SumY=0;
   double Sum1=0;
   double Slope=0;
//double c;

   for(int x=0; x<=p-1;x++)
     {
      c=Close[x+i];
      SumY+=c;
      Sum1+=x*c;
     }
   double SumBars=p*(p-1)*0.5;
   double SumSqrBars=(p-1)*p*(2*p-1)/6;
   double Sum2=SumBars*SumY;
   double Num1=p*Sum1-Sum2;
   double Num2=SumBars*SumBars-p*SumSqrBars;
   if(Num2!=0) Slope=Num1/Num2;
   else Slope=0;
   double Intercept=(SumY-Slope*SumBars)/p;
   double linregval=Intercept+Slope*(p-1);
   return(linregval);
  }
//+------------------------------------------------------------------+

Comments