Indicator · Pine Script v6 · Own pane

Ehlers Correlation Cycle (CCOR)

by mihakralj

This indicator computes the Ehlers Correlation Cycle in a separate pane.

View on GitHub

GitHub stars99on the repository
Pine versionv6current
Lines of code107
Settings3inputs
LicenceApache-2.0
Last update4 weeks ago27 Sep 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

107 lines of Pine Script v6 from lib/cycles/ccor/ccor.pine, published under Apache-2.0. Keep the author's credit and licence lines when you reuse it.

ccor.pine
// Licensed under the Apache License, Version 2.0
// © mihakralj
//@version=6
indicator("Ehlers Correlation Cycle (CCOR)", "CCOR", overlay=false)

//@function Computes Ehlers Correlation Cycle — extracts cycle phase via Pearson correlation of price
//          with cosine (Real) and negative-sine (Imag) reference waves of a presumed fixed period.
//          Converts to phasor angle with monotonic constraint, and derives market state.
//@param source Series to analyze
//@param period Presumed dominant cycle wavelength
//@param threshold Angle rate-of-change threshold (degrees) for trend/cycle state detection
//@returns [real, imag, angle, state] — correlation components, phasor angle, market state (+1/-1/0)
//@reference John F. Ehlers, "Correlation As A Cycle Indicator" (Stocks & Commodities, TASC Jun 2020)
//@optimized O(period) per bar for dual correlation loops; O(1) state variables
ccor(series float source, simple int period, simple float threshold) =>
    if period <= 0
        runtime.error("Period must be greater than 0")
    if threshold <= 0
        runtime.error("Threshold must be greater than 0")

    var float prev_angle = 0.0

    float price = nz(source)

    // --- Correlate price with cosine wave (Real component) ---
    float sx_r = 0.0
    float sy_r = 0.0
    float sxx_r = 0.0
    float sxy_r = 0.0
    float syy_r = 0.0
    for count = 0 to period - 1
        float x = nz(source[count], price)
        float y = math.cos(2.0 * math.pi * count / float(period))
        sx_r += x
        sy_r += y
        sxx_r += x * x
        sxy_r += x * y
        syy_r += y * y

    float n = float(period)
    float denom_r = (n * sxx_r - sx_r * sx_r) * (n * syy_r - sy_r * sy_r)
    float real_val = denom_r > 0.0 ? (n * sxy_r - sx_r * sy_r) / math.sqrt(denom_r) : 0.0

    // --- Correlate price with negative sine wave (Imaginary component) ---
    float sx_i = 0.0
    float sy_i = 0.0
    float sxx_i = 0.0
    float sxy_i = 0.0
    float syy_i = 0.0
    for count = 0 to period - 1
        float x = nz(source[count], price)
        float y = -math.sin(2.0 * math.pi * count / float(period))
        sx_i += x
        sy_i += y
        sxx_i += x * x
        sxy_i += x * y
        syy_i += y * y

    float denom_i = (n * sxx_i - sx_i * sx_i) * (n * syy_i - sy_i * sy_i)
    float imag_val = denom_i > 0.0 ? (n * sxy_i - sx_i * sy_i) / math.sqrt(denom_i) : 0.0

    // --- Compute phasor angle (degrees) with quadrant resolution ---
    float angle = 0.0
    if imag_val != 0.0
        angle := 90.0 + math.todegrees(math.atan(real_val / imag_val))
    if imag_val > 0.0
        angle -= 180.0

    // --- Monotonic constraint: angle cannot go backward ---
    float saved_prev = prev_angle
    if angle < prev_angle
        angle := prev_angle
    prev_angle := angle

    // --- Market state detection ---
    // Small angle change → trending; large angle change → cycling
    float angle_change = math.abs(angle - saved_prev)
    int state = 0
    if angle_change < threshold and angle <= 0.0
        state := -1    // downtrend
    if angle_change < threshold and angle >= 0.0
        state := 1     // uptrend
    // state = 0 → cycling mode

    [real_val, imag_val, angle, state]

// ---------- Main loop ----------

// Inputs
i_period    = input.int(20, "Period", minval=2)
i_threshold = input.float(9.0, "State Threshold (degrees)", minval=0.1, step=0.5)
i_source    = input.source(close, "Source")

// Calculation
[real_out, imag_out, angle_out, state_out] = ccor(i_source, i_period, i_threshold)

// Scaled Real/Imag for display: map [-1,+1] → [-100,+100]
float real_scaled = real_out * 100.0
float imag_scaled = imag_out * 100.0

// Colors based on Real vs Imag crossover
color sig_color = real_scaled > imag_scaled ? color.new(color.green, 0) : color.new(color.red, 0)

// Plots
plot(real_scaled, "Real (×100)", color=sig_color, linewidth=2)
plot(imag_scaled, "Imag (×100)", color=color.gray, linewidth=1)
hline(0, "Zero", color=color.gray, linestyle=hline.style_dotted)

Overview

Ehlers Correlation Cycle (CCOR) correlates a chosen price source with cosine and negative-sine waves of a fixed period, then converts those real and imaginary components into a phasor angle that cannot move backward. A small angle change below the degree threshold marks an uptrend or downtrend; otherwise the state is cycling. Period, threshold, and source are the main settings. It has no entries, exits, or position sizing.

Details

Published by

mihakralj on GitHub

Type

Indicator, Pine Script v6

Draws

Separate pane below the chart

Category

Oscillators & momentum, Trend

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

algorithmic-tradingcsharpdotnetpinescriptpythonquantitative-financesimdta-libtechnical-analysistechnical-indicators

Settings

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

SettingTypeDefaultVariable
Periodint20i_period
State Threshold (degrees)float9.0i_threshold
Sourcesourceclosei_source
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