Hi SleepyZ!
The show on expansion side of this script works perfectly fine and I love it! I just have some additional requests to fix the plots for the non expansion side of the chart if possible.
1. Can you change this so when choosing "show no" on expansion the plots does not go over the expansion side? the plots can stop with the very last candle on the chart. I need to keep the expansion side clean to avoid overlapping studies.
2. Is there anyway possible the coefficient and price info are not a fixed bubble to the left side of the screen when choosing "show values on left" but follows the current chart just like the actual Fibonacci Retracements tool does in TOS?
Attaching images for your reference.
1. This should keep the plot of the lines within the active bars. The bubbles will work for left and onexpansion before the lastbaroftheday. The right will only work for lastbarofday occurring.
2. The labels created with drawing tool can only be created in thinkscript with bubbles. I have reformated the bubbles to only include what you depicted in the drawing tool. The left bubbles can be adjusted with the input bubblemover.
Code:
# IB_Fib_Pivots
# Author: Kory Gill, @korygill
#
# VERSION HISTORY (sortable date and time (your local time is fine), and your initials
# 20190906-1900-KG - Created.
# 20230601 Sleepyz - Added Onexpansion and Bubbles
# ...
declare hide_on_daily;
declare once_per_bar;
input AggregationPeriod = AggregationPeriod.DAY;
#
# logic
#
def nan = Double.NaN;
def isRollover = GetYYYYMMDD() != GetYYYYMMDD()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def afterEnd = GetTime() > RegularTradingEnd(GetYYYYMMDD());
def firstBarOfDay = if (beforeStart[1] == 1 and beforeStart == 0) or (isRollover and beforeStart == 0) then 1 else 0;
def lastBarOfDay = if
(afterEnd[-1] == 1 and afterEnd == 0) or
(isRollover[-1] and firstBarOfDay[-1])
then 1
else 0;
input onexpansion = no;
def pc = close(period = AggregationPeriod)[1];
def ph = high(period = AggregationPeriod)[1];
def pl = low(period = AggregationPeriod)[1];
def pcext = if IsNaN(close) then pcext[1] else pc;
def phext = if IsNaN(close) then phext[1] else ph;
def plext = if IsNaN(close) then plext[1] else pl;
def delta = if onexpansion then phext - plext else ph - pl;
def pp = if onexpansion then (pcext[1] + phext[1] + plext[1]) / 3 else if firstBarOfDay then (pc + ph + pl) / 3 else if lastBarOfDay or IsNaN(close) then nan else pp[1];
def s1 = if onexpansion then pp - (.382 * delta) else if firstBarOfDay then pp - (.382 * delta) else if lastBarOfDay or IsNaN(close) then nan else s1[1];
def s2 = if onexpansion then pp - (.618 * delta) else if firstBarOfDay then pp - (.618 * delta) else if lastBarOfDay or IsNaN(close) then nan else s2[1];
def s3 = if onexpansion then pp - (1 * delta) else if firstBarOfDay then pp - (1 * delta) else if lastBarOfDay or IsNaN(close) then nan else s3[1];
def r1 = if onexpansion then pp + (.382 * delta) else if firstBarOfDay then pp + (.382 * delta) else if lastBarOfDay or IsNaN(close) then nan else r1[1];
def r2 = if onexpansion then pp + (.618 * delta) else if firstBarOfDay then pp + (.618 * delta) else if lastBarOfDay or IsNaN(close) then nan else r2[1];
def r3 = if onexpansion then pp + (1 * delta) else if firstBarOfDay then pp + (1 * delta) else if lastBarOfDay or IsNaN(close) then nan else r3[1];
plot ppp = if onexpansion and !IsNaN(close) then Double.NaN else pp;
plot ps1 = if onexpansion and !IsNaN(close) then Double.NaN else s1;
plot ps2 = if onexpansion and !IsNaN(close) then Double.NaN else s2;
plot ps3 = if onexpansion and !IsNaN(close) then Double.NaN else s3;
plot pr1 = if onexpansion and !IsNaN(close) then Double.NaN else r1;
plot pr2 = if onexpansion and !IsNaN(close) then Double.NaN else r2;
plot pr3 = if onexpansion and !IsNaN(close) then Double.NaN else r3;
ppp.SetDefaultColor(Color.RED);
ps1.SetDefaultColor(Color.GREEN);
ps2.SetDefaultColor(Color.WHITE);
ps3.SetDefaultColor(Color.MAGENTA);
pr1.SetDefaultColor(Color.YELLOW);
pr2.SetDefaultColor(Color.VIOLET);
pr3.SetDefaultColor(Color.CYAN);
input show_bubble = yes;
input show_price = yes;
input show_coeff = yes;
input bubble_display = {default left, right, none};
input left_bubblemover = -19;
def b = left_bubblemover;
def b1 = b + 1;
AddChartBubble(
(if show_bubble and !onexpansion
then if bubble_display == bubble_display.left
then if IsNaN(close) then Double.NaN else IsNaN(ppp[b1]) and !IsNaN(ppp[b])
else if bubble_display == bubble_display.right
then lastBarOfDay == 0 and lastBarOfDay[-1] == 1
else Double.NaN
else if show_bubble and onexpansion
then BarNumber() == HighestAll(BarNumber())
else Double.NaN), if bubble_display == bubble_display.right then ppp else ppp[b], if show_price then AsPercent(0) + " " + if bubble_display == bubble_display.right then AsText(ppp) else AsText(ppp[b]) else ""
, ppp.TakeValueColor() );
AddChartBubble(
(if show_bubble and !onexpansion
then if bubble_display == bubble_display.left
then if IsNaN(close) then Double.NaN else IsNaN(ppp[b1]) and !IsNaN(ppp[b])
else if bubble_display == bubble_display.right
then lastBarOfDay == 0 and lastBarOfDay[-1] == 1
else Double.NaN
else if show_bubble and onexpansion
then BarNumber() == HighestAll(BarNumber())
else Double.NaN), if bubble_display == bubble_display.right then ps1 else ps1[b],
(if show_coeff then AsPercent(-.382) else "") +
if show_price then "" + if bubble_display == bubble_display.right then AsText(ps1) else AsText(ps1[b]) else "", ps1.TakeValueColor());
AddChartBubble(
(if show_bubble and !onexpansion
then if bubble_display == bubble_display.left
then if IsNaN(close) then Double.NaN else IsNaN(ppp[b1]) and !IsNaN(ppp[b])
else if bubble_display == bubble_display.right
then lastBarOfDay == 0 and lastBarOfDay[-1] == 1
else Double.NaN
else if show_bubble and onexpansion
then BarNumber() == HighestAll(BarNumber())
else Double.NaN), if bubble_display == bubble_display.right then ps2 else ps2[b],
(if show_coeff then AsPercent(-.618) else "") +
if show_price then "" + if bubble_display == bubble_display.right then AsText(ps2) else AsText(ps2[b]) else "", ps2.TakeValueColor());
AddChartBubble(
(if show_bubble and !onexpansion
then if bubble_display == bubble_display.left
then if IsNaN(close) then Double.NaN else IsNaN(ppp[b1]) and !IsNaN(ppp[b])
else if bubble_display == bubble_display.right
then lastBarOfDay == 0 and lastBarOfDay[-1] == 1
else Double.NaN
else if show_bubble and onexpansion
then BarNumber() == HighestAll(BarNumber())
else Double.NaN), if bubble_display == bubble_display.right then ps3 else ps3[b],
(if show_coeff then AsPercent(-1.0) else "") +
if show_price then "" + if bubble_display == bubble_display.right then AsText(ps3) else AsText(ps3[b]) else "", ps3.TakeValueColor());
AddChartBubble(
(if show_bubble and !onexpansion
then if bubble_display == bubble_display.left
then if IsNaN(close) then Double.NaN else IsNaN(ppp[b1]) and !IsNaN(ppp[b])
else if bubble_display == bubble_display.right
then lastBarOfDay == 0 and lastBarOfDay[-1] == 1
else Double.NaN
else if show_bubble and onexpansion
then BarNumber() == HighestAll(BarNumber())
else Double.NaN), if bubble_display == bubble_display.right then pr1 else pr1[b],
(if show_coeff then AsPercent(.382) else "") +
if show_price then "" + if bubble_display == bubble_display.right then AsText(pr1) else AsText(pr1[b]) else "", pr1.TakeValueColor());
AddChartBubble(
(if show_bubble and !onexpansion
then if bubble_display == bubble_display.left
then if IsNaN(close) then Double.NaN else IsNaN(ppp[b1]) and !IsNaN(ppp[b])
else if bubble_display == bubble_display.right
then lastBarOfDay == 0 and lastBarOfDay[-1] == 1
else Double.NaN
else if show_bubble and onexpansion
then BarNumber() == HighestAll(BarNumber())
else Double.NaN), if bubble_display == bubble_display.right then pr2 else pr2[b],
(if show_coeff then AsPercent(.618) else "") +
if show_price then "" + if bubble_display == bubble_display.right then AsText(pr2) else AsText(pr2[b]) else "", pr2.TakeValueColor());
AddChartBubble(
(if show_bubble and !onexpansion
then if bubble_display == bubble_display.left
then if IsNaN(close) then Double.NaN else IsNaN(ppp[b1]) and !IsNaN(ppp[b])
else if bubble_display == bubble_display.right
then lastBarOfDay == 0 and lastBarOfDay[-1] == 1
else Double.NaN
else if show_bubble and onexpansion
then BarNumber() == HighestAll(BarNumber())
else Double.NaN), if bubble_display == bubble_display.right then pr3 else pr3[b],
(if show_coeff then AsPercent(1.0) else "") +
if show_price then "" + if bubble_display == bubble_display.right then AsText(pr3) else AsText(pr3[b]) else "", pr3.TakeValueColor());
ppp.HideBubble();
ps1.HideBubble();
ps2.HideBubble();
ps3.HideBubble();
pr1.HideBubble();
pr2.HideBubble();
pr3.HideBubble();
#
Last edited: