OPEN-SOURCE SCRIPT
ML Adaptive SuperTrend Strategy [trade_crush]

# ML Adaptive SuperTrend Strategy - User Guide
## Introduction
The **ML Adaptive SuperTrend Strategy** is a sophisticated trading tool that combines traditional trend-following logic with **Machine Learning (K-Means Clustering)** to dynamically adapt to market volatility. Unlike standard SuperTrend indicators that use a fixed ATR, this strategy analyzes historical volatility to categorize the current market into distinct clusters, providing more precise entries and exits.
> [!IMPORTANT]
> **Special Thanks:** This strategy is based on the innovative work of **AlgoAlpha**. You can explore their extensive library of high-quality indicators and strategies on TradingView: [AlgoAlpha's Profile](tradingview.com/u/AlgoAlpha/).
---
## Machine Learning Engine (K-Means)
The core of this strategy is its ability to "learn" from recent market behavior.
- **K-Means Clustering**: The script takes the last $N$ bars of ATR data and runs an iterative clustering algorithm to find three "centroids" representing **High**, **Medium**, and **Low** volatility.
- **Adaptive ATR**: Based on the current volatility, the strategy selects the nearest centroid to use as the ATR value for the SuperTrend calculation. This ensures the trailing stop tightens during low volatility and widens during high volatility to avoid "noise".
---
## Key Features
### 1. Non-Repainting Signals
- **Confirm Signals**: When enabled, signals are only triggered after a bar closes. This ensures that the arrows and entries you see on the chart are permanent and reliable for backtesting.
### 2. Intelligent Risk Management
- **Multiple SL/TP Types**: Choose between **Percentage** based stops or **ATR** based stops for both Stop Loss and Take Profit.
- **Trailing Stop Loss (TSL)**:
- Supports both Percentage and ATR modes.
- **Activation Offset**: Only activates the trailing mechanism after the price has moved a certain percentage in your favor, protecting early-stage trades.
### 3. Risk-Based Position Sizing
- **Dynamic Quantity**: If enabled, the strategy automatically calculates the trade size based on your **Risk % Per Trade** and the distance to your **Stop Loss**. This ensures you never lose more than your defined risk on a single trade.
---
## User Input Guide
### SuperTrend & ML Settings
- **ATR Length**: The window used to calculate market volatility.
- **SuperTrend Factor**: The multiplier that determines the distance of the trailing stop from the price.
- **Use ML Adaptive ATR**: Toggle between the ML-enhanced logic and standard ATR.
- **Training Data Length**: How many historical bars the ML engine analyzes to find clusters.
### Risk Management
- **Stop Loss Type**: Set to Percentage, ATR, or None.
- **TS Activation Offset**: The profit buffer required before the trailing stop starts following the price.
- **Use Risk-Based Sizing**: Toggle this to let the script manage your position size automatically.
---
## How to Trade with This Strategy
1. **Monitor the Dashboard**: Check the top-right table to see which volatility cluster the market is currently in.
2. **Observe the Fills**: The adaptive fills (green/red) visualize the "breathing room" the strategy is giving the price.
3. **Execution**: The strategy enters on "ML Bullish" (Triangle Up) and "ML Bearish" (Triangle Down) signals.
4. **Exits**: The script will automatically exit based on your SL, TP, or Trailing Stop settings.
---
## Credits
Original Concept: **AlgoAlpha**
Strategy Conversion & Enhancements: **Antigravity AI**
## Introduction
The **ML Adaptive SuperTrend Strategy** is a sophisticated trading tool that combines traditional trend-following logic with **Machine Learning (K-Means Clustering)** to dynamically adapt to market volatility. Unlike standard SuperTrend indicators that use a fixed ATR, this strategy analyzes historical volatility to categorize the current market into distinct clusters, providing more precise entries and exits.
> [!IMPORTANT]
> **Special Thanks:** This strategy is based on the innovative work of **AlgoAlpha**. You can explore their extensive library of high-quality indicators and strategies on TradingView: [AlgoAlpha's Profile](tradingview.com/u/AlgoAlpha/).
---
## Machine Learning Engine (K-Means)
The core of this strategy is its ability to "learn" from recent market behavior.
- **K-Means Clustering**: The script takes the last $N$ bars of ATR data and runs an iterative clustering algorithm to find three "centroids" representing **High**, **Medium**, and **Low** volatility.
- **Adaptive ATR**: Based on the current volatility, the strategy selects the nearest centroid to use as the ATR value for the SuperTrend calculation. This ensures the trailing stop tightens during low volatility and widens during high volatility to avoid "noise".
---
## Key Features
### 1. Non-Repainting Signals
- **Confirm Signals**: When enabled, signals are only triggered after a bar closes. This ensures that the arrows and entries you see on the chart are permanent and reliable for backtesting.
### 2. Intelligent Risk Management
- **Multiple SL/TP Types**: Choose between **Percentage** based stops or **ATR** based stops for both Stop Loss and Take Profit.
- **Trailing Stop Loss (TSL)**:
- Supports both Percentage and ATR modes.
- **Activation Offset**: Only activates the trailing mechanism after the price has moved a certain percentage in your favor, protecting early-stage trades.
### 3. Risk-Based Position Sizing
- **Dynamic Quantity**: If enabled, the strategy automatically calculates the trade size based on your **Risk % Per Trade** and the distance to your **Stop Loss**. This ensures you never lose more than your defined risk on a single trade.
---
## User Input Guide
### SuperTrend & ML Settings
- **ATR Length**: The window used to calculate market volatility.
- **SuperTrend Factor**: The multiplier that determines the distance of the trailing stop from the price.
- **Use ML Adaptive ATR**: Toggle between the ML-enhanced logic and standard ATR.
- **Training Data Length**: How many historical bars the ML engine analyzes to find clusters.
### Risk Management
- **Stop Loss Type**: Set to Percentage, ATR, or None.
- **TS Activation Offset**: The profit buffer required before the trailing stop starts following the price.
- **Use Risk-Based Sizing**: Toggle this to let the script manage your position size automatically.
---
## How to Trade with This Strategy
1. **Monitor the Dashboard**: Check the top-right table to see which volatility cluster the market is currently in.
2. **Observe the Fills**: The adaptive fills (green/red) visualize the "breathing room" the strategy is giving the price.
3. **Execution**: The strategy enters on "ML Bullish" (Triangle Up) and "ML Bearish" (Triangle Down) signals.
4. **Exits**: The script will automatically exit based on your SL, TP, or Trailing Stop settings.
---
## Credits
Original Concept: **AlgoAlpha**
Strategy Conversion & Enhancements: **Antigravity AI**
نص برمجي مفتوح المصدر
بروح TradingView الحقيقية، قام مبتكر هذا النص البرمجي بجعله مفتوح المصدر، بحيث يمكن للمتداولين مراجعة وظائفه والتحقق منها. شكرا للمؤلف! بينما يمكنك استخدامه مجانًا، تذكر أن إعادة نشر الكود يخضع لقواعد الموقع الخاصة بنا.
إخلاء المسؤولية
لا يُقصد بالمعلومات والمنشورات أن تكون، أو تشكل، أي نصيحة مالية أو استثمارية أو تجارية أو أنواع أخرى من النصائح أو التوصيات المقدمة أو المعتمدة من TradingView. اقرأ المزيد في شروط الاستخدام.
نص برمجي مفتوح المصدر
بروح TradingView الحقيقية، قام مبتكر هذا النص البرمجي بجعله مفتوح المصدر، بحيث يمكن للمتداولين مراجعة وظائفه والتحقق منها. شكرا للمؤلف! بينما يمكنك استخدامه مجانًا، تذكر أن إعادة نشر الكود يخضع لقواعد الموقع الخاصة بنا.
إخلاء المسؤولية
لا يُقصد بالمعلومات والمنشورات أن تكون، أو تشكل، أي نصيحة مالية أو استثمارية أو تجارية أو أنواع أخرى من النصائح أو التوصيات المقدمة أو المعتمدة من TradingView. اقرأ المزيد في شروط الاستخدام.