Indicator · Pine Script v6 · Own pane

Ehlers Phasor Analysis (PHASOR)

by mihakralj

Ehlers Phasor Analysis calculates the phasor angle, derived cycle period, and market trend state using John Ehlers' cycle analysis techniques.

View on GitHub

GitHub stars47on the repository
Pine versionv6current
Lines of code118
Settings4inputs
LicenceMIT
Last update8 months ago28 Feb 2026

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

118 lines of Pine Script v6 from indicators/cycles/phasor.pine, published under MIT. Keep the author's credit and licence lines when you reuse it.

phasor.pine
// The MIT License (MIT)
// © mihakralj
//@version=6
indicator("Ehlers Phasor Analysis (PHASOR)", shorttitle="PHASOR", overlay=false)

//@function Calculates the Ehlers Phasor Angle, Derived Period, and Trend State.
//@param src The source series to analyze.
//@param period The fixed cycle period to correlate against. Default is 28.
//@returns A tuple: `[float finalPhasorAngle, float derivedPeriod, int trendState]`.
phasor(series float src, simple int period = 28) =>
    float sx_corr = 0.0
    float sy_cos_corr = 0.0
    float sxx_corr = 0.0
    float sxy_cos_corr = 0.0
    float syy_cos_corr = 0.0
    for i = 0 to period - 1
        float x_val = nz(src[i])
        float y_val_cos = math.cos(2 * math.pi * i / period)
        sx_corr += x_val
        sy_cos_corr += y_val_cos
        sxx_corr += x_val * x_val
        sxy_cos_corr += x_val * y_val_cos
        syy_cos_corr += y_val_cos * y_val_cos
    float real_part = 0.0
    float den_cos = (period * sxx_corr - sx_corr * sx_corr) * (period * syy_cos_corr - sy_cos_corr * sy_cos_corr)
    if den_cos > 0
        real_part := (period * sxy_cos_corr - sx_corr * sy_cos_corr) / math.sqrt(den_cos)
    sx_corr := 0.0
    sxx_corr := 0.0
    float sy_sin_corr = 0.0
    float sxy_sin_corr = 0.0
    float syy_sin_corr = 0.0
    for i = 0 to period - 1
        float x_val = nz(src[i])
        float y_val_sin = -math.sin(2 * math.pi * i / period) // Negative sine as per Ehlers
        sx_corr += x_val
        sxx_corr += x_val * x_val
        sy_sin_corr += y_val_sin
        sxy_sin_corr += x_val * y_val_sin
        syy_sin_corr += y_val_sin * y_val_sin
    float imag_part = 0.0
    float den_sin = (period * sxx_corr - sx_corr * sx_corr) * (period * syy_sin_corr - sy_sin_corr * sy_sin_corr)
    if den_sin > 0
        imag_part := (period * sxy_sin_corr - sx_corr * sy_sin_corr) / math.sqrt(den_sin)
    float current_raw_phase = 0.0
    if real_part != 0.0
        current_raw_phase := 90.0 - math.atan(imag_part / real_part) * 180.0 / math.pi
        if real_part < 0.0
            current_raw_phase -= 180.0
    else if imag_part != 0.0
        current_raw_phase := imag_part > 0.0 ? 0.0 : 180.0
    var float core_Phasor_unwrapped_state = na
    if not na(core_Phasor_unwrapped_state[1])
        float diff = current_raw_phase - core_Phasor_unwrapped_state[1]
        if diff > 180.0
            current_raw_phase -= 360.0
        else if diff < -180.0
            current_raw_phase += 360.0
    core_Phasor_unwrapped_state := na(core_Phasor_unwrapped_state[1]) ? current_raw_phase : core_Phasor_unwrapped_state[1] + (current_raw_phase - core_Phasor_unwrapped_state[1])
    float calculated_Phasor_val = core_Phasor_unwrapped_state
    var float final_Phasor_state = na
    if na(final_Phasor_state[1])
        final_Phasor_state := calculated_Phasor_val
    else
        if calculated_Phasor_val < final_Phasor_state[1] and ((calculated_Phasor_val > -135 and final_Phasor_state[1] < 135) or (calculated_Phasor_val < -90 and final_Phasor_state[1] < -90))
            final_Phasor_state := final_Phasor_state[1]
        else
            final_Phasor_state := calculated_Phasor_val
    var float derivedPeriod_calc_state = na 
    float angle_Change_For_Period = final_Phasor_state - nz(final_Phasor_state[1], final_Phasor_state)
    if nz(angle_Change_For_Period) == 0 and not na(derivedPeriod_calc_state[1])
        if derivedPeriod_calc_state[1] != 0
            angle_Change_For_Period := 360.0 / derivedPeriod_calc_state[1]
        else
            angle_Change_For_Period := 0.0
    if nz(angle_Change_For_Period) <= 0 and not na(derivedPeriod_calc_state[1])
        if derivedPeriod_calc_state[1] != 0
            angle_Change_For_Period := 360.0 / derivedPeriod_calc_state[1]
        else
            angle_Change_For_Period := 0.0
    if nz(angle_Change_For_Period) != 0.0
        derivedPeriod_calc_state := 360.0 / angle_Change_For_Period
    else if not na(derivedPeriod_calc_state[1])
        derivedPeriod_calc_state := derivedPeriod_calc_state[1]
    else
        derivedPeriod_calc_state := 60.0 
    derivedPeriod_calc_state := math.max(1.0, math.min(derivedPeriod_calc_state, 60.0))
    var int trendState_calc_state = 0 
    float angle_Change_For_State = final_Phasor_state - nz(final_Phasor_state[1], final_Phasor_state)
    int currentTrendState_calc = 0 
    if angle_Change_For_State <= 6.0 
        if final_Phasor_state >= 90.0 or final_Phasor_state <= -90.0
            currentTrendState_calc := 1 
        else if final_Phasor_state > -90.0 and final_Phasor_state < 90.0
            currentTrendState_calc := -1 
    trendState_calc_state := currentTrendState_calc
    [final_Phasor_state, derivedPeriod_calc_state, trendState_calc_state]

// ---------- Inputs ----------
i_period = input.int(28, "Period", minval=1, group="Phasor Settings")
i_source = input.source(close, "Source", group="Phasor Settings")
showDerivedPeriod = input.bool(false, "Show Derived Period", group="Optional Plots", inline="derived_period")
showTrendState = input.bool(false, "Show Trend State Variable", group="Optional Plots", inline="trend_state")

// ---------- Calculations ----------
// Call the main function to get all values
[phasorAngle, derivedPeriodValue, trendStateValue] = phasor(i_source, i_period)

// ---------- Plotting Phasor Angle ----------
plot(phasorAngle, "Phasor Angle", color=color.yellow, linewidth=2)


// ---------- Optional Plots ----------
// Plot for Derived Period
plot(showDerivedPeriod ? derivedPeriodValue : na, "Derived Period", color=color.yellow, linewidth=2)

// Plot for Trend State
plot(showTrendState ? trendStateValue : na, "Trend State", color=color.yellow, linewidth=2, style=plot.style_histogram)

Overview

Ehlers Phasor Analysis (PHASOR) uses correlation against cosine and sine waveforms over a customizable lookback period to extract in-phase and quadrature components, tracking phase accumulation over time. The indicator plots in a separate pane below the price chart. Settings allow users to adjust the correlation period, price source, and toggle additional outputs for derived cycle period and trend state.

Details

Published by

mihakralj on GitHub

Type

Indicator, Pine Script v6

Draws

Separate pane below the chart

Category

Trend, Oscillators & momentum

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Topics

pinescriptpinescript-indicatorstechnical-analysistradingview-indicators

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
Periodint28i_period
Sourcesourceclosei_source
Show Derived PeriodboolfalseshowDerivedPeriod
Show Trend State VariableboolfalseshowTrendState
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

Schaff Trend Cycle

by everget
948 stars

This indicator plots the Schaff Trend Cycle in a separate pane. It subtracts a slow exponential moving average from a fast one, applies a stochastic cycle twice, and smooths each result to form a 0–100 oscillator. Line color follows the slope, or the upper and

Indicator v4 Oscillators & momentumTrend
Updated 10 days ago · GPL-3.0 GitHub repository of Schaff Trend Cycle

Beep Boop

by magic8bot
409 stars

This indicator calculates a modified MACD oscillator and signal line alongside a trend EMA filter, plotting the output as a stylized column histogram in a separate pane. Instead of plotting true MACD distance, it quantifies positive and negative histogram mome

Indicator v4 Oscillators & momentumTrend
Updated 3 years ago · MIT GitHub repository of Beep Boop

[RS]Standardized Trend Map

by ricardosantos79
261 stars

Standardized Trend Map calculates series momentum relative to active price range across 40 distinct lookback periods ranging from 2 to 80 bars on an exponential moving average source. Each standardized calculation rounds to an integer values of -1, 0, or 1 to

Indicator v4 TrendOscillators & momentum
Updated 4 years ago · None stated GitHub repository of [RS]Standardized Trend Map

ADX Histogram

by harryguiacorn
219 stars

This indicator calculates and displays the Average Directional Index (ADX) as a column histogram in a separate pane. It evaluates market trend strength by calculating directional movement metrics based on specified smoothing and DI length inputs. When the ADX

Indicator v5 TrendOscillators & momentum
Updated 1 year ago · None stated GitHub repository of ADX Histogram
218 stars

Multi-Timeframe Analysis Suite calculates trend direction and momentum conditions across five user-selected timeframes using moving averages, RSI, and Stochastic oscillators. It evaluates moving average slope and oscillator levels to compute trend alignment, m

Indicator v6 Screeners & dashboardsTrend
Updated 3 months ago · None stated GitHub repository of Multi-Timeframe Analysis Suite

More indicators

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers