-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtrendline-support-resitance.pine
231 lines (219 loc) · 12.1 KB
/
trendline-support-resitance.pine
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
//@version=5
indicator("Profitable Trader 2024", overlay = true, max_bars_back = 501)
//------------------------------------------------------------------------------
//Settings
//------------------------------------------------------------------------------
// Settings for Trendlines with Breaks
length = input.int(14, 'Swing Detection Lookback')
mult = input.float(1., 'Slope', minval = 0, step = .1)
calcMethod = input.string('Atr', 'Slope Calculation Method', options = ['Atr','Stdev','Linreg'])
backpaint = input(true, tooltip = 'Backpainting offset displayed elements in the past. Disable backpainting to see real time information returned by the indicator.')
upCss = input.color(color.teal, 'Up Trendline Color', group = 'Style')
dnCss = input.color(color.red, 'Down Trendline Color', group = 'Style')
showExt = input(true, 'Show Extended Lines')
// Settings for Support/Resistance Channels
prd = input.int(defval = 10, title="Pivot Period", minval = 4, maxval = 30, group = "Settings 🔨", tooltip="Used while calculating Pivot Points, checks left&right bars")
ppsrc = input.string(defval = 'High/Low', title="Source", options = ['High/Low', 'Close/Open'], group = "Settings 🔨", tooltip="Source for Pivot Points")
ChannelW = input.int(defval = 5, title = "Maximum Channel Width %", minval = 1, maxval = 8, group = "Settings 🔨", tooltip="Calculated using Highest/Lowest levels in 300 bars")
minstrength = input.int(defval = 1, title = "Minimum Strength", minval = 1, group = "Settings 🔨", tooltip = "Channel must contain at least 2 Pivot Points")
maxnumsr = input.int(defval = 6, title = "Maximum Number of S/R", minval = 1, maxval = 10, group = "Settings 🔨", tooltip = "Maximum number of Support/Resistance Channels to Show") - 1
loopback = input.int(defval = 290, title = "Loopback Period", minval = 100, maxval = 400, group = "Settings 🔨", tooltip="While calculating S/R levels it checks Pivots in Loopback Period")
res_col = input.color(defval = color.new(color.red, 75), title = "Resistance Color", group = "Colors 🟡🟢🟣")
sup_col = input.color(defval = color.new(color.lime, 75), title = "Support Color", group = "Colors 🟡🟢🟣")
inch_col = input.color(defval = color.new(color.gray, 75), title = "Color When Price in Channel", group = "Colors 🟡🟢🟣")
showpp = input.bool(defval = false, title = "Show Pivot Points", group = "Extras ⏶⏷")
showsrbroken = input.bool(defval = false, title = "Show Broken Support/Resistance", group = "Extras ⏶⏷")
showthema1en = input.bool(defval = false, title = "MA 1", inline = "ma1")
showthema1len = input.int(defval = 5, title = "", inline = "ma1")
showthema1type = input.string(defval = "SMA", title = "", options = ["SMA", "EMA"], inline = "ma1")
showthema2en = input.bool(defval = false, title = "MA 2", inline = "ma2")
showthema2len = input.int(defval = 50, title = "", inline = "ma2")
showthema2type = input.string(defval = "SMA", title = "", options = ["SMA", "EMA"], inline = "ma2")
showthema3en = input.bool(defval = false, title = "MA 3", inline = "ma2")
showthema3len = input.int(defval = 200, title = "", inline = "ma3")
showthema3type = input.string(defval = "SMA", title = "", options = ["SMA", "EMA"], inline = "ma3")
//------------------------------------------------------------------------------
//Calculations
//------------------------------------------------------------------------------
// Variables for Trendlines with Breaks
var upper = 0.
var lower = 0.
var slope_ph = 0.
var slope_pl = 0.
var offset = backpaint ? length : 0
n = bar_index
src = close
ph = ta.pivothigh(length, length)
pl = ta.pivotlow(length, length)
//Slope Calculation Method
slope = switch calcMethod
'Atr' => ta.atr(length) / length * mult
'Stdev' => ta.stdev(src,length) / length * mult
'Linreg' => math.abs(ta.sma(src * n, length) - ta.sma(src, length) * ta.sma(n, length)) / ta.variance(n, length) / 2 * mult
slope_ph := ph ? slope : slope_ph
slope_pl := pl ? slope : slope_pl
upper := ph ? ph : upper - slope_ph
lower := pl ? pl : lower + slope_pl
var upos = 0
var dnos = 0
upos := ph ? 0 : close > upper - slope_ph * length ? 1 : upos
dnos := pl ? 0 : close < lower + slope_pl * length ? 1 : dnos
// Variables for Support/Resistance Channels
ma1 = showthema1en ? (showthema1type == "SMA" ? ta.sma(close, showthema1len) : ta.ema(close, showthema1len)) : na
ma2 = showthema2en ? (showthema2type == "SMA" ? ta.sma(close, showthema2len) : ta.ema(close, showthema2len)) : na
ma3 = showthema3en ? (showthema3type == "SMA" ? ta.sma(close, showthema3len) : ta.ema(close, showthema3len)) : na
plot(ma1, color = not na(ma1) ? color.blue : na)
plot(ma2, color = not na(ma2) ? color.red : na)
plot(ma3, color = not na(ma3) ? color.purple : na)
float src1 = ppsrc == 'High/Low' ? high : math.max(close, open)
float src2 = ppsrc == 'High/Low' ? low: math.min(close, open)
float ph_sr = ta.pivothigh(src1, prd, prd)
float pl_sr = ta.pivotlow(src2, prd, prd)
plotshape(ph_sr and showpp, text = "H", style = shape.labeldown, color = na, textcolor = color.red, location = location.abovebar, offset = -prd)
plotshape(pl_sr and showpp, text = "L", style = shape.labelup, color = na, textcolor = color.lime, location = location.belowbar, offset = -prd)
prdhighest = ta.highest(300)
prdlowest = ta.lowest(300)
cwidth = (prdhighest - prdlowest) * ChannelW / 100
var pivotvals= array.new_float(0)
var pivotlocs= array.new_float(0)
if ph_sr or pl_sr
array.unshift(pivotvals, ph_sr ? ph_sr : pl_sr)
array.unshift(pivotlocs, bar_index)
for x = array.size(pivotvals) - 1 to 0
if bar_index - array.get(pivotlocs, x) > loopback
array.pop(pivotvals)
array.pop(pivotlocs)
continue
break
get_sr_vals(ind)=>
float lo = array.get(pivotvals, ind)
float hi = lo
int numpp = 0
for y = 0 to array.size(pivotvals) - 1
float cpp = array.get(pivotvals, y)
float wdth = cpp <= hi ? hi - cpp : cpp - lo
if wdth <= cwidth
if cpp <= hi
lo := math.min(lo, cpp)
else
hi := math.max(hi, cpp)
numpp := numpp + 20
[hi, lo, numpp]
var suportresistance = array.new_float(20, 0)
changeit(x, y)=>
tmp = array.get(suportresistance, y * 2)
array.set(suportresistance, y * 2, array.get(suportresistance, x * 2))
array.set(suportresistance, x * 2, tmp)
tmp := array.get(suportresistance, y * 2 + 1)
array.set(suportresistance, y * 2 + 1, array.get(suportresistance, x * 2 + 1))
array.set(suportresistance, x * 2 + 1, tmp)
if ph_sr or pl_sr
supres = array.new_float(0)
stren = array.new_float(10, 0)
for x = 0 to array.size(pivotvals) - 1
[hi, lo, strength] = get_sr_vals(x)
array.push(supres, strength)
array.push(supres, hi)
array.push(supres, lo)
for x = 0 to array.size(pivotvals) - 1
h = array.get(supres, x * 3 + 1)
l = array.get(supres, x * 3 + 2)
s = 0
for y = 0 to loopback
if (high[y] <= h and high[y] >= l) or
(low[y] <= h and low[y] >= l)
s := s + 1
array.set(supres, x * 3, array.get(supres, x * 3) + s)
array.fill(suportresistance, 0)
src = 0
for x = 0 to array.size(pivotvals) - 1
stv = -1.
stl = -1
for y = 0 to array.size(pivotvals) - 1
if array.get(supres, y * 3) > stv and array.get(supres, y * 3) >= minstrength * 20
stv := array.get(supres, y * 3)
stl := y
if stl >= 0
hh = array.get(supres, stl * 3 + 1)
ll = array.get(supres, stl * 3 + 2)
array.set(suportresistance, src * 2, hh)
array.set(suportresistance, src * 2 + 1, ll)
array.set(stren, src, array.get(supres, stl * 3))
for y = 0 to array.size(pivotvals) - 1
if (array.get(supres, y * 3 + 1) <= hh and array.get(supres, y * 3 + 1) >= ll) or
(array.get(supres, y * 3 + 2) <= hh and array.get(supres, y * 3 + 2) >= ll)
array.set(supres, y * 3, -1)
src += 1
if src >= 10
break
for x = 0 to 8
for y = x + 1 to 9
if array.get(stren, y) > array.get(stren, x)
tmp = array.get(stren, y)
array.set(stren, y, array.get(stren, x))
changeit(x, y)
get_level(ind)=>
float ret = na
if ind < array.size(suportresistance)
if array.get(suportresistance, ind) != 0
ret := array.get(suportresistance, ind)
ret
get_color(ind)=>
color ret = na
if ind < array.size(suportresistance)
if array.get(suportresistance, ind) != 0
ret := array.get(suportresistance, ind) > close and array.get(suportresistance, ind + 1) > close ? res_col :
array.get(suportresistance, ind) < close and array.get(suportresistance, ind + 1) < close ? sup_col :
inch_col
ret
var srchannels = array.new_box(10)
for x = 0 to math.min(9, maxnumsr)
box.delete(array.get(srchannels, x))
srcol = get_color(x * 2)
if not na(srcol)
array.set(srchannels, x,
box.new(left = bar_index, top = get_level(x * 2), right = bar_index + 1, bottom = get_level(x * 2 + 1),
border_color = srcol,
border_width = 1,
extend = extend.both,
bgcolor = srcol))
resistancebroken = false
supportbroken = false
not_in_a_channel = true
for x = 0 to math.min(9, maxnumsr)
if close <= array.get(suportresistance, x * 2) and close >= array.get(suportresistance, x * 2 + 1)
not_in_a_channel := false
if not_in_a_channel
for x = 0 to math.min(9, maxnumsr)
if close[1] <= array.get(suportresistance, x * 2) and close > array.get(suportresistance, x * 2)
resistancebroken := true
if close[1] >= array.get(suportresistance, x * 2 + 1) and close < array.get(suportresistance, x * 2 + 1)
supportbroken := true
//------------------------------------------------------------------------------
//Extended Lines for Trendlines
//------------------------------------------------------------------------------
var uptl = line.new(na,na,na,na, color = upCss, style = line.style_dashed, extend = extend.right)
var dntl = line.new(na,na,na,na, color = dnCss, style = line.style_dashed, extend = extend.right)
if ph and showExt
uptl.set_xy1(n-offset, backpaint ? ph : upper - slope_ph * length)
uptl.set_xy2(n-offset+1, backpaint ? ph - slope : upper - slope_ph * (length+1))
if pl and showExt
dntl.set_xy1(n-offset, backpaint ? pl : lower + slope_pl * length)
dntl.set_xy2(n-offset+1, backpaint ? pl + slope : lower + slope_pl * (length+1))
//------------------------------------------------------------------------------
//Plots for Trendlines
//------------------------------------------------------------------------------
plot(backpaint ? upper : upper - slope_ph * length, 'Upper', color = ph ? na : upCss, offset = -offset)
plot(backpaint ? lower : lower + slope_pl * length, 'Lower', color = pl ? na : dnCss, offset = -offset)
// Breakouts for Trendlines
plotshape(upos > upos[1] ? low : na, "Upper Break", shape.labelup, location.absolute, upCss, text = "B", textcolor = color.white, size = size.tiny)
plotshape(dnos > dnos[1] ? high : na, "Lower Break", shape.labeldown, location.absolute, dnCss, text = "B", textcolor = color.white, size = size.tiny)
//------------------------------------------------------------------------------
//Alerts
//------------------------------------------------------------------------------
alertcondition(upos > upos[1], 'Upward Breakout', 'Price broke the down-trendline upward')
alertcondition(dnos > dnos[1], 'Downward Breakout', 'Price broke the up-trendline downward')
alertcondition(resistancebroken, title = "Resistance Broken", message = "Resistance Broken")
alertcondition(supportbroken, title = "Support Broken", message = "Support Broken")
plotshape(showsrbroken and resistancebroken, style = shape.triangleup, location = location.belowbar, color = color.new(color.lime, 0), size = size.tiny)
plotshape(showsrbroken and supportbroken, style = shape.triangledown, location = location.abovebar, color = color.new(color.red, 0), size = size.tiny)