OPEN-SOURCE SCRIPT
HMA Crossover + ATR + Curvature (Long & Short)

📏 Hull Moving Averages (Trend Filters)
- fastHMA = ta.hma(close, fastLength)
- slowHMA = ta.hma(close, slowLength)
These two HMAs act as dynamic trend indicators:
- A bullish crossover of fast over slow HMA signals a potential long setup.
- A bearish crossunder triggers short interest.
⚡️ Curvature (Acceleration Filter)
- curv = ta.change(ta.change(fastHMA))
This calculates the second-order change (akin to the second derivative) of the fast HMA — effectively the acceleration of the trend. It serves as a filter:
- For long entries: curv > curvThresh (positive acceleration)
- For short entries: curv < -curvThresh (negative acceleration)
It helps eliminate weak or stagnating moves by requiring momentum behind the crossover.
📈 Volatility-Based Risk Management (ATR)
- atr = ta.atr(atrLength)
- stopLoss = atr * atrMult
- trailStop = atr * trailMult
These define your:
- Initial stop loss: scaled to recent volatility using ATR and atrMult.
- Trailing stop: also ATR-scaled, to lock in gains dynamically as price moves favorably.
💰 Position Sizing via Risk Percent
- capital = strategy.equity
- riskCapital = capital * (riskPercent / 100)
- qty = riskCapital / stopLoss
This dynamically calculates the position size (qty) such that if the stop loss is hit, the loss does not exceed the predefined percentage of account equity. It’s a volatility-adjusted position sizing method, keeping your risk consistent regardless of market conditions.
📌 Execution Logic
- Long Entry: on bullish HMA crossover with rising curvature.
- Short Entry: on bearish crossover with falling curvature.
- Exits: use ATR-based trailing stops.
- Position is closed when trend conditions reverse (e.g., bearish crossover exits the long).
This framework gives you:
- Trend-following logic (via HMAs)
- Momentum confirmation (via curvature)
- Volatility-aware execution and exits (via ATR)
- Risk-controlled dynamic sizing
Want to get surgical and test what happens if we use curvature on the difference between HMAs instead? That might give some cool insights into trend strength transitions.
- fastHMA = ta.hma(close, fastLength)
- slowHMA = ta.hma(close, slowLength)
These two HMAs act as dynamic trend indicators:
- A bullish crossover of fast over slow HMA signals a potential long setup.
- A bearish crossunder triggers short interest.
⚡️ Curvature (Acceleration Filter)
- curv = ta.change(ta.change(fastHMA))
This calculates the second-order change (akin to the second derivative) of the fast HMA — effectively the acceleration of the trend. It serves as a filter:
- For long entries: curv > curvThresh (positive acceleration)
- For short entries: curv < -curvThresh (negative acceleration)
It helps eliminate weak or stagnating moves by requiring momentum behind the crossover.
📈 Volatility-Based Risk Management (ATR)
- atr = ta.atr(atrLength)
- stopLoss = atr * atrMult
- trailStop = atr * trailMult
These define your:
- Initial stop loss: scaled to recent volatility using ATR and atrMult.
- Trailing stop: also ATR-scaled, to lock in gains dynamically as price moves favorably.
💰 Position Sizing via Risk Percent
- capital = strategy.equity
- riskCapital = capital * (riskPercent / 100)
- qty = riskCapital / stopLoss
This dynamically calculates the position size (qty) such that if the stop loss is hit, the loss does not exceed the predefined percentage of account equity. It’s a volatility-adjusted position sizing method, keeping your risk consistent regardless of market conditions.
📌 Execution Logic
- Long Entry: on bullish HMA crossover with rising curvature.
- Short Entry: on bearish crossover with falling curvature.
- Exits: use ATR-based trailing stops.
- Position is closed when trend conditions reverse (e.g., bearish crossover exits the long).
This framework gives you:
- Trend-following logic (via HMAs)
- Momentum confirmation (via curvature)
- Volatility-aware execution and exits (via ATR)
- Risk-controlled dynamic sizing
Want to get surgical and test what happens if we use curvature on the difference between HMAs instead? That might give some cool insights into trend strength transitions.
開源腳本
本著TradingView的真正精神,此腳本的創建者將其開源,以便交易者可以查看和驗證其功能。向作者致敬!雖然您可以免費使用它,但請記住,重新發佈程式碼必須遵守我們的網站規則。
免責聲明
這些資訊和出版物並不意味著也不構成TradingView提供或認可的金融、投資、交易或其他類型的意見或建議。請在使用條款閱讀更多資訊。
開源腳本
本著TradingView的真正精神,此腳本的創建者將其開源,以便交易者可以查看和驗證其功能。向作者致敬!雖然您可以免費使用它,但請記住,重新發佈程式碼必須遵守我們的網站規則。
免責聲明
這些資訊和出版物並不意味著也不構成TradingView提供或認可的金融、投資、交易或其他類型的意見或建議。請在使用條款閱讀更多資訊。