Advertisement
xmd79

Pivotal Moments

Aug 5th, 2023
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.81 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. // © allanster
  3.  
  4. //@version=5
  5. indicator('Pivotal Moments', overlay = true, max_lines_count = 500, max_labels_count = 500)
  6.  
  7. // Draws lines for each of up to 500 pivots that have never been revisited at the present moment in time.
  8.  
  9. tooltipD = "Delays deletion by nBars or nTime of any Pivot lines that are touched or crossed by realtime bars formed after indicator has been loaded " +
  10. "on chart.\n\nWhen using nBars the bar duration is from next bar until close of Nth bar. This is the recommended delay method.\n\nWhen using nTime " +
  11. "units the time duration is the minimum time of delay desired, the actual time delay may exceed specified amount. Note that this delay method can be " +
  12. "computationally quite expensive and may cause erratic behavior when used with a large delay, large number of lines, or when the indicator has been " +
  13. "loaded for a long while.\n\nElapsed time is checked upon each price or volume update. Frequency of the updates is dependent upon liquidity. If time " +
  14. "delay is set to 3 seconds and the next bar update after touch does not occur until 5 seconds later then the delay would be 5 seconds since that is " +
  15. "when the next check for elapsed time occurs."
  16. tooltipQ = "Extend levels nBars to the right of current bar. Show number of newest Pivot levels to keep"
  17.  
  18. inBarsL_ = input.int (defval = 2, title = 'Pivot Bars Left', minval = 0, maxval = 100, inline = '0')
  19. inBars_R = input.int (defval = 2, title = ' Right', minval = 0, maxval = 100, inline = '0')
  20. inStyles = input.string(defval = 'Dotted', title = 'Lines Show As', options = ['Dashed', 'Dotted', 'Solid'], inline = '1')
  21. inWidths = input.int (defval = 1, title = ' Wide ', inline = '1')
  22. inDelayN = input.int (defval = 0, title = 'Delay Removal', minval = 0, step = 1, inline = '2')
  23. inDelayT = input.string(defval = 'nBars', title = ' Units', options = ['nBars', 'nSecs', 'nMins', 'nHrs', 'nDays'], inline = '2', tooltip = tooltipD)
  24. inOffset = input.int (defval = 7, title = 'Extend Right    ', minval = 0, maxval = 100, step = 1, inline = '3')
  25. inQueueN = input.int (defval = 50, title = ' Show ', minval = 0, maxval = 500, step = 1, inline = '3', tooltip = tooltipQ)
  26. inColrHi = input.color (defval = color.new(#ff9800, 0), title = 'Colors Used   Hi', inline = '4')
  27. inColrLo = input.color (defval = color.new(#00e676, 0), title = 'Lo', inline = '4')
  28. inColrHL = input.color (defval = color.new(#9c27b0, 0), title = 'HL', inline = '4')
  29. inLabels = input (defval = true, title = 'Show Price Labels', inline = '5')
  30. inPlaced = input (defval = false, title = 'On Right', inline = '5')
  31. inColors = input (defval = false, title = 'Show Pivot Bars', inline = '6')
  32.  
  33. drawLabl(_offset, _y, _colr, _styl)=> label.new(bar_index + _offset, _y, str.tostring(_y, format.mintick), xloc.bar_index,
  34. yloc.price, color(na), not _styl ? label.style_none : label.style_label_left, _colr, size.normal, text.align_left)
  35.  
  36. drawLine(_inset, _offset, _y, _colr, _styl, _wide)=> line.new(bar_index - _inset, _y, bar_index + _offset, _y, xloc.bar_index,
  37. extend.none, _colr, _styl == 'Dashed' ? line.style_dashed : _styl == 'Dotted' ? line.style_dotted : line.style_solid, _wide)
  38.  
  39. pivotsHi = ta.pivothigh(high, inBarsL_, inBars_R) // hi pivot occurs
  40. pivotsLo = ta.pivotlow( low, inBarsL_, inBars_R) // lo pivot occurs
  41. sourceHi = high[inBars_R] // price value of pivot when hi pivot is confirmed
  42. sourceLo = low[inBars_R] // price value of pivot when lo pivot is confirmed
  43. isColrHi = inLabels ? inColrHi : na // disable labels with na or use transparent color(na)
  44. isColrLo = inLabels ? inColrLo : na // disable labels with na or use transparent color(na)
  45. delayAmt =
  46. inDelayT == 'nBars' ? inDelayN + 1 :
  47. inDelayT == 'nSecs' ? inDelayN * 1e3 :
  48. inDelayT == 'nMins' ? inDelayN * 6e4 :
  49. inDelayT == 'nHrs' ? inDelayN * 3.6e6 : 2.52e7 // added + 1 to nBars to extend to the open of the Nth + 1 bar
  50. // when delay type is nTime convert nSecs to nMilliseconds
  51.  
  52. type pivType // declare UD type
  53. string Type // declare field for string value of 'h' or 'l'
  54. float Valu // declare field for source value of pivot
  55. int BarI // declare field for bar_index when touched
  56. int Time // declare field for bar time when touched
  57. bool Mark // declare field for flagging touched lines
  58. label Labl // declare field for label of source value
  59. line Line // declare field for line of source value
  60.  
  61. var pivArray = array.new<pivType>() // declare an array of UDT type to hold UDT objects
  62. if pivotsHi // if condition occurs
  63. piv = pivType.new('h', sourceHi, na, na, false,
  64. drawLabl(inOffset, sourceHi, isColrHi, inPlaced),
  65. drawLine(inBars_R, inOffset, sourceHi, inColrHi, inStyles, inWidths)) // create an object of UDT type
  66. array.push(pivArray, piv) // populate array with element object
  67. if pivotsLo // if condition occurs
  68. piv = pivType.new('l', sourceLo, na, na, false,
  69. drawLabl(inOffset, sourceLo, isColrLo, inPlaced),
  70. drawLine(inBars_R, inOffset, sourceLo, inColrLo, inStyles, inWidths)) // create an object of UDT type
  71. array.push(pivArray, piv)
  72. snapShot = timenow // create a single timestamp for loop, less accurate but more efficient
  73. for i = (array.size(pivArray) == 0 ?
  74. na : array.size(pivArray) - 1) to 0 by 1 // loop backwards through array of objects
  75. obj = array.get(pivArray, i) // assign the array's element i to arbitrarily named 'obj'
  76. breaksHi = obj.Type == 'h' and high >= obj.Valu
  77. breaksLo = obj.Type == 'l' and low <= obj.Valu
  78. if obj.Mark == false and (breaksHi or breaksLo) // check unflagged element objects for source touches
  79. obj.BarI := bar_index // set element object's BarI field for later evaluating elapsed bars
  80. obj.Time := timenow // set element object's Time field for later evaluating elapsed time
  81. obj.Mark := true // set element object's flag field for later removal once delay has been met
  82. trgtValu = str.tostring(obj.Valu, '#.00')
  83. srceValu = str.tostring(close, '#.00')
  84. if breaksHi
  85. alert(syminfo.prefix + ' ' + syminfo.ticker + ' ' +
  86. timeframe.period +
  87. '\nPrice Touched Pivot Hi\nPrice: ' +
  88. srceValu + '\nPivot: ' + trgtValu,
  89. alert.freq_once_per_bar) // alert when pivot hi is touched or broken
  90. if breaksLo
  91. alert(syminfo.prefix + ' ' + syminfo.ticker + ' ' +
  92. timeframe.period +
  93. '\nPrice Touched Pivot Lo\nPivot: ' +
  94. trgtValu + '\nPrice: ' + srceValu,
  95. alert.freq_once_per_bar) // alert when pivot lo is touched or broken
  96. if obj.Mark == true and (barstate.ishistory or inDelayN == 0 or
  97. (inDelayT == 'nBars' and bar_index - obj.BarI >= delayAmt) or
  98. (inDelayT != 'nBars' and snapShot - obj.Time >= delayAmt)) // check if delayed touches have met required amount of delay
  99. label.delete(obj.Labl) // if stored label is touched delete drawing
  100. line.delete(obj.Line) // if stored line is touched delete drawing
  101. array.remove(pivArray, i) // remove any touched element objects
  102. label.set_x(obj.Labl, bar_index + inOffset) // offset remaining labels for current bar realignment
  103. line.set_x2(obj.Line, bar_index + inOffset) // offset remaining lines for current bar realignment
  104.  
  105. trashBin = array.size(pivArray) - inQueueN // get current number of items exceeding queue
  106. if trashBin > 0 // if number of elements exceeds queue
  107. for elmnt = trashBin - 1 to 0 by 1 // loop backwards through elements to be trashed
  108. getEl = array.get(pivArray, elmnt) // get object element from array
  109. label.delete(getEl.Labl) // delete drawing before removal of object element
  110. line.delete(getEl.Line) // delete drawing before removal of object element
  111. array.remove(pivArray, elmnt) // remove object element from items array
  112.  
  113. barColor = pivotsHi and pivotsLo ? inColrHL :
  114. pivotsHi ? inColrHi : pivotsLo ? inColrLo : na // colorize purple for double fractals
  115. barcolor(inColors and pivotsHi ? barColor : na, -inBars_R) // colorize hi pivot bars
  116. barcolor(inColors and pivotsLo ? barColor : na, -inBars_R) // colorize lo pivot bars
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement