Machine Learning: SuperTrend Strategy TP/SL
Oct 16, 2023

The Machine Learning: SuperTrend Strategy TP/SL indicator enhances the traditional SuperTrend calculation by integrating k-Nearest Neighbor (KNN) logic and dynamic risk management features to better filter market noise and capitalize on trend momentum. It utilizes historical trend data and distance-based filtering to refine trend transitions while providing built-in take-profit and stop-loss mechanisms.
Usage
The Usage section describes how the script can be used to identify trend shifts and manage trade exits. The indicator applies machine learning to the standard Average True Range (ATR) based SuperTrend to minimize false signals during consolidation.
- Trend Identification: When the price crosses the ML-refined SuperTrend line, the direction shifts. A green line indicates a bullish trend, while a red line indicates a bearish trend.
- Machine Learning Filtering: The KNN logic sorts previous SuperTrend calculations. By filtering for data that follows the same direction and falls within specific distance bounds, the tool produces a more responsive "jagged" pattern during parabolic moves, aiming to stay within the trend longer than traditional versions.
- Take Profit (TP) & Stop Loss (SL):
- The Take Profit moves in accordance with the SuperTrend using a multiplier. It is designed to capture gains during strong momentum bursts before price reverts to the baseline.
- The Stop Loss helps prevent "signal flashing" (rapidly switching buy/sell signals) during sideways markets. Increasing the SL multiplier can help maintain a position through minor volatility, though it may affect the overall win/loss ratio.
Details
The strategy relies on a k-Nearest Neighbor (KNN) algorithm to average historical SuperTrend values. It calculates a "Fast" and "Slow" ML data stream to determine the distance bounds for the KNN search.
- Data Normalization: The script includes options for "Rationalized" source types (Close, HL2, ATR), which use a kernel-based smoothing method to reduce noise before the SuperTrend is calculated.
- KNN Logic: The algorithm identifies the $k$ most similar historical data points based on the distance between fast and slow averages. Only values within the detected Min/Max distance are averaged (using either a Mean or Exponential calculation).
- Momentum Optimization: By using a Trailing Take Profit, the strategy attempts to exit positions when price deviates significantly from the mean, protecting capital against sharp reversals that often follow parabolic moves.
Settings
SuperTrend
- ATR Length: The lookback period for the Average True Range used in the base calculation.
- Factor: The multiplier applied to the ATR to set the distance of the SuperTrend from price.
- Stop Loss Multiplier: Sets the distance for the protective stop. Set to 0 to disable.
- Take Profit Multiplier: Determines the distance for the trailing take profit target.
- Allow Same Bar Entry and Exit: Enables or disables the execution of a trade entry and exit within a single price bar.
Machine Learning
- Only Factor Same Trend Direction: If enabled, the KNN algorithm only considers historical data points that match the current trend direction (bullish or bearish).
- Rationalized Source Type: Selects which price components (HL2, Close, ATR) are smoothed using rational quadratic kernels.
- Machine Learning Type: Chooses between KNN Average, KNN Exponential Average, Simple Average, or None.
- Machine Learning Smoothing Type: Sets the moving average type (SMA, EMA, or VWMA) for the Fast and Slow ML data streams.
- KNN Distance Type: Specifies whether the algorithm filters by Minimum distance, Maximum distance, or Both.
- Machine Learning Length: The total historical lookback window for the ML data array.
- k-Nearest Neighbour (KNN) Length: The number of neighboring data points to include in the average.
- Fast/Slow ML Data Length: The periods used to create the distance metrics for the KNN algorithm.
FAQ
How does the Machine Learning component improve the SuperTrend? It filters out historical anomalies by only averaging previous trend data that share similar distance characteristics, making the indicator more adaptive to current volatility.
Why does the SuperTrend line look "jagged" sometimes? This is a result of the KNN logic reacting to parabolic price movements. When fewer historical points meet the strict distance criteria, the average moves more drastically, which is a normal function of the k-Nearest Neighbor calculation.
How can I access this tool? You can get access on the LuxAlgo Library for charting platforms like TradingView, MetaTrader (MT4/MT5), and NinjaTrader for free.
Free indicator
Get free access to this indicator on the platforms below.
Unlock the entire LuxAlgo Library
Every indicator, every strategy, full charts, and complete access to Quant — our AI agent.