TASC's November 2022 edition Traders' Tips includes an article by John Ehlers titled "Recurring Phase Of Cycle Analysis". This is the code that implements the phasor analysis indicator presented in this publication.
The article explores the use of phasor analysis to identify market trends.
An ordinary rotating phasor diagram is a two-dimensional vector, anchored to the origin, whose rotation rate corresponds to the cycle period in the price data stream. Similarly, Ehlers' phasor is a representation of angular phase rotation along the course of time. Its angle reflects the current phase of the cycle. Angles -180, -90, +90 and +180 degrees correspond to the beginning, valley, peak and end of the cycle, respectively.
If the observed cycle is very long, the market can be considered trending. In his article, John Ehlers defined trending behavior to occur when the derived instantaneous cycle period value is greater that 60 bars. The author also introduced guidelines for long and short entries in a trending state. Depending on the tuning of the indicator period input, a long entry position may occur when the phasor angle is around the approximate vicinity of −90 degrees, while a short entry position may occur when the phasor angle will be around the approximate vicinity of +90 degrees. Applying these definitive guidelines, the author proposed a state variable that is indicated by +1 for a trending long position, 0 for cycling, and −1 for a trending short position (or out).
The phasor angle, the cycle period, and the state variable are made available with three selectable display modes provided for this TradingView indicator.
The calculations are carried out as follows.
First, the price data stream is correlated with cosine and sine of a fixed cycle period. This produces two new data streams that correspond to the projections of the frequency domain phasor diagram to the horizontal (so-called real) and vertical (so-called imaginary) axis respectively. The wavelength of the cycle period input should be set for the midrange vicinities of the phasor to coincide with the peaks and valleys of the charted price data.
Secondly, the phase angle of the phasor is easily computed as the arctangent of the ratio of the imaginary component to the real component. The difference between the current phasor values and its last is then employed to calculate a derived instantaneous period and market state. This computation is then repeated successively for each individual bar over the entire duration of the data set.
Pine news broadcasts: t.me/PineCodersSquawkBox or twitter.com/PineCoders
In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in a publication is governed by House Rules. You can favorite it to use it on a chart.