Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // © Rathack
- // edited by @taylorbray
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // -- // -- // -- Ultra High/Low
- //////////////////////////////////////////////////////////////////////////////////////////
- // -- // --
- ////////////////////////////////////////////////////////////
- // --
- //////////////////////////////
- //
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //@version=5
- indicator("Pivot Highs/Lows", overlay = true, max_lines_count = 500, max_labels_count = 500)
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // -- // -- // -- user inputs
- //////////////////////////////////////////////////////////////////////////////////////////
- // -- // -- pivot high
- // edit by @taylorbray
- string group1 = "////////////////////////// HIGH //////////////////////////"
- bool showPivotsHigh = input.bool (true , "" , inline = "h1", group = group1)
- float sourceHigh = input.source (high , "Pivot High | Source" , inline = "h1", group = group1)
- int lengthLeftHigh = input.int (10 , "Length Left" , minval = 1 , inline = "h2", group = group1)
- int lengthRightHigh = input.int (10 , "Right" , minval = 1 , inline = "h2", group = group1)
- string strStyleHigh = input.string ("―――" , "Style", ["―――", "---", "•••"] , inline = "h3", group = group1)
- int widthHigh = input.int (1 , "" , minval = 1 , inline = "h3", group = group1)
- color colorHigh = input.color (#72727296, "" , inline = "h3", group = group1)
- bool showPriceHigh = input.bool (true , "Show Price" , inline = "h4", group = group1)
- bool extendHigh = input.bool (false , "Extend Lines" , inline = "h4", group = group1)
- bool showPurgedHigh = input.bool (true , "Purged" , inline = "h5", group = group1)
- string strStyleHighP = input.string ("―――" , "" , ["―――", "---", "•••"] , inline = "h5", group = group1)
- int widthHighP = input.int (1 , "" , minval = 1 , inline = "h5", group = group1)
- color colorPurgeHigh = input.color (#72727296, "" , inline = "h5", group = group1)
- string timeframeHigh = input.timeframe ("" , "Timeframe", tooltip = "Don't use a too low tf" , group = group1)
- //////////////////////////////////////////////////////////////////////////////////////////
- // -- // -- pivot low
- string group2 = "////////////////////////// LOW //////////////////////////"
- bool showPivotsLow = input.bool (true , "" , inline = "l1", group = group2)
- float sourceLow = input.source (low , "Pivot Low | Source" , inline = "l1", group = group2)
- int lengthLeftLow = input.int (10 , "Length Left" , minval = 1 , inline = "l2", group = group2)
- int lengthRightLow = input.int (10 , "Right" , minval = 1 , inline = "l2", group = group2)
- string strStyleLow = input.string ("―――" , "Style", [ "―――", "---", "•••"] , inline = "l3", group = group2)
- int widthLow = input.int (1 , "" , minval = 1 , inline = "l3", group = group2)
- color colorLow = input.color (#72727296, "" , inline = "l3", group = group2)
- bool showPriceLow = input.bool (true , "Show Price" , inline = "l4", group = group2)
- bool extendLow = input.bool (false , "Extend Lines" , inline = "l4", group = group2)
- bool showPurgedLow = input.bool (true , "Purged" , inline = "l5", group = group2)
- string strStyleLowP = input.string ("―――" , "" , [ "―――", "---", "•••"] , inline = "l5", group = group2)
- int widthLowP = input.int (1 , "" , minval = 1 , inline = "l5", group = group2)
- color colorPurgeLow = input.color (#72727296, "" , inline = "l5", group = group2)
- string timeframeLow = input.timeframe ("" , "Timeframe", tooltip = "Don't use a too low tf" , group = group2)
- //////////////////////////////////////////////////////////////////////////////////////////
- // -- // -- previous high/low
- string group3 = "/////////////////// PREVIOUS HIGH/LOW ///////////////////"
- bool showPreDayHL = input.bool (false , "Previous Day High/Low" , inline = "p1", group = group3)
- color colorPreDayH = input.color (color.new(#ff9100, 0), "" , inline = "p1", group = group3)
- color colorPreDayL = input.color (color.new(#0084ff, 0), "" , inline = "p1", group = group3)
- bool showPreWeekHL = input.bool (false , "Previous Week High/Low" , inline = "p2", group = group3)
- color colorPreWeekH = input.color (color.new(#ff9100, 0), "" , inline = "p2", group = group3)
- color colorPreWeekL = input.color (color.new(#0084ff, 0), "" , inline = "p2", group = group3)
- color colorPreOverH = input.color (color.new(#ffffff, 0), "Color High/Low for Overlap" , inline = "p3", group = group3)
- color colorPreOverL = input.color (color.new(#ffffff, 0), "" , inline = "p3", group = group3)
- bool showPurgedPH = input.bool (true , "Highlight Purged Previous High" , group = group3)
- bool showPurgedPL = input.bool (true , "Highlight Purged Previous Low" , group = group3)
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // -- // -- // -- functions
- ////////////////////////////////////////////////////////////
- // @function Get the length multiplier
- // @param _high (series bool) true == pivot high false == pivot low
- // @returns length multiplier (series float)
- _getLengthMultiplier(series bool _high) =>
- series int timeframeSeconds_ = timeframe.in_seconds(timeframe.period)
- series int timeframeHighSeconds_ = timeframe.in_seconds(timeframeHigh )
- series int timeframeLowSeconds_ = timeframe.in_seconds(timeframeLow )
- series float lengthMultiplier_ = _high ? timeframeHighSeconds_ / timeframeSeconds_
- : timeframeLowSeconds_ / timeframeSeconds_
- lengthMultiplier_
- ////////////////////////////////////////////////////////////
- // @function Check if two variables are the same
- // @param _preValue The first variable
- // @param _value The second variable
- // @returns true or false
- isPreValue(_preValue, _value) =>
- _preValue == _value ? true : false
- ////////////////////////////////////////////////////////////
- // @function Get the label text/color for Pre High/Low
- // @param _preMajorDay (series float) previous day high/low
- // @param _preMajorWeek (series float) previous Week high/low
- // @param _pivotPrice (series float) actual pivot price
- // @param _isHigh (series bool) true == high false == low
- // @param _isPurged (series bool) true == purged
- // @returns [text_, color_] label text, line/label color
- getTextColorInfo(series float _preMajorDay , series float _preMajorWeek , series float _pivotPrice
- , series bool _isHigh , series bool _isPurged ) =>
- series string text_ = ""
- series color color_ = na
- series string sPreDay_ = _isHigh ? " PDH" : " PDL"
- series string sPreWeek_ = _isHigh ? " PWH" : " PWL"
- series color cPreDay_ = _isHigh ? colorPreDayH : colorPreDayL
- series color cPreWeek_ = _isHigh ? colorPreWeekH : colorPreWeekL
- series color cPreOver_ = _isHigh ? colorPreOverH : colorPreOverL
- series color cColor_ = (_isHigh and _isPurged) ? colorPurgeHigh
- : (not _isHigh and _isPurged) ? colorPurgeLow
- : (_isHigh) ? colorHigh
- : colorLow
- series bool isPreDay = isPreValue(_preMajorDay , _pivotPrice)
- series bool isPreWeek = isPreValue(_preMajorWeek, _pivotPrice)
- if (showPreDayHL and showPreWeekHL)
- text_ := isPreDay ? str.tostring(_pivotPrice, "0.00000") + sPreDay_
- : isPreWeek ? str.tostring(_pivotPrice, "0.00000") + sPreWeek_
- : str.tostring(_pivotPrice, "0.00000")
- if (isPreDay) and (isPreWeek)
- text_ := str.tostring(_pivotPrice, "0.00000") + sPreDay_ + " &" + sPreWeek_
- color_ := isPreDay ? cPreDay_
- : isPreWeek ? cPreWeek_
- : cColor_
- if (isPreDay) and (isPreWeek)
- color_ := cPreOver_
- else if (showPreDayHL and not showPreWeekHL)
- text_ := isPreDay ? str.tostring(_pivotPrice, "0.00000") + sPreDay_
- : str.tostring(_pivotPrice, "0.00000")
- color_ := isPreDay ? cPreDay_
- : cColor_
- else if (not showPreDayHL and showPreWeekHL)
- text_ := isPreWeek ? str.tostring(_pivotPrice, "0.00000") + sPreWeek_
- : str.tostring(_pivotPrice, "0.00000")
- color_ := isPreWeek ? cPreWeek_
- : cColor_
- else
- text_ := str.tostring(_pivotPrice, "0.00000")
- color_ := cColor_
- [text_, color_]
- ////////////////////////////////////////////////////////////
- // @function Convert a string in line style
- // @param _string (series string) The String to convert
- // @returns line.style_dashed
- // or line.style_dotted
- // or line.style_solid
- getLineStyle(series string _string) =>
- switch _string
- "---" => line.style_dashed
- "•••" => line.style_dotted
- "―――" => line.style_solid
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // -- // -- // -- calculation
- //////////////////////////////
- // high
- series string styleHigh = getLineStyle(strStyleHigh )
- series string styleHighP = getLineStyle(strStyleHighP)
- series float pivotHighPrice = 0.0
- //////////////////////////////
- // low
- series string styleLow = getLineStyle(strStyleLow )
- series string styleLowP = getLineStyle(strStyleLowP)
- series float pivotLowPrice = 0.0
- //////////////////////////////////////////////////////////////////////////////////////////
- // -- // -- identify previous day/week high/low
- //////////////////////////////
- // high
- series float preDayHigh = request.security(syminfo.tickerid, "D", high[1])
- series float preWeekHigh = request.security(syminfo.tickerid, "W", high[1])
- //////////////////////////////
- // low
- series float preDayLow = request.security(syminfo.tickerid, "D", low[1])
- series float preWeekLow = request.security(syminfo.tickerid, "W", low[1])
- //////////////////////////////////////////////////////////////////////////////////////////
- // -- // -- identify pivot high/low
- //////////////////////////////
- // high
- lengthLeftHigh := int(lengthLeftHigh * _getLengthMultiplier(true))
- lengthRightHigh := int(lengthRightHigh * _getLengthMultiplier(true))
- series float pivotHigh = ta.pivothigh(sourceHigh, lengthLeftHigh, lengthRightHigh)
- pivotHighPrice := na(pivotHigh) ? pivotHighPrice[1] : sourceHigh[lengthRightHigh]
- //////////////////////////////
- // low
- lengthLeftLow := int(lengthLeftLow * _getLengthMultiplier(false))
- lengthRightLow := int(lengthRightLow * _getLengthMultiplier(false))
- series float pivotLow = ta.pivotlow(sourceLow, lengthLeftLow, lengthRightLow)
- pivotLowPrice := na(pivotLow) ? pivotLowPrice[1] : sourceLow[lengthRightLow]
- //////////////////////////////////////////////////////////////////////////////////////////
- // -- // -- initialize line storage array
- //////////////////////////////
- // high
- var array<line> pivotHighLines = array.new_line ()
- var array<label> pivotHighLabels = array.new_label()
- //////////////////////////////
- // low
- var array<line> pivotLowLines = array.new_line ()
- var array<label> pivotLowLabels = array.new_label()
- //////////////////////////////////////////////////////////////////////////////////////////
- // -- // -- function draw pivot high/low
- ////////////////////////////////////////////////////////////
- // @function Draw the pivot high
- // @param _pivotHighPrice (series float) Current pivot high price
- drawPivotHigh(series float _pivotHighPrice) =>
- array.push(pivotHighLines, line.new(bar_index - lengthRightHigh, _pivotHighPrice, bar_index, _pivotHighPrice
- , xloc.bar_index, extendHigh ? extend.right : extend.none
- , colorHigh, styleHigh, widthHigh))
- if (showPriceHigh)
- array.push(pivotHighLabels, label.new(bar_index, _pivotHighPrice, str.tostring(_pivotHighPrice, "0.00000")
- , xloc.bar_index, yloc.price, color.new(#ffffff, 100)
- , extendHigh ? label.style_label_down : label.style_label_left
- , colorHigh, size.normal))
- ////////////////////////////////////////////////////////////
- // @function Draw the pivot low
- // @param _pivotLowPrice (series float) Current pivot low price
- drawPivotLow(series float _pivotLowPrice) =>
- array.push(pivotLowLines, line.new(bar_index - lengthRightLow, _pivotLowPrice, bar_index, _pivotLowPrice
- , xloc.bar_index, extendLow ? extend.right : extend.none
- , colorLow, styleLow, widthLow))
- if (showPriceLow)
- array.push(pivotLowLabels, label.new(bar_index, _pivotLowPrice, str.tostring(_pivotLowPrice, "0.00000")
- , xloc.bar_index, yloc.price, color.new(#ffffff, 100)
- , extendLow ? label.style_label_up : label.style_label_left
- , colorLow, size.normal))
- //////////////////////////////////////////////////////////////////////////////////////////
- // -- // -- function update pivot high/low
- ////////////////////////////////////////////////////////////
- // @function Update pivot high's
- // @param _pivotHighPrice (series float) Current pivot high price
- updatePivotHigh(series float _pivotHighPrice) =>
- for [index, value] in pivotHighLines
- series float pivotHighPrice_ = line.get_y1(value)
- series int pivotHighBar_ = line.get_x1(value)
- [text_, color_] = getTextColorInfo(preDayHigh, preWeekHigh, pivotHighPrice_, true, false)
- if (showPriceHigh)
- series label labelID_ = array.get(pivotHighLabels, index)
- label.set_x (labelID_, bar_index)
- label.set_text (labelID_, text_ )
- label.set_textcolor(labelID_, color_ )
- line.set_x2 (value, bar_index)
- line.set_color(value, color_ )
- if (not na(pivotHighPrice_)) and ((sourceHigh >= pivotHighPrice_) or (sourceHigh[1] >= pivotHighPrice_))
- if (showPurgedHigh)
- [textP_, colorP_] = getTextColorInfo(preDayHigh, preWeekHigh, pivotHighPrice_, true, true)
- if (not showPurgedPH)
- colorP_ := colorPurgeHigh
- line.new(pivotHighBar_ + 1, pivotHighPrice_
- , bar_index - 1 - (bar_index - 1 - pivotHighBar_ + 1) / 2, pivotHighPrice_
- , xloc.bar_index, extend.none, colorP_, styleHighP, widthHighP)
- line.new(bar_index - (bar_index - 1 - pivotHighBar_ + 1) / 2 + 1, pivotHighPrice_
- , bar_index - 1, pivotHighPrice_
- , xloc.bar_index, extend.none, colorP_, styleHighP, widthHighP)
- label.new(bar_index - (bar_index - 1 - pivotHighBar_ + 1) / 2, pivotHighPrice_
- , "x", xloc.bar_index, yloc.price, color.new(#ffffff, 100)
- , label.style_label_center, colorP_)
- label.new(bar_index - (bar_index - 1 - pivotHighBar_ + 1) / 2, pivotHighPrice_
- , "x", xloc.bar_index, yloc.price, color.new(#ffffff, 100)
- , label.style_label_center, colorP_)
- line.delete(array.remove(pivotHighLines, index))
- if (showPriceHigh)
- label.delete(array.remove(pivotHighLabels, index))
- ////////////////////////////////////////////////////////////
- // @function Update pivot low's
- // @param _pivotLowPrice (series float) Current pivot low price
- updatePivotLow(float _pivotLowPrice) =>
- for [index, value] in pivotLowLines
- series float pivotLowPrice_ = line.get_y1(value)
- series int pivotLowBar_ = line.get_x1(value)
- [text_, color_] = getTextColorInfo(preDayLow, preWeekLow, pivotLowPrice_, false, false)
- if (showPriceLow)
- series label labelID_ = array.get(pivotLowLabels, index)
- label.set_x (labelID_, bar_index)
- label.set_text (labelID_, text_ )
- label.set_textcolor(labelID_, color_ )
- line.set_x2 (value, bar_index)
- line.set_color(value, color_ )
- if (not na(pivotLowPrice_)) and ((sourceLow <= pivotLowPrice_) or (sourceLow[1] <= pivotLowPrice_))
- if (showPurgedLow)
- [textP_, colorP_] = getTextColorInfo(preDayLow, preWeekLow, pivotLowPrice_, false, true)
- if (not showPurgedPL)
- colorP_ := colorPurgeLow
- line.new(pivotLowBar_ + 1, pivotLowPrice_
- , bar_index - 1 - (bar_index - 1 - pivotLowBar_ + 1) / 2, pivotLowPrice_
- , xloc.bar_index, extend.none, colorP_, styleLowP, widthLowP)
- line.new(bar_index - (bar_index - 1 - pivotLowBar_ + 1) / 2 + 1, pivotLowPrice_
- , bar_index - 1, pivotLowPrice_
- , xloc.bar_index, extend.none, colorP_, styleLowP, widthLowP)
- label.new(bar_index - (bar_index - 1 - pivotLowBar_ + 1) / 2, pivotLowPrice_
- , "x", xloc.bar_index, yloc.price, color.new(#ffffff, 100)
- , label.style_label_center, colorP_)
- label.new(bar_index - (bar_index - 1 - pivotLowBar_ + 1) / 2, pivotLowPrice_
- , "x", xloc.bar_index, yloc.price, color.new(#ffffff, 100)
- , label.style_label_center, colorP_)
- line.delete(array.remove(pivotLowLines, index))
- if (showPriceLow)
- label.delete(array.remove(pivotLowLabels, index))
- //////////////////////////////////////////////////////////////////////////////////////////
- // -- // -- call functions
- //////////////////////////////
- // high
- if (array.size(pivotHighLines) >= 51)
- line.delete(array.shift(pivotHighLines))
- if (showPriceHigh)
- label.delete(array.shift(pivotHighLabels))
- if (bool(pivotHigh)) and (pivotHighPrice != pivotHighPrice[1]) and (showPivotsHigh)
- drawPivotHigh(pivotHigh)
- updatePivotHigh(pivotHigh)
- //////////////////////////////
- // low
- if (array.size(pivotLowLines) >= 51)
- line.delete(array.shift(pivotLowLines))
- if (showPriceLow)
- label.delete(array.shift(pivotLowLabels))
- if (bool(pivotLow)) and (pivotLowPrice != pivotLowPrice[1]) and (showPivotsLow)
- drawPivotLow(pivotLow)
- updatePivotLow(pivotLow)
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment