0bps

Strategy vault / Machine learning

Machine Learning Adaptive SuperTrend Quantitative Trading Strategy

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…

Author
ChaoZhang
Complexity
Starter · 80 lines
Parameters
0
Backtest bar
10m · intraday
Tested on
BTC/USDT · futures

IMG

Overview

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.

Strategy Principles

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.

Strategy Advantages

  1. Strong adaptability: Dynamically adjusts market volatility assessment through machine learning methods to adapt to different market environments.
  2. Comprehensive risk control: Dynamic stop-loss and take-profit mechanism based on ATR automatically adjusts risk control parameters according to market volatility.
  3. False signal filtering: Effectively filters out false signals during high volatility periods through volatility clustering.
  4. Wide application range: Strategy can be applied to multiple markets including forex, cryptocurrency, stocks, and commodities.
  5. Multi-timeframe compatibility: Works well across different timeframes from 15-minute to monthly charts.

Strategy Risks

  1. Parameter sensitivity: Selection of ATR length, SuperTrend factor, and other parameters significantly affects strategy performance.
  2. Trend reversal risk: May experience significant drawdowns during sudden trend reversals.
  3. Market environment dependency: May generate frequent trades and accumulate trading costs in ranging markets.
  4. Computational complexity: Machine learning components increase strategy computational complexity, potentially affecting real-time execution efficiency.

Strategy Optimization Directions

  1. Optimize volatility clustering algorithm: Consider using more advanced clustering methods like DBSCAN or GMM to improve market state classification accuracy.
  2. Incorporate multiple timeframe analysis: Combine longer-term trend analysis to improve trade direction accuracy.
  3. Dynamic parameter adjustment: Develop adaptive parameter adjustment mechanisms to automatically optimize ATR length and SuperTrend factor based on market performance.
  4. Add market sentiment indicators: Integrate market sentiment indicators based on volume and price momentum to improve signal quality.
  5. Enhance money management: Introduce more sophisticated position sizing algorithms to optimize capital utilization efficiency.

Summary

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.

Source Pine Script

/*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")

Published by ChaoZhang on the FMZ strategy square · last modified 2025-01-17. Collected in the Quant Trading Vault. Unvalidated research: re-test with your own data and costs before trusting it.

Build this with AgenKit

A spec is not a trading system. Paste this into your coding agent and AgenKit runs a specialist team — architecture, test-first build, risk gates, review — with paper trading on by default.

in Claude Code, Cursor, Codex & 4 more
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/Machine-Learning-Adaptive-SuperTrend-Quantitative-Trading-Strategy.md
  1. npx agenkit install engineering-kit in your project
  2. Paste the command into your agent and approve each phase
  3. Get tests, backtest, risk controls and a paper-trading switch

Understand it first

Before you run this, read the lesson behind the idea and how to backtest it honestly. Specs like this one are the survivors of an unknown number of trials; deflate their results accordingly.

Related specs

esc

Try , , or .

↑↓ move↵ open