xmd79

Trend Lines | ʟucᴀs ᴀcнᴀvᴀʟ

Jul 7th, 2022
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.70 KB | None | 0 0
  1. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  2. // © LonesomeTheBlue
  3. //
  4. //@version=5
  5. indicator('Trend Lines | ʟucᴀs ᴀcнᴀvᴀʟ', overlay=true)
  6.  
  7. color resistanceColor = input.color(color.red, '', group='resistance ▼', inline='resistance')
  8. color supportColor = input.color(color.lime, '', group='support ▲', inline='support')
  9.  
  10. int resistanceOffsetBars = input.int(15, '', 1, group='resistance ▼', inline='resistance', tooltip='Number of bars to look back')
  11. int supportOffsetBars = input.int(15, '', 1, group='support ▲', inline='support', tooltip='Number of bars to look back')
  12.  
  13. int resistanceTotalBars = 2 * resistanceOffsetBars + 1
  14. int supportTotalBars = 2 * supportOffsetBars + 1
  15.  
  16. int highestBars = ta.highestbars(high, resistanceTotalBars)
  17. float resistance = highestBars == -resistanceOffsetBars ? high[resistanceOffsetBars] : na
  18.  
  19. int lowestBars = ta.lowestbars(low, supportTotalBars)
  20. float support = lowestBars == -supportOffsetBars ? low[supportOffsetBars] : na
  21.  
  22. plotshape(resistance, '', shape.labeldown, location.abovebar, color.new(color.white, 100), -resistanceOffsetBars, '▼', color.new(resistanceColor, 0))
  23. plotshape(support, '', shape.labelup, location.belowbar, color.new(color.white, 100), -supportOffsetBars, '▲', color.new(supportColor, 0))
  24.  
  25. getCoordinates(bool condition, float source, int occurrence, int offsetBars ) =>
  26. x = ta.valuewhen(condition, bar_index - offsetBars, occurrence)
  27. y = ta.valuewhen(condition, source, occurrence)
  28. [x, y]
  29.  
  30. trendLine() =>
  31. line.new(na, na, na, na, xloc.bar_index, extend.right, color.white, line.style_dashed)
  32.  
  33. crossLabel(string crossType) =>
  34. label.new(na, na, '', xloc.bar_index, yloc.price, crossType == 'over' ? color.lime : color.red, label.style_xcross)
  35.  
  36. plotTrendLine(int x0, float y0, int x1, float y1, float y2, line lineId) =>
  37. if y0 > y1 and y2 > y1
  38. line.set_xy1(lineId, x1, y1)
  39.  
  40. if y0 < y1 and y2 < y1
  41. line.set_xy1(lineId, x1, y1)
  42.  
  43. line.set_xy2(lineId, x0, y0)
  44.  
  45. [rx0, ry0] = getCoordinates(resistance, resistance, 0, resistanceOffsetBars)
  46. [rx1, ry1] = getCoordinates(resistance, resistance, 1, resistanceOffsetBars)
  47. [rx2, ry2] = getCoordinates(resistance, resistance, 2, resistanceOffsetBars)
  48.  
  49. [sx0, sy0] = getCoordinates(support, support, 0, supportOffsetBars)
  50. [sx1, sy1] = getCoordinates(support, support, 1, supportOffsetBars)
  51. [sx2, sy2] = getCoordinates(support, support, 2, supportOffsetBars)
  52.  
  53. var line resistanceTrendLine = trendLine()
  54. var line supportTrendLine = trendLine()
  55.  
  56. var label crossOverLabel = crossLabel('over')
  57. var label crossUnderLabel = crossLabel('under')
  58.  
  59. var line resistanceExtend = na
  60. var line supportExtend = na
  61.  
  62. if not na(resistance)
  63. plotTrendLine(rx0, ry0, rx1, ry1, ry2, resistanceTrendLine)
  64. label.set_xy(crossOverLabel, na, na)
  65. line.set_x2(resistanceExtend[1], rx0)
  66. resistanceExtend := line.new(rx0, ry0, bar_index, ry0, xloc.bar_index, extend.none, resistanceColor)
  67.  
  68. if not na(support)
  69. plotTrendLine(sx0, sy0, sx1, sy1, sy2, supportTrendLine)
  70. label.set_xy(crossUnderLabel, na, na)
  71. line.set_x2(supportExtend[1], sx0)
  72. supportExtend := line.new(sx0, sy0, bar_index, sy0, xloc.bar_index, extend.none, supportColor)
  73.  
  74. line.set_x2(resistanceExtend, bar_index)
  75. line.set_x2(supportExtend, bar_index)
  76.  
  77.  
  78. bool supportBreak = ta.crossunder(close, sy0)
  79. bool showSupportBreaks = input.bool(true, 'Show Breaks', group='support ▲')
  80.  
  81. bool resistanceBreak = ta.crossover(close, ry0)
  82. bool showResistanceBreaks = input.bool(true, 'Show Breaks', group='resistance ▼', inline='break')
  83.  
  84. plotshape(showSupportBreaks and supportBreak ? sy0 : na, '', shape.xcross, location.absolute, color.red, size=size.small)
  85. plotshape(showResistanceBreaks and resistanceBreak ? ry0 : na, '', shape.xcross, location.absolute, color.lime, size=size.small)
  86.  
  87. if supportBreak
  88. alert('Support Break', alert.freq_once_per_bar_close)
  89.  
  90. if resistanceBreak
  91. alert('Resistance Break', alert.freq_once_per_bar_close)
  92.  
  93. float resistanceTrendLinePrice = line.get_price(resistanceTrendLine, bar_index)
  94. float supportTrendLinePrice = line.get_price(supportTrendLine, bar_index)
  95.  
  96. bool resistanceTrendLineCrossOver = ta.crossover(close, resistanceTrendLinePrice)
  97. bool supportTrendLineCrossUnder = ta.crossover(close, supportTrendLinePrice)
  98.  
  99. if resistanceTrendLineCrossOver
  100. label.set_xy(crossOverLabel, bar_index, resistanceTrendLinePrice)
  101. alert('Resistance Trendline Cross Over', alert.freq_once_per_bar_close)
  102.  
  103. if supportTrendLineCrossUnder
  104. label.set_xy(crossUnderLabel, bar_index, supportTrendLinePrice)
  105. alert('Support Trendline Cross Over', alert.freq_once_per_bar_close)
  106.  
Advertisement
Add Comment
Please, Sign In to add comment