Neighboring Trailing Stop [LuxAlgo]The Neighboring Trailing Stop indicator provides a dynamic, data-driven trailing stop-loss mechanism by analyzing the local distribution of prices relative to the current market position. It uses a "k-nearest neighbors" approach to identify support and resistance levels based on historical price clusters, offering a unique alternative to standard volatility-based stops like ATR.
🔶 USAGE
The indicator helps traders identify trend reversals and manage open positions by providing a protective stop that adjusts based on where price has historically "congested" near the current level.
🔹 Trend Interpretation
Bullish Trend: When the stop-loss line is below the price (colored green), the market is in a bullish state. The trailing stop will only move upward, securing profits as the price creates new local distribution highs.
Bearish Trend: When the stop-loss line is above the price (colored red), the market is in a bearish state. The trailing stop will only move downward, following the price as it explores lower distribution zones.
🔹 Signals
The script plots "B" (Buy) and "S" (Sell) labels at the point of trend reversals. These occurs when the price breaks through the current "neighboring" distribution boundary, suggesting a shift in the local market structure.
🔶 DETAILS
The methodology behind this indicator is rooted in non-parametric statistics and price distribution analysis rather than simple moving averages or volatility.
🔹 Price Distribution & K-Neighbors
The script maintains a "Historical Buffer" of recent prices in a sorted array. For every new bar, it identifies the position of the current price within this sorted distribution. It then looks at k neighbors above the price and k neighbors below the price.
🔹 Percentile Bands
Within these local "neighborhoods" of price data, the script calculates a specific percentile. This allows the indicator to ignore outliers and focus on where the bulk of historical price action occurred near the current level. If the price moves into a "discovery" phase where it exceeds the range of its historical neighbors, a trend reversal is triggered.
🔹 Trailing Logic
The resulting levels are smoothed via an SMA to prevent erratic jumps. The trailing stop is "ratcheted"—meaning it can only move in the direction of the trend (up for long, down for short)—ensuring that realized gains are protected even if the distribution neighborhood expands.
🔶 SETTINGS
Historical Buffer (Bars): Determines how many historical price points are kept in the distribution memory. A larger buffer considers more history, leading to more stable but slower-reacting levels.
Neighboring Range (K): The number of price points to analyze immediately above and below the current price. Smaller values make the stop more sensitive to local price changes.
Percentile: Controls the depth within the neighborhood used for the stop level. A higher percentile (e.g., 90) places the stop further away from the current price.
Smoothing: Applies a Simple Moving Average (SMA) to the raw distribution levels to create a smoother trailing line.
Indikator Pine Script®






















