input ShowLines = yes;

input ShowClouds = yes;

input ShowBubbles = yes;

input intensity = 1.7;

input bubblemover = 1;

def BM = BubbleMover + 1;

def BubbleSpot = IsNaN(close[bubbleMover]) && !IsNaN(close[bm]);

def hh = HighestAll(high);

def ll = LowestAll(low);

def dif = hh - ll;

def h1 = if BarNumber() == 1 then 0 else if high == hh then BarNumber() else h1[1];

def l1 = if BarNumber() == 1 then 0 else if low == ll then BarNumber() else l1[1];

def flip = h1 > l1;

def edgeh = if flip then ll else hh;

def edgel = if flip then hh else ll;

def begin = Min(h1, l1);

def useclouds = ShowClouds and (h1 != 0 and l1 != 0);

def uselines = ShowLines and (h1 != 0 and l1 != 0);

def fib786 = if !flip then ll + dif * 0.786 else hh - dif * 0.786;

def fib618 = if !flip then ll + dif * 0.618 else hh - dif * 0.618;

def fib500 = if !flip then ll + dif * 0.500 else hh - dif * 0.500;

def fib382 = if !flip then ll + dif * 0.382 else hh - dif * 0.382;

def fib236 = if !flip then ll + dif * 0.236 else hh - dif * 0.236;

def cc = Min(75 * intensity / 2, 254);

def cc2 = Min(125 * intensity / 2, 254);

def cc3 = Min(175 * intensity / 2, 254);

plot p1 = if uselines then edgeh else Double.NaN;

plot p2 = if uselines then fib786 else Double.NaN;

plot p3 = if uselines then fib618 else Double.NaN;

plot p4 = if uselines then fib500 else Double.NaN;

plot p5 = if uselines then fib382 else Double.NaN;

plot p6 = if uselines then fib236 else Double.NaN;

plot p7 = if uselines then edgel else Double.NaN;

p1.SetStyle(Curve.SHORT_DASH);

p2.SetStyle(Curve.SHORT_DASH);

p3.SetStyle(Curve.SHORT_DASH);

p4.SetStyle(Curve.SHORT_DASH);

p5.SetStyle(Curve.SHORT_DASH);

p6.SetStyle(Curve.SHORT_DASH);

p7.SetStyle(Curve.SHORT_DASH);

p1.SetDefaultColor(CreateColor(0, cc3, cc));

p2.SetDefaultColor(CreateColor(0, cc2, cc));

p3.SetDefaultColor(CreateColor(0, cc, cc));

p4.SetDefaultColor(CreateColor(cc, cc, cc2));

p5.SetDefaultColor(CreateColor(cc, 0, cc));

p6.SetDefaultColor(CreateColor(cc2, 0, cc));

p7.SetDefaultColor(CreateColor(cc3, 0, cc));

p1.SetLineWeight(2);

p4.SetLineWeight(2);

p7.SetLineWeight(2);

# build the candle

def o;

def h;

def l;

def c;

o = GetValue(open, -0);

h = GetValue(high, -0);

l = GetValue(low, -0);

c = GetValue(close, -0);

# just the UP candles

def UpO;

def UpH;

def UpL;

def UpC;

if o <= c

then {

UpO = o;

UpH = h;

UpL = l;

UpC = c;

} else {

UpO = Double.NaN;

UpH = Double.NaN;

UpL = Double.NaN;

UpC = Double.NaN;

}

# just the DOWN candles

def DnO;

def DnH;

def DnL;

def DnC;

if o > c

then {

DnO = o;

DnH = h;

DnL = l;

DnC = c;

} else {

DnO = Double.NaN;

DnH = Double.NaN;

DnL = Double.NaN;

DnC = Double.NaN;

}

# Plot the new Chart

#addcloud(hh, ll, createcolor(cc, cc, cc), createcolor(cc, cc, cc));

AddCloud(if useclouds then edgeh else Double.NaN, fib786, CreateColor(0, cc3, cc2), CreateColor(0, cc3, cc2));

AddCloud(if useclouds then fib786 else Double.NaN, fib618, CreateColor(0, cc2, cc2), CreateColor(0, cc2, cc2));

AddCloud(if useclouds then fib618 else Double.NaN, fib500, CreateColor(0, cc, cc), CreateColor(0, cc, cc));

AddCloud(if useclouds then fib500 else Double.NaN, fib382, CreateColor(cc, 0, cc), CreateColor(cc, 0, cc));

AddCloud(if useclouds then fib382 else Double.NaN, fib236, CreateColor(cc2, 0, cc2), CreateColor(cc2, 0, cc2));

AddCloud(if useclouds then fib236 else Double.NaN, edgel, CreateColor(cc3, 0, cc2), CreateColor(cc3, 0, cc2));

AddChartBubble(BubbleSpot and ShowBubbles, edgeh[bm] , if !flip[bm] then "FIBHigh" else "FIBLow", if !flip[bm] then CreateColor(0, cc3, cc3) else CreateColor(cc3, 0, cc3), if !flip[bm] then yes else no);

AddChartBubble(BubbleSpot and ShowBubbles, Fib786[bm] , "78.6%", if !flip[bm] then CreateColor(0, cc3, cc2) else CreateColor(cc3, 0, cc2), if !flip[bm] then yes else no);

AddChartBubble(BubbleSpot and ShowBubbles, Fib618[bm] , "61.8%", if !flip[bm] then CreateColor(0, cc2, cc) else CreateColor(cc2, 0, cc), if !flip[bm] then yes else no);

AddChartBubble(BubbleSpot and ShowBubbles, Fib500[bm] , "50%", CreateColor(cc, cc, cc2), if !flip[bm] then yes else no);

AddChartBubble(BubbleSpot and ShowBubbles, Fib382[bm] , "38.2%", if !flip[bm] then CreateColor(cc2, 0, cc) else CreateColor(0, cc2, cc), if !flip[bm] then yes else no);

AddChartBubble(BubbleSpot and ShowBubbles, Fib236[bm] , "23.6%", if !flip[bm] then CreateColor(cc3, 0, cc2) else CreateColor(0, cc3, cc2), if !flip[bm] then yes else no);

AddChartBubble(BubbleSpot and ShowBubbles, edgel[bm] , if !flip[bm] then "FIBLow" else "FIBHigh", if !flip[bm] then CreateColor(cc3, 0, cc3) else CreateColor(0, cc3, cc3), if !flip[bm] then no else yes);