# orb_breakouts01_00
#https://usethinkscript.com/threads/show-which-stocks-trade-outside-the-zone.13988/
#show which stocks trade outside the zone
def na = double.nan;
def bn = barnumber();
def newday = GetDay() <> GetDay()[1];
def daycnt = if bn == 1 then 1
else if isnan(close) then na
else if newday then daycnt[1] + 1
else daycnt[1];
def dayz = highestall(daycnt);
input days_of_data = 4;
def isday = (daycnt >= (dayz - days_of_data + 1));
def days_err = (days_of_data > dayz);
addlabel(1, " ", color.black);
addlabel(days_err, "========== not enough days on chart ===========", color.cyan);
addlabel(1, " ", color.black);
addlabel(1, days_of_data + " days", color.yellow);
addverticalline(!isday[1] and isday, days_of_data + " Days", color.cyan);
input agg = AggregationPeriod.THIRTY_MIN;
def orbhi = high(period = agg);
def orblo = low(period = agg);
def hi = if isnan(daycnt) then na else if newday then orbhi else hi[1];
def lo = if isnan(daycnt) then na else if newday then orblo else lo[1];
plot h2 = hi;
plot l2 = lo;
h2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
h2.SetDefaultColor(Color.LIME);
h2.HideBubble();
l2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
l2.SetDefaultColor(Color.LIME);
l2.HideBubble();
#input BeginTime = 0930;
def endtime = 1600;
def chartagg = GetAggregationPeriod();
def chartmin = chartagg / (1000 * 60);
#addlabel(yes, chartmin + " min", color.yellow);
def endtimeadj = if endtime == 1600 then 1560 - chartmin else endtime;
#def daystart3 = if SecondsTillTime(BeginTime) == 0 and SecondsFromTime(BeginTime) == 0 then 1 else 0;
def dayend3 = if SecondsTillTime(endtimeadj) == 0 and SecondsFromTime(endtimeadj) == 0 then 1 else 0;
#label 1 it would show how many times candle closed above or below the breakout area.
def r1up = if !isday then 0 else (close > hi);
def r1dwn = if !isday then 0 else (close < lo);
def r1 = r1up or r1dwn;
#label 2 how many times it crossed back below the breakout area after closing above.
#def r2 = if newday then 0 else close crosses below hi;
def r2 = if newday or !isday or bn == 1 then 0 else (close crosses below hi);
#label 3 how many times it ended the day above the breakout area after the breakout
def r3 = if newday or !isday then 0 else (close > hi and dayend3);
def r1cnt = if !isday or bn == 1 then 0 else if r1 then r1cnt[1] + 1 else r1cnt[1];
def r2cnt = if !isday or bn == 1 then 0 else if r2 then r2cnt[1] + 1 else r2cnt[1];
addlabel(1, "Rule 1 count: " + r1cnt, color.white);
addlabel(1, "Rule 2 count: " + r2cnt, color.red);
addlabel(1, "Rule 3: " + r3, color.green);
#--------------------
input test1 = no;
addchartbubble(test1, low,
r1 + "\n" +
r1cnt + "\n" +
r2 + "\n" +
r2cnt + "\n" +
r3
, color.yellow, no);
input test_rule1 = yes;
plot zr1a = if test_rule1 and r1up then high * 1.001 else na;
zr1a.SetPaintingStrategy(PaintingStrategy.points);
zr1a.SetDefaultColor(Color.white);
zr1a.SetLineWeight(3);
zr1a.HideBubble();
plot zr1b = if test_rule1 and r1dwn then low * 0.999 else na;
zr1b.SetPaintingStrategy(PaintingStrategy.points);
zr1b.SetDefaultColor(Color.white);
zr1b.SetLineWeight(3);
zr1b.HideBubble();
input test_rule2 = yes;
plot zr2 = if test_rule2 and r2 then low * 0.999 else na;
zr2.SetPaintingStrategy(PaintingStrategy.triangles);
zr2.SetDefaultColor(Color.red);
zr2.SetLineWeight(4);
zr2.HideBubble();
input test_rule3 = yes;
plot zr3 = if test_rule3 and r3 then high * 1.002 else na;
zr3.SetPaintingStrategy(PaintingStrategy.squares);
zr3.SetDefaultColor(Color.green);
zr3.SetLineWeight(4);
zr3.HideBubble();
input show_day_end = no;
plot zbuy = if show_day_end and dayend3 then close else na;
zbuy.SetPaintingStrategy(PaintingStrategy.points);
zbuy.SetDefaultColor(Color.white);
zbuy.SetLineWeight(3);
zbuy.HideBubble();
#