#def lastbar = !isnan(close[0]) and isnan(close[-1]);
#def src = close;
#input length = 20;
#input mult = 2.0;
#def mult2 = if mult > 50 then 50 else if mult < 0.001 then 0.001 else mult;
# inside and outside bars
def inBar = low > low[1] and high < high[1];
def outBar = low < low[1] and high > high[1];
def holyGrail = low[1] < low[2] and high[1] > high[2] and low > low[1] and high < high[1];
#def nirvana = low[1] > low[2] and high[1] < high[2] and low < low[1] and high > high[1];
input bubble_ht_offset = 0.0006;
DefineGlobalColor("IB", Color.yellow);
DefineGlobalColor("OB", Color.blue);
addchartbubble(inbar, (high * (1 + bubble_ht_offset)), "IB", GlobalColor("IB"));
addchartbubble(outbar, (high * (1 + bubble_ht_offset)), "OB", GlobalColor("OB"));
def off = 1;
#AssignPriceColor(if (inbar and !holygrail) then Color.YELLOW else Color.CURRENT);
#AssignPriceColor(if (outbar and !holygrail) then Color.Blue else Color.CURRENT);
#AddVerticalLine(holygrail, "InBar", Color.ORANGE, Curve.FIRM);
#AddVerticalLine(holygrail[-1], "OUTBar", Color.WHITE, Curve.FIRM);
#AddVerticalLine(holygrail[+1], "HolyGrail", Color.RED, Curve.FIRM);
#########################
#Workaround Cloud between vertical lines using addchart()
def h = Double.POSITIVE_INFINITY;
def l = Double.NEGATIVE_INFINITY;
def xoutbar = if holygrail[-1] and outbar then 1 else 0;
def xinbar = if holygrail and inbar then 1 else 0;
def xboth = if xoutbar == 1
then 1 else
if xboth[1] == 1 and xinbar == 1
then 1 else 0;
input cloud = {default out_holygrail, in_holygrail, out_in, NONE};
def xH = if cloud == cloud.NONE
then Double.NaN else
if cloud == cloud.out_in and xoutbar == 1 or
cloud == cloud.out_holygrail and xboth or
cloud == cloud.in_holygrail and xinbar == 1
then h else Double.NaN;
def xL = if cloud == cloud.NONE
then Double.NaN else
if cloud == cloud.out_in and xoutbar == 1 or
cloud == cloud.out_holygrail and xboth or
cloud == cloud.in_holygrail and xinbar == 1
then l else Double.NaN;
defineGlobalColor("Cloud", color.gray);
AddChart(high = xH, low = xL, open = xH, close = xL, type = ChartType.CANDLE, growColor = globalcolor("Cloud"));
##########################
#Display candles hidden by above workaround
def rh = if cloud == cloud.NONE
then Double.NaN else
if cloud == cloud.out_in and xoutbar == 1 or
cloud == cloud.out_holygrail and xboth or
cloud == cloud.in_holygrail and xinbar == 1
then high else Double.NaN;
def rl = if cloud == cloud.NONE
then Double.NaN else
if cloud == cloud.out_in and xoutbar == 1 or
cloud == cloud.out_holygrail and xboth or
cloud == cloud.in_holygrail and xinbar == 1
then low else Double.NaN;
def rhg = if close >= open then rh else Double.NaN;
#AddChart(high = rhg, low = rl, open = close, close = open, growColor = Color.GREEN, type = ChartType.CANDLE);
#def rhr = if close < open then rh else Double.NaN;
#AddChart(high = rhr, low = rl, open = open, close = close, growColor = Color.RED, type = ChartType.CANDLE);
#####################################################################################
def nirvana = low[1] > low[2] and low < low[1] and high[1] < high[2] and high > high[1];
def xoutbar1 = if Nirvana[-1] and inbar then 1 else 0;
def xinbar1 = if Nirvana and outbar then 1 else 0;
def xboth1 = if xoutbar1 == 1
then 1 else
if xboth1[1] == 1 and xinbar1 == 1
then 1 else 0;
input cloud1 = {default out_Nirvana, in_nirvana, out_in, NONE};
def xHH = if cloud1 == cloud1.NONE
then Double.NaN else
if cloud1 == cloud1.out_in and xinbar1 == 1 or
cloud1 == cloud1.out_Nirvana and xboth1 or
cloud1 == cloud1.in_Nirvana and xoutbar1 == 1
then h else Double.NaN;
def xLL = if cloud == cloud.NONE
then Double.NaN else
if cloud1 == cloud1.out_in and xinbar1 == 1 or
cloud1 == cloud1.out_Nirvana and xboth1 or
cloud1 == cloud1.in_Nirvana and xoutbar1 == 1
then l else Double.NaN;
defineGlobalColor("Cloud1", color.blue);
AddChart(high = xHH, low = xLL, open = xHH, close = xLL, type = ChartType.CANDLE, growColor = globalcolor("Cloud1"));