
This strategy is a machine learning-based adaptive SuperTrend trading system that enhances the reliability of the traditional SuperTrend indicator by integrating volatility clustering, adaptive ATR trend detection, and structured entry/exit mechanisms. The core concept lies in classifying market volatility through machine learning methods, executing trend-following trades in suitable market conditions, while employing dynamic stop-loss and take-profit levels for risk control.
The strategy consists of three key components: 1) Adaptive SuperTrend calculation based on ATR for determining trend direction and turning points; 2) K-means-based volatility clustering that categorizes market states into high, medium, and low volatility environments; 3) Differentiated trading rules based on volatility environments. It seeks trending opportunities in low volatility environments while maintaining caution in high volatility conditions. The system captures trend reversal signals using ta.crossunder and ta.crossover functions, combined with price position relative to the SuperTrend line.
This strategy creates an intelligent trend-following system by combining machine learning techniques with traditional technical analysis methods. Its core advantages lie in its adaptability and risk control capabilities, achieving intelligent market state identification through volatility clustering. While risks such as parameter sensitivity exist, continuous optimization and refinement can help maintain stable performance across various market environments. Traders are advised to thoroughly test parameter sensitivity and optimize based on specific market characteristics when implementing the strategy in live trading.
/*backtest
start: 2025-01-09 00:00:00
end: 2025-01-16 00:00:00
period: 10m
basePeriod: 10m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT","balance":49999}]
*/
//@version=5
strategy("Adaptive SuperTrend Strategy", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=200)
// Import Indicator Components
atr_len = input.int(10, "ATR Length", group="SuperTrend Settings")
fact = input.float(3, "SuperTrend Factor", group="SuperTrend Settings")
training_data_period = input.int(100, "Training Data Length", group="K-Means Settings")
// Volatility Clustering
volatility = ta.atr(atr_len)
upper = ta.highest(volatility, training_data_period)
lower = ta.lowest(volatility, training_data_period)
high_volatility = lower + (upper-lower) * 0.75
medium_volatility = lower + (upper-lower) * 0.5
low_volatility = lower + (upper-lower) * 0.25
cluster = volatility >= high_volatility ? 0 : volatility >= medium_volatility ? 1 : 2
// SuperTrend Calculation
pine_supertrend(factor, atr) =>
src = hl2
upperBand = src + factor * atr
lowerBand = src - factor * atr
prevLowerBand = nz(lowerBand[1])
prevUpperBand = nz(upperBand[1])
lowerBand := lowerBand > prevLowerBand or close[1] < prevLowerBand ? lowerBand : prevLowerBand
upperBand := upperBand < prevUpperBand or close[1] > prevUpperBand ? upperBand : prevUpperBand
int _direction = na
float superTrend = na
prevSuperTrend = superTrend[1]
if na(atr[1])
_direction := 1
else if prevSuperTrend == prevUpperBand
_direction := close > upperBand ? -1 : 1
else
_direction := close < lowerBand ? 1 : -1
superTrend := _direction == -1 ? lowerBand : upperBand
[superTrend, _direction]
[ST, dir] = pine_supertrend(fact, volatility)
// Entry Conditions
longEntry = ta.crossunder(dir, 0) and cluster > 1 and close > ST
shortEntry = ta.crossover(dir, 0) and cluster == 0 and close < ST
// Stop Loss & Take Profit
atr_mult = input.float(2, "ATR Multiplier for SL/TP", group="Risk Management")
sl = atr_mult * ta.atr(atr_len)
longStopLoss = close - sl
longTakeProfit = close + (sl * 1.5)
shortStopLoss = close + sl
shortTakeProfit = close - (sl * 1.5)
// Execute Trades
if longEntry
strategy.entry("Long", strategy.long)
strategy.exit("Take Profit", from_entry="Long", limit=longTakeProfit, stop=longStopLoss)
if shortEntry
strategy.entry("Short", strategy.short)
strategy.exit("Take Profit", from_entry="Short", limit=shortTakeProfit, stop=shortStopLoss)
// Plot SuperTrend
plot(ST, title="SuperTrend", color=dir > 0 ? color.green : color.red, linewidth=2)
// Alerts
alertcondition(longEntry, title="Long Entry Signal", message="Buy Signal - Trend Shift Up")
alertcondition(shortEntry, title="Short Entry Signal", message="Sell Signal - Trend Shift Down")