Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This source code is subject to the terms of the Mozilla Public License 2.0
- // © Mkansal
- //@version=5
- indicator('Pivot Points by kansal ', overlay=true)
- prd = input.int(defval=2, title='Pivot Point Period', minval=1, maxval=50)
- Factor = input.float(defval=3, title='ATR Factor', minval=1, step=0.1)
- Pd = input.int(defval=10, title='ATR Period', minval=1)
- showpivot = input(defval=false, title='Show Pivot Points')
- showlabel = input(defval=true, title='Show Buy/Sell Labels')
- showcl = input(defval=false, title='Show PP Center Line')
- showsr = input(defval=false, title='Show Support/Resistance')
- float ph = na
- float pl = na
- ph := ta.pivothigh(prd, prd)
- pl := ta.pivotlow(prd, prd)
- plotshape(ph and showpivot, text='H', style=shape.labeldown, color=na, textcolor=color.new(color.red, 0), location=location.abovebar, offset=-prd, transp=0)
- plotshape(pl and showpivot, text='L', style=shape.labeldown, color=na, textcolor=color.new(color.lime, 0), location=location.belowbar, offset=-prd, transp=0)
- float center = na
- center := center[1]
- float lastpp = ph ? ph : pl ? pl : na
- if lastpp
- if na(center)
- center := lastpp
- center
- else
- center := (center * 2 + lastpp) / 3
- center
- Up = center - Factor * ta.atr(Pd)
- Dn = center + Factor * ta.atr(Pd)
- float TUp = na
- float TDown = na
- Trend = 0
- TUp := close[1] > TUp[1] ? math.max(Up, TUp[1]) : Up
- TDown := close[1] < TDown[1] ? math.min(Dn, TDown[1]) : Dn
- Trend := close > TDown[1] ? 1 : close < TUp[1] ? -1 : nz(Trend[1], 1)
- Trailingsl = Trend == 1 ? TUp : TDown
- linecolor = Trend == 1 and nz(Trend[1]) == 1 ? color.lime : Trend == -1 and nz(Trend[1]) == -1 ? color.red : na
- plot(Trailingsl, color=linecolor, linewidth=2, title='PP SuperTrend')
- plot(showcl ? center : na, color=showcl ? center < hl2 ? color.blue : color.red : na, transp=0)
- bsignal = Trend == 1 and Trend[1] == -1
- ssignal = Trend == -1 and Trend[1] == 1
- plotshape(bsignal and showlabel ? Trailingsl : na, title='Buy', text='Buy', location=location.absolute, style=shape.labelup, size=size.tiny, color=color.new(color.lime, 0), textcolor=color.new(color.black, 0))
- plotshape(ssignal and showlabel ? Trailingsl : na, title='Sell', text='Sell', location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.new(color.red, 0), textcolor=color.new(color.white, 0))
- float resistance = na
- float support = na
- support := pl ? pl : support[1]
- resistance := ph ? ph : resistance[1]
- plot(showsr and support ? support : na, color=showsr and support ? color.lime : na, style=plot.style_circles, offset=-prd)
- plot(showsr and resistance ? resistance : na, color=showsr and resistance ? color.red : na, style=plot.style_circles, offset=-prd)
- alertcondition(Trend == 1 and Trend[1] == -1, title='Buy Signal', message='Buy Signal')
- alertcondition(Trend == -1 and Trend[1] == 1, title='Sell Signal', message='Sell Signal')
- alertcondition(ta.change(Trend), title='Trend Changed', message='Trend Changed')
- gr = 'LENGTH LEFT / RIGHT'
- leftLenH = input.int(title='Pivot High', defval=10, minval=1, inline='Pivot High', group=gr)
- rightLenH = input.int(title='/', defval=10, minval=1, inline='Pivot High', group=gr)
- colorH = input.color(title='', defval=#E3EFFD, inline='Pivot High', group=gr)
- leftLenL = input.int(title='Pivot Low', defval=10, minval=1, inline='Pivot Low', group=gr)
- rightLenL = input.int(title='/', defval=10, minval=1, inline='Pivot Low', group=gr)
- colorL = input.color(title='', defval=#E3EFFD, inline='Pivot Low', group=gr)
- phu = ta.pivothigh(leftLenH, rightLenH)
- pll = ta.pivotlow(leftLenL, rightLenL)
- drawLabel(_offset, _pivot, _style, _color) =>
- if not na(_pivot)
- label.new(bar_index[_offset], _pivot, str.tostring(_pivot, format.mintick), style=_style, color=_color, textcolor=#131722)
- drawLabel(rightLenH, phu, label.style_label_down, colorH)
- drawLabel(rightLenL, pll, label.style_label_up, colorL)
- depthTooltip = 'The minimum number of bars that will be taken into account when calculating the indicator.'
- depth = input.int(title='Depth', defval=10, minval=1, inline='Pivots', tooltip=depthTooltip)
- reverse = input(false, 'Reverse')
- var extendLeft = input.bool(false, 'Extend Left | Extend Right', inline='Extend Lines')
- var extendRight = input.bool(true, '', inline='Extend Lines')
- var extending = extend.none
- if extendLeft and extendRight
- extending := extend.both
- extending
- if extendLeft and not extendRight
- extending := extend.left
- extending
- if not extendLeft and extendRight
- extending := extend.right
- extending
- prices = input(true, 'Show Prices')
- levels = input.bool(true, 'Show Levels', inline='Levels')
- levelsFormat = input.string('Values', '', options=['Values', 'Percent'], inline='Levels')
- labelsPosition = input.string('Left', 'Labels Position', options=['Left', 'Right'])
- var float upperThreshold = 0.236
- var float lowerThreshold = 1.0
- var line lineLastHL = na
- var line lineLastLH = na
- var line lineLast = na
- var float iLastH = 0
- var float iLastL = 0
- var float pLastH = 0
- var float pLastL = 0
- var float[] iPivotsH = array.new_float()
- var float[] iPivotsL = array.new_float()
- var float[] pPivotsH = array.new_float()
- var float[] pPivotsL = array.new_float()
- var isHighLast = false
- var float startPrice = na
- var float endPrice = na
- var float diff = na
- pivots(src, length, isHigh) =>
- l2 = length * 2
- c = nz(src[length])
- ok = true
- for i = 0 to l2 by 1
- if isHigh and src[i] > c
- ok := false
- ok
- if not isHigh and src[i] < c
- ok := false
- ok
- if ok
- [bar_index[length], c]
- else
- [int(na), float(na)]
- [iH, pH] = pivots(high, depth / 2, true)
- [iL, pL] = pivots(low, depth / 2, false)
- countPivotsH = array.size(iPivotsH)
- countPivotsL = array.size(iPivotsL)
- if countPivotsH > 0 and countPivotsL > 0
- iLastH := array.get(iPivotsH, countPivotsH - 1)
- iLastL := array.get(iPivotsL, countPivotsL - 1)
- isHighLast := iLastH > iLastL
- iLastH := 1
- if isHighLast
- if not na(iH)
- pLastH := array.get(pPivotsH, countPivotsH - 1)
- if pH > pLastH
- array.set(iPivotsH, countPivotsH - 1, iH)
- array.set(pPivotsH, countPivotsH - 1, pH)
- iH := na
- iH
- else
- if not na(iL)
- pLastL := array.get(pPivotsL, countPivotsL - 1)
- if pL < pLastL
- array.set(iPivotsL, countPivotsL - 1, iL)
- array.set(pPivotsL, countPivotsL - 1, pL)
- iL := na
- iL
- if not na(iH)
- array.push(iPivotsH, iH)
- array.push(pPivotsH, pH)
- if not na(iL)
- array.push(iPivotsL, iL)
- array.push(pPivotsL, pL)
- pPivotsHCopy = array.copy(pPivotsH)
- pPivotsLCopy = array.copy(pPivotsL)
- iPivotsHCopy = array.copy(iPivotsH)
- iPivotsLCopy = array.copy(iPivotsL)
- if barstate.islast
- for j = bar_index to 0 by 1
- if array.size(iPivotsHCopy) == 0 or array.size(iPivotsLCopy) == 0
- break
- iLastH := array.pop(iPivotsHCopy)
- iLastL := array.pop(iPivotsLCopy)
- pLastH := array.pop(pPivotsHCopy)
- pLastL := array.pop(pPivotsLCopy)
- iPrevPivot = 0.0
- pPrevPivot = 0.0
- isHighLast := iLastH > iLastL
- if isHighLast
- for i = array.size(iPivotsHCopy) - 1 to 0 by 1
- if i < 0
- break
- else if array.get(iPivotsHCopy, i) < iLastL
- break
- if array.get(pPivotsHCopy, i) > pLastH
- iLastH := array.pop(iPivotsHCopy)
- pLastH := array.pop(pPivotsHCopy)
- pLastH
- else
- array.remove(iPivotsHCopy, i)
- array.remove(pPivotsHCopy, i)
- else
- for i = array.size(iPivotsLCopy) - 1 to 0 by 1
- if i < 0
- break
- else if array.get(iPivotsLCopy, i) < iLastH
- break
- if array.get(pPivotsLCopy, i) < pLastL
- iLastL := array.pop(iPivotsLCopy)
- pLastL := array.pop(pPivotsLCopy)
- pLastL
- else
- array.remove(iPivotsLCopy, i)
- array.remove(pPivotsLCopy, i)
- if array.size(iPivotsHCopy) == 0 or array.size(iPivotsLCopy) == 0
- break
- isHighLast := iLastH > iLastL
- if isHighLast
- iPrevPivot := array.get(iPivotsHCopy, array.size(iPivotsHCopy) - 1)
- pPrevPivot := array.get(pPivotsHCopy, array.size(iPivotsHCopy) - 1)
- pPrevPivot
- else
- iPrevPivot := array.get(iPivotsLCopy, array.size(iPivotsLCopy) - 1)
- pPrevPivot := array.get(pPivotsLCopy, array.size(iPivotsLCopy) - 1)
- pPrevPivot
- if isHighLast
- startPrice := pPrevPivot
- endPrice := pLastL
- diff := math.abs(startPrice - endPrice)
- if pLastH > endPrice + diff * lowerThreshold or pLastH < endPrice + diff * upperThreshold
- array.push(iPivotsLCopy, iLastL)
- array.push(pPivotsLCopy, pLastL)
- continue
- line.delete(lineLastHL)
- line.delete(lineLastLH)
- line.delete(lineLast)
- lineLastHL := line.new(int(iPrevPivot), pPrevPivot, int(iLastL), pLastL, color=color.red, width=1, style=line.style_dashed)
- lineLastLH := line.new(int(iLastL), pLastL, int(iLastH), pLastH, color=color.green, width=1, style=line.style_dashed)
- lineLast := lineLastLH
- lineLast
- else
- startPrice := pPrevPivot
- endPrice := pLastH
- diff := math.abs(startPrice - endPrice)
- if pLastL < endPrice - diff * lowerThreshold or pLastL > endPrice - diff * upperThreshold
- array.push(iPivotsHCopy, iLastH)
- array.push(pPivotsHCopy, pLastH)
- continue
- line.delete(lineLastHL)
- line.delete(lineLastLH)
- line.delete(lineLast)
- lineLastLH := line.new(int(iPrevPivot), pPrevPivot, int(iLastH), pLastH, color=color.red, width=1, style=line.style_dashed)
- lineLastHL := line.new(int(iLastH), pLastH, int(iLastL), pLastL, color=color.green, width=1, style=line.style_dashed)
- lineLast := lineLastHL
- lineLast
- break
- _draw_line(price, col) =>
- var id = line.new(0, price, 0, price, color=col, width=1, extend=extending)
- if not na(lineLast)
- line.set_xy1(id, line.get_x1(lineLast), price)
- line.set_xy2(id, line.get_x2(lineLast), price)
- _draw_label(price, txt, txtColor) =>
- if not na(price)
- x = labelsPosition == 'Left' ? line.get_x1(lineLast) : not extendRight ? line.get_x2(lineLast) : bar_index
- labelStyle = labelsPosition == 'Left' ? label.style_label_right : label.style_label_left
- align = labelsPosition == 'Left' ? text.align_right : text.align_left
- labelsAlignStrLeft = txt + '\n \n'
- labelsAlignStrRight = ' ' + txt + '\n \n'
- labelsAlignStr = labelsPosition == 'Left' ? labelsAlignStrLeft : labelsAlignStrRight
- var id = label.new(x=x, y=price, text=labelsAlignStr, textcolor=txtColor, style=labelStyle, textalign=align, color=#00000000)
- label.set_xy(id, x, price)
- label.set_text(id, labelsAlignStr)
- label.set_textcolor(id, txtColor)
- _wrap(txt) =>
- '(' + str.tostring(txt, '#.##') + ')'
- _label_txt(level, price) =>
- if not na(price)
- l = levelsFormat == 'Values' ? str.tostring(level) : str.tostring(level * 100) + '%'
- (levels ? l : '') + (prices ? _wrap(price) : '')
- _crossing_level(sr, r) =>
- r > sr and r < sr[1] or r < sr and r > sr[1]
- diff := (isHighLast ? -1 : 1) * math.abs(startPrice - endPrice)
- offset = isHighLast ? line.get_y1(lineLastLH) - line.get_y2(lineLastLH) : line.get_y1(lineLastHL) - line.get_y2(lineLastHL)
- offset := (isHighLast ? -1 : 1) * math.abs(offset)
- processLevel(show, value, colorL) =>
- float m = value
- r = endPrice - offset + (reverse ? -1 : 1) * diff * m
- if show
- _draw_line(r, colorL)
- _draw_label(r, _label_txt(m, r), colorL)
- if _crossing_level(close, r)
- alert('Autofib: ' + syminfo.ticker + ' crossing level ' + str.tostring(value))
- show_0 = input.bool(true, '', inline='Level0')
- value_0 = input.int(0, '', inline='Level0')
- color_0 = input.color(#787b86, '', inline='Level0')
- processLevel(show_0, value_0, color_0)
- show_0_236 = input.bool(true, '', inline='Level0')
- value_0_236 = input.float(0.236, '', inline='Level0')
- color_0_236 = input.color(#f44336, '', inline='Level0')
- processLevel(show_0_236, value_0_236, color_0_236)
- show_0_382 = input.bool(true, '', inline='Level1')
- value_0_382 = input.float(0.382, '', inline='Level1')
- color_0_382 = input.color(#81c784, '', inline='Level1')
- processLevel(show_0_382, value_0_382, color_0_382)
- show_0_5 = input.bool(true, '', inline='Level1')
- value_0_5 = input.float(0.5, '', inline='Level1')
- color_0_5 = input.color(#4caf50, '', inline='Level1')
- processLevel(show_0_5, value_0_5, color_0_5)
- show_0_618 = input.bool(true, '', inline='Level2')
- value_0_618 = input.float(0.618, '', inline='Level2')
- color_0_618 = input.color(#009688, '', inline='Level2')
- processLevel(show_0_618, value_0_618, color_0_618)
- show_0_65 = input.bool(false, '', inline='Level2')
- value_0_65 = input.float(0.65, '', inline='Level2')
- color_0_65 = input.color(#009688, '', inline='Level2')
- processLevel(show_0_65, value_0_65, color_0_65)
- show_0_786 = input.bool(true, '', inline='Level3')
- value_0_786 = input.float(0.786, '', inline='Level3')
- color_0_786 = input.color(#64b5f6, '', inline='Level3')
- processLevel(show_0_786, value_0_786, color_0_786)
- show_1 = input.bool(true, '', inline='Level3')
- value_1 = input.int(1, '', inline='Level3')
- color_1 = input.color(#787b86, '', inline='Level3')
- processLevel(show_1, value_1, color_1)
- show_1_272 = input.bool(false, '', inline='Level4')
- value_1_272 = input.float(1.272, '', inline='Level4')
- color_1_272 = input.color(#81c784, '', inline='Level4')
- processLevel(show_1_272, value_1_272, color_1_272)
- show_1_414 = input.bool(false, '', inline='Level4')
- value_1_414 = input.float(1.414, '', inline='Level4')
- color_1_414 = input.color(#f44336, '', inline='Level4')
- processLevel(show_1_414, value_1_414, color_1_414)
- show_1_618 = input.bool(true, '', inline='Level5')
- value_1_618 = input.float(1.618, '', inline='Level5')
- color_1_618 = input.color(#2196f3, '', inline='Level5')
- processLevel(show_1_618, value_1_618, color_1_618)
- show_1_65 = input.bool(false, '', inline='Level5')
- value_1_65 = input.float(1.65, '', inline='Level5')
- color_1_65 = input.color(#2196f3, '', inline='Level5')
- processLevel(show_1_65, value_1_65, color_1_65)
- show_2_618 = input.bool(true, '', inline='Level6')
- value_2_618 = input.float(2.618, '', inline='Level6')
- color_2_618 = input.color(#f44336, '', inline='Level6')
- processLevel(show_2_618, value_2_618, color_2_618)
- show_2_65 = input.bool(false, '', inline='Level6')
- value_2_65 = input.float(2.65, '', inline='Level6')
- color_2_65 = input.color(#f44336, '', inline='Level6')
- processLevel(show_2_65, value_2_65, color_2_65)
- show_3_618 = input.bool(true, '', inline='Level7')
- value_3_618 = input.float(3.618, '', inline='Level7')
- color_3_618 = input.color(#9c27b0, '', inline='Level7')
- processLevel(show_3_618, value_3_618, color_3_618)
- show_3_65 = input.bool(false, '', inline='Level7')
- value_3_65 = input.float(3.65, '', inline='Level7')
- color_3_65 = input.color(#9c27b0, '', inline='Level7')
- processLevel(show_3_65, value_3_65, color_3_65)
- show_4_236 = input.bool(true, '', inline='Level8')
- value_4_236 = input.float(4.236, '', inline='Level8')
- color_4_236 = input.color(#e91e63, '', inline='Level8')
- processLevel(show_4_236, value_4_236, color_4_236)
- show_4_618 = input.bool(false, '', inline='Level8')
- value_4_618 = input.float(4.618, '', inline='Level8')
- color_4_618 = input.color(#81c784, '', inline='Level8')
- processLevel(show_4_618, value_4_618, color_4_618)
- show_neg_0_236 = input.bool(false, '', inline='Level9')
- value_neg_0_236 = input.float(-0.236, '', inline='Level9')
- color_neg_0_236 = input.color(#f44336, '', inline='Level9')
- processLevel(show_neg_0_236, value_neg_0_236, color_neg_0_236)
- show_neg_0_382 = input.bool(false, '', inline='Level9')
- value_neg_0_382 = input.float(-0.382, '', inline='Level9')
- color_neg_0_382 = input.color(#81c784, '', inline='Level9')
- processLevel(show_neg_0_382, value_neg_0_382, color_neg_0_382)
- show_neg_0_618 = input.bool(false, '', inline='Level10')
- value_neg_0_618 = input.float(-0.618, '', inline='Level10')
- color_neg_0_618 = input.color(#009688, '', inline='Level10')
- processLevel(show_neg_0_618, value_neg_0_618, color_neg_0_618)
- show_neg_0_65 = input.bool(false, '', inline='Level10')
- value_neg_0_65 = input.float(-0.65, '', inline='Level10')
- color_neg_0_65 = input.color(#009688, '', inline='Level10')
- processLevel(show_neg_0_65, value_neg_0_65, color_neg_0_65)
Advertisement
Add Comment
Please, Sign In to add comment