Advertisement
xmd79

Pivot TrendLine with type

Jan 3rd, 2023
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. // ___ ____________ _____ _ __
  2. // / | / ____/ ____/ / ___/(_)___ _____ ____ _/ /
  3. // / /| |/ / __/ / __ \__ \/ / __ `/ __ \/ __ `/ /
  4. // / ___ / /_/ / /_/ / ___/ / / /_/ / / / / /_/ / /
  5. // /_/ |_\____/\____/ /____/_/\__, /_/ /_/\__,_/_/
  6. // /____/
  7. // ==========================================================================
  8. // @description The simplest version of the indicator automatically draws trendLine on your charts, with newest type features, with build-in functions only.
  9. // @version 1.0
  10. // @date 2022/12/28
  11. // @author feibilanceon
  12. // You can use it anywhere but please keep the author info.
  13.  
  14. //@version=5
  15. indicator("Pivot TrendLine with type", shorttitle="P_L_t", overlay=true)
  16.  
  17. // Config
  18. pivotLength = input(20, title = "Pivot Length")
  19.  
  20. leftBars = pivotLength
  21. rightBars = pivotLength
  22.  
  23. onlyLastLine = input(true, title = "Only show last trend line")
  24.  
  25. // Type
  26. type pivotPoint
  27. int barIndex
  28. float price
  29.  
  30. var highPivots = array.new<pivotPoint>()
  31. var lowPivots = array.new<pivotPoint>()
  32.  
  33. // Pivot High
  34. swh = ta.pivothigh(leftBars, rightBars)
  35.  
  36. if swh
  37. array.push(highPivots, pivotPoint.new(bar_index - rightBars, swh))
  38.  
  39. // Pivot Low
  40.  
  41. swl = ta.pivotlow(leftBars, rightBars)
  42.  
  43. if swl
  44. array.push(lowPivots, pivotPoint.new(bar_index - rightBars, swl))
  45.  
  46. // Plot
  47. var lineHigh = line.new(0,0,0,0, extend=extend.right)
  48.  
  49. pivotPoint lastHighPivot = array.size(highPivots) > 2 ? array.get(highPivots,array.size(highPivots) - 1) : na
  50. pivotPoint secondLastHighPivot = array.size(highPivots) > 2 ? array.get(highPivots,array.size(highPivots) - 2) : na
  51.  
  52. if array.size(highPivots) > 2
  53. if lastHighPivot.barIndex != secondLastHighPivot.barIndex
  54. if onlyLastLine
  55. line.delete(lineHigh)
  56. line.set_color(lineHigh, color.new(color.red, 80))
  57. line.set_extend(lineHigh, extend.none)
  58. lineHigh := line.new(secondLastHighPivot.barIndex, secondLastHighPivot.price, lastHighPivot.barIndex, lastHighPivot.price, extend=extend.right, color=color.red)
  59.  
  60. var lineLow = line.new(0,0,0,0, extend=extend.right)
  61.  
  62. pivotPoint lastLowPivot = array.size(lowPivots) > 2 ? array.get(lowPivots,array.size(lowPivots) - 1) : na
  63. pivotPoint secondLastLowPivot = array.size(lowPivots) > 2 ? array.get(lowPivots,array.size(lowPivots) - 2) : na
  64.  
  65. if array.size(lowPivots) > 2
  66. if lastLowPivot.barIndex != secondLastLowPivot.barIndex
  67. if onlyLastLine
  68. line.delete(lineLow)
  69. line.set_color(lineLow, color.new(color.green, 80))
  70. line.set_extend(lineLow, extend.none)
  71. lineLow := line.new(secondLastLowPivot.barIndex, secondLastLowPivot.price, lastLowPivot.barIndex, lastLowPivot.price, extend=extend.right,color=color.green)
  72.  
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement