HalfTrend Strategy
gunebakan
plain_text
9 months ago
4.5 kB
28
Indexable
//@version=6
// Copyright (c) 2021-present, Alex Orekhov (everget)
// HalfTrend Strategy
// Converted from indicator to strategy with entry/exit and alert conditions
// May be freely distributed under the terms of the GPL-3.0 license.
strategy("HalfTrend Strategy", overlay = true, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, pyramiding = 1)
// === INPUTS ===
const string calcGroup = 'Calculation'
amplitude = input.int(2, title = 'Amplitude', group = calcGroup)
channelDeviation = input.int(2, title = 'Channel Deviation', group = calcGroup)
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)
// === VARIABLES ===
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)
// === TREND LOGIC ===
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
// === HT & CHANNELS ===
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
// === PLOTTING ===
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)
// === SIGNALS ===
buySignal = not na(arrowUp) and trend == 0 and trend[1] == 1
sellSignal = not na(arrowDown) and trend == 1 and trend[1] == 0
// === PLOT SHAPES ===
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)
// === STRATEGY EXECUTION ===
if buySignal
strategy.entry("Long", strategy.long)
if sellSignal
strategy.close("Long")
// === ALERT CONDITIONS ===
// Bu koşullar, TradingView'de "Add Alert" ile kullanılır.
alertcondition(buySignal, title = "HalfTrend Buy Signal", message = "🟢 HalfTrend BUY Signal on {{ticker}} - Price: {{close}}")
alertcondition(sellSignal, title = "HalfTrend Sell Signal", message = "🔴 HalfTrend SELL Signal on {{ticker}} - Price: {{close}}")Editor is loading...
Leave a Comment