Guest User

Pivot Highs/Lows

a guest
Dec 31st, 2025
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.01 KB | None | 0 0
  1. // This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  2. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3. // © Rathack
  4. // edited by @taylorbray
  5. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6. // -- // -- // -- Ultra High/Low
  7. //////////////////////////////////////////////////////////////////////////////////////////
  8. // -- // --
  9. ////////////////////////////////////////////////////////////
  10. // --
  11. //////////////////////////////
  12. //
  13. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  14. //@version=5
  15. indicator("Pivot Highs/Lows", overlay = true, max_lines_count = 500, max_labels_count = 500)
  16.  
  17. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  18. // -- // -- // -- user inputs
  19.  
  20. //////////////////////////////////////////////////////////////////////////////////////////
  21. // -- // -- pivot high
  22.  
  23. // edit by @taylorbray
  24.  
  25. string group1 = "////////////////////////// HIGH //////////////////////////"
  26.  
  27. bool showPivotsHigh = input.bool (true , "" , inline = "h1", group = group1)
  28. float sourceHigh = input.source (high , "Pivot High | Source" , inline = "h1", group = group1)
  29.  
  30. int lengthLeftHigh = input.int (10 , "Length Left" , minval = 1 , inline = "h2", group = group1)
  31. int lengthRightHigh = input.int (10 , "Right" , minval = 1 , inline = "h2", group = group1)
  32.  
  33. string strStyleHigh = input.string ("―――" , "Style", ["―――", "---", "•••"] , inline = "h3", group = group1)
  34. int widthHigh = input.int (1 , "" , minval = 1 , inline = "h3", group = group1)
  35. color colorHigh = input.color (#72727296, "" , inline = "h3", group = group1)
  36.  
  37. bool showPriceHigh = input.bool (true , "Show Price" , inline = "h4", group = group1)
  38. bool extendHigh = input.bool (false , "Extend Lines" , inline = "h4", group = group1)
  39.  
  40. bool showPurgedHigh = input.bool (true , "Purged" , inline = "h5", group = group1)
  41. string strStyleHighP = input.string ("―――" , "" , ["―――", "---", "•••"] , inline = "h5", group = group1)
  42. int widthHighP = input.int (1 , "" , minval = 1 , inline = "h5", group = group1)
  43. color colorPurgeHigh = input.color (#72727296, "" , inline = "h5", group = group1)
  44.  
  45. string timeframeHigh = input.timeframe ("" , "Timeframe", tooltip = "Don't use a too low tf" , group = group1)
  46.  
  47. //////////////////////////////////////////////////////////////////////////////////////////
  48. // -- // -- pivot low
  49.  
  50. string group2 = "////////////////////////// LOW //////////////////////////"
  51.  
  52. bool showPivotsLow = input.bool (true , "" , inline = "l1", group = group2)
  53. float sourceLow = input.source (low , "Pivot Low | Source" , inline = "l1", group = group2)
  54.  
  55. int lengthLeftLow = input.int (10 , "Length Left" , minval = 1 , inline = "l2", group = group2)
  56. int lengthRightLow = input.int (10 , "Right" , minval = 1 , inline = "l2", group = group2)
  57.  
  58. string strStyleLow = input.string ("―――" , "Style", [ "―――", "---", "•••"] , inline = "l3", group = group2)
  59. int widthLow = input.int (1 , "" , minval = 1 , inline = "l3", group = group2)
  60. color colorLow = input.color (#72727296, "" , inline = "l3", group = group2)
  61.  
  62. bool showPriceLow = input.bool (true , "Show Price" , inline = "l4", group = group2)
  63. bool extendLow = input.bool (false , "Extend Lines" , inline = "l4", group = group2)
  64.  
  65. bool showPurgedLow = input.bool (true , "Purged" , inline = "l5", group = group2)
  66. string strStyleLowP = input.string ("―――" , "" , [ "―――", "---", "•••"] , inline = "l5", group = group2)
  67. int widthLowP = input.int (1 , "" , minval = 1 , inline = "l5", group = group2)
  68. color colorPurgeLow = input.color (#72727296, "" , inline = "l5", group = group2)
  69.  
  70. string timeframeLow = input.timeframe ("" , "Timeframe", tooltip = "Don't use a too low tf" , group = group2)
  71.  
  72. //////////////////////////////////////////////////////////////////////////////////////////
  73. // -- // -- previous high/low
  74.  
  75. string group3 = "/////////////////// PREVIOUS HIGH/LOW ///////////////////"
  76.  
  77. bool showPreDayHL = input.bool (false , "Previous Day High/Low" , inline = "p1", group = group3)
  78. color colorPreDayH = input.color (color.new(#ff9100, 0), "" , inline = "p1", group = group3)
  79. color colorPreDayL = input.color (color.new(#0084ff, 0), "" , inline = "p1", group = group3)
  80.  
  81. bool showPreWeekHL = input.bool (false , "Previous Week High/Low" , inline = "p2", group = group3)
  82. color colorPreWeekH = input.color (color.new(#ff9100, 0), "" , inline = "p2", group = group3)
  83. color colorPreWeekL = input.color (color.new(#0084ff, 0), "" , inline = "p2", group = group3)
  84.  
  85. color colorPreOverH = input.color (color.new(#ffffff, 0), "Color High/Low for Overlap" , inline = "p3", group = group3)
  86. color colorPreOverL = input.color (color.new(#ffffff, 0), "" , inline = "p3", group = group3)
  87.  
  88. bool showPurgedPH = input.bool (true , "Highlight Purged Previous High" , group = group3)
  89. bool showPurgedPL = input.bool (true , "Highlight Purged Previous Low" , group = group3)
  90.  
  91. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  92. // -- // -- // -- functions
  93.  
  94. ////////////////////////////////////////////////////////////
  95. // @function Get the length multiplier
  96. // @param _high (series bool) true == pivot high false == pivot low
  97. // @returns length multiplier (series float)
  98.  
  99. _getLengthMultiplier(series bool _high) =>
  100.  
  101. series int timeframeSeconds_ = timeframe.in_seconds(timeframe.period)
  102. series int timeframeHighSeconds_ = timeframe.in_seconds(timeframeHigh )
  103. series int timeframeLowSeconds_ = timeframe.in_seconds(timeframeLow )
  104.  
  105. series float lengthMultiplier_ = _high ? timeframeHighSeconds_ / timeframeSeconds_
  106. : timeframeLowSeconds_ / timeframeSeconds_
  107.  
  108. lengthMultiplier_
  109.  
  110.  
  111. ////////////////////////////////////////////////////////////
  112. // @function Check if two variables are the same
  113. // @param _preValue The first variable
  114. // @param _value The second variable
  115. // @returns true or false
  116.  
  117. isPreValue(_preValue, _value) =>
  118.  
  119. _preValue == _value ? true : false
  120.  
  121.  
  122. ////////////////////////////////////////////////////////////
  123. // @function Get the label text/color for Pre High/Low
  124. // @param _preMajorDay (series float) previous day high/low
  125. // @param _preMajorWeek (series float) previous Week high/low
  126. // @param _pivotPrice (series float) actual pivot price
  127. // @param _isHigh (series bool) true == high false == low
  128. // @param _isPurged (series bool) true == purged
  129. // @returns [text_, color_] label text, line/label color
  130.  
  131. getTextColorInfo(series float _preMajorDay , series float _preMajorWeek , series float _pivotPrice
  132. , series bool _isHigh , series bool _isPurged ) =>
  133.  
  134. series string text_ = ""
  135. series color color_ = na
  136. series string sPreDay_ = _isHigh ? " PDH" : " PDL"
  137. series string sPreWeek_ = _isHigh ? " PWH" : " PWL"
  138. series color cPreDay_ = _isHigh ? colorPreDayH : colorPreDayL
  139. series color cPreWeek_ = _isHigh ? colorPreWeekH : colorPreWeekL
  140. series color cPreOver_ = _isHigh ? colorPreOverH : colorPreOverL
  141. series color cColor_ = (_isHigh and _isPurged) ? colorPurgeHigh
  142. : (not _isHigh and _isPurged) ? colorPurgeLow
  143. : (_isHigh) ? colorHigh
  144. : colorLow
  145.  
  146. series bool isPreDay = isPreValue(_preMajorDay , _pivotPrice)
  147. series bool isPreWeek = isPreValue(_preMajorWeek, _pivotPrice)
  148.  
  149. if (showPreDayHL and showPreWeekHL)
  150.  
  151. text_ := isPreDay ? str.tostring(_pivotPrice, "0.00000") + sPreDay_
  152. : isPreWeek ? str.tostring(_pivotPrice, "0.00000") + sPreWeek_
  153. : str.tostring(_pivotPrice, "0.00000")
  154. if (isPreDay) and (isPreWeek)
  155. text_ := str.tostring(_pivotPrice, "0.00000") + sPreDay_ + " &" + sPreWeek_
  156.  
  157. color_ := isPreDay ? cPreDay_
  158. : isPreWeek ? cPreWeek_
  159. : cColor_
  160. if (isPreDay) and (isPreWeek)
  161. color_ := cPreOver_
  162.  
  163. else if (showPreDayHL and not showPreWeekHL)
  164.  
  165. text_ := isPreDay ? str.tostring(_pivotPrice, "0.00000") + sPreDay_
  166. : str.tostring(_pivotPrice, "0.00000")
  167.  
  168. color_ := isPreDay ? cPreDay_
  169. : cColor_
  170.  
  171. else if (not showPreDayHL and showPreWeekHL)
  172.  
  173. text_ := isPreWeek ? str.tostring(_pivotPrice, "0.00000") + sPreWeek_
  174. : str.tostring(_pivotPrice, "0.00000")
  175.  
  176. color_ := isPreWeek ? cPreWeek_
  177. : cColor_
  178.  
  179. else
  180.  
  181. text_ := str.tostring(_pivotPrice, "0.00000")
  182. color_ := cColor_
  183.  
  184. [text_, color_]
  185.  
  186.  
  187. ////////////////////////////////////////////////////////////
  188. // @function Convert a string in line style
  189. // @param _string (series string) The String to convert
  190. // @returns line.style_dashed
  191. // or line.style_dotted
  192. // or line.style_solid
  193.  
  194. getLineStyle(series string _string) =>
  195.  
  196. switch _string
  197.  
  198. "---" => line.style_dashed
  199. "•••" => line.style_dotted
  200. "―――" => line.style_solid
  201.  
  202.  
  203. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  204. // -- // -- // -- calculation
  205.  
  206. //////////////////////////////
  207. // high
  208.  
  209. series string styleHigh = getLineStyle(strStyleHigh )
  210. series string styleHighP = getLineStyle(strStyleHighP)
  211. series float pivotHighPrice = 0.0
  212.  
  213. //////////////////////////////
  214. // low
  215.  
  216. series string styleLow = getLineStyle(strStyleLow )
  217. series string styleLowP = getLineStyle(strStyleLowP)
  218. series float pivotLowPrice = 0.0
  219.  
  220. //////////////////////////////////////////////////////////////////////////////////////////
  221. // -- // -- identify previous day/week high/low
  222.  
  223. //////////////////////////////
  224. // high
  225.  
  226. series float preDayHigh = request.security(syminfo.tickerid, "D", high[1])
  227. series float preWeekHigh = request.security(syminfo.tickerid, "W", high[1])
  228.  
  229. //////////////////////////////
  230. // low
  231.  
  232. series float preDayLow = request.security(syminfo.tickerid, "D", low[1])
  233. series float preWeekLow = request.security(syminfo.tickerid, "W", low[1])
  234.  
  235. //////////////////////////////////////////////////////////////////////////////////////////
  236. // -- // -- identify pivot high/low
  237.  
  238. //////////////////////////////
  239. // high
  240.  
  241. lengthLeftHigh := int(lengthLeftHigh * _getLengthMultiplier(true))
  242. lengthRightHigh := int(lengthRightHigh * _getLengthMultiplier(true))
  243.  
  244. series float pivotHigh = ta.pivothigh(sourceHigh, lengthLeftHigh, lengthRightHigh)
  245. pivotHighPrice := na(pivotHigh) ? pivotHighPrice[1] : sourceHigh[lengthRightHigh]
  246.  
  247. //////////////////////////////
  248. // low
  249.  
  250. lengthLeftLow := int(lengthLeftLow * _getLengthMultiplier(false))
  251. lengthRightLow := int(lengthRightLow * _getLengthMultiplier(false))
  252.  
  253. series float pivotLow = ta.pivotlow(sourceLow, lengthLeftLow, lengthRightLow)
  254. pivotLowPrice := na(pivotLow) ? pivotLowPrice[1] : sourceLow[lengthRightLow]
  255.  
  256. //////////////////////////////////////////////////////////////////////////////////////////
  257. // -- // -- initialize line storage array
  258.  
  259. //////////////////////////////
  260. // high
  261.  
  262. var array<line> pivotHighLines = array.new_line ()
  263. var array<label> pivotHighLabels = array.new_label()
  264.  
  265. //////////////////////////////
  266. // low
  267.  
  268. var array<line> pivotLowLines = array.new_line ()
  269. var array<label> pivotLowLabels = array.new_label()
  270.  
  271. //////////////////////////////////////////////////////////////////////////////////////////
  272. // -- // -- function draw pivot high/low
  273.  
  274. ////////////////////////////////////////////////////////////
  275. // @function Draw the pivot high
  276. // @param _pivotHighPrice (series float) Current pivot high price
  277.  
  278. drawPivotHigh(series float _pivotHighPrice) =>
  279.  
  280. array.push(pivotHighLines, line.new(bar_index - lengthRightHigh, _pivotHighPrice, bar_index, _pivotHighPrice
  281. , xloc.bar_index, extendHigh ? extend.right : extend.none
  282. , colorHigh, styleHigh, widthHigh))
  283.  
  284. if (showPriceHigh)
  285.  
  286. array.push(pivotHighLabels, label.new(bar_index, _pivotHighPrice, str.tostring(_pivotHighPrice, "0.00000")
  287. , xloc.bar_index, yloc.price, color.new(#ffffff, 100)
  288. , extendHigh ? label.style_label_down : label.style_label_left
  289. , colorHigh, size.normal))
  290.  
  291.  
  292. ////////////////////////////////////////////////////////////
  293. // @function Draw the pivot low
  294. // @param _pivotLowPrice (series float) Current pivot low price
  295.  
  296. drawPivotLow(series float _pivotLowPrice) =>
  297.  
  298. array.push(pivotLowLines, line.new(bar_index - lengthRightLow, _pivotLowPrice, bar_index, _pivotLowPrice
  299. , xloc.bar_index, extendLow ? extend.right : extend.none
  300. , colorLow, styleLow, widthLow))
  301.  
  302. if (showPriceLow)
  303.  
  304. array.push(pivotLowLabels, label.new(bar_index, _pivotLowPrice, str.tostring(_pivotLowPrice, "0.00000")
  305. , xloc.bar_index, yloc.price, color.new(#ffffff, 100)
  306. , extendLow ? label.style_label_up : label.style_label_left
  307. , colorLow, size.normal))
  308.  
  309.  
  310. //////////////////////////////////////////////////////////////////////////////////////////
  311. // -- // -- function update pivot high/low
  312.  
  313. ////////////////////////////////////////////////////////////
  314. // @function Update pivot high's
  315. // @param _pivotHighPrice (series float) Current pivot high price
  316.  
  317. updatePivotHigh(series float _pivotHighPrice) =>
  318.  
  319. for [index, value] in pivotHighLines
  320.  
  321. series float pivotHighPrice_ = line.get_y1(value)
  322. series int pivotHighBar_ = line.get_x1(value)
  323.  
  324. [text_, color_] = getTextColorInfo(preDayHigh, preWeekHigh, pivotHighPrice_, true, false)
  325.  
  326. if (showPriceHigh)
  327.  
  328. series label labelID_ = array.get(pivotHighLabels, index)
  329.  
  330. label.set_x (labelID_, bar_index)
  331. label.set_text (labelID_, text_ )
  332. label.set_textcolor(labelID_, color_ )
  333.  
  334. line.set_x2 (value, bar_index)
  335. line.set_color(value, color_ )
  336.  
  337. if (not na(pivotHighPrice_)) and ((sourceHigh >= pivotHighPrice_) or (sourceHigh[1] >= pivotHighPrice_))
  338.  
  339. if (showPurgedHigh)
  340.  
  341. [textP_, colorP_] = getTextColorInfo(preDayHigh, preWeekHigh, pivotHighPrice_, true, true)
  342.  
  343. if (not showPurgedPH)
  344.  
  345. colorP_ := colorPurgeHigh
  346.  
  347. line.new(pivotHighBar_ + 1, pivotHighPrice_
  348. , bar_index - 1 - (bar_index - 1 - pivotHighBar_ + 1) / 2, pivotHighPrice_
  349. , xloc.bar_index, extend.none, colorP_, styleHighP, widthHighP)
  350.  
  351. line.new(bar_index - (bar_index - 1 - pivotHighBar_ + 1) / 2 + 1, pivotHighPrice_
  352. , bar_index - 1, pivotHighPrice_
  353. , xloc.bar_index, extend.none, colorP_, styleHighP, widthHighP)
  354.  
  355. label.new(bar_index - (bar_index - 1 - pivotHighBar_ + 1) / 2, pivotHighPrice_
  356. , "x", xloc.bar_index, yloc.price, color.new(#ffffff, 100)
  357. , label.style_label_center, colorP_)
  358.  
  359. label.new(bar_index - (bar_index - 1 - pivotHighBar_ + 1) / 2, pivotHighPrice_
  360. , "x", xloc.bar_index, yloc.price, color.new(#ffffff, 100)
  361. , label.style_label_center, colorP_)
  362.  
  363. line.delete(array.remove(pivotHighLines, index))
  364.  
  365. if (showPriceHigh)
  366.  
  367. label.delete(array.remove(pivotHighLabels, index))
  368.  
  369.  
  370. ////////////////////////////////////////////////////////////
  371. // @function Update pivot low's
  372. // @param _pivotLowPrice (series float) Current pivot low price
  373.  
  374. updatePivotLow(float _pivotLowPrice) =>
  375.  
  376. for [index, value] in pivotLowLines
  377.  
  378. series float pivotLowPrice_ = line.get_y1(value)
  379. series int pivotLowBar_ = line.get_x1(value)
  380.  
  381. [text_, color_] = getTextColorInfo(preDayLow, preWeekLow, pivotLowPrice_, false, false)
  382.  
  383. if (showPriceLow)
  384.  
  385. series label labelID_ = array.get(pivotLowLabels, index)
  386.  
  387. label.set_x (labelID_, bar_index)
  388. label.set_text (labelID_, text_ )
  389. label.set_textcolor(labelID_, color_ )
  390.  
  391. line.set_x2 (value, bar_index)
  392. line.set_color(value, color_ )
  393.  
  394. if (not na(pivotLowPrice_)) and ((sourceLow <= pivotLowPrice_) or (sourceLow[1] <= pivotLowPrice_))
  395.  
  396. if (showPurgedLow)
  397.  
  398. [textP_, colorP_] = getTextColorInfo(preDayLow, preWeekLow, pivotLowPrice_, false, true)
  399.  
  400. if (not showPurgedPL)
  401.  
  402. colorP_ := colorPurgeLow
  403.  
  404. line.new(pivotLowBar_ + 1, pivotLowPrice_
  405. , bar_index - 1 - (bar_index - 1 - pivotLowBar_ + 1) / 2, pivotLowPrice_
  406. , xloc.bar_index, extend.none, colorP_, styleLowP, widthLowP)
  407.  
  408. line.new(bar_index - (bar_index - 1 - pivotLowBar_ + 1) / 2 + 1, pivotLowPrice_
  409. , bar_index - 1, pivotLowPrice_
  410. , xloc.bar_index, extend.none, colorP_, styleLowP, widthLowP)
  411.  
  412. label.new(bar_index - (bar_index - 1 - pivotLowBar_ + 1) / 2, pivotLowPrice_
  413. , "x", xloc.bar_index, yloc.price, color.new(#ffffff, 100)
  414. , label.style_label_center, colorP_)
  415.  
  416. label.new(bar_index - (bar_index - 1 - pivotLowBar_ + 1) / 2, pivotLowPrice_
  417. , "x", xloc.bar_index, yloc.price, color.new(#ffffff, 100)
  418. , label.style_label_center, colorP_)
  419.  
  420. line.delete(array.remove(pivotLowLines, index))
  421.  
  422. if (showPriceLow)
  423.  
  424. label.delete(array.remove(pivotLowLabels, index))
  425.  
  426.  
  427. //////////////////////////////////////////////////////////////////////////////////////////
  428. // -- // -- call functions
  429.  
  430. //////////////////////////////
  431. // high
  432.  
  433. if (array.size(pivotHighLines) >= 51)
  434.  
  435. line.delete(array.shift(pivotHighLines))
  436.  
  437. if (showPriceHigh)
  438. label.delete(array.shift(pivotHighLabels))
  439.  
  440. if (bool(pivotHigh)) and (pivotHighPrice != pivotHighPrice[1]) and (showPivotsHigh)
  441.  
  442. drawPivotHigh(pivotHigh)
  443.  
  444. updatePivotHigh(pivotHigh)
  445.  
  446. //////////////////////////////
  447. // low
  448.  
  449. if (array.size(pivotLowLines) >= 51)
  450.  
  451. line.delete(array.shift(pivotLowLines))
  452.  
  453. if (showPriceLow)
  454. label.delete(array.shift(pivotLowLabels))
  455.  
  456. if (bool(pivotLow)) and (pivotLowPrice != pivotLowPrice[1]) and (showPivotsLow)
  457.  
  458. drawPivotLow(pivotLow)
  459.  
  460. updatePivotLow(pivotLow)
  461.  
  462. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment