#Auto_Fib_on_Displaced_Bar
input aggmode = {default current, agg_input};
input agg = AggregationPeriod.FIVE_MIN;
input onexpansion = yes;
input displace = 1;
input Fpos236 = .236;
input Fpos382 = .382;
input Fpos50 = .5;
input Fpos618 = .618;
input Fpos764 = .764;
def hi;
def lo;
switch (aggmode) {
case current:
hi = high[displace];
lo = low[displace];
case agg_input:
hi = high(period = agg)[displace];
lo = low(period = agg)[displace];
}
def rhi = if IsNaN(close) then rhi[1] else hi;
def rlo = if IsNaN(close) then rlo[1] else lo;
def na = Double.NaN;
def range = rhi - rlo;
def F236 = rhi - (range * Fpos236);
def F382 = rhi - (range * Fpos382);
def F50 = rhi - (range * Fpos50);
def F618 = rhi - (range * Fpos618);
def F764 = rhi - (range * Fpos764);
plot ORH;
plot ORL;
plot Fib50;
plot Fib236;
plot Fib382;
plot Fib618;
plot Fib764;
ORH = if onexpansion and !IsNaN(close) then na else rhi;
ORL = if onexpansion and !IsNaN(close) then na else rlo;
Fib50 = if onexpansion and !IsNaN(close) then na else F50;
Fib236 = if onexpansion and !IsNaN(close) then na else F236;
Fib382 = if onexpansion and !IsNaN(close) then na else F382;
Fib618 = if onexpansion and !IsNaN(close) then na else F618;
Fib764 = if onexpansion and !IsNaN(close) then na else F764;
ORH.SetDefaultColor(Color.GREEN);
ORH.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ORH.SetLineWeight(2);
ORL.SetDefaultColor(Color.RED);
ORL.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ORL.SetLineWeight(2);
Fib50.SetDefaultColor(Color.WHITE);
Fib50.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Fib50.SetLineWeight(1);
Fib236.SetDefaultColor(Color.CYAN);
Fib236.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Fib236.SetLineWeight(1);
Fib382.SetDefaultColor(Color.MAGENTA);
Fib382.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Fib382.SetLineWeight(1);
Fib618.SetDefaultColor(Color.YELLOW);
Fib618.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Fib618.SetLineWeight(1);
Fib764.SetDefaultColor(Color.DARK_RED);
Fib764.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Fib764.SetLineWeight(2);
input showfib_bubbles = yes;
input n = 1;
def n1 = n + 1;
AddChartBubble(showfib_bubbles and !IsNaN(close[n1 + if onexpansion then 1 else 0]) and IsNaN(close[n]), ORH[n1], ORH[n1], Color.GREEN, yes);
AddChartBubble(showfib_bubbles and !IsNaN(close[n1 + if onexpansion then 1 else 0]) and IsNaN(close[n]), ORL[n1], ORL[n1], Color.RED, no);
AddChartBubble(showfib_bubbles and !IsNaN(close[n1 + if onexpansion then 1 else 0]) and IsNaN(close[n]), Fib618[n1], AsPercent(Fpos618), Color.CYAN, yes);
AddChartBubble(showfib_bubbles and !IsNaN(close[n1 + if onexpansion then 1 else 0]) and IsNaN(close[n]), Fib236[n1], AsPercent(Fpos236), Color.YELLOW, yes);
AddChartBubble(showfib_bubbles and !IsNaN(close[n1 + if onexpansion then 1 else 0]) and IsNaN(close[n]), Fib50[n1], AsPercent(Fpos50), Color.YELLOW, yes);
AddChartBubble(showfib_bubbles and !IsNaN(close[n1 + if onexpansion then 1 else 0]) and IsNaN(close[n]), Fib382[n1], AsPercent(Fpos382), Color.WHITE, yes);
AddChartBubble(showfib_bubbles and !IsNaN(close[n1 + if onexpansion then 1 else 0]) and IsNaN(close[n]), Fib764[n1], AsPercent(Fpos764), Color.YELLOW, yes);
#