// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ashwagamma
//@version=5
indicator("myFibPivots", overlay=true, max_bars_back = 1000, max_lines_count = 100)
quantizeToTick(float val) =>
float(int(val/syminfo.mintick+0.5))*syminfo.mintick
fibrange(pv, h, l, ratio) =>
x = pv + (h - l) * ratio
quantizeToTick(x)
// Function prints a message at the bottom-right of the chart.
f_print(_text) =>
var table _t = table.new(position.bottom_right, 1, 1)
table.cell(_t, 0, 0, str.tostring(_text), bgcolor = color.yellow)
//
updateLineLabel(arrLine, arrLabel, idx, startTime, endTime, val) =>
line _line = na
label _label = na
_line := array.get(arrLine, idx)
_line.set_xy1(startTime, val)
_line.set_xy2(endTime, val)
_label := array.get(arrLabel, idx)
_label.set_xy(endTime, val)
_label.set_text(str.tostring(val))
//Pivot Points
pivotTimeFrame = input.timeframe("1D", group = "Main Settings")
showlastLTF = input(title='Show Only Last Period', defval=true, group = "Main Settings")
showLatestCPR = input(title='Show Latest CPR', defval=true, group = "Main Settings")
showRetiredCPR = input(title='Show Retired CPR', defval=true, group = "Main Settings")
showRolling = input(title='Show Rolling', defval=true, group = "Main Settings")
plotAllHistoricalLines = input(title='Plot Historical', defval=false, group = "Main Settings")
colorRes = input.color(color.orange, title = "Resistance Color", group = "Style Settings")
colorSup = input.color(color.orange, title = "Support Color", group = "Style Settings")
colorPivot = input.color(color.blue, title = "Pivot Color", group = "Style Settings")
colorCPR = input.color(color.blue, title = "CPR Color", group = "Style Settings")
widthPVP = input.int(3, minval=1, title = "Pivot Line Width", group = "Style Settings")
width = input.int(2, minval=1, title = "Line Width", group = "Style Settings")
widthMinor = input.int(1, minval=1, title = "Line Width Minor", group = "Style Settings")
trans = input.int(0, minval=0, title = "Line Transparency", group = "Style Settings")
transCPR = input.int(80, minval=1, title = "CPR Transparency", group = "Style Settings")
transRetiredCPR = input.int(90, minval=1, title = "Retired CPR Transparency", group = "Style Settings")
transMinor = input.int(0, minval=0, title = "Line Transparency Minor", group = "Style Settings")
var float rollingHigh = na
var float rollingLow = na
var float closeBarLastTF = na
var float rollingLowTFChg = na
var float rollingHighTFChg = na
float[] fibList = array.from(0.236, 0.382, 0.5, 0.618, 0.786, 1, 1.272, 1.382, 1.618, 2)
int[] transp = array.from(25, 0, 25, 0, 25, 0, 25, 65, 65, 65)
string[] sizeLabel = array.from(size.small, size.normal, size.small, size.normal, size.small, size.normal, size.small, size.small, size.small, size.small)
int[] transpRollingDefault = array.from(40, 25, 65, 25, 65, 25, 65, 65, 65, 65)
int[] transpHide = array.from(100, 100, 100, 100, 100, 100, 100, 100, 100, 100)
int[] transpRollingLabelDefault = array.from(65, 65, 100, 65, 100, 65, 100, 100, 100, 100)
int[] transpRolling = showRolling? transpRollingDefault: transpHide
int[] transpRollingLabel = showRolling? transpRollingLabelDefault: transpHide
int[] lineWidth = array.from(1, 2, 1, 2, 1, 2, 1, 1, 1, 1)
int[] lineWidthRolling = array.from(1, 2, 1, 2, 1, 2, 1, 1, 1, 1)
int lineWidthPivot = 3
var valArrRes = array.new<float>(10, 0)
var valArrSup = array.new<float>(10, 0)
var valArrRollingRes = array.new<float>(10, 0)
var valArrRollingSup = array.new<float>(10, 0)
var line linePivot = line.new(time, 0, time+1, 0, xloc=xloc.bar_time, color=color.new(colorPivot, trans), width=lineWidthPivot, style=line.style_solid)
var box boxCPR = box.new(time, 0, time+1, 0, xloc=xloc.bar_time, bgcolor=na, border_color = na)
var line lineRollingPivot = line.new(time, 0, time+1, 0, xloc=xloc.bar_time, color=color.new(colorPivot, trans), width=lineWidthPivot, style=line.style_dotted)
var label labelPivot = label.new(time, 0, "", xloc=xloc.bar_time, color=color.new(na,100), textcolor=colorPivot, size=size.normal, style=label.style_label_left, textalign = text.align_center)
var label labelRollingPivot = label.new(time, 0, "", xloc=xloc.bar_time, color=color.new(na,100), textcolor=colorPivot, size=size.small, style=label.style_label_left, textalign = text.align_center)
var line[] lineArrRes = array.new_line(10, na)
var line[] lineArrSup = array.new_line(10, na)
var line[] lineArrRollingRes = array.new_line(10, na)
var line[] lineArrRollingSup = array.new_line(10, na)
var label[] labelArrRes = array.new_label(10, na)
var label[] labelArrSup = array.new_label(10, na)
var label[] labelArrRollingRes = array.new_label(10, na)
var label[] labelArrRollingSup = array.new_label(10, na)
int numMilliSecondsInPivotTF = timeframe.in_seconds(pivotTimeFrame) * 1000
int numMilliSecondsInDay = timeframe.in_seconds("1D") * 1000
type pivotLine
line _line = na
box _boxCPR = na
bool isLatest = true
bool isRetired = false
int retireTime = 0
var pivotMatrix = matrix.new<pivotLine>(0,1)
pHigh = request.security(syminfo.tickerid, pivotTimeFrame, high[1], lookahead=barmerge.lookahead_on)
pLow = request.security(syminfo.tickerid, pivotTimeFrame, low[1], lookahead=barmerge.lookahead_on)
pClose = request.security(syminfo.tickerid, pivotTimeFrame, close[1], lookahead=barmerge.lookahead_on)
barsSinceTFChange = nz(ta.barssince(timeframe.change(pivotTimeFrame)))
closeBarLastTF := close[barsSinceTFChange+1]
rollingHighTFChg := ta.highest(na(barsSinceTFChange[1]) ? 1 : barsSinceTFChange[1]+2)
rollingLowTFChg := ta.lowest(na(barsSinceTFChange[1]) ? 1 : barsSinceTFChange[1]+2)
closeForPVP = pivotTimeFrame == "1D" ? closeBarLastTF : pClose
pvp = quantizeToTick((pHigh + pLow + closeForPVP) / 3)
bcPivot = quantizeToTick((pHigh + pLow) / 2)
tcPivot = quantizeToTick(2*pvp - bcPivot)
pvpRolling = quantizeToTick((rollingHighTFChg + rollingLowTFChg + close) / 3)
taChangePVP = ta.change(pvp)
// plot(taChangePVP+5240)
var int startTime = na
var int startTimeRolling = na
var int endTime = na
var int endTimeRolling = na
if timeframe.change(pivotTimeFrame)
startTime := time
endTime := startTime + numMilliSecondsInPivotTF
if dayofweek(time) == dayofweek.thursday or dayofweek(time) == dayofweek.friday
startTimeRolling := endTime + 15*numMilliSecondsInDay/4
endTimeRolling := endTime + 4*numMilliSecondsInDay
else
startTimeRolling := endTime + 7*numMilliSecondsInDay/4
endTimeRolling := endTime + 2*numMilliSecondsInDay
if na(array.get(lineArrRes,0)) // initialize the arrays of lines
for i = 0 to array.size(fibList) - 1
array.set(lineArrRes, i, line.new(time, 0, time+1, 0, xloc=xloc.bar_time, color=color.new(colorRes, array.get(transp, i)), width=array.get(lineWidth, i), style=line.style_solid))
array.set(labelArrRes, i, label.new(time, 0, "", xloc=xloc.bar_time, color=color.new(color.white,100), textcolor=color.new(colorRes, array.get(transp, i)), size=array.get(sizeLabel, i), style=label.style_label_left, textalign = text.align_center))
array.set(lineArrSup, i, line.new(time, 0, time+1, 0, xloc=xloc.bar_time, color=color.new(colorSup, array.get(transp, i)), width=array.get(lineWidth, i), style=line.style_solid))
array.set(labelArrSup, i, label.new(time, 0, "", xloc=xloc.bar_time, color=color.new(color.white,100), textcolor=color.new(colorSup, array.get(transp, i)), size=array.get(sizeLabel, i), style=label.style_label_left, textalign = text.align_center))
array.set(lineArrRollingRes, i, line.new(time, 0, time+1, 0, xloc=xloc.bar_time, color=color.new(colorRes, array.get(transpRolling, i)), width=array.get(lineWidthRolling, i), style=line.style_dotted))
array.set(labelArrRollingRes, i, label.new(time, 0, "", xloc=xloc.bar_time, color=color.new(color.white,100), textcolor=color.new(colorRes, array.get(transpRollingLabel, i)), size=size.small, style=label.style_label_left, textalign = text.align_center))
array.set(lineArrRollingSup, i, line.new(time, 0, time+1, 0, xloc=xloc.bar_time, color=color.new(colorSup, array.get(transpRolling, i)), width=array.get(lineWidthRolling, i), style=line.style_dotted))
array.set(labelArrRollingSup, i, label.new(time, 0, "", xloc=xloc.bar_time, color=color.new(color.white,100), textcolor=color.new(colorSup, array.get(transpRollingLabel, i)), size=size.small, style=label.style_label_left, textalign = text.align_center))
else
if timeframe.change(pivotTimeFrame)
for i = 0 to array.size(fibList) - 1
_valRes = fibrange(pvp, pHigh, pLow, array.get(fibList, i))
updateLineLabel(lineArrRes, labelArrRes, i, startTime, endTime, _valRes)
_valSup = fibrange(pvp, pHigh, pLow, -array.get(fibList, i))
updateLineLabel(lineArrSup, labelArrSup, i, startTime, endTime, _valSup)
if barstate.isconfirmed or timeframe.change(pivotTimeFrame)
for i = 0 to array.size(fibList) - 1
_valRollingRes = fibrange(pvpRolling, rollingHighTFChg, rollingLowTFChg, array.get(fibList, i))
updateLineLabel(lineArrRollingRes, labelArrRollingRes, i, startTimeRolling, endTimeRolling, _valRollingRes)
_valRollingSup = fibrange(pvpRolling, rollingHighTFChg, rollingLowTFChg, -array.get(fibList, i))
updateLineLabel(lineArrRollingSup, labelArrRollingSup, i, startTimeRolling, endTimeRolling, _valRollingSup)
tcpvp = ta.change(pvp)
// plot(pvp, color=color.fuchsia)
// plot(pvpRolling, color=color.maroon)
if barstate.isconfirmed or timeframe.change(pivotTimeFrame)// and ta.change(pvpRolling) != 0 //session.islastbar_regular
linePivot.set_xy1(startTime, pvp)
linePivot.set_xy2(endTime, pvp)
labelPivot.set_xy(endTime, pvp)
labelPivot.set_text(str.tostring(pvp))
boxCPR.set_left(startTime)
boxCPR.set_right(endTime)
boxCPR.set_bottom(bcPivot)
boxCPR.set_top(tcPivot)
lineRollingPivot.set_xy1(startTimeRolling, pvpRolling)
lineRollingPivot.set_xy2(endTimeRolling, pvpRolling)
labelRollingPivot.set_xy(endTimeRolling, pvpRolling)
labelRollingPivot.set_text(str.tostring(pvpRolling))
if timeframe.change(pivotTimeFrame) //and ta.change(pvp) != 0 //session.islastbar_regular
if pivotMatrix.rows() > 0
pivotLine _pivotLine = pivotMatrix.get(pivotMatrix.rows()-1,0)
_pivotLine.isLatest := false
pivotMatrix.add_row()
pivotMatrix.set(pivotMatrix.rows()-1,0,pivotLine.new(linePivot.copy(), boxCPR.copy()))
// retire pivot lines that have been breached, and extend lines that have not yet been touched
for i = (pivotMatrix.rows()>0?pivotMatrix.rows()-1:na) to 0 by 1
pivotLine _pivotLine = pivotMatrix.get(i, 0)
if not(_pivotLine.isRetired) //and not _pivotLine.isLatest // i.e. line is still active
if low < _pivotLine._line.get_y1() and high > _pivotLine._line.get_y1()
_pivotLine.isRetired := true
_pivotLine.retireTime := time
if _pivotLine.isLatest
_pivotLine._boxCPR.set_right(endTime)
_pivotLine._line.set_x2(endTime)
_pivotLine._line.set_color(colorCPR)
if showLatestCPR
_pivotLine._boxCPR.set_bgcolor(color.new(colorCPR, transCPR))
else
_pivotLine._line.set_width(1)
if _pivotLine._line.get_x2() - _pivotLine._line.get_x1() >= numMilliSecondsInPivotTF
_pivotLine._line.set_color(colorCPR)
_pivotLine._line.set_style(line.style_solid)
else
_pivotLine._line.set_color(colorCPR)
if not _pivotLine.isRetired
_pivotLine._line.set_x2(endTime)
_pivotLine._boxCPR.set_right(endTime)
_pivotLine._boxCPR.set_bgcolor(color.new(colorCPR, transCPR))
else
_pivotLine._line.set_x2(_pivotLine.retireTime)
_pivotLine._boxCPR.set_right(_pivotLine.retireTime)
if showRetiredCPR
_pivotLine._boxCPR.set_bgcolor(color.new(colorCPR, transRetiredCPR))
else
_pivotLine._boxCPR.set_bgcolor(na)
f0 = input.float(defval=0.236, title='F0', group = "Levels")
f1 = input.float(defval=0.382, title='F1', group = "Levels")
f2 = input.float(defval=0.5, title='F2', group = "Levels")
f3 = input.float(defval=0.618, title='F3', group = "Levels")
f4 = input.float(defval=0.786, title='F4', group = "Levels")
f5 = input.float(defval=1.000, title='F5', group = "Levels")
f6 = input.float(defval=1.272, title='F6', group = "Levels")
f7 = input.float(defval=1.382, title='F7', group = "Levels")
f8 = input.float(defval=1.618, title='F8', group = "Levels")
f9 = input.float(defval=2, title='F9', group = "Levels")
vR0 = fibrange(pvp, pHigh, pLow, f0)
vR1 = fibrange(pvp, pHigh, pLow, f1)
vR2 = fibrange(pvp, pHigh, pLow, f2)
vR3 = fibrange(pvp, pHigh, pLow, f3)
vR4 = fibrange(pvp, pHigh, pLow, f4)
vR5 = fibrange(pvp, pHigh, pLow, f5)
vR6 = fibrange(pvp, pHigh, pLow, f6)
vR7 = fibrange(pvp, pHigh, pLow, f7)
vR8 = fibrange(pvp, pHigh, pLow, f8)
vR9 = fibrange(pvp, pHigh, pLow, f9)
vS0 = fibrange(pvp, pHigh, pLow, -f0)
vS1 = fibrange(pvp, pHigh, pLow, -f1)
vS2 = fibrange(pvp, pHigh, pLow, -f2)
vS3 = fibrange(pvp, pHigh, pLow, -f3)
vS4 = fibrange(pvp, pHigh, pLow, -f4)
vS5 = fibrange(pvp, pHigh, pLow, -f5)
vS6 = fibrange(pvp, pHigh, pLow, -f6)
vS7 = fibrange(pvp, pHigh, pLow, -f7)
vS8 = fibrange(pvp, pHigh, pLow, -f8)
vS9 = fibrange(pvp, pHigh, pLow, -f9)
pvplot = plot(plotAllHistoricalLines ? tcpvp != 0 ? na : pvp : na, color=color.new(colorPivot,trans), title='PP', style=plot.style_linebr, linewidth=widthPVP, display = display.pane)
res0 = plot(plotAllHistoricalLines and vR0 > 0 ? ta.change(vR0) != 0 ? na : vR0 : na, color=color.new(colorRes,transMinor), title='R0', style=plot.style_linebr, linewidth=widthMinor, display = display.pane)
res1 = plot(plotAllHistoricalLines and vR1 > 0 ? ta.change(vR1) != 0 ? na : vR1 : na, color=color.new(colorRes,trans), title='R1', style=plot.style_linebr, linewidth=width, display = display.pane)
res2 = plot(plotAllHistoricalLines and vR2 > 0 ? ta.change(vR2) != 0 ? na : vR2 : na, color=color.new(colorRes,transMinor), title='R2', style=plot.style_linebr, linewidth=widthMinor, display = display.pane)
res3 = plot(plotAllHistoricalLines and vR3 > 0 ? ta.change(vR3) != 0 ? na : vR3 : na, color=color.new(colorRes,trans), title='R3', style=plot.style_linebr, linewidth=width, display = display.pane)
res4 = plot(plotAllHistoricalLines and vR4 > 0 ? ta.change(vR4) != 0 ? na : vR4 : na, color=color.new(colorRes,transMinor), title='R4', style=plot.style_linebr, linewidth=widthMinor, display = display.pane)
res5 = plot(plotAllHistoricalLines and vR5 > 0 ? ta.change(vR5) != 0 ? na : vR5 : na, color=color.new(colorRes,trans), title='R5', style=plot.style_linebr, linewidth=width, display = display.pane)
res6 = plot(plotAllHistoricalLines and vR6 > 0 ? ta.change(vR6) != 0 ? na : vR6 : na, color=color.new(colorRes,transMinor), title='R6', style=plot.style_linebr, linewidth=widthMinor, display = display.pane)
res7 = plot(plotAllHistoricalLines and vR7 > 0 ? ta.change(vR7) != 0 ? na : vR7 : na, color=color.new(colorRes,transMinor), title='R7', style=plot.style_linebr, linewidth=widthMinor, display = display.pane)
res8 = plot(plotAllHistoricalLines and vR8 > 0 ? ta.change(vR8) != 0 ? na : vR8 : na, color=color.new(colorRes,transMinor), title='R8', style=plot.style_linebr, linewidth=widthMinor, display = display.pane)
res9 = plot(plotAllHistoricalLines and vR9 > 0 ? ta.change(vR9) != 0 ? na : vR9 : na, color=color.new(colorRes,transMinor), title='R9', style=plot.style_linebr, linewidth=widthMinor, display = display.pane)
sup0 = plot(plotAllHistoricalLines and vS0 > 0 ? ta.change(vS0) != 0 ? na : vS0 : na, color=color.new(colorSup,transMinor), title='S0', style=plot.style_linebr, linewidth=widthMinor, display = display.pane)
sup1 = plot(plotAllHistoricalLines and vS1 > 0 ? ta.change(vS1) != 0 ? na : vS1 : na, color=color.new(colorSup,trans), title='S1', style=plot.style_linebr, linewidth=width, display = display.pane)
sup2 = plot(plotAllHistoricalLines and vS2 > 0 ? ta.change(vS2) != 0 ? na : vS2 : na, color=color.new(colorSup,transMinor), title='S2', style=plot.style_linebr, linewidth=widthMinor, display = display.pane)
sup3 = plot(plotAllHistoricalLines and vS3 > 0 ? ta.change(vS3) != 0 ? na : vS3 : na, color=color.new(colorSup,trans), title='S3', style=plot.style_linebr, linewidth=width, display = display.pane)
sup4 = plot(plotAllHistoricalLines and vS4 > 0 ? ta.change(vS4) != 0 ? na : vS4 : na, color=color.new(colorSup,transMinor), title='S4', style=plot.style_linebr, linewidth=widthMinor, display = display.pane)
sup5 = plot(plotAllHistoricalLines and vS5 > 0 ? ta.change(vS5) != 0 ? na : vS5 : na, color=color.new(colorSup,trans), title='S5', style=plot.style_linebr, linewidth=width, display = display.pane)
sup6 = plot(plotAllHistoricalLines and vS6 > 0 ? ta.change(vS6) != 0 ? na : vS6 : na, color=color.new(colorSup,transMinor), title='S6', style=plot.style_linebr, linewidth=widthMinor, display = display.pane)
sup7 = plot(plotAllHistoricalLines and vS7 > 0 ? ta.change(vS7) != 0 ? na : vS7 : na, color=color.new(colorSup,transMinor), title='S7', style=plot.style_linebr, linewidth=widthMinor, display = display.pane)
sup8 = plot(plotAllHistoricalLines and vS8 > 0 ? ta.change(vS8) != 0 ? na : vS8 : na, color=color.new(colorSup,transMinor), title='S8', style=plot.style_linebr, linewidth=widthMinor, display = display.pane)
sup9 = plot(plotAllHistoricalLines and vS9 > 0 ? ta.change(vS9) != 0 ? na : vS9 : na, color=color.new(colorSup,transMinor), title='S9', style=plot.style_linebr, linewidth=widthMinor, display = display.pane)
// bgcolor(timeframe.change(pivotTimeFrame) ? color.fuchsia : na)
// var bool lvs = na
// // line _line = array.get(lineArrRes, 5)
// // lvs := high>_line.get_y1()
// lvs := timeframe.change(pivotTimeFrame) and (open < linePivot.get_y1())
// bgcolor(lvs[1]==false and lvs==true ? color.black : na)
Show all 290 lines