#TickA_SineWavePeriods
Download scriptThis script is designed to identify and visualize the underlying cycle of a market's price movement. Rather than simply looking at whether a price is going up or down, it attempts to calculate the "heartbeat" or the natural frequency of the market's fluctuations.
Core Logic Overview
The indicator operates by treating price movements as a series of waves (oscillations). It uses mathematical principles—specifically trigonometry and signal processing—to decompose the complex movement of the market into a measurable cycle period. Here is how the logic flows:
1. Price Processing The script begins by establishing a baseline for the current price (calculating the average between the Ask and Bid prices). It then looks at the change in price over short intervals to determine the momentum of the movement.
2. Wave Analysis (In-Phase and Quadrature) To understand these movements, the script breaks them down into two components:
- One component tracks the "forward" progress of the wave.
- The other component tracks the "side" progress (the perpendicular motion). By comparing these two components, the script can determine exactly where the price currently sits within its current cycle (its "phase").
3. Calculating Phase Shift The logic then calculates the "Delta Phase," which is essentially measuring how much the market's cycle has moved in a single step. It performs several checks to ensure these calculations are accurate, specifically correcting for "wraparounds"—moments where the cycle completes and starts over at zero.
4. Identifying the Instantaneous Period This is the most critical part of the logic. The script looks backward across a specific window of time (defined by the user's length setting) and adds up all those small phase shifts. When the sum of these shifts reaches a full 360-degree rotation, the script identifies how many bars it took to complete that circle. This result is the "Instantaneous Period."
5. Smoothing and Final Output Because market data can be noisy, the script doesn't just take one raw calculation. It applies a smoothing formula to the calculated period to prevent erratic jumps in the line. Finally, it rounds this value to the nearest whole number and displays it as a trend line.
Summary for the User
In simple terms, instead of telling you "the price is high" or "the price is low," this script tells you how long the current market cycle lasts. It identifies the dominant "swing" duration of the market at any given moment, allowing you to see the underlying rhythm of the price action as a continuous trend line.
//+------------------------------------------------------------------+
//| Instantaneous Trend Line by John Ehlers |
//| Copyright © JDP |
//| |
//| |
//+------------------------------------------------------------------+
#property copyright "JDP"
#property link ""
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Red
//---- buffers
extern int Len=200;
double Periods[];
double Value1[],Value2,Value3,Value4,Value5[2],Value11[2];
double RealPart,ImagPart,DCPhase;
double Pi;
double Price[],InPhase[2],Quadrature[2],Phase[2],DeltaPhase[],InstPeriod[2],Period_;
double test[];
int LastTm;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
string short_name;
IndicatorBuffers(4);
//---- indicator line
SetIndexStyle(0,DRAW_LINE,EMPTY,2,Red);
SetIndexBuffer(0,Periods);
SetIndexBuffer(2,Value1);
SetIndexBuffer(1,Price);
SetIndexBuffer(3,DeltaPhase);
SetIndexEmptyValue(0,0);
//---- name for DataWindow and indicator subwindow label
short_name="Sine Wave Periods";
IndicatorShortName(short_name);
SetIndexLabel(0,short_name);
//----
SetIndexDrawBegin(0,30);
//----
return(0);
}
int deinit(){return(0);}
int start()
{
int i,shift,count;
Pi = 4 * MathArctan(1);
if (LastTm < Time[0]) {
for (i = 0;i<1000;i++) {
Periods[i] = Periods[i+1];
Value1[i] = Value1[i+1];
Price[i] = Price[i+1];
DeltaPhase[i] = DeltaPhase[i+1];
}
LastTm = Time[0];
}
for (i = 1001;i>0;i--) {
Periods[i] = Periods[i-1];
Value1[i] = Value1[i-1];
Price[i] = Price[i-1];
DeltaPhase[i] = DeltaPhase[i-1];
}
i = 0;
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]=(Ask+Bid)/2;
//Price[shift]=iCustom(Symbol(),0,"Heiken_Ashi_Smoothed",6,shift);
// {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])))*(180/Pi);
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<=Len;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]);///Period_ = IntPortion(Value5)
Periods[shift]= Period_;
//----
}
return(0);
}
Comments