# Camarilla Pivot Range
# Assembled using useThinkScript.com
input showonlyLastPeriod = yes;
input show_bubbles = yes;#Hint show_bubbles: turns all bubbles on or off
input show_bubble_price = yes;#Hint show_bubble_price: displays price in bubble
input aggregationPeriod = AggregationPeriod.DAY;
def dayCount = CompoundValue(1, if GetYYYYMMDD() != GetYYYYMMDD()[1] then dayCount[1] + 1 else dayCount[1], 0);
def thisDay = (HighestAll(dayCount) - dayCount) ;
def HIGHprev = high(period = aggregationPeriod)[1];
def LOWprev = low(period = aggregationPeriod)[1];
def CLOSEprev = close(period = aggregationPeriod)[1];
def range = HIGHprev - LOWprev;
plot PP = if showonlyLastPeriod and !IsNaN(close(period = aggregationPeriod)[-2])
then Double.NaN
else (HIGHprev + LOWprev + CLOSEprev) / 3;
plot R5 = (HIGHprev / LOWprev) * CLOSEprev;
plot R4 = CLOSEprev + range * (1.1) / 2;
plot R3 = CLOSEprev + range * (1.1) / 4;
plot R2 = CLOSEprev + range * (1.1) / 6;
plot R1 = CLOSEprev + range * (1.1) / 12;
plot S1 = CLOSEprev - range * (1.1) / 12;
plot S2 = CLOSEprev - range * (1.1) / 6;
plot S3 = CLOSEprev - range * (1.1) / 4;
plot S4 = CLOSEprev - range * (1.1) / 2;
plot S5 = (CLOSEprev - (R5 - CLOSEprev));
R1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
R2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
R3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
R4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
R5.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
S1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
S2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
S3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
S4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
S5.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
AddChartBubble( show_bubbles and
if showonlyLastPeriod
then thisDay[1] == 1 and thisDay == 0
else R5 != R5[1],
R5,
"R5: " + if !show_bubble_price then ""
else AsText(Round(R5 / TickSize(), 0) * TickSize()), Color. WHITE);
AddChartBubble( show_bubbles and
if showonlyLastPeriod
then thisDay[1] == 1 and thisDay == 0
else R4 != R4[1],
R4,
"R4: " + if !show_bubble_price then ""
else AsText(Round(R4 / TickSize(), 0) * TickSize()), Color. WHITE);
AddChartBubble( show_bubbles and
if showonlyLastPeriod
then thisDay[1] == 1 and thisDay == 0
else R3 != R3[1],
R3,
"R3: " + if !show_bubble_price then ""
else AsText(Round(R3 / TickSize(), 0) * TickSize()), Color. WHITE);
AddChartBubble( show_bubbles and
if showonlyLastPeriod
then thisDay[1] == 1 and thisDay == 0
else R2 != R2[1],
R2,
"R2: " + if !show_bubble_price then ""
else AsText(Round(R2 / TickSize(), 0) * TickSize()), Color. WHITE);
AddChartBubble( show_bubbles and
if showonlyLastPeriod
then thisDay[1] == 1 and thisDay == 0
else R1 != R1[1],
R1,
"R1: " + if !show_bubble_price then ""
else AsText(Round(R1 / TickSize(), 0) * TickSize()), Color. WHITE);
AddChartBubble( show_bubbles and
if showonlyLastPeriod
then thisDay[1] == 1 and thisDay == 0
else PP != PP[1],
PP,
"PP: " + if !show_bubble_price then ""
else AsText(Round(PP / TickSize(), 0) * TickSize()), Color.WHITE);
AddChartBubble( show_bubbles and
if showonlyLastPeriod
then thisDay[1] == 1 and thisDay == 0
else S5 != S5[1],
S5,
"S5: " + if !show_bubble_price then ""
else AsText(Round(S5 / TickSize(), 0) * TickSize()), Color.WHITE, no);
AddChartBubble( show_bubbles and
if showonlyLastPeriod
then thisDay[1] == 1 and thisDay == 0
else S4 != S4[1],
S4,
"S4: " + if !show_bubble_price then ""
else AsText(Round(S4 / TickSize(), 0) * TickSize()), Color.WHITE, no);
AddChartBubble( show_bubbles and
if showonlyLastPeriod
then thisDay[1] == 1 and thisDay == 0
else S3 != S3[1],
S3,
"S3: " + if !show_bubble_price then ""
else AsText(Round(S3 / TickSize(), 0) * TickSize()), Color.WHITE, no);
AddChartBubble( show_bubbles and
if showonlyLastPeriod
then thisDay[1] == 1 and thisDay == 0
else S2 != S2[1],
S2,
"S2: " + if !show_bubble_price then ""
else AsText(Round(S2 / TickSize(), 0) * TickSize()), Color.WHITE, no);
AddChartBubble( show_bubbles and
if showonlyLastPeriod
then thisDay[1] == 1 and thisDay == 0
else S1 != S1[1],
S1,
"S1: " + if !show_bubble_price then ""
else AsText(Round(S1 / TickSize(), 0) * TickSize()), Color.WHITE, no);