OPEN-SOURCE SCRIPT

Market Time Cycle (Machine Learning: K-Means Clustering)

1 212
🕰️Market Time Cycle (Machine Learning: K-Means Clustering)

▶️Overview

The Market Time Cycle Oscillator is a sophisticated predictive analysis tool designed to decode the "temporal DNA" of financial markets. While conventional oscillators (like RSI or Stochastics) measure price momentum and overbought/oversold levels, this indicator focuses on the Time Domain.

It identifies recurring intervals between market pivots to estimate the mathematical probability of the next reversal point.
By leveraging K-Means Clustering, it doesn't just look for a single cycle but identifies multiple dominant frequencies simultaneously, providing a probabilistic "heat map" for future Pivot Highs and Pivot Lows.

▶️Technical Core: The K-Means Advantage

1. From Rigid Cycles to Dynamic Clusters
Traditional cycle analysis (like Fourier Transforms) often struggles with "noise" and the non-stationary nature of market data. Market cycles are rarely fixed; they expand and contract.
This indicator uses K-Means Clustering, an unsupervised machine learning algorithm, to solve this:
Observation: It measures the bar-index distance between historical pivots.
Clustering: Instead of averaging these distances, K-Means groups them into K distinct clusters (centroids).
Result: It can identify, for example, a short-term 20-bar cycle and a mid-term 60-bar cycle existing at the same time, without them cancelling each other out.

2. Gaussian Probability Waves
Once the dominant cycle lengths (centroids) are identified, the engine doesn't just plot a single line at a fixed future date. It recognizes that "history rhymes but doesn't repeat perfectly."
Mathematical Projection: Each cycle is projected forward from the most recent pivots.
Gaussian Distribution: A Normal (Gaussian) distribution curve is applied to each projection. The peak represents the most likely timing, while the "wings" represent the statistical margin of error.
Aggregation: All probability waves are summed to create the final "Total Probability" cloud seen on the oscillator.

▶️The Bipolar Logic: A Dual-Force Perspective

The indicator is split into two halves to provide a clear view of opposing market forces:
Positive Side (Upper Cloud): Summation of probabilities for a Pivot High. When this cloud peaks, the market is entering a "Time Window" where price historically finds a ceiling and begins to move downward.
Negative Side (Lower Cloud): Summation of probabilities for a Pivot Low. A peak here indicates a high statistical likelihood of a market floor and an upward reversal.

▶️Key Features

ML-Driven Adaptability: The engine retrains its K-Means centroids every time a new pivot is confirmed, allowing it to adapt to "Cycle Compression" or "Cycle Expansion" in real-time.
Multi-Layered Analysis: It distinguishes between "Standard" (trend-aligned) and "Inverse" (counter-trend) patterns, capturing the nuances of complex market structures.
Visibility Scaling: The intensity of the clouds dynamically adjusts based on the current price's position within its recent range, highlighting setups that have both time and price confluence.

Optimized Performance: Features a high-speed caching logic that limits heavy ML calculations to pivot confirmation events, ensuring a lag-free experience even on high-frequency charts.

▶️Settings Explained

Pivot Settings (Left/Right): Determines the "strength" of the pivots used for training. Higher values focus on major macro cycles; lower values focus on micro noise.
Number of Clusters (K): How many different "Cycle Identities" the machine should find. Usually, 2 or 3 is optimal for capturing both short and medium terms.
Distribution Width (Sigma): Controls the "Focus." A lower Sigma makes the peaks very sharp (precise timing), while a higher Sigma provides a broader, safer window.Memory Window: The depth of history used to train the K-Means engine.

Disclaimer

Cycle analysis is a study of mathematical probability. While history provides a map, external fundamental shocks ("Black Swans") can break any cycle. Always utilize rigorous risk management. If you find this ML-based approach valuable, please support the script with a like!

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.