Skip to content

Commit 942448d

Browse files
authored
Update halftrend.pine
1 parent 3cd38fc commit 942448d

File tree

1 file changed

+68
-48
lines changed

1 file changed

+68
-48
lines changed

‎trailing_stops/halftrend.pine

Lines changed: 68 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1-
//@version=4
1+
//@version=6
22
// Copyright (c) 2021-present, Alex Orekhov (everget)
33
// HalfTrend script may be freely distributed under the terms of the GPL-3.0 license.
4-
study("HalfTrend", overlay=true)
4+
indicator('HalfTrend', overlay = true)
55

6-
amplitude = input(title="Amplitude", defval=2)
7-
channelDeviation = input(title="Channel Deviation", defval=2)
8-
showArrows = input(title="Show Arrows", defval=true)
9-
showChannels = input(title="Show Channels", defval=true)
6+
const string calcGroup = 'Calculation'
7+
amplitude = input.int(2, title = 'Amplitude', group = calcGroup)
8+
channelDeviation = input.int(2, title = 'Channel Deviation', group = calcGroup)
9+
10+
const string visualGroup = 'Visuals'
11+
showArrows = input.bool(true, title = 'Show Arrows', group = visualGroup)
12+
showChannels = input.bool(true, title = 'Show Channels', group = visualGroup)
13+
showLabels = input.bool(true, title = 'Show Buy/Sell Labels', group = visualGroup)
14+
15+
//---
1016

1117
var int trend = 0
1218
var int nextTrend = 0
@@ -20,65 +26,79 @@ float atrLow = 0.0
2026
float arrowUp = na
2127
float arrowDown = na
2228

23-
atr2 = atr(100) / 2
29+
atr2 = ta.atr(100) / 2
2430
dev = channelDeviation * atr2
2531

26-
highPrice = high[abs(highestbars(amplitude))]
27-
lowPrice = low[abs(lowestbars(amplitude))]
28-
highma = sma(high, amplitude)
29-
lowma = sma(low, amplitude)
32+
highPrice = high[math.abs(ta.highestbars(amplitude))]
33+
lowPrice = low[math.abs(ta.lowestbars(amplitude))]
34+
highma = ta.sma(high, amplitude)
35+
lowma = ta.sma(low, amplitude)
3036

3137
if nextTrend == 1
32-
maxLowPrice := max(lowPrice, maxLowPrice)
38+
maxLowPrice := math.max(lowPrice, maxLowPrice)
3339

34-
if highma < maxLowPrice and close < nz(low[1], low)
35-
trend := 1
36-
nextTrend := 0
37-
minHighPrice := highPrice
40+
if highma < maxLowPrice and close < nz(low[1], low)
41+
trend := 1
42+
nextTrend := 0
43+
minHighPrice := highPrice
44+
minHighPrice
3845
else
39-
minHighPrice := min(highPrice, minHighPrice)
46+
minHighPrice := math.min(highPrice, minHighPrice)
4047

41-
if lowma > minHighPrice and close > nz(high[1], high)
42-
trend := 0
43-
nextTrend := 1
44-
maxLowPrice := lowPrice
48+
if lowma > minHighPrice and close > nz(high[1], high)
49+
trend := 0
50+
nextTrend := 1
51+
maxLowPrice := lowPrice
52+
maxLowPrice
4553

4654
if trend == 0
47-
if not na(trend[1]) and trend[1] != 0
48-
up := na(down[1]) ? down : down[1]
49-
arrowUp := up - atr2
50-
else
51-
up := na(up[1]) ? maxLowPrice : max(maxLowPrice, up[1])
52-
atrHigh := up + dev
53-
atrLow := up - dev
55+
if not na(trend[1]) and trend[1] != 0
56+
up := na(down[1]) ? down : down[1]
57+
arrowUp := up - atr2
58+
arrowUp
59+
else
60+
up := na(up[1]) ? maxLowPrice : math.max(maxLowPrice, up[1])
61+
up
62+
atrHigh := up + dev
63+
atrLow := up - dev
64+
atrLow
5465
else
55-
if not na(trend[1]) and trend[1] != 1
56-
down := na(up[1]) ? up : up[1]
57-
arrowDown := down + atr2
58-
else
59-
down := na(down[1]) ? minHighPrice : min(minHighPrice, down[1])
60-
atrHigh := down + dev
61-
atrLow := down - dev
66+
if not na(trend[1]) and trend[1] != 1
67+
down := na(up[1]) ? up : up[1]
68+
arrowDown := down + atr2
69+
arrowDown
70+
else
71+
down := na(down[1]) ? minHighPrice : math.min(minHighPrice, down[1])
72+
down
73+
atrHigh := down + dev
74+
atrLow := down - dev
75+
atrLow
6276

6377
ht = trend == 0 ? up : down
6478

65-
var color buyColor = color.blue
66-
var color sellColor = color.red
79+
const color buyColor = color.blue
80+
const color sellColor = color.red
81+
const color textColor = color.white
82+
const color buyFillColor = color.new(buyColor, 85)
83+
const color sellFillColor = color.new(sellColor, 85)
6784

6885
htColor = trend == 0 ? buyColor : sellColor
69-
htPlot = plot(ht, title="HalfTrend", linewidth=2, color=htColor)
86+
htPlot = plot(ht, title = 'HalfTrend', linewidth = 2, color = htColor)
87+
88+
atrHighPlot = plot(showChannels ? atrHigh : na, title = 'ATR High', style = plot.style_circles, color = sellColor)
89+
atrLowPlot = plot(showChannels ? atrLow : na, title = 'ATR Low', style = plot.style_circles, color = buyColor)
7090

71-
atrHighPlot = plot(showChannels ? atrHigh : na, title="ATR High", style=plot.style_circles, color=sellColor)
72-
atrLowPlot = plot(showChannels ? atrLow : na, title="ATR Low", style=plot.style_circles, color=buyColor)
91+
fill(htPlot, atrHighPlot, title = 'ATR High Ribbon', color = sellFillColor)
92+
fill(htPlot, atrLowPlot, title = 'ATR Low Ribbon', color = buyFillColor)
7393

74-
fill(htPlot, atrHighPlot, title="ATR High Ribbon", color=sellColor)
75-
fill(htPlot, atrLowPlot, title="ATR Low Ribbon", color=buyColor)
94+
buySignal = not na(arrowUp) and trend == 0 and trend[1] == 1
95+
sellSignal = not na(arrowDown) and trend == 1 and trend[1] == 0
7696

77-
buySignal = not na(arrowUp) and (trend == 0 and trend[1] == 1)
78-
sellSignal = not na(arrowDown) and (trend == 1 and trend[1] == 0)
97+
plotshape(showArrows and buySignal ? atrLow : na, title = 'Arrow Up', style = shape.triangleup, location = location.absolute, size = size.tiny, color = buyColor)
98+
plotshape(showArrows and sellSignal ? atrHigh : na, title = 'Arrow Down', style = shape.triangledown, location = location.absolute, size = size.tiny, color = sellColor)
7999

80-
plotshape(showArrows and buySignal ? atrLow : na, title="Arrow Up", style=shape.triangleup, location=location.absolute, size=size.tiny, color=buyColor)
81-
plotshape(showArrows and sellSignal ? atrHigh : na, title="Arrow Down", style=shape.triangledown, location=location.absolute, size=size.tiny, color=sellColor)
100+
plotshape(showLabels and buySignal ? atrLow : na, title = 'Buy Label', text = 'Buy', location = location.absolute, style = shape.labelup, size = size.tiny, color = buyColor, textcolor = textColor)
101+
plotshape(showLabels and sellSignal ? atrHigh : na, title = 'Sell Label', text = 'Sell', location = location.absolute, style = shape.labeldown, size = size.tiny, color = sellColor, textcolor = textColor)
82102

83-
alertcondition(buySignal, title="Alert: HalfTrend Buy", message="HalfTrend Buy")
84-
alertcondition(sellSignal, title="Alert: HalfTrend Sell", message="HalfTrend Sell")
103+
alertcondition(buySignal, title = 'HalfTrend Buy', message = 'HalfTrend Buy, {{exchange}}:{{ticker}}')
104+
alertcondition(sellSignal, title = 'HalfTrend Sell', message = 'HalfTrend Sell, {{exchange}}:{{ticker}}')

0 commit comments

Comments
 (0)