# golden_01c
# https://www.tradingview.com/script/uYiilgYb-TW-Golden-Indicators/
# convert tradingview study
#// TW Golden Indicators Package
#// This package features BB (with 20 ma), inside bar color change (white), outside bar color change (black), and holy grail 2-candle pattern background higlhlight (yellow)
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;
# https://tlc.thinkorswim.com/center/reference/thinkScript/Constants/AverageType
# EXPONENTIAL, HULL, SIMPLE, WEIGHTED, WILDERS
input avg1_type = AverageType.simple;
def basis = MovingAverage(avg1_type, src, length);
def dev = mult2 * StDev( src , length );
def upper = basis + dev;
def lower = basis - dev;
# horz lines
plot basisLine = highestAll(if isNaN(close[-1]) then basis else Double.NaN);
basisline.SetDefaultColor(Color.red);
basisline.SetStyle(Curve.SHORT_DASH);
plot upperLine = highestAll(if isNaN(close[-1]) then upper else Double.NaN);
upperline.SetDefaultColor(Color.magenta);
plot lowerLine = highestAll(if isNaN(close[-1]) then lower else Double.NaN);
lowerline.SetDefaultColor(Color.magenta);
# 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];
input bubble_ht_offset = 0.0006;
addchartbubble(inbar, (high * (1 + bubble_ht_offset)), "IB", color.yellow, yes);
addchartbubble(outbar, (low * (1 - bubble_ht_offset)), "OB", color.blue, no);
def off = 1;
addchartbubble(lastbar[off] and holyGrail[off] and inbar[off], close, "Holy Grail" + "\nInbar", color.yellow, yes);
addchartbubble(lastbar[off] and holyGrail[off] and outbar[off], close, "Holy Grail" + "\nOutbar", color.yellow, yes);
plot hg2 = holygrail;
hg2.SetPaintingStrategy(PaintingStrategy.BOOLEAN_wedge_up);
hg2.SetDefaultColor(Color.white);
hg2.setlineweight(3);
#
# add cloud
def x = (inbar or inbar[1]);
def xtop = if x then double.POSITIVE_INFINITY else double.nan;
addcloud(xtop, double.NEGATIVE_INFINITY, color.light_gray, color.light_gray);
#