input daysback = 1;
input p1 = .01;
input p2 = .02;
input p3 = .03;
input p4 = .04;
def c = close;
def ymd = GetYYYYMMDD();
def candles = !IsNaN(close);
def capture = candles and ymd != ymd[1];
def dayCount = CompoundValue(1, if capture then dayCount[1] + 1 else dayCount[1], 0);
def thisDay = (HighestAll(dayCount) - dayCount) ;
#def openday = if thisday>daysback then double.nan else if thisday==daysback then open(period=aggregationPeriod.DAY) else openday[1];
def rth = Between(GetTime(), RegularTradingStart(GetYYYYMMDD()), RegularTradingEnd(GetYYYYMMDD()));
def openday = if thisDay > daysback then Double.NaN else if thisDay == daysback and rth[1] == 1 and rth == 0 then close else openday[1];
def openbar = if close == openday then BarNumber() else openbar[1];
plot o = if BarNumber() < openbar then Double.NaN else openday;
plot o1 = o + o * p1;
plot o2 = o + o * p2;
plot o3 = o + o * p3;
plot o4 = o + o * p4;
plot o1_ = o - o * p1;
plot o2_ = o - o * p2;
plot o3_ = o - o * p3;
plot o4_ = o - o * p4;
# Style
DefineGlobalColor("P", Color.GREEN);
DefineGlobalColor("N", Color.RED);
o1.SetDefaultColor(GlobalColor("P"));
o2.SetDefaultColor(GlobalColor("P"));
o3.SetDefaultColor(GlobalColor("P"));
o4.SetDefaultColor(GlobalColor("P"));
o1_.SetDefaultColor(GlobalColor("N"));
o2_.SetDefaultColor(GlobalColor("N"));
o3_.SetDefaultColor(GlobalColor("N"));
o4_.SetDefaultColor(GlobalColor("N"));
#Bubbles (Moveable)
input showbubbles = yes;
input bubblemover = 3;
def bm = bubblemover;
def bm1 = bm + 1;
input PercentileA1 = "65";
input PercentileA2 = "88";
input PercentileA3 = "95";
input PercentileA4 = "98";
input PercentileB1 = "61";
input PercentileB2 = "84";
input PercentileB3 = "94";
input PercentileB4 = "97";
AddCloud(o1_, o1, Color.GREEN, Color.GREEN, yes);
AddCloud(o1, o2, Color.ORANGE, Color.ORANGE, yes);
AddCloud(o1_, o2_, Color.ORANGE, Color.ORANGE, yes);
AddCloud(o2, o3, Color.MAGENTA, Color.MAGENTA, yes);
AddCloud(o2_, o3_, Color.MAGENTA, Color.MAGENTA, yes);
AddCloud(o3, o4, Color.RED, Color.RED, yes);
AddCloud(o3_, o4_, Color.RED, Color.RED, yes);
def x = BarNumber();
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o1[bm], PercentileA1, o1.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o2[bm], PercentileA2, o2.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o3[bm], PercentileA3, o3.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o4[bm], PercentileA4, o4.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o1_[bm], PercentileB1, o1_.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o2_[bm], PercentileB2, o2_.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o3_[bm], PercentileB3, o3_.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o4_[bm], PercentileB4, o4_.TakeValueColor());
#AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o[bm], "Open", o.TakeValueColor());
#AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o1[bm], p1, o1.TakeValueColor());
#AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o2[bm], p2, o2.TakeValueColor());
#AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o3[bm], p3, o3.TakeValueColor());
#AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o4[bm], p4, o4.TakeValueColor());
#AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o1_[bm], p1, o1_.TakeValueColor());
#AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o2_[bm], p2, o2_.TakeValueColor());
#AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), #o3_[bm], p3, o3_.TakeValueColor());
#AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o4_[bm], p4, o4_.TakeValueColor());
# Alerts
input usealerts = yes;
Alert(usealerts and c crosses o1, "+" + p1, Alert.BAR, Sound.Chimes);
Alert(usealerts and c crosses o2, "+" + p2, Alert.BAR, Sound.Chimes);
Alert(usealerts and c crosses o3, "+" + p3, Alert.BAR, Sound.Chimes);
Alert(usealerts and c crosses o4, "+" + p4, Alert.BAR, Sound.Chimes);
Alert(usealerts and c crosses o1_, "-" + p1, Alert.BAR, Sound.Ding);
Alert(usealerts and c crosses o2_, "-" + p2, Alert.BAR, Sound.Ding);
Alert(usealerts and c crosses o3_, "-" + p3, Alert.BAR, Sound.Ding);
Alert(usealerts and c crosses o4_, "-" + p4, Alert.BAR, Sound.Ding);