Instant TrendLine

Download script
Author: Poul Trade Forum
Instant TrendLine

The Instantaneous Trend Line is a sophisticated technical analysis tool designed to identify and smooth out market trends by analyzing the underlying cyclical nature of price movements. Unlike standard moving averages that simply look at past prices, this script employs complex mathematical principles to "measure" the rhythm of the market in real-time.

How the Logic Works

The script operates through a multi-step process to determine the current direction and stability of the market:

1. Signal Extraction (Phase Analysis) The logic begins by analyzing the relationship between current prices and historical prices. It breaks down these price movements into two distinct components: "In-Phase" and "Quadrature." By treating price action like a wave, the script uses trigonometric calculations (specifically ArcTangent) to determine the "phase"—essentially finding where we are in the current market cycle at any given moment.

2. Measuring Market Cycles (The Instantaneous Period) One of the most unique features of this script is its ability to calculate an "Instantaneous Period." Instead of using a fixed timeframe (like a 50-day average), the tool calculates how long it actually takes for the market cycle to complete based on current volatility and momentum. It tracks the change in the market's "phase" over time and sums these changes to identify the dominant cycle length dynamically.

3. Dynamic Smoothing Because markets can be noisy, the script applies a smoothing filter to the calculated period. This ensures that the trend detection doesn't jump erratically between different cycle lengths due to minor price fluctuations, providing a more stable visual output for the trader.

4. Dual Trendline Generation The final output produces two distinct lines on your chart:

  • The Red Trendline: This is a dynamic average based on the "Instantaneous Period." It calculates the average price over the specific duration that the script has determined to be the current dominant cycle. Because the period changes as market conditions evolve, this line adapts automatically to faster or slower market regimes.
  • The Blue Trendline: This serves as a secondary reference point, utilizing a weighted calculation to provide a smoother, more consistent view of the price action, helping traders distinguish between immediate noise and broader movement.

Summary for the User

In plain terms, this tool doesn't just look at what the price was; it tries to understand how fast the market is "breathing." By identifying the current cycle length automatically, it draws a trendline that adjusts its sensitivity based on whether the market is moving in a slow, long-wave pattern or a fast, short-wave fluctuation.

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
//+------------------------------------------------------------------+
//|                          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