Instant TrendLine_v2

Author: Poul Trade Forum
Instant TrendLine_v2

This script functions as a sophisticated technical analysis tool designed to identify and visualize market trends by calculating "instantaneous" cycles. Unlike standard moving averages that look at a fixed number of past bars, this logic dynamically adapts to the underlying rhythm of the price action.

Here is a breakdown of how the logic works in plain terms:

Core Logic & Methodology

The script operates by treating price movements as a series of waves or cycles. To do this, it performs several complex mathematical steps to "clean" and interpret the market data:

1. Data Preparation Instead of looking at raw high and low prices, the script first identifies the midpoint of each candle. It then calculates the velocity of these midpoints over a short window to determine how fast the price is moving in a specific direction.

2. Cycle Analysis (The Wave Phase) The tool breaks down the price movement into two components: "In-Phase" and "Quadrature." Think of this like analyzing a circle—it determines where the current price is located within a complete rotation of a trend cycle. By using trigonometry, it identifies the "Phase" (the angle) of the current market wave.

3. Detecting the Instantaneous Period This is the most unique part of the script. Rather than assuming a cycle lasts for 20 or 50 bars, the script calculates a "Delta Phase"—the change in the wave's position from one moment to the next. It then sums these changes until they complete a full 360-degree rotation. This tells the script exactly how long the current market cycle is at any given second (the "Instantaneous Period").

4. Dynamic Trend Smoothing Once the script knows the length of the current cycle, it uses that information to draw the trend lines:

  • The Main Trend Line (Red): The script calculates a simple average based on the length of the cycle it just discovered. If the market is moving in a long, slow cycle, the average covers more data; if the cycle is short and fast, the average covers less. This allows the line to "stretch" or "shrink" to fit the current market volatility.
  • The Secondary Trend Line (Blue): This provides an additional layer of smoothing by blending the current price with a slight offset of recent prices, helping to filter out minor "noise" while remaining responsive to larger shifts.

Summary for the User

In essence, this script doesn't just follow the price; it tries to understand the rhythm of the price. By identifying how long a typical market cycle lasts at any specific moment, it produces trend lines that adapt automatically to changes in market speed.

  • When the market is in a stable, long-term move, the lines provide a broad overview.
  • When the market becomes choppy or shifts into a faster rhythm, the lines adjust their sensitivity to reflect that new reality.
Price Data Components
Series array that contains open time of each bar
Miscellaneous
Implements a curve of type %1
10 Views
0 Downloads
0 Favorites
Instant TrendLine_v2
//+------------------------------------------------------------------+
//|                          Instantaneous Trend Line by John Ehlers |
//|                               Copyright © 2004, Poul_Trade_Forum |
//|                                                         Aborigen |
//|                                          http://forex.kbpauk.ru/ |
//+------------------------------------------------------------------+
#property copyright "Poul Trade Forum"
#property link      "http://forex.kbpauk.ru/"
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 Blue
//---- buffers
double TRBuffer[];
double ZLBuffer[];
double Value1[],Value2,Value3,Value4,Value5[2],Value11[2];
double Price[],InPhase[2],Quadrature[2],Phase[2],DeltaPhase[],InstPeriod[2],Period_,Trendline;
double test[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   string short_name;
   IndicatorBuffers(5);
//---- indicator line
   SetIndexStyle(0,DRAW_LINE,EMPTY,2,Red);
   SetIndexStyle(1,DRAW_LINE,EMPTY,2,Blue);
   SetIndexBuffer(0,TRBuffer);
   SetIndexBuffer(1,ZLBuffer);
   SetIndexBuffer(2,Value1);
   SetIndexBuffer(3,Price);
   SetIndexBuffer(4,DeltaPhase);
   SetIndexEmptyValue(0,0);
   SetIndexEmptyValue(1,0);
//---- name for DataWindow and indicator subwindow label
   short_name="Instantaneous Trend";
   IndicatorShortName(short_name);
   SetIndexLabel(0,short_name);
//----
   SetIndexDrawBegin(0,30);
   SetIndexDrawBegin(1,30);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int deinit(){return(0);}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted(),i,shift,count;
   if (counted_bars==0) counted_bars=100;
   i=(Bars-counted_bars)-2;
   for(shift=i; shift>=0;shift--)
     {
      InPhase[1] =InPhase[0]; Quadrature[1] =Quadrature[0];
      Phase[1]=Phase[0]; InstPeriod[1]=InstPeriod[0];
      Value5[1] =Value5[0];Value11[1] =Value11[0];
      Price[shift]=(High[shift+1]+Low[shift+1])/2;
      //  {Compute InPhase and Quadrature components}
      Value1[shift]=Price[shift] - Price[shift+6];
      Value2 =Value1[shift+3];
      Value3 =0.75*(Value1[shift] - Value1[shift+6]) + 0.25*(Value1[shift+2] - Value1[shift+4]);
      InPhase[0]=0.33*Value2 + 0.67*InPhase[1];
      Quadrature[0]=0.2*Value3 + 0.8*Quadrature[1];
      //   {Use ArcTangent to compute the current phase}
      if (MathAbs(InPhase[0]+InPhase[1])>0) Phase[0]=MathArctan(MathAbs((Quadrature[0]+Quadrature[1])/(InPhase[0]+InPhase[1])));
      //   {Resolve the ArcTangent ambiguity}
      if (InPhase[0] < 0 && Quadrature[0] > 0)  Phase[0]=180 - Phase[0];
      if (InPhase[0] < 0 && Quadrature[0] < 0)  Phase[0]=180 + Phase[0];
      if (InPhase[0] > 0 && Quadrature[0] < 0)  Phase[0]=360 - Phase[0];
      //   {Compute a differential    phase, resolve phase wraparound, and limit delta phase errors}
      DeltaPhase[shift]=Phase[1] - Phase[0];
      if (Phase[1] < 90 &&  Phase[0] > 270) DeltaPhase[shift]=360 + Phase[1] - Phase[0];
      if (DeltaPhase[shift] < 1)  DeltaPhase[shift]=1;
      if (DeltaPhase[shift] > 60) DeltaPhase[shift]=60;
      //   {Sum DeltaPhases  to reach 360  degrees. The sum is the instantaneous period.}
      InstPeriod[0]=0;
      Value4=0;
      for(count=0;count<=40;count++)
        {
         Value4=Value4 + DeltaPhase[shift+count];
         if  (Value4 > 360 && InstPeriod[0] ==0) InstPeriod[0]=count;
        }
      //   {Resolve Instantaneous  Period    errors and  smooth}
      if (InstPeriod[0]==0) InstPeriod[0]=InstPeriod[1];
      Value5[0]=0.25*(InstPeriod[0]) + 0.75*Value5[1];
      //   {Compute Trendline as simple average over the measured dominant cycle period}
      Period_=MathCeil(Value5[0]); Trendline=0;///Period_ = IntPortion(Value5)
      for(count=0;count<=Period_ + 1;count++)
        {
         Trendline=Trendline + Price[shift+count];
        }
      if (Period_ > 0) Trendline=Trendline   /(Period_  + 2);
      Value11[0]=0.33*(Price[shift] + 0.5*(Price[shift] - Price[shift+3])) + 0.67*Value11[1];
      TRBuffer[shift]=Trendline;
      ZLBuffer[shift]=Value11[0];
      //----
     }
/*
Comment("Last Tick:",TimeToStr(CurTime(),TIME_DATE|TIME_SECONDS),"\n",
        "Instant Trendline","\n",
        "Red Trend: ",TRBuffer[0],"\n",
        "Blue Trend: ",ZLBuffer[0]);*/
   return(0);
  }
//+------------------------------------------------------------------+

Comments