HA TrendBeast Histogram Oscillator

ChartTrader-X · study · 63 行 · 点赞 13 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ChartTrader-X
 
//@version=6
indicator(title="Heikin Ashi TrendBeast", shorttitle="HA TrendBeast Histogram Oscillator", timeframe="", timeframe_gaps=true)
 
baseLength    = input.int(10, title="MA Length (Stage 1)")
refineLength  = input.int(10, title="MA Length (Stage 2)")
smoothMode    = input.string("EMA", title="MA Type", options=["RMA", "EMA", "SMA", "WMA", "HMA"])
trigLength    = input.int(5, title="Signal Line Length")
 
// ---- MA dispatcher, clamped to >= 0 ----
calcSmooth(src, length, mode) =>
    val = switch mode
        "RMA" => ta.rma(src, length)
        "EMA" => ta.ema(src, length)
        "SMA" => ta.sma(src, length)
        "WMA" => ta.wma(src, length)
        "HMA" => ta.hma(src, length)
        => ta.ema(src, length)
    math.max(val, 0.0)
 
// ---- Stage 1: smooth raw price ----
basePriceOpen  = calcSmooth(open,  baseLength, smoothMode)
basePriceClose = calcSmooth(close, baseLength, smoothMode)
 
synthClose = (basePriceOpen + basePriceClose) / 2
var float synthOpen = na
synthOpen := na(synthOpen[1]) ? (basePriceOpen + basePriceClose) / 2 : (synthOpen[1] + synthClose[1]) / 2
 
// ---- Stage 2: refine the synthetic HA values ----
refinedOpen  = calcSmooth(synthOpen,  refineLength, smoothMode)
refinedClose = calcSmooth(synthClose, refineLength, smoothMode)
 
// ---- Core histogram value ----
momentumBar = refinedClose - refinedOpen
 
// ---- Signal line: smoothed version of momentumBar, for crossover confirmation ----
trigLine = ta.sma(momentumBar, trigLength)
 
// ---- 4-state momentum classification ----
bullState       = momentumBar > 0
strongBullState = bullState and momentumBar > trigLine
weakBullState   = bullState and momentumBar <= trigLine
bearState       = momentumBar <= 0
strongBearState = bearState and momentumBar < trigLine
weakBearState   = bearState and momentumBar >= trigLine
 
barColor = strongBullState ? #009688 : weakBullState ? #80CBC4 : strongBearState ? #F44336 : #FFAB91
 
plot(momentumBar, "Smoothed HA Momentum", color=barColor, style=plot.style_columns)
plot(trigLine, "Signal", color=color.new(color.gray, 40), linewidth=1)
 
// ---- Alerts on state transitions ----
bullFading   = ta.crossunder(momentumBar, trigLine) and momentumBar > 0
bearFading   = ta.crossover(momentumBar, trigLine) and momentumBar < 0
flipToBull   = ta.crossover(momentumBar, 0)
flipToBear   = ta.crossunder(momentumBar, 0)
 
alertcondition(flipToBull, title="Trend flipped bullish", message="Smoothed HA Momentum crossed above zero")
alertcondition(flipToBear, title="Trend flipped bearish", message="Smoothed HA Momentum crossed below zero")
alertcondition(bullFading, title="Bullish momentum weakening", message="Bullish trend losing momentum vs signal line")
alertcondition(bearFading, title="Bearish momentum weakening", message="Bearish trend losing momentum vs signal line")

← 返回列表