本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// Copyright (c) 2021-present, Alex Orekhov (everget)
// Faster HalfTrend Script to reduce signal lag.
indicator('sam 2', overlay = true)
const string calcGroup = 'Calculation Settings'
amplitude = input.int(2, title = 'Amplitude (Lower = Faster Signal)', minval = 1, group = calcGroup)
channelDeviation = input.int(1, title = 'Channel Deviation (Lower = Faster Signal)', minval = 1, group = calcGroup)
const string filterGroup = 'Trend Filter Settings'
useEmaFilter = input.bool(true, title = 'Enable Fast Trend Filter?', group = filterGroup)
emaPeriod = input.int(50, title = 'EMA Period (50 = Fast Filter)', group = filterGroup)
showEma = input.bool(true, title = 'Show EMA Line', group = filterGroup)
const string visualGroup = 'Visuals'
showArrows = input.bool(true, title = 'Show Arrows', group = visualGroup)
showChannels = input.bool(true, title = 'Show Channels', group = visualGroup)
showLabels = input.bool(true, title = 'Show Buy/Sell Labels', group = visualGroup)
//--- EMA Filter Calculation (50 EMA for faster response)
emaFilter = ta.ema(close, emaPeriod)
plot(showEma ? emaFilter : na, title = '50 EMA Filter', color = color.orange, linewidth = 2)
//--- HalfTrend Calculation
var int trend = 0
var int nextTrend = 0
var float maxLowPrice = nz(low[1], low)
var float minHighPrice = nz(high[1], high)
var float up = 0.0
var float down = 0.0
float atrHigh = 0.0
float atrLow = 0.0
float arrowUp = na
float arrowDown = na
atr2 = ta.atr(100) / 2
dev = channelDeviation * atr2
highPrice = high[math.abs(ta.highestbars(amplitude))]
lowPrice = low[math.abs(ta.lowestbars(amplitude))]
highma = ta.sma(high, amplitude)
lowma = ta.sma(low, amplitude)
if nextTrend == 1
maxLowPrice := math.max(lowPrice, maxLowPrice)
if highma < maxLowPrice and close < nz(low[1], low)
trend := 1
nextTrend := 0
minHighPrice := highPrice
else
minHighPrice := math.min(highPrice, minHighPrice)
if lowma > minHighPrice and close > nz(high[1], high)
trend := 0
nextTrend := 1
maxLowPrice := lowPrice
if trend == 0
if not na(trend[1]) and trend[1] != 0
up := na(down[1]) ? down : down[1]
arrowUp := up - atr2
else
up := na(up[1]) ? maxLowPrice : math.max(maxLowPrice, up[1])
atrHigh := up + dev
atrLow := up - dev
else
if not na(trend[1]) and trend[1] != 1
down := na(up[1]) ? up : up[1]
arrowDown := down + atr2
else
down := na(down[1]) ? minHighPrice : math.min(minHighPrice, down[1])
atrHigh := down + dev
atrLow := down - dev
ht = trend == 0 ? up : down
const color buyColor = color.blue
const color sellColor = color.red
const color textColor = color.white
const color buyFillColor = color.new(buyColor, 85)
const color sellFillColor = color.new(sellColor, 85)
htColor = trend == 0 ? buyColor : sellColor
htPlot = plot(ht, title = 'HalfTrend', linewidth = 2, color = htColor)
atrHighPlot = plot(showChannels ? atrHigh : na, title = 'ATR High', style = plot.style_circles, color = sellColor)
atrLowPlot = plot(showChannels ? atrLow : na, title = 'ATR Low', style = plot.style_circles, color = buyColor)
fill(htPlot, atrHighPlot, title = 'ATR High Ribbon', color = sellFillColor)
fill(htPlot, atrLowPlot, title = 'ATR Low Ribbon', color = buyFillColor)
// --- Signal Calculation ---
rawBuySignal = not na(arrowUp) and trend == 0 and trend[1] == 1
rawSellSignal = not na(arrowDown) and trend == 1 and trend[1] == 0
// Apply Fast 50 EMA Filter
buySignal = useEmaFilter ? (rawBuySignal and close > emaFilter) : rawBuySignal
sellSignal = useEmaFilter ? (rawSellSignal and close < emaFilter) : rawSellSignal
plotshape(showArrows and buySignal ? atrLow : na, title = 'Arrow Up', style = shape.triangleup, location = location.absolute, size = size.tiny, color = buyColor)
plotshape(showArrows and sellSignal ? atrHigh : na, title = 'Arrow Down', style = shape.triangledown, location = location.absolute, size = size.tiny, color = sellColor)
plotshape(showLabels and buySignal ? atrLow : na, title = 'Buy Label', text = 'Buy', location = location.absolute, style = shape.labelup, size = size.tiny, color = buyColor, textcolor = textColor)
plotshape(showLabels and sellSignal ? atrHigh : na, title = 'Sell Label', text = 'Sell', location = location.absolute, style = shape.labeldown, size = size.tiny, color = sellColor, textcolor = textColor)
alertcondition(buySignal, title = 'Fast HalfTrend Buy', message = 'HalfTrend Buy, {{exchange}}:{{ticker}}')
alertcondition(sellSignal, title = 'Fast HalfTrend Sell', message = 'HalfTrend Sell, {{exchange}}:{{ticker}}')