Advertisement
PineCoders

Last 3 (Jamie)

Feb 10th, 2020
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.61 KB | None | 0 0
  1. //@version=4
  2. study("Last 3", "", true)
  3. labelsOn = input(true)
  4. lookback = input(50)
  5.  
  6. // Saved pivot levels.
  7. var float tph1 = 0.
  8. var float tph2 = 0.
  9. var float tph3 = 0.
  10. var float tpl1 = low
  11. var float tpl2 = low
  12. var float tpl3 = low
  13. // bar_index where pivot was detected.
  14. var int tph1B = na
  15. var int tph2B = na
  16. var int tph3B = na
  17. var int tpl1B = na
  18. var int tpl2B = na
  19. var int tpl3B = na
  20.  
  21. // Here goes whatever method you use to find new pivots.
  22. p = 3
  23. pH = pivothigh(p, p)
  24. pL = pivotlow(p, p)
  25.  
  26. // Detect new pivot.
  27. newPH = not na(pH)
  28. newPL = not na(pL)
  29.  
  30. // Current upper/lower bounds
  31. var float p2a = na
  32. var float p1a = na
  33.  
  34. // Finds the lowest previous high level needing replacement, if one is < the pivot about to be replaced by a new one, or is outside lookback period.
  35. f_replacePH(_lastP) =>
  36. // _lasP: last active pivot just about to be replaced by a newly found pivot.
  37. _r = 0
  38. // Skip first pivot found because no previous one exists.
  39. if not na(_lastP)
  40. // Handle first bars or cases where the pivot if too far away so requires replacement.
  41. if bar_index - tph3B > lookback or na(tph3B)
  42. _r := 3
  43. else
  44. if bar_index - tph2B > lookback or na(tph2B)
  45. _r := 2
  46. else
  47. if bar_index - tph1B > lookback or na(tph1B)
  48. _r := 1
  49. // No mandatory replacement must occur because we found a saved pivot outside lookback; see if the last active pivot is > saved levels and select the smallest of those.
  50. if _r == 0
  51. _delta = 0.
  52. if _lastP > nz(tph1)
  53. _r := 1
  54. _delta := _lastP - tph1
  55. if _lastP > nz(tph2) and _lastP - tph2 > _delta
  56. _r := 2
  57. _delta := _lastP - tph2
  58. if _lastP > nz(tph3) and _lastP - tph3 > _delta
  59. _r := 3
  60. _r
  61.  
  62. f_replacePL(_lastP) =>
  63. _r = 0
  64. if not na(_lastP)
  65. if bar_index - tpl3B > lookback or na(tpl3B)
  66. _r := 3
  67. else
  68. if bar_index - tpl2B > lookback or na(tpl2B)
  69. _r := 2
  70. else
  71. if bar_index - tpl1B > lookback or na(tpl1B)
  72. _r := 1
  73. if _r == 0
  74. _delta = 0.
  75. if _lastP < nz(tpl1)
  76. _r := 1
  77. _delta := tpl1 - _lastP
  78. if _lastP < nz(tpl2) and tpl2 - _lastP > _delta
  79. _r := 2
  80. _delta := tpl2 - _lastP
  81. if _lastP < nz(tpl3) and tpl3 - _lastP > _delta
  82. _r := 3
  83. _r
  84.  
  85. // We will save no of replaced level for debugging.
  86. rH = 0
  87. rL = 0
  88.  
  89. // A new pivot is detected, update old levels and current pivot.
  90. if newPH
  91. // New pivot found. Verify if one of the saved pivots needs to be replaced by the current pivot before we start walking with the newly found pivot.
  92. rH := f_replacePH(p2a)
  93. // rL := f_replacePL(p2a)
  94. // Check if a high level requires replacement.
  95. if rH == 1
  96. tph1 := nz(p2a)
  97. tph1B := bar_index
  98. else
  99. if rH == 2
  100. tph2 := nz(p2a)
  101. tph2B := bar_index
  102. else
  103. if rH == 3
  104. tph3 := nz(p2a)
  105. tph3B := bar_index
  106. else
  107. // Check if a low level requires replacement.
  108. if rL == 1
  109. tpl1 := nz(p2a)
  110. tpl1B := bar_index
  111. else
  112. if rL == 2
  113. tpl2 := nz(p2a)
  114. tpl2B := bar_index
  115. else
  116. if rL == 3
  117. tpl3 := nz(p2a)
  118. tpl3B := bar_index
  119. // Save new high pivot found.
  120. p2a := pH
  121. else
  122. if newPL
  123. // rH := f_replacePH(p1a)
  124. rL := f_replacePL(p1a)
  125. // Check if a high level requires replacement.
  126. if rH == 1
  127. tph1 := nz(p1a)
  128. tph1B := bar_index
  129. else
  130. if rH == 2
  131. tph2 := nz(p1a)
  132. tph2B := bar_index
  133. else
  134. if rH == 3
  135. tph3 := nz(p1a)
  136. tph3B := bar_index
  137. else
  138. // Check if a low level requires replacement.
  139. if rL == 1
  140. tpl1 := nz(p1a)
  141. tpl1B := bar_index
  142. else
  143. if rL == 2
  144. tpl2 := nz(p1a)
  145. tpl2B := bar_index
  146. else
  147. if rL == 3
  148. tpl3 := nz(p1a)
  149. tpl3B := bar_index
  150. // Save new low pivot found.
  151. p1a := pL
  152.  
  153.  
  154. // Plot current pivots
  155. plot(p2a, "p2a", not newPH ? color.green : na)
  156. plot(p1a, "p1a", not newPL ? color.maroon : na)
  157.  
  158. // Plot 3 levels, if required. We need to check if level is too old because older ones are only replaced when a new pivot is found.
  159. plot(bar_index - tph1B < lookback and tph1 > p2a ? tph1 : na, "tph1", color.green, 2, plot.style_circles)
  160. plot(bar_index - tph2B < lookback and tph2 > p2a ? tph2 : na, "tph2", color.green, 3, plot.style_circles)
  161. plot(bar_index - tph3B < lookback and tph3 > p2a ? tph3 : na, "tph3", color.green, 4, plot.style_circles)
  162. plot(bar_index - tpl1B < lookback and tpl1 < p1a ? tpl1 : na, "tpl1", color.maroon, 2, plot.style_circles)
  163. plot(bar_index - tpl2B < lookback and tpl2 < p1a ? tpl2 : na, "tpl2", color.maroon, 3, plot.style_circles)
  164. plot(bar_index - tpl3B < lookback and tpl3 < p1a ? tpl3 : na, "tpl3", color.maroon, 4, plot.style_circles)
  165.  
  166.  
  167.  
  168. // Debugging plots.
  169. plotchar(tph1 , "tph1 ", "", location.top)
  170. plotchar(tph2 , "tph2 ", "", location.top)
  171. plotchar(tph3 , "tph3 ", "", location.top)
  172. plotchar(tph1B, "tph1B", "", location.top)
  173. plotchar(tph2B, "tph2B", "", location.top)
  174. plotchar(tph3B, "tph3B", "", location.top)
  175. plotchar(tpl1 , "tpl1 ", "", location.top)
  176. plotchar(tpl2 , "tpl2 ", "", location.top)
  177. plotchar(tpl3 , "tpl3 ", "", location.top)
  178. plotchar(tpl1B, "tpl1B", "", location.top)
  179. plotchar(tpl2B, "tpl2B", "", location.top)
  180. plotchar(tpl3B, "tpl3B", "", location.top)
  181. plotchar(rH, "rH", "", location.top)
  182. plotchar(rL, "rL", "", location.top)
  183.  
  184. if newPH and labelsOn
  185. label.new(bar_index, p2a,
  186. "Previous P: " + tostring(p2a[1]) +
  187. "\nCurrent P: " + tostring(p2a) +
  188. "\nBar: " + tostring(bar_index) +
  189. "\nReplaces: " + tostring(rH) +
  190. "\ntph1: " + tostring(tph1[1]) + "(" + tostring(bar_index - tph1B[1]) + ")►" + tostring(tph1) +
  191. "\ntph2: " + tostring(tph2[1]) + "(" + tostring(bar_index - tph2B[1]) + ")►" + tostring(tph2) +
  192. "\ntph3: " + tostring(tph3[1]) + "(" + tostring(bar_index - tph3B[1]) + ")►" + tostring(tph3),
  193. color = #00000000,
  194. style = label.style_labeldown,
  195. textcolor = color.lime,
  196. textalign = text.align_left)
  197.  
  198. if newPL and labelsOn
  199. label.new(bar_index, p1a,
  200. "Previous P: " + tostring(p1a[1]) +
  201. "\nCurrent P: " + tostring(p1a) +
  202. "\nBar: " + tostring(bar_index) +
  203. "\nReplaces: " + tostring(rL) +
  204. "\ntpl1: " + tostring(tpl1[1]) + "(" + tostring(bar_index - tpl1B[1]) + ")►" + tostring(tpl1) +
  205. "\ntpl2: " + tostring(tpl2[1]) + "(" + tostring(bar_index - tpl2B[1]) + ")►" + tostring(tpl2) +
  206. "\ntpl3: " + tostring(tpl3[1]) + "(" + tostring(bar_index - tpl3B[1]) + ")►" + tostring(tpl3),
  207. color = #00000000,
  208. style = label.style_labelup,
  209. textcolor = color.red,
  210. textalign = text.align_left)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement