This GPL-3.0 adaptation preserves the reference state and line calculation, adds valid input bounds, and gates its markers and alert conditions at bar close. It omits decorative fills and labels. The line and bands still update while the candle is open. Compare them with the pinned reference on completed candles before relying on the adaptation.
Adapted from everget under GPL-3.0. Close-confirmed signals are indicator conditions, not trade orders. Syntax acceptance does not establish numerical parity or trading performance.pine
//@version=6
// Copyright (c) 2021-present, Alex Orekhov (everget)
// HalfTrend adaptation: GPL-3.0. https://www.gnu.org/licenses/gpl-3.0.html
// Source: https://github.com/everget/tradingview-pinescript-indicators
// Changes: bounded inputs, simpler display, close-confirmed markers and alerts.
indicator("HalfTrend: confirmed signals", overlay = true)
int amplitude = input.int(2, "Amplitude", minval = 1)
int channelDeviation = input.int(2, "Channel Deviation", minval = 0)
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 arrowUp = na
float arrowDown = na
float atr2 = ta.atr(100) / 2
float dev = channelDeviation * atr2
float highPrice = high[math.abs(ta.highestbars(amplitude))]
float lowPrice = low[math.abs(ta.lowestbars(amplitude))]
float highma = ta.sma(high, amplitude)
float 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])
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])
float ht = trend == 0 ? up : down
float atrHigh = ht + dev
float atrLow = ht - dev
bool buyConfirmed = barstate.isconfirmed and not na(arrowUp) and trend == 0 and trend[1] == 1
bool sellConfirmed = barstate.isconfirmed and not na(arrowDown) and trend == 1 and trend[1] == 0
plot(ht, "HalfTrend", trend == 0 ? color.blue : color.red, 2)
plot(atrHigh, "ATR High", color.red)
plot(atrLow, "ATR Low", color.blue)
plotshape(buyConfirmed ? atrLow : na, "Confirmed buy", shape.triangleup, location.absolute, color.blue, size = size.tiny)
plotshape(sellConfirmed ? atrHigh : na, "Confirmed sell", shape.triangledown, location.absolute, color.red, size = size.tiny)
alertcondition(buyConfirmed, "HalfTrend confirmed buy", "HalfTrend up flip at close: {{exchange}}:{{ticker}}")
alertcondition(sellConfirmed, "HalfTrend confirmed sell", "HalfTrend down flip at close: {{exchange}}:{{ticker}}")