OPEN-SOURCE SCRIPT

Bicoherence

245
Bicoherence

A higher-order spectral measure for detecting nonlinear phase coupling in price returns. Where the standard power spectrum tells you how much energy sits at each frequency, the bispectrum tells you whether the phases of three frequencies (f₁, f₂, and f₁+f₂) are systematically locked together — a textbook signature of nonlinear dynamics. Bicoherence is the bispectrum normalized to [0, 1]: zero means no coupling at that frequency triple, one means perfect phase locking.
Linear (Gaussian) processes sit near zero across the bifrequency plane. When markets transition into regimes where price is driven by genuine nonlinear dynamics — feedback loops, regime shifts, wave-coupling effects — bicoherence rises, often well before conventional indicators move.
How it works
For each bar, the most recent (K−1)·step + N samples of log returns are split into K Hann-windowed segments of length N=32, with configurable overlap (default 50%, the Welch standard). Each segment is transformed by discrete Fourier transform. Triple products X(f₁)·X(f₂)·X*(f₁+f₂) are accumulated across segments for every (f₁, f₂) in the principal non-redundant domain {1 ≤ f₂ ≤ f₁, f₁+f₂ ≤ N/2}, alongside the magnitude products needed for normalization.
Per the standard direct estimator:
b²(f₁, f₂) = |⟨triple⟩|² / (⟨|X(f₁)X(f₂)|²⟩ · ⟨|X(f₁+f₂)|²⟩)
The script reports the mean and peak of b² across that domain on every bar.

How to read it

The thicker line is mean bicoherence — the average of b² over all 64 pairs in the principal domain. The thinner, dimmer line is peak bicoherence — the maximum across the same domain.
The dotted reference is 1/K, the theoretical mean of b² under a linear-Gaussian null hypothesis. Values that consistently sit above this line indicate genuine nonlinear coupling rather than estimation noise.
Line color shifts from cyan (low coupling) through deep purple (high coupling). A faint background tint marks bars where peak bicoherence exceeds 0.7 — the regime where at least one frequency triple is showing exceptional phase locking.

Inputs

  • Segments (K) — number of Hann-windowed sub-windows averaged per bar. More segments produce a smoother, less variance-prone estimate but require more history (history needed = (K−1)·step + N). Default 4.
  • Segment overlap — overlap between consecutive segments. 50% is Welch's recommendation and the default.
  • Source — input series for the log-return calculation. Default close.
  • Compute over last N bars — Pine has a 40-second runtime budget per script. Bicoherence is heavy, so the calculation is restricted to recent history. Default 1500. Lower this if you hit time-outs.
  • Visuals — toggles for peak line, baseline reference, glow, and regime tint, plus customizable colors for low/high coupling, peak, and neutral states.


Built-in alerts

  • Significant nonlinearity — mean bicoherence crosses 1.5× the linear-Gaussian baseline
  • Strong phase coupling — peak bicoherence crosses 0.5
  • Extreme phase coupling — peak bicoherence crosses 0.7
  • Returning to linearity — mean bicoherence drops back below the baseline


Notes

The FFT length is fixed at N=32 — enough for 16 frequency bins below Nyquist while keeping the DFT and triple-product accumulation tractable inside Pine's runtime budget.
Bicoherence assumes a well-mixed input series. Long stretches of zero returns from non-trading hours can produce spurious low-frequency coupling, so prefer active sessions when interpreting the result.
This is a diagnostic tool, not a signal generator. It tells you whether returns are exhibiting nonlinear structure.

Five years of work on a trading system left me with dozens of indicators that ultimately didn't earn a place in the final build. They're not failures — they're tools that solved problems I no longer needed solved. So instead of shelving them, I'm publishing the majority of them open-source.
If you're a discretionary trader, take what's useful. If you're a systems builder, the source is yours to dissect, modify, and improve. The best return on five years of work is for it to keep working — for someone.
If you use this script — or part of it — in your own work, please credit the original with a link back to my profile.
Note: these indicators have been updated to Pine Script v6 — some manually, some with AI assistance.

إخلاء المسؤولية

لا يُقصد بالمعلومات والمنشورات أن تكون، أو تشكل، أي نصيحة مالية أو استثمارية أو تجارية أو أنواع أخرى من النصائح أو التوصيات المقدمة أو المعتمدة من TradingView. اقرأ المزيد في شروط الاستخدام.