#fib_peaks_latest
#https://usethinkscript.com/threads/fib-retracement-based-on-the-high-low-from-market-open-close.19588/
#fib retracement based on the high / low from market open / close
#https://usethinkscript.com/threads/zigzag-high-low-with-supply-demand-zones-for-thinkorswim.172/#post-7048
#post10
#robert
input Fib_basis = {default Pivot, RTHours};
def rth = SecondsFromTime(0930) >= 0 and SecondsFromTime(1600) <= 0;
def na = Double.NaN;
def bn = BarNumber();
#def lastBar = HighestAll(if IsNaN(close) then 0 else bn);
def lastbn = HighestAll(if IsNaN(close) then 0 else bn);
def lastbar = bn == lastbn;
#def lastbar = (!isnan(close) and isnan(close[-1]));
def highx = high;
def lowx = low;
input length = 7;
def offset = Min(length - 1, lastbn - bn);
def peak = highx > Highest(highx[1], length - 1) and highx == GetValue(Highest(highx, length), -offset);
def valley = lowx < Lowest(lowx[1], length - 1) and lowx == GetValue(Lowest(lowx, length), -offset);
input arrows = no;
plot zhi = if arrows and peak then high * 1.001 else na;
plot zlo = if arrows and valley then low * 0.999 else na;
zlo.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
zlo.SetDefaultColor(Color.RED);
zlo.SetLineWeight(1);
zlo.HideBubble();
zhi.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
zhi.SetDefaultColor(Color.GREEN);
zhi.SetLineWeight(1);
zhi.HideBubble();
# identify the very last peak point
def lastpeakbn = HighestAll(if peak then bn else 0);
def lastpeakvalue = if bn == lastpeakbn then high else lastpeakvalue[1];
#plot hi1 = if bn < lastpeakbn then Double.NaN else lastpeakvalue;
#hi1.SetDefaultColor(Color.green);
# identify the very last valley point
def lastvalleybn = HighestAll(if valley then bn else 0);
def lastvalleyvalue = if bn == lastvalleybn then low else lastvalleyvalue[1];
#plot low1 = if bn < lastvalleybn then Double.NaN else lastvalleyvalue;
#low1.SetDefaultColor(Color.RED);
def diag1 = if bn == 1 then na
else if bn == lastpeakbn then high
else if bn == lastvalleybn then low
else na;
plot zdiag1 = diag1;
zdiag1.EnableApproximation();
zdiag1.SetDefaultColor(Color.YELLOW);
#zdiag1.setlineweight(1);
zdiag1.HideBubble();
zdiag1.SetStyle(Curve.MEDIUM_DASH);
def lastpvbn = Max(lastpeakbn, lastvalleybn);
#High/Low defined using Volumeprofile data
def bars = 100000;
def period = bn - 1;
def count = CompoundValue(1,
if rth and period != period[1]
then (count[1] + period - period[1]) % bars
else count[1], 0);
def cond = count < count[1] + period - period[1];
profile vol = VolumeProfile("startNewProfile" = cond, "onExpansion" = no, "numberOfProfiles" = bars, "pricePerRow" = PricePerRow.TICKSIZE , "value area percent" = 0);
def hProfile = if rth and IsNaN(vol.GetHighest())
then hProfile[1]
else vol.GetHighest();
def lProfile = if rth and IsNaN(vol.GetLowest())
then lProfile[1]
else vol.GetLowest();
def RTH_high = if rth then hProfile else na;
def RTH_low = if rth then lProfile else na;
plot hi = if Fib_basis == Fib_basis.RTHours then RTH_high else if Fib_basis == Fib_basis.Pivot and bn > lastpvbn then lastpeakvalue else na;
plot lo = if Fib_basis == Fib_basis.RTHours then RTH_low else if Fib_basis == Fib_basis.Pivot and bn > lastpvbn then lastvalleyvalue else na;
hi.SetDefaultColor(Color.WHITE);
lo.SetDefaultColor(Color.WHITE);
def hh = if GetDay() == GetLastDay() and SecondsFromTime(0930) == 0 then high else if rth and high > hh[1] then high else hh[1];
def ll = if GetDay() != GetLastDay() and SecondsFromTime(0930) == 0 then low else if rth and low < ll[1] then low else ll[1];
def hibar = if high == (hh) then bn else hibar[1];
def lobar = if low == (ll) then bn else lobar[1];
#AddChartBubble(1, low, bn + "\n" + hibar + "\n" + lobar, Color.WHITE, no);
def c0 = 0.000;
def c1 = 0.236;
def c2 = 0.382;
def c3 = 0.500;
def c4 = 0.618;
def c5 = 0.786;
def c6 = 1.000;
def rng = hi - lo;
def f1;
def f2;
def f3;
def f4;
def f5;
def dir = if Fib_basis == Fib_basis.RTHours
then if lobar < hibar
then -1
else 1
else if Fib_basis == Fib_basis.Pivot
then if lastpeakbn > lastvalleybn
then -1
else 1
else dir[1];
if dir > 0
then {
f1 = lo + (rng * c1);
f2 = lo + (rng * c2);
f3 = lo + (rng * c3);
f4 = lo + (rng * c4);
f5 = lo + (rng * c5);
} else {
f1 = hi - (rng * c1);
f2 = hi - (rng * c2);
f3 = hi - (rng * c3);
f4 = hi - (rng * c4);
f5 = hi - (rng * c5);
}
plot zf1 = if Fib_basis == Fib_basis.RTHours
then if GetDay() != GetLastDay() or bn < Max(HighestAll(hibar), HighestAll(lobar))
then na
else f1
else if Fib_basis == Fib_basis.Pivot
then f1
else na;
plot zf2 = if Fib_basis == Fib_basis.RTHours
then if GetDay() != GetLastDay() or bn < Max(HighestAll(hibar), HighestAll(lobar))
then na
else f2
else if Fib_basis == Fib_basis.Pivot
then f2
else na;
plot zf3 = if Fib_basis == Fib_basis.RTHours
then if GetDay() != GetLastDay() or bn < Max(HighestAll(hibar), HighestAll(lobar))
then na
else f3
else if Fib_basis == Fib_basis.Pivot
then f3
else na;
plot zf4 = if Fib_basis == Fib_basis.RTHours
then if GetDay() != GetLastDay() or bn < Max(HighestAll(hibar), HighestAll(lobar))
then na
else f4
else if Fib_basis == Fib_basis.Pivot
then f4
else na;
plot zf5 = if Fib_basis == Fib_basis.RTHours
then if GetDay() != GetLastDay() or bn < Max(HighestAll(hibar), HighestAll(lobar))
then na
else f5
else if Fib_basis == Fib_basis.Pivot
then f5
else na;
zf1.SetDefaultColor(Color.GRAY);
zf2.SetDefaultColor(Color.ORANGE);
zf3.SetDefaultColor(Color.MAGENTA);
zf4.SetDefaultColor(Color.ORANGE);
zf5.SetDefaultColor(Color.GRAY);
input show_bubbles = yes;
input bubblemover = 2;
def b = bubblemover;
def b1 = b + 1;
AddChartBubble(show_bubbles and (if Fib_basis == Fib_basis.Pivot then bn == HighestAll(bn - bubblemover) else IsNaN(close[b]) and !IsNaN(close[b1]) or !IsNaN(hi[b1]) and IsNaN(hi [b] )) , hi[b1], if dir[b1] < 0 then 0 else 1, hi.TakeValueColor());
zf1.HideBubble();
AddChartBubble(show_bubbles and (if Fib_basis == Fib_basis.Pivot then bn == HighestAll(bn - bubblemover) else IsNaN(close[b]) and !IsNaN(close[b1]) or !IsNaN(lo[b1]) and IsNaN(lo [b] )) , lo[b1], if dir[b1] < 0 then 1 else 0, lo.TakeValueColor());
zf1.HideBubble();
AddChartBubble(show_bubbles and (if Fib_basis == Fib_basis.Pivot then bn == HighestAll(bn - bubblemover) else IsNaN(close[b]) and !IsNaN(close[b1]) or !IsNaN(zf1[b1]) and IsNaN(zf1 [b] )) , zf1[b1], c1, zf1.TakeValueColor());
zf1.HideBubble();
AddChartBubble(show_bubbles and (if Fib_basis == Fib_basis.Pivot then bn == HighestAll(bn - bubblemover) else IsNaN(close[b]) and !IsNaN(close[b1]) or !IsNaN(zf2[b1]) and IsNaN(zf2 [b] )) , zf2[b1], c2, zf2.TakeValueColor());
zf2.HideBubble();
AddChartBubble(show_bubbles and (if Fib_basis == Fib_basis.Pivot then bn == HighestAll(bn - bubblemover) else IsNaN(close[b]) and !IsNaN(close[b1]) or !IsNaN(zf3[b1]) and IsNaN(zf3 [b] )) , zf3[b1], c3, zf3.TakeValueColor());
zf3.HideBubble();
AddChartBubble(show_bubbles and (if Fib_basis == Fib_basis.Pivot then bn == HighestAll(bn - bubblemover) else IsNaN(close[b]) and !IsNaN(close[b1]) or !IsNaN(zf4[b1]) and IsNaN(zf4 [b] )) , zf4[b1], c4, zf4.TakeValueColor());
zf4.HideBubble();
AddChartBubble(show_bubbles and (if Fib_basis == Fib_basis.Pivot then bn == HighestAll(bn - bubblemover) else IsNaN(close[b]) and !IsNaN(close[b1]) or !IsNaN(zf5[b1]) and IsNaN(zf5 [b] )) , zf5[b1], c5, zf5.TakeValueColor());
zf5.HideBubble();
#