# Complete Pivot Setup
# Created by @tony_futures
# Inspired from @PostyTrades and @SergeTrades
#declare hide_on_daily;
#Modified to add Quarter aggregationPeriod
input aggregationPeriod = {default DAY, WEEK, MONTH, QUARTER, YEAR};
input showCPR = no;
input showPrevious = no;
input showReversalZone = yes;
input showLastStand = yes;
input showLabels = yes;
input showBullTargets = yes;
input showBearTargets = yes;
input showTodayOnly = yes;
input manualClose = 0.00;
input RoundLevel = 0;
#Quarters Defined
def quarter = (GetMonth() - 1) % 3;
def qtr = if quarter == 0 and quarter[1] != 0 then qtr[1] + 1 else qtr[1];
def candles = !IsNaN(close);
def capture = candles and qtr != qtr[1];
def qtrCount = CompoundValue(1, if capture then qtrCount[1] + 1 else qtrCount[1], 0);
def thisqtr = (HighestAll(qtrCount) - qtrCount) ;
def Today = if aggregationPeriod == aggregationPeriod.DAY and GetLastDay() == GetDay() then 1 else if aggregationPeriod == aggregationPeriod.MONTH and GetLastMonth() == GetMonth() then 1 else if aggregationPeriod == aggregationPeriod.WEEK and GetLastWeek() == GetWeek() then 1 else if aggregationPeriod == aggregationPeriod.QUARTER and thisqtr == 0 then 1 else if aggregationPeriod == aggregationPeriod.YEAR and GetLastYear() == GetYear() then 1 else 0;
def prevHigh = high(period = aggregationPeriod)[1];
def prevLow = low(period = aggregationPeriod)[1];
def prevClose1 = close(period = aggregationPeriod)[1];
def prevClose = if manualClose != 0 then manualClose else prevClose1;
#Addlabel(aggregationPeriod == AggregationPeriod.DAY," Daily Close: " + prevClose, Color.WHITE);
def pivot = (prevHigh + prevLow + prevClose) / 3.0;
def bc = (prevHigh + prevLow) / 2.0;
def tc = (pivot - bc) + pivot;
def prevDiff = prevHigh - prevLow;
def prevDiff2 = prevDiff * 1.1 / 2;
def prevDiff4 = prevDiff * 1.1 / 4;
def prevDiff8 = prevDiff * 1.1 / 8;
def smallR1 = prevClose + (prevDiff * 1.1 / 12);
def smallS1 = prevClose - (prevDiff * 1.1 / 12);
def R1 = prevDiff4 + prevClose;
def R3 = RoundDown(prevDiff2 + prevClose, RoundLevel);
def R2 = R3 - prevDiff8;
def R4 = RoundDown(prevDiff + prevClose, RoundLevel);
def R5 = RoundDown((R3 + (1.168 * (R3 - R1))), RoundLevel);
def S1 = prevClose - prevDiff4;
def S3 = RoundUp(prevClose - prevDiff2, RoundLevel);
def S2 = S3 + prevDiff8;
def S4 = RoundUp(prevClose - prevDiff, RoundLevel);
def S5 = RoundUp((S3 - (1.168 * ( S1 - S3))), RoundLevel);
# Central Pivot Range - turn off by changing showCPR to no
plot PLine = if showCPR and showTodayOnly and Today then pivot else if showCPR and !showTodayOnly then pivot else Double.NaN;
plot BCLine = if showCPR and showTodayOnly and Today then bc else if showCPR and !showTodayOnly then bc else Double.NaN;
plot TCLine = if showCPR and showTodayOnly and Today then tc else if showCPR and !showTodayOnly then tc else Double.NaN;
PLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PLine.SetDefaultColor(Color.WHITE);
BCLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
BCLine.SetDefaultColor(Color.CYAN);
TCLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
TCLine.SetDefaultColor(Color.MAGENTA);
# Previous Day High/Low - turn off by changing showPrevious to no
plot PreviousHigh = if showPrevious and showTodayOnly and Today then RoundDown(prevHigh, RoundLevel) else if showPrevious and !showTodayOnly then RoundDown(prevHigh, RoundLevel) else Double.NaN;
PreviousHigh.SetDefaultColor(Color.GRAY);
plot PreviousLow = if showPrevious and showTodayOnly and Today then RoundUp(prevLow, RoundLevel) else if showPrevious and !showTodayOnly then RoundUp(prevLow, RoundLevel) else Double.NaN;
PreviousLow.SetDefaultColor(Color.GRAY);
# Reversal zones - turn off by changing showReversalZone to no
AddCloud(if showReversalZone and showTodayOnly and Today then R1 else if showReversalZone and !showTodayOnly then R1 else Double.NaN, R2, Color.DARK_ORANGE, Color.DARK_ORANGE);
AddCloud(if showReversalZone and showTodayOnly and Today then S1 else if showReversalZone and !showTodayOnly then S1 else Double.NaN, S2, Color.LIGHT_GREEN, Color.LIGHT_GREEN);
AddLabel(showReversalZone and showLabels, "Bear zone: " + RoundDown(R1, RoundLevel) + " to " + RoundDown(R2, RoundLevel), Color.LIGHT_RED);
AddLabel(showReversalZone and showLabels, "Bull zone: " + RoundDown(S1, RoundLevel) + " to " + RoundDown(S2, RoundLevel), Color.LIGHT_GREEN);
# Last stand lines - turn off by changing ShowLastStand to no
plot bullLastStand = if showLastStand and showTodayOnly and Today then S3 else if showLastStand and !showTodayOnly then S3 else Double.NaN;
bullLastStand.SetDefaultColor(Color.GREEN);
plot bearLastStand = if showLastStand and showTodayOnly and Today then R3 else if showLastStand and !showTodayOnly then R3 else Double.NaN;
bearLastStand.SetDefaultColor(Color.RED);
input showBubblesLeft = yes;
AddChartBubble(showBubblesLeft and showLastStand and Today and !Today[1], R3, "Bear Last Stand: " + R3, Color.GRAY, yes);
AddChartBubble(showBubblesLeft and showLastStand and Today and !Today[1], S3, "Bull Last Stand: " + S3, Color.GRAY, no);
AddChartBubble(showBubblesLeft and showPrevious and Today and !Today[1], prevHigh, "Previous High: " + prevHigh, Color.GRAY, yes);
AddChartBubble(showBubblesLeft and showPrevious and Today and !Today[1], prevLow, "Previous Low: " + prevLow, Color.GRAY, no);
plot bull1 = if showBullTargets and showTodayOnly and Today then R5 else if showBullTargets and !showTodayOnly then R5 else Double.NaN;
bull1.SetDefaultColor(Color.MAGENTA);
plot bear1 = if showBearTargets and showTodayOnly and Today then S5 else if showBearTargets and !showTodayOnly then S5 else Double.NaN;
bear1.SetDefaultColor(Color.CYAN);
AddChartBubble(showBubblesLeft and showBullTargets and Today and !Today[1], R5, "Bull Target 1: " + R5, Color.GRAY, yes);
AddChartBubble(showBubblesLeft and showBearTargets and Today and !Today[1], S5, "Bear Target 1: " + S5, Color.GRAY, no);
plot bull2 = if showBullTargets and showTodayOnly and Today then R4 else if showBullTargets and !showTodayOnly then R4 else Double.NaN;
bull2.SetDefaultColor(Color.MAGENTA);
plot bear2 = if showBearTargets and showTodayOnly and Today then S4 else if showBearTargets and !showTodayOnly then S4 else Double.NaN;
bear2.SetDefaultColor(Color.CYAN);
AddChartBubble(showBubblesLeft and showBullTargets and Today and !Today[1], R4, "Bull Target 2: " + R4, Color.GRAY, yes);
AddChartBubble(showBubblesLeft and showBearTargets and Today and !Today[1], S4, "Bear Target 2: " + S4, Color.GRAY, no);
input showOpen = yes;
input Open_Time = 0930;
AddVerticalLine(SecondsFromTime(Open_Time)[1] < 0 and SecondsFromTime(Open_Time) >= 0 and Today and showOpen, Concat("Open", ""), Color.GREEN, Curve.POINTS);
input showBubblesRight = No;
input displaceRightBubble = 3;
def showBubbleNow = !IsNaN(close) and IsNaN(close[-1]);
AddChartBubble(showBubblesRight and showLastStand and showBubbleNow[displaceRightBubble], R3, "Bear Last Stand: " + R3, Color.GRAY, yes);
AddChartBubble(showBubblesRight and showLastStand and showBubbleNow[displaceRightBubble], S3, "Bull Last Stand: " + S3, Color.GRAY, no);
AddChartBubble(showBubblesRight and showPrevious and showBubbleNow[displaceRightBubble], PreviousHigh, "Previous High: " + RoundDown(PreviousHigh, RoundLevel), Color.GRAY, yes);
AddChartBubble(showBubblesRight and showPrevious and showBubbleNow[displaceRightBubble], PreviousLow, "Previous Low: " + PreviousLow, Color.GRAY, no);
AddChartBubble(showBubblesRight and showBullTargets and showBubbleNow[displaceRightBubble], R5, "Bull Target 1: " + R5, Color.GRAY, yes);
AddChartBubble(showBubblesRight and showBearTargets and showBubbleNow[displaceRightBubble], S5, "Bear Target 1: " + S5, Color.GRAY, no);
AddChartBubble(showBubblesRight and showBullTargets and showBubbleNow[displaceRightBubble], R4, "Bull Target 2: " + R4, Color.GRAY, yes);
AddChartBubble(showBubblesRight and showBearTargets and showBubbleNow[displaceRightBubble], S4, "Bear Target 2: " + S4, Color.GRAY, no);
input showWeeklyLabels = yes;
AddChartBubble(showWeeklyLabels and aggregationPeriod == aggregationPeriod.WEEK and Today and !Today[1], (R2 - R1) / 2 + R1, "Weekly", Color.WHITE, yes);
AddChartBubble(showWeeklyLabels and aggregationPeriod == aggregationPeriod.WEEK and Today and !Today[1], (S1 - S2) / 2 + S2, "Weekly", Color.WHITE, no);
input showSmallCamPoints = no;
plot smallCamResistance = if showSmallCamPoints and Today then smallR1 else Double.NaN;
smallCamResistance.SetDefaultColor(Color.LIGHT_RED);
plot smallCamSupport = if showSmallCamPoints and Today then smallS1 else Double.NaN;
smallCamSupport.SetDefaultColor(Color.LIGHT_GREEN);