Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=4
- study("Last 3", "", true)
- labelsOn = input(true)
- lookback = input(50)
- // Saved pivot levels.
- var float tph1 = 0.
- var float tph2 = 0.
- var float tph3 = 0.
- var float tpl1 = low
- var float tpl2 = low
- var float tpl3 = low
- // bar_index where pivot was detected.
- var int tph1B = na
- var int tph2B = na
- var int tph3B = na
- var int tpl1B = na
- var int tpl2B = na
- var int tpl3B = na
- // Here goes whatever method you use to find new pivots.
- p = 3
- pH = pivothigh(p, p)
- pL = pivotlow(p, p)
- // Detect new pivot.
- newPH = not na(pH)
- newPL = not na(pL)
- // Current upper/lower bounds
- var float p2a = na
- var float p1a = na
- // 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.
- f_replacePH(_lastP) =>
- // _lasP: last active pivot just about to be replaced by a newly found pivot.
- _r = 0
- // Skip first pivot found because no previous one exists.
- if not na(_lastP)
- // Handle first bars or cases where the pivot if too far away so requires replacement.
- if bar_index - tph3B > lookback or na(tph3B)
- _r := 3
- else
- if bar_index - tph2B > lookback or na(tph2B)
- _r := 2
- else
- if bar_index - tph1B > lookback or na(tph1B)
- _r := 1
- // 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.
- if _r == 0
- _delta = 0.
- if _lastP > nz(tph1)
- _r := 1
- _delta := _lastP - tph1
- if _lastP > nz(tph2) and _lastP - tph2 > _delta
- _r := 2
- _delta := _lastP - tph2
- if _lastP > nz(tph3) and _lastP - tph3 > _delta
- _r := 3
- _r
- f_replacePL(_lastP) =>
- _r = 0
- if not na(_lastP)
- if bar_index - tpl3B > lookback or na(tpl3B)
- _r := 3
- else
- if bar_index - tpl2B > lookback or na(tpl2B)
- _r := 2
- else
- if bar_index - tpl1B > lookback or na(tpl1B)
- _r := 1
- if _r == 0
- _delta = 0.
- if _lastP < nz(tpl1)
- _r := 1
- _delta := tpl1 - _lastP
- if _lastP < nz(tpl2) and tpl2 - _lastP > _delta
- _r := 2
- _delta := tpl2 - _lastP
- if _lastP < nz(tpl3) and tpl3 - _lastP > _delta
- _r := 3
- _r
- // We will save no of replaced level for debugging.
- rH = 0
- rL = 0
- // A new pivot is detected, update old levels and current pivot.
- if newPH
- // 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.
- rH := f_replacePH(p2a)
- // rL := f_replacePL(p2a)
- // Check if a high level requires replacement.
- if rH == 1
- tph1 := nz(p2a)
- tph1B := bar_index
- else
- if rH == 2
- tph2 := nz(p2a)
- tph2B := bar_index
- else
- if rH == 3
- tph3 := nz(p2a)
- tph3B := bar_index
- else
- // Check if a low level requires replacement.
- if rL == 1
- tpl1 := nz(p2a)
- tpl1B := bar_index
- else
- if rL == 2
- tpl2 := nz(p2a)
- tpl2B := bar_index
- else
- if rL == 3
- tpl3 := nz(p2a)
- tpl3B := bar_index
- // Save new high pivot found.
- p2a := pH
- else
- if newPL
- // rH := f_replacePH(p1a)
- rL := f_replacePL(p1a)
- // Check if a high level requires replacement.
- if rH == 1
- tph1 := nz(p1a)
- tph1B := bar_index
- else
- if rH == 2
- tph2 := nz(p1a)
- tph2B := bar_index
- else
- if rH == 3
- tph3 := nz(p1a)
- tph3B := bar_index
- else
- // Check if a low level requires replacement.
- if rL == 1
- tpl1 := nz(p1a)
- tpl1B := bar_index
- else
- if rL == 2
- tpl2 := nz(p1a)
- tpl2B := bar_index
- else
- if rL == 3
- tpl3 := nz(p1a)
- tpl3B := bar_index
- // Save new low pivot found.
- p1a := pL
- // Plot current pivots
- plot(p2a, "p2a", not newPH ? color.green : na)
- plot(p1a, "p1a", not newPL ? color.maroon : na)
- // 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.
- plot(bar_index - tph1B < lookback and tph1 > p2a ? tph1 : na, "tph1", color.green, 2, plot.style_circles)
- plot(bar_index - tph2B < lookback and tph2 > p2a ? tph2 : na, "tph2", color.green, 3, plot.style_circles)
- plot(bar_index - tph3B < lookback and tph3 > p2a ? tph3 : na, "tph3", color.green, 4, plot.style_circles)
- plot(bar_index - tpl1B < lookback and tpl1 < p1a ? tpl1 : na, "tpl1", color.maroon, 2, plot.style_circles)
- plot(bar_index - tpl2B < lookback and tpl2 < p1a ? tpl2 : na, "tpl2", color.maroon, 3, plot.style_circles)
- plot(bar_index - tpl3B < lookback and tpl3 < p1a ? tpl3 : na, "tpl3", color.maroon, 4, plot.style_circles)
- // Debugging plots.
- plotchar(tph1 , "tph1 ", "", location.top)
- plotchar(tph2 , "tph2 ", "", location.top)
- plotchar(tph3 , "tph3 ", "", location.top)
- plotchar(tph1B, "tph1B", "", location.top)
- plotchar(tph2B, "tph2B", "", location.top)
- plotchar(tph3B, "tph3B", "", location.top)
- plotchar(tpl1 , "tpl1 ", "", location.top)
- plotchar(tpl2 , "tpl2 ", "", location.top)
- plotchar(tpl3 , "tpl3 ", "", location.top)
- plotchar(tpl1B, "tpl1B", "", location.top)
- plotchar(tpl2B, "tpl2B", "", location.top)
- plotchar(tpl3B, "tpl3B", "", location.top)
- plotchar(rH, "rH", "", location.top)
- plotchar(rL, "rL", "", location.top)
- if newPH and labelsOn
- label.new(bar_index, p2a,
- "Previous P: " + tostring(p2a[1]) +
- "\nCurrent P: " + tostring(p2a) +
- "\nBar: " + tostring(bar_index) +
- "\nReplaces: " + tostring(rH) +
- "\ntph1: " + tostring(tph1[1]) + "(" + tostring(bar_index - tph1B[1]) + ")►" + tostring(tph1) +
- "\ntph2: " + tostring(tph2[1]) + "(" + tostring(bar_index - tph2B[1]) + ")►" + tostring(tph2) +
- "\ntph3: " + tostring(tph3[1]) + "(" + tostring(bar_index - tph3B[1]) + ")►" + tostring(tph3),
- color = #00000000,
- style = label.style_labeldown,
- textcolor = color.lime,
- textalign = text.align_left)
- if newPL and labelsOn
- label.new(bar_index, p1a,
- "Previous P: " + tostring(p1a[1]) +
- "\nCurrent P: " + tostring(p1a) +
- "\nBar: " + tostring(bar_index) +
- "\nReplaces: " + tostring(rL) +
- "\ntpl1: " + tostring(tpl1[1]) + "(" + tostring(bar_index - tpl1B[1]) + ")►" + tostring(tpl1) +
- "\ntpl2: " + tostring(tpl2[1]) + "(" + tostring(bar_index - tpl2B[1]) + ")►" + tostring(tpl2) +
- "\ntpl3: " + tostring(tpl3[1]) + "(" + tostring(bar_index - tpl3B[1]) + ")►" + tostring(tpl3),
- color = #00000000,
- style = label.style_labelup,
- textcolor = color.red,
- textalign = text.align_left)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement