Fibonacci Pivot Points for ThinkOrSwim

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.

Screenshot 2023-06-01 130049.png

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:

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

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.
I'm a little lost here and can't find the new code you have mentioned. Where can I find this new code?
 
See above. Just added again as it appeared the image must have overlaid it before.
Thanks for the follow up SleepyZ.
it appears to have a minor glitch when choosing "no" onexpansion where it is plotting all other previous days only except for the current day needed. It will be great if we can also choose to see previous days or only current days bubbles.
Please see the attached for your reference.
 

Attachments

  • Screenshot 2023-06-02 at 8.52.26 AM.png
    Screenshot 2023-06-02 at 8.52.26 AM.png
    360 KB · Views: 121
Thanks for the follow up SleepyZ.
it appears to have a minor glitch when choosing "no" onexpansion where it is plotting all other previous days only except for the current day needed. It will be great if we can also choose to see previous days or only current days bubbles.
Please see the attached for your reference.

Thanks for letting me know! Try this

Screenshot 2023-06-02 101822.png
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 = -27;
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 if GetDay() == GetLastDay() and !onexpansion then IsNaN(close[-1]) and !IsNaN(close) else 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 if GetDay() == GetLastDay() and !onexpansion then IsNaN(close[-1]) and !IsNaN(close) else 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 if GetDay() == GetLastDay() and !onexpansion then IsNaN(close[-1]) and !IsNaN(close) else 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 if GetDay() == GetLastDay() and !onexpansion then IsNaN(close[-1]) and !IsNaN(close) else 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 if GetDay() == GetLastDay() and !onexpansion then IsNaN(close[-1]) and !IsNaN(close) else 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 if GetDay() == GetLastDay() and !onexpansion then IsNaN(close[-1]) and !IsNaN(close) else 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 if GetDay() == GetLastDay() and !onexpansion then IsNaN(close[-1]) and !IsNaN(close) else 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();

#
 
Thanks for letting me know! Try this
one minor glitch here. when choosing "no" onexpansion the show bubble "right" does not work, it only works with the "left" option. And in the other hand, when choosing "yes" onexpansion the bubbles can not be adjusted or moved whether using the "left" nor "right" plot options. I think maybe removing the show "left" or "right" option and changing it instead with show bubbles yes/no for both when "yes" onexpansion and "no" onexpansion will make it easier to move and adjust these bubbles.
 
Last edited:
one minor glitch here. when choosing "no" onexpansion the show bubble "right" does not work, it only works with the "left" option. And in the other hand, when choosing "yes" onexpansion the bubbles can not be adjusted or moved whether using the "left" nor "right" plot options.

This should fix the right bubble on the last day

The bubblemover only works on the left bubble as it was needed to avoid the bubble overlaying price action. The right and onexpansion bubble placement did not present this problem.

The right expansion in the settings must be greater than the bubblemover length for left to avoid a bubble in the right expansion next day's left bubble.

Screenshot 2023-06-02 141716.png
Screenshot 2023-06-02 141127.png
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 = -18;
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 if GetDay() == GetLastDay() then IsNaN(ppp[-1]) and !IsNaN(ppp) else 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 if GetDay() == GetLastDay() and !onexpansion then IsNaN(ps1[-1]) and !IsNaN(ps1) else 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 if GetDay() == GetLastDay() and !onexpansion then IsNaN(ps2[-1]) and !IsNaN(ps2) else 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 if GetDay() == GetLastDay() and !onexpansion then IsNaN(ps3[-1]) and !IsNaN(ps3) else 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 if GetDay() == GetLastDay() and !onexpansion then IsNaN(pr1[-1]) and !IsNaN(pr1) else 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 if GetDay() == GetLastDay() and !onexpansion then IsNaN(pr2[-1]) and !IsNaN(pr2) else 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 if GetDay() == GetLastDay() and !onexpansion then IsNaN(pr3[-1]) and !IsNaN(pr3) else 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();

#
 
This should fix the right bubble on the last day

The bubblemover only works on the left bubble as it was needed to avoid the bubble overlaying price action. The right and onexpansion bubble placement did not present this problem.

The right expansion in the settings must be greater than the bubblemover length for left to avoid a bubble in the right expansion next day's left bubble.
Thanks for the support once again SleepyZ. You are an absolute genius!
 
In appreciation of the tips shared in this thread, here's my "raw fib lines" indicator I wrote. It's based on previous period range. It displays "close price" so that you can see price in real time moving among the lines clearly.

Code:
input ShowTodayOnly = Yes;
def s=ShowTodayOnly;
def today=if s==0 OR getday()==getlastday() then 1 else 0;
def ThisWeek = if s==0 OR getweek()==getlastweek() then 1 else 0;
Def ThisMonth = if s==0 OR getmonth()==getlastmonth() then 1 else 0;
def c = close(period=aggregationperiod.day);

plot cl = if today == 1 then  (C) else double.nan;
cl.setpaintingstrategy(paintingstrategy.horizontal);
cl.setlineweight(3);

# ================================ FIB LINES ==================================

input lookback = 1;
input Timeframe = AggregationPeriod.week;

def Prange = high(period = Timeframe)[lookback] - low(period = Timeframe)[lookback];
def FibExt1 = 1.382;
def FibExt2 = 1.618;
def Two36 = .236;
def Three82 = .382;
def Six18 = .618;
def Seven86 = .786;
def PLevel = .886;

def yhigh = high(period = Timeframe)[1];

plot fib_ext_down2 = if today == 1 then yhigh - (Prange * FibExt2) else if
                        ThisWeek == 1 then yhigh - (Prange * FibExt2) else if
                        ThisMonth == 1 then yhigh - (Prange * FibExt2) else
                        double.nan;
fib_ext_down2.SetDefaultColor(Color.GREEN);
fib_ext_down2.SetStyle(Curve.FIRM);
fib_ext_down2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
    #fib_ext_down2.HideTitle();

plot fib_ext_down1 = if today == 1 then yhigh - (Prange * FibExt1) else if
                        ThisWeek == 1 then yhigh - (Prange * FibExt1) else if
                        ThisMonth == 1 then yhigh - (Prange * FibExt1) else
                        double.nan;
fib_ext_down1.SetDefaultColor(Color.YELLOW);
fib_ext_down1.SetStyle(Curve.FIRM);
fib_ext_down1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
#fib_ext_down1.HideTitle();

plot plow = if today == 1 then low(period = Timeframe)[lookback] else if
                ThisWeek == 1 then low(period=Timeframe)[lookback] else if
                ThisMonth == 1 then low(period=Timeframe)[lookback] else
                double.nan;
plow.SetDefaultColor(Color.GREEN);
plow.SetStyle(Curve.SHORT_DASH);
plow.SetLineWeight(2);
plow.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
    #plow.HideTitle();

plot LO114 = if today == 1 then yhigh - (Prange * Plevel) else if
                ThisWeek == 1 then yhigh - (Prange * Plevel) else if
                ThisMonth == 1 then yhigh - (Prange * Plevel) else
                double.nan;
LO114.SetDefaultColor(Color.CYAN);
LO114.SetStyle(Curve.FIRM);
LO114.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

plot twentythreesix = if today == 1 then plow + (prange * two36) else double.nan;
twentythreesix.setdefaultcolor(color.yellow);
twentythreesix.setstyle(curve.firm);
twentythreesix.setpaintingstrategy(paintingstrategy.horizontal);


plot ThirtyEightTwo = if today == 1 then plow + (Prange * Three82) else if
                        ThisWeek == 1 then plow + (Prange * Three82) else if
                        ThisMonth == 1 then plow + (Prange * Three82) else                     
                        double.nan;
ThirtyEightTwo.SetDefaultColor(Color.MAGENTA);
ThirtyEightTwo.SetStyle(Curve.FIRM);
ThirtyEightTwo.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
    #ThirtyEightTwo.HideTitle();

plot fib_mid = if today == 1 then (yhigh + plow) / 2 else if
                  ThisWeek == 1 then (yhigh + plow) / 2 else if
                  ThisMonth == 1 then (yhigh + plow) / 2 else
                  double.nan;
fib_mid.SetDefaultColor(CreateColor(255, 140, 0));
fib_mid.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
    #fib_mid.setdefaultcolor(color.gray);
fib_mid.SetStyle(Curve.FIRM);
fib_mid.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
    #fib_mid.hidetitle();

plot sixtyoneeight = if today == 1 then plow + (Prange * Six18) else if
                        ThisWeek == 1 then plow + (Prange * Six18) else if
                        ThisMonth == 1 then plow + (Prange * Six18) else
                        double.nan;
sixtyoneeight.SetDefaultColor(Color.MAGENTA);
sixtyoneeight.SetStyle(Curve.FIRM);
sixtyoneeight.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
    #sixtyoneeight.HideTitle();

plot SevenEightSix = if today == 1 then  plow + (prange * seven86) else if
                        ThisWeek == 1 then  plow + (prange * seven86) else if
                        ThisMonth == 1 then  plow + (prange * seven86) else
                        double.nan;
SevenEightSix.setdefaultcolor(color.yellow);
SevenEightSix.setstyle(curve.firm);
SevenEightSix.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

plot HI886 = if today == 1 then plow + (Prange * PLevel) else if
                ThisWeek == 1 then plow + (Prange * PLevel) else if
                ThisMonth == 1 then plow + (Prange * PLevel) else
                double.nan;
HI886.SetDefaultColor(Color.CYAN);
HI886.SetStyle(Curve.FIRM);
HI886.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

plot phigh = if today == 1 then yhigh else if
                ThisWeek == 1 then yhigh else if
                ThisMonth == 1 then yhigh else
                double.nan;
phigh.SetDefaultColor(Color.RED);
phigh.SetStyle(Curve.SHORT_DASH);
phigh.SetLineWeight(2);
phigh.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
    #phigh.HideTitle();

plot fib_ext_up1 = if today == 1 then plow + (Prange * FibExt1) else if
                      ThisWeek == 1 then plow + (Prange * FibExt1) else if
                      ThisMonth == 1 then plow + (Prange * FibExt1) else
                      double.nan;
fib_ext_up1.SetDefaultColor(Color.YELLOW);
fib_ext_up1.SetStyle(Curve.FIRM);
fib_ext_up1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
    #fib_ext_up1.HideTitle();

plot fib_ext_up2 = if today == 1 then plow + (Prange * FibExt2) else if
                      ThisWeek == 1 then plow + (Prange * FibExt2) else if
                      ThisMonth == 1 then plow + (Prange * FibExt2) else
                      double.nan;
fib_ext_up2.SetDefaultColor(Color.RED);
fib_ext_up2.SetStyle(Curve.FIRM);
fib_ext_up2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
471 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top