Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
- // © LonesomeTheBlue
- //
- //@version=5
- indicator('Trend Lines | ʟucᴀs ᴀcнᴀvᴀʟ', overlay=true)
- color resistanceColor = input.color(color.red, '', group='resistance ▼', inline='resistance')
- color supportColor = input.color(color.lime, '', group='support ▲', inline='support')
- int resistanceOffsetBars = input.int(15, '', 1, group='resistance ▼', inline='resistance', tooltip='Number of bars to look back')
- int supportOffsetBars = input.int(15, '', 1, group='support ▲', inline='support', tooltip='Number of bars to look back')
- int resistanceTotalBars = 2 * resistanceOffsetBars + 1
- int supportTotalBars = 2 * supportOffsetBars + 1
- int highestBars = ta.highestbars(high, resistanceTotalBars)
- float resistance = highestBars == -resistanceOffsetBars ? high[resistanceOffsetBars] : na
- int lowestBars = ta.lowestbars(low, supportTotalBars)
- float support = lowestBars == -supportOffsetBars ? low[supportOffsetBars] : na
- plotshape(resistance, '', shape.labeldown, location.abovebar, color.new(color.white, 100), -resistanceOffsetBars, '▼', color.new(resistanceColor, 0))
- plotshape(support, '', shape.labelup, location.belowbar, color.new(color.white, 100), -supportOffsetBars, '▲', color.new(supportColor, 0))
- getCoordinates(bool condition, float source, int occurrence, int offsetBars ) =>
- x = ta.valuewhen(condition, bar_index - offsetBars, occurrence)
- y = ta.valuewhen(condition, source, occurrence)
- [x, y]
- trendLine() =>
- line.new(na, na, na, na, xloc.bar_index, extend.right, color.white, line.style_dashed)
- crossLabel(string crossType) =>
- label.new(na, na, '', xloc.bar_index, yloc.price, crossType == 'over' ? color.lime : color.red, label.style_xcross)
- plotTrendLine(int x0, float y0, int x1, float y1, float y2, line lineId) =>
- if y0 > y1 and y2 > y1
- line.set_xy1(lineId, x1, y1)
- if y0 < y1 and y2 < y1
- line.set_xy1(lineId, x1, y1)
- line.set_xy2(lineId, x0, y0)
- [rx0, ry0] = getCoordinates(resistance, resistance, 0, resistanceOffsetBars)
- [rx1, ry1] = getCoordinates(resistance, resistance, 1, resistanceOffsetBars)
- [rx2, ry2] = getCoordinates(resistance, resistance, 2, resistanceOffsetBars)
- [sx0, sy0] = getCoordinates(support, support, 0, supportOffsetBars)
- [sx1, sy1] = getCoordinates(support, support, 1, supportOffsetBars)
- [sx2, sy2] = getCoordinates(support, support, 2, supportOffsetBars)
- var line resistanceTrendLine = trendLine()
- var line supportTrendLine = trendLine()
- var label crossOverLabel = crossLabel('over')
- var label crossUnderLabel = crossLabel('under')
- var line resistanceExtend = na
- var line supportExtend = na
- if not na(resistance)
- plotTrendLine(rx0, ry0, rx1, ry1, ry2, resistanceTrendLine)
- label.set_xy(crossOverLabel, na, na)
- line.set_x2(resistanceExtend[1], rx0)
- resistanceExtend := line.new(rx0, ry0, bar_index, ry0, xloc.bar_index, extend.none, resistanceColor)
- if not na(support)
- plotTrendLine(sx0, sy0, sx1, sy1, sy2, supportTrendLine)
- label.set_xy(crossUnderLabel, na, na)
- line.set_x2(supportExtend[1], sx0)
- supportExtend := line.new(sx0, sy0, bar_index, sy0, xloc.bar_index, extend.none, supportColor)
- line.set_x2(resistanceExtend, bar_index)
- line.set_x2(supportExtend, bar_index)
- bool supportBreak = ta.crossunder(close, sy0)
- bool showSupportBreaks = input.bool(true, 'Show Breaks', group='support ▲')
- bool resistanceBreak = ta.crossover(close, ry0)
- bool showResistanceBreaks = input.bool(true, 'Show Breaks', group='resistance ▼', inline='break')
- plotshape(showSupportBreaks and supportBreak ? sy0 : na, '', shape.xcross, location.absolute, color.red, size=size.small)
- plotshape(showResistanceBreaks and resistanceBreak ? ry0 : na, '', shape.xcross, location.absolute, color.lime, size=size.small)
- if supportBreak
- alert('Support Break', alert.freq_once_per_bar_close)
- if resistanceBreak
- alert('Resistance Break', alert.freq_once_per_bar_close)
- float resistanceTrendLinePrice = line.get_price(resistanceTrendLine, bar_index)
- float supportTrendLinePrice = line.get_price(supportTrendLine, bar_index)
- bool resistanceTrendLineCrossOver = ta.crossover(close, resistanceTrendLinePrice)
- bool supportTrendLineCrossUnder = ta.crossover(close, supportTrendLinePrice)
- if resistanceTrendLineCrossOver
- label.set_xy(crossOverLabel, bar_index, resistanceTrendLinePrice)
- alert('Resistance Trendline Cross Over', alert.freq_once_per_bar_close)
- if supportTrendLineCrossUnder
- label.set_xy(crossUnderLabel, bar_index, supportTrendLinePrice)
- alert('Support Trendline Cross Over', alert.freq_once_per_bar_close)
Advertisement
Add Comment
Please, Sign In to add comment