PSAR Structural Gauge [KNN Engine]PSAR Structural Gauge - On-Chart Visualizer & HUD
This indicator is an advanced, machine-learning-assisted structural mapping tool. It moves beyond traditional momentum oscillators by evaluating the "elasticity" of price relative to a Parabolic SAR, utilizing dynamically anchored volatility arrays and a 6-Dimensional K-Nearest Neighbors (KNN) classification algorithm.
Operating as an on-chart interface and risk management suite, it overlays structural geometries, Maximum Favorable Excursion (MFE) tracking boxes, and simulated Kelly Criterion position sizing directly onto your price chart.
The Core Engine: Dynamic Structural Tether (Elasticity)
Unlike standard oscillators that measure raw price change over fixed periods, this engine measures the "Tension" or "Tether" between price and the underlying trend structure (represented by a global Parabolic SAR).
Anomaly Detection (The Anchor): The script continuously tracks volume using a rolling Z-score. When a significant institutional liquidity event occurs (volume spikes above your defined Z-score threshold), the script drops an "Anchor."
Dynamic 24x Array: Upon an anchor trigger, the script initializes a new tracking period. It measures the raw distance between the current price and the Parabolic SAR, and divides that distance by the accumulated True Range (volatility) that has occurred since the anchor was dropped. It tracks up to 24 of these anchored "elasticity" metrics simultaneously.
Z-Score Standardization (Meta-Mean): To distill 24 different elasticity profiles into a single readable metric, the script calculates their cross-sectional average (the "Meta-Mean"). It then calculates the Z-Score of this structural tether, providing a standardized measure of whether the current trend is hyper-extended (high tension) or at equilibrium.
The Machine Learning Module (KNN)
The engine evaluates market conditions by feeding a 6-Dimensional sensory matrix into a K-Nearest Neighbors (KNN) classification algorithm. The six features evaluated are:
Structural Tether (Z-Score): The current standardized elasticity of the market.
Tether Velocity: The bar-to-bar momentum of the elasticity.
Price Velocity: Z-Scored rate of change of price from a baseline 50-SMA.
Cluster Consensus: Percentage of the 24 active anchors aligned in the same directional state.
Historical Rate of Change: An 8-bar lookback of tether velocity.
Tension Compression: Standard deviation of the 24 anchors to identify tightly coiled structural energy.
The algorithm maps this 6D state and calculates the Euclidean distance to a memory bank of the last 500 historical setups. It isolates the K most mathematically identical past environments and evaluates their success rates, returning a real-time Probability Score.
On-Chart Visuals & MFE/LFE Box Engine
Rather than cluttering your chart with disjointed arrows, this visualizer groups continuous, same-direction KNN signals into clean structural "Streaks."
Independent Line Mitigation: The AI projects forward-looking target lines (Scalp target and Stop Loss floor). These lines actively track price tick-by-tick. The exact moment price intersects a target, that specific line halts, solidifies, and stops extending, leaving a permanent structural footprint.
Profit Box (Blue): Stretches vertically from your initial entry up to the projected target. It dynamically expands to the right, snapping exactly to the bar that achieved the Maximum Favorable Excursion (MFE).
Risk Box (Red): Drops from the entry to the absolute minimum anchored stop loss, highlighting the exact Lowest Favorable Excursion (LFE) (Maximum Adverse Excursion) of the active structural streak.
Streak Termination: A structural streak only ends when the absolute minimum Stop Loss is breached or an opposing KNN signal flips the mathematical momentum.
The Agent Diagnostic Dashboard (HUD)
The built-in dashboard provides real-time diagnostic transparency into the engine's internal state:
Market Context: Live metrics for Structural Tether (Z), Tether Vector, Tension Compression, and Tether Consensus.
AI Probability: The live win-rate probability generated by the companion 6D KNN engine.
Live Portfolio Tracking: Simulates a compounding equity curve. It tallies the Hit Rate of the KNN algorithm and computes a simulated dollar PnL based on your starting capital.
Dynamic Bet Sizing (Kelly Criterion): Utilizing the KNN probability and your defined Risk/Reward parameters, the dashboard simulates optimal position sizing via a dampened Kelly Criterion formula. It actively scales simulated risk up during high-probability setups and scales down when the edge decreases, capping at a strict maximum to prevent ruin.
Disclaimer: The portfolio tracker and Kelly sizing metrics are simulated diagnostic tools meant for the educational evaluation of the KNN algorithm's historical efficacy. This tool provides statistical classification of historical data, which does not constitute financial advice, predictive guarantees, or real-world trading results.
Pine Script® Indikator






















