1
- //@version=4
1
+ //@version=6
2
2
// Copyright (c) 2021-present, Alex Orekhov (everget)
3
3
// 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)
5
5
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
+ //---
10
16
11
17
var int trend = 0
12
18
var int nextTrend = 0
@@ -20,65 +26,79 @@ float atrLow = 0.0
20
26
float arrowUp = na
21
27
float arrowDown = na
22
28
23
- atr2 = atr(100) / 2
29
+ atr2 = ta. atr(100) / 2
24
30
dev = channelDeviation * atr2
25
31
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)
30
36
31
37
if nextTrend == 1
32
- maxLowPrice := max(lowPrice, maxLowPrice)
38
+ maxLowPrice := math. max(lowPrice, maxLowPrice)
33
39
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
38
45
else
39
- minHighPrice := min(highPrice, minHighPrice)
46
+ minHighPrice := math. min(highPrice, minHighPrice)
40
47
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
45
53
46
54
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
54
65
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
62
76
63
77
ht = trend == 0 ? up : down
64
78
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)
67
84
68
85
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)
70
90
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 )
73
93
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
76
96
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 )
79
99
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 )
82
102
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