Instant TrendLine_v1
Download scriptThis script functions as a sophisticated technical analysis tool designed to identify and visualize market trends by calculating the "instantaneous" behavior of price cycles. Rather than simply looking at past averages, it attempts to understand the underlying rhythm of the market to determine the current dominant direction.
Core Logic Overview
The indicator operates through a multi-step mathematical process to filter out market noise and isolate the primary trend:
1. Price Signal Processing The script begins by calculating the midpoint of price action (the average between the high and low of each candle). It then evaluates the difference between these prices over specific intervals to identify raw movement. These initial values serve as the "raw data" for the rest of the calculations.
2. Phase and Cycle Analysis The tool employs advanced trigonometry to convert price movements into "phases." By analyzing the relationship between the current price action and its immediate history, it determines the cycle's progression. Essentially, it treats market movement like a wave, identifying where the wave is currently peaking, troughing, or moving through its mid-cycle.
3. Determining the Instantaneous Period One of the script's most unique features is its ability to calculate a dynamic "Instantaneous Period." Instead of using a fixed timeframe (like a 20-day or 50-day average), the script continuously monitors how long it takes for the market cycle to complete. It sums up these tiny shifts in phase to determine exactly how long the current dominant cycle lasts at any specific moment.
4. Trendline Generation Once the script knows the length of the current dominant cycle, it creates a trendline by calculating a moving average based specifically on that dynamic period. This means the trendline adjusts its "length" automatically:
- If the market cycle speeds up (becomes shorter), the trendline reacts more quickly.
- If the market cycle slows down (becomes longer), the trendline becomes smoother and covers more ground.
5. Dual Trend Visualization The script ultimately produces two distinct visual outputs on the chart:
- The Main Trend: A primary line representing the long-term dominant cycle based on the dynamic period calculation.
- The Secondary Reference: A secondary, smoothed price value that provides a point of comparison to help traders see the relationship between the immediate price and the calculated trend.
Summary for the User
In plain terms, this tool is designed to "listen" to the heartbeat of the market. It figures out how fast the market is currently "pulsing" and then draws a trendline that perfectly matches that speed. This allows traders to see the prevailing direction of the market while accounting for changes in volatility and cycle length in real-time.
/*-----------------------------+
| |
| Shared by www.Aptrafx.com |
| |
+------------------------------*/
//+------------------------------------------------------------------+
//| 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