High and Low with Fibonacci Retracement Indicator for ThinkorSwim

Hi @SleepyZ ,
I am using the code you posted in post #18 and flipping the retracement in either an uptrend or downtrend doesn't appear to be working if you select No for "Show Today Only". Is it possible to have it flip for the previous time period aggregations? Right now, it flips all of them based on the current timeframe.
O4Hr0Uh.png

That was in response to what BTExpress posted as his latest version of what he was using. That version was not what I had previously posted in post #16. I often only respond to the request posed and the code presented by the requestor. I did not notice that BTExpress's code was diffeerent in post #17 than what I posted in post #16. Sorry for any confusion.

Here is that previous code in post #16, with an adjustment to the def ymd, that I just noticed was needed to account for higher aggregationperiod choices than DAY. The def ymd was part of logic to flip the fibs.

This should work now for your situation and the higher agg choices than DAY. The code in post #16 was revised to the below code.

Thank you
Ruby:
#
# WalkingBallista & BenTen
# https://usethinkscript.com/d/153-high-and-low-with-fibonacci-retracement-indicator-for-thinkorswim
input aggregationPeriod = AggregationPeriod.DAY;
input ShowTodayOnly     = yes;
input showBubble        = yes;
input length            = 21;

def bn = BarNumber();
def hh = high(period=aggregationPeriod);
def ll = low(period=aggregationPeriod);
def ymd      = if aggregationPeriod == aggregationPeriod.DAY
               then getday()
               else if aggregationPeriod == aggregationPeriod.WEEK
               then getweek()
               else if aggregationPeriod == aggregationPeriod.MONTH
               then getmonth()
               else getyear();
def candles  = !IsNaN(close);
def capture  = candles and ymd != ymd[1];
def dayCount = CompoundValue(1, if capture then dayCount[1] + 1 else dayCount[1], 0);
def thisDay  = (HighestAll(dayCount) - dayCount) + 1 ;

addlabel(highestall(thisday)<21, "Chart Time Interval set to " + highestall(thisday) +" days. Chart Time Interval must be set to at least 20 days");

def ph = if thisDay == 21 then hh
         else if thisDay < 21 and hh > ph[1]
         then hh else ph[1];
def pl = if bn == 1 then low
         else if thisDay == 21
         then ll
         else if thisDay < 21 and ll < pl[1]
         then ll else pl[1];
def phbar = if hh == ph then bn else Double.NaN;
def plbar = if ll  == pl then bn else Double.NaN;

#AddLabel(1, ph + " " + highestall(phbar) + " " + pl + " " + HighestAll(plbar));

def day     = GetDay();
def Month   = if GetMonth() == GetLastMonth() then 1 else 0;
def year    = GetYear();
def lastDay = GetLastDay();
def lastmonth = GetLastMonth();
def lastyear  = GetLastYear();
def isToday   = If(day == lastDay and Month == lastmonth and year == lastyear, 1, 0);

def istodaybarnumber = Highest(if isToday then bn else Double.NaN, length);

def PO    = open(period = aggregationPeriod);
def Today = if GetDay() == GetLastDay() then 1 else 0;
def Week  = if GetWeek() == GetLastWeek() then 1 else 0;

def range = ph - pl;

input fib  = 1.618;
input fib1 = 1.272;
input fib2 = 0.618;
input fib3 = 0.236;
input fib4 = 0.382;
input fib5 = 0.5;
input fib6 = 0.786;
input fib7 = 2.618;

plot h;
plot h1;
plot h2;
plot h3;
plot h4;
plot h5;
plot h6;
plot h7;
plot h8;
plot h9;
plot h10;
plot highofline;
plot lowofline;


if ShowTodayOnly and !IsNaN(close(period = aggregationPeriod)[-1])
then {
    h   = Double.NaN;
    h1  = Double.NaN;
    h2  = Double.NaN;
    h3  = Double.NaN;
    h4  = Double.NaN;
    h5  = Double.NaN;
    h6  = Double.NaN;
    h7  = Double.NaN;
    h8  = Double.NaN;
    h9  = Double.NaN;
    h10 = Double.NaN;
    highofline = Double.NaN;
    lowofline  = Double.NaN;
} else {

    if HighestAll(phbar) > HighestAll(plbar)
    then {
        h  = ph + range * (fib);
        h1 = ph + range * (fib1);
        h2 = ph - range * (fib2);
        h3 = ph - range * (fib3);
        h4 = ph - range * (fib4);
        h5 = ph - range * (fib5);
        h6 = ph - range * (fib6);
        h7 = ph - range * (fib7);
        h8 = pl + range * (fib1);
        h9 = pl + range * (fib);
        h10 = pl + range * (fib7);
        highofline = LowestAll(pl);
        lowofline  = HighestAll(ph);
    }
    else
    {
        h  = ph + range * (fib - 1);
        h1 = ph + range * (fib1 - 1);
        h2 = ph + range * (fib2 - 1);
        h3 = ph + range * (fib3 - 1);
        h4 = ph + range * (fib4 - 1);
        h5 = ph + range * (fib5 - 1);
        h6 = ph + range * (fib6 - 1);
        h7 = ph + range * (fib7 - 1);
        h8 = pl - range * (fib1 - 1);
        h9 = pl - range * (fib - 1);
        h10 = pl - range * (fib7 - 1);
        highofline = HighestAll(ph);
        lowofline  = LowestAll(pl);
    }

}

h.DefineColor("Color", CreateColor(50, 150, 200));
h.AssignValueColor(h.Color("Color"));
h.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

h1.DefineColor("Color", Color.MAGENTA);
h1.AssignValueColor(h1.Color("Color"));
h1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

h2.DefineColor("Color", Color.GREEN);
h2.AssignValueColor(h2.Color("Color"));
h2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

h3.DefineColor("Color", Color.RED);
h3.AssignValueColor(h3.Color("Color"));
h3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

h4.DefineColor("Color", Color.GREEN);
h4.AssignValueColor(h4.Color("Color"));
h4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

h5.DefineColor("Color", Color.YELLOW);
h5.AssignValueColor(h5.Color("Color"));
h5.SetPaintingStrategy(PaintingStrategy.DASHES);

h6.DefineColor("Color", Color.RED);
h6.AssignValueColor(h6.Color("Color"));
h6.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

h7.DefineColor("Color", CreateColor(100, 100, 200));
h7.AssignValueColor(h7.Color("Color"));
h7.SetPaintingStrategy(PaintingStrategy.LINE);

h8.DefineColor("Color", Color.MAGENTA);
h8.AssignValueColor(h8.Color("Color"));
h8.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

h9.DefineColor("Color", CreateColor(50, 150, 200));
h9.AssignValueColor(h9.Color("Color"));
h9.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

h10.DefineColor("Color", CreateColor(100, 100, 200));
h10.AssignValueColor(h10.Color("Color"));
h10.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);


highofline.DefineColor("Color", Color.WHITE);
highofline.AssignValueColor(highofline.Color("Color"));
highofline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);


lowofline.DefineColor("Color", Color.WHITE);
lowofline.AssignValueColor(highofline.Color("Color"));
lowofline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

input n = 3;
def n1 = n + 1;
#AddChartBubble(isnan(close[n]) and  !IsNaN(close[n1]), h4[n], concat(concat(fib4*100,"%") , concat(" $", round(h4[n1], 2))), color.green, yes);
AddChartBubble(IsNaN(close[n]) and  !IsNaN(close[n1]), lowofline[n1], Concat(Concat(0, "%") , Concat(" $", Round(lowofline[n1], 2))), Color.WHITE, yes);
AddChartBubble(  IsNaN(close[n]) and  !IsNaN(close[n1]), highofline[n1], Concat(Concat(100, "%") , Concat(" $", Round(highofline[n1], 2))), Color.WHITE, yes);
AddChartBubble( IsNaN(close[n]) and  !IsNaN(close[n1]), h4[n1], Concat(Concat(fib4 * 100, "%") , Concat(" $", Round(h4[n1], 2))), Color.GREEN, yes);
AddChartBubble( IsNaN(close[n]) and  !IsNaN(close[n1]), h2[n1], Concat(Concat(fib2 * 100, "%") , Concat(" $", Round(h2[n1], 2))), Color.GREEN, yes);
AddChartBubble( IsNaN(close[n]) and  !IsNaN(close[n1]), h3[n1], Concat(Concat(fib3 * 100, "%") , Concat(" $", Round(h3[n1], 2))), Color.RED, yes);
AddChartBubble( IsNaN(close[n]) and  !IsNaN(close[n1]), h5[n1], Concat(Concat(fib5 * 100, "%") , Concat(" $", Round(h5[n1], 2))), Color.YELLOW, yes);
AddChartBubble( IsNaN(close[n]) and  !IsNaN(close[n1]), h6[n1], Concat(Concat(fib6 * 100, "%") , Concat(" $", Round(h6[n1], 2))), Color.RED, yes);
 
@SleepyZ , thanks for the updated code. I made some changes so that the variable for length is used throughout the code. However, the levels are off for all previous days on the charts. For example, use a 30min chart, set the indicator to use the "Day" aggregation, select a length of 1, and select No for "Show Today Only". Below is what it looks like and the chart is smashed together. After you expand the chart, the levels are correct for the current day but are off for the rest.

VHCosMM.png


Below is the updated code.

Code:
#

# WalkingBallista & BenTen

# https://usethinkscript.com/d/153-high-and-low-with-fibonacci-retracement-indicator-for-thinkorswim

input aggregationPeriod = AggregationPeriod.DAY;

input ShowTodayOnly     = yes;

input showBubble        = yes;

input length            = 21;



def bn = BarNumber();

def hh = high(period=aggregationPeriod);

def ll = low(period=aggregationPeriod);

def ymd      = if aggregationPeriod == aggregationPeriod.DAY

               then getday()

               else if aggregationPeriod == aggregationPeriod.WEEK

               then getweek()

               else if aggregationPeriod == aggregationPeriod.MONTH

               then getmonth()

               else getyear();

def candles  = !IsNaN(close);

def capture  = candles and ymd != ymd[1];

def dayCount = CompoundValue(1, if capture then dayCount[1] + 1 else dayCount[1], 0);

def thisDay  = (HighestAll(dayCount) - dayCount) + 1 ;



addlabel(highestall(thisday)<length, "Chart Time Interval set to " + highestall(thisday) +" days. Chart Time Interval must be set to at least 20 days");



def ph = if thisDay == length then hh

         else if thisDay < length and hh > ph[1]

         then hh else ph[1];

def pl = if bn == 1 then low

         else if thisDay == length

         then ll

         else if thisDay < length and ll < pl[1]

         then ll else pl[1];

def phbar = if hh == ph then bn else Double.NaN;

def plbar = if ll  == pl then bn else Double.NaN;



#AddLabel(1, ph + " " + highestall(phbar) + " " + pl + " " + HighestAll(plbar));



def day     = GetDay();

def Month   = if GetMonth() == GetLastMonth() then 1 else 0;

def year    = GetYear();

def lastDay = GetLastDay();

def lastmonth = GetLastMonth();

def lastyear  = GetLastYear();

def isToday   = If(day == lastDay and Month == lastmonth and year == lastyear, 1, 0);



def istodaybarnumber = Highest(if isToday then bn else Double.NaN, length);



def PO    = open(period = aggregationPeriod);

def Today = if GetDay() == GetLastDay() then 1 else 0;

def Week  = if GetWeek() == GetLastWeek() then 1 else 0;



def range = ph - pl;



input fib  = 1.618;

input fib1 = 1.272;

input fib2 = 0.618;

input fib3 = 0.236;

input fib4 = 0.382;

input fib5 = 0.5;

input fib6 = 0.786;

input fib7 = 2.618;



plot h;

plot h1;

plot h2;

plot h3;

plot h4;

plot h5;

plot h6;

plot h7;

plot h8;

plot h9;

plot h10;

plot highofline;

plot lowofline;





if ShowTodayOnly and !IsNaN(close(period = aggregationPeriod)[-1])

then {

    h   = Double.NaN;

    h1  = Double.NaN;

    h2  = Double.NaN;

    h3  = Double.NaN;

    h4  = Double.NaN;

    h5  = Double.NaN;

    h6  = Double.NaN;

    h7  = Double.NaN;

    h8  = Double.NaN;

    h9  = Double.NaN;

    h10 = Double.NaN;

    highofline = Double.NaN;

    lowofline  = Double.NaN;

} else {



    if HighestAll(phbar) > HighestAll(plbar)

    then {

        h  = ph + range * (fib);

        h1 = ph + range * (fib1);

        h2 = ph - range * (fib2);

        h3 = ph - range * (fib3);

        h4 = ph - range * (fib4);

        h5 = ph - range * (fib5);

        h6 = ph - range * (fib6);

        h7 = ph - range * (fib7);

        h8 = pl + range * (fib1);

        h9 = pl + range * (fib);

        h10 = pl + range * (fib7);

        highofline = LowestAll(pl);

        lowofline  = HighestAll(ph);

    }

    else

    {

        h  = ph + range * (fib - 1);

        h1 = ph + range * (fib1 - 1);

        h2 = ph + range * (fib2 - 1);

        h3 = ph + range * (fib3 - 1);

        h4 = ph + range * (fib4 - 1);

        h5 = ph + range * (fib5 - 1);

        h6 = ph + range * (fib6 - 1);

        h7 = ph + range * (fib7 - 1);

        h8 = pl - range * (fib1 - 1);

        h9 = pl - range * (fib - 1);

        h10 = pl - range * (fib7 - 1);

        highofline = HighestAll(ph);

        lowofline  = LowestAll(pl);

    }



}



h.DefineColor("Color", CreateColor(50, 150, 200));

h.AssignValueColor(h.Color("Color"));

h.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);



h1.DefineColor("Color", Color.MAGENTA);

h1.AssignValueColor(h1.Color("Color"));

h1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);



h2.DefineColor("Color", Color.GREEN);

h2.AssignValueColor(h2.Color("Color"));

h2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);



h3.DefineColor("Color", Color.RED);

h3.AssignValueColor(h3.Color("Color"));

h3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);



h4.DefineColor("Color", Color.GREEN);

h4.AssignValueColor(h4.Color("Color"));

h4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);



h5.DefineColor("Color", Color.YELLOW);

h5.AssignValueColor(h5.Color("Color"));

h5.SetPaintingStrategy(PaintingStrategy.DASHES);



h6.DefineColor("Color", Color.RED);

h6.AssignValueColor(h6.Color("Color"));

h6.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);



h7.DefineColor("Color", CreateColor(100, 100, 200));

h7.AssignValueColor(h7.Color("Color"));

h7.SetPaintingStrategy(PaintingStrategy.LINE);



h8.DefineColor("Color", Color.MAGENTA);

h8.AssignValueColor(h8.Color("Color"));

h8.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);



h9.DefineColor("Color", CreateColor(50, 150, 200));

h9.AssignValueColor(h9.Color("Color"));

h9.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);



h10.DefineColor("Color", CreateColor(100, 100, 200));

h10.AssignValueColor(h10.Color("Color"));

h10.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);





highofline.DefineColor("Color", Color.WHITE);

highofline.AssignValueColor(highofline.Color("Color"));

highofline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);





lowofline.DefineColor("Color", Color.WHITE);

lowofline.AssignValueColor(highofline.Color("Color"));

lowofline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);



input n = 3;

def n1 = n + 1;

#AddChartBubble(isnan(close[n]) and  !IsNaN(close[n1]), h4[n], concat(concat(fib4*100,"%") , concat(" $", round(h4[n1], 2))), color.green, yes);

AddChartBubble(IsNaN(close[n]) and  !IsNaN(close[n1]), lowofline[n1], Concat(Concat(0, "%") , Concat(" $", Round(lowofline[n1], 2))), Color.WHITE, yes);

AddChartBubble(  IsNaN(close[n]) and  !IsNaN(close[n1]), highofline[n1], Concat(Concat(100, "%") , Concat(" $", Round(highofline[n1], 2))), Color.WHITE, yes);

AddChartBubble( IsNaN(close[n]) and  !IsNaN(close[n1]), h4[n1], Concat(Concat(fib4 * 100, "%") , Concat(" $", Round(h4[n1], 2))), Color.GREEN, yes);

AddChartBubble( IsNaN(close[n]) and  !IsNaN(close[n1]), h2[n1], Concat(Concat(fib2 * 100, "%") , Concat(" $", Round(h2[n1], 2))), Color.GREEN, yes);

AddChartBubble( IsNaN(close[n]) and  !IsNaN(close[n1]), h3[n1], Concat(Concat(fib3 * 100, "%") , Concat(" $", Round(h3[n1], 2))), Color.RED, yes);

AddChartBubble( IsNaN(close[n]) and  !IsNaN(close[n1]), h5[n1], Concat(Concat(fib5 * 100, "%") , Concat(" $", Round(h5[n1], 2))), Color.YELLOW, yes);

AddChartBubble( IsNaN(close[n]) and  !IsNaN(close[n1]), h6[n1], Concat(Concat(fib6 * 100, "%") , Concat(" $", Round(h6[n1], 2))), Color.RED, yes);
 
@SleepyZ , thanks for the updated code. I made some changes so that the variable for length is used throughout the code. However, the levels are off for all previous days on the charts. For example, use a 30min chart, set the indicator to use the "Day" aggregation, select a length of 1, and select No for "Show Today Only". Below is what it looks like and the chart is smashed together. After you expand the chart, the levels are correct for the current day but are off for the rest.

VHCosMM.png


Below is the updated code.

Code:
#

# WalkingBallista & BenTen

# https://usethinkscript.com/d/153-high-and-low-with-fibonacci-retracement-indicator-for-thinkorswim

input aggregationPeriod = AggregationPeriod.DAY;

input ShowTodayOnly     = yes;

input showBubble        = yes;

input length            = 21;



def bn = BarNumber();

def hh = high(period=aggregationPeriod);

def ll = low(period=aggregationPeriod);

def ymd      = if aggregationPeriod == aggregationPeriod.DAY

               then getday()

               else if aggregationPeriod == aggregationPeriod.WEEK

               then getweek()

               else if aggregationPeriod == aggregationPeriod.MONTH

               then getmonth()

               else getyear();

def candles  = !IsNaN(close);

def capture  = candles and ymd != ymd[1];

def dayCount = CompoundValue(1, if capture then dayCount[1] + 1 else dayCount[1], 0);

def thisDay  = (HighestAll(dayCount) - dayCount) + 1 ;



addlabel(highestall(thisday)<length, "Chart Time Interval set to " + highestall(thisday) +" days. Chart Time Interval must be set to at least 20 days");



def ph = if thisDay == length then hh

         else if thisDay < length and hh > ph[1]

         then hh else ph[1];

def pl = if bn == 1 then low

         else if thisDay == length

         then ll

         else if thisDay < length and ll < pl[1]

         then ll else pl[1];

def phbar = if hh == ph then bn else Double.NaN;

def plbar = if ll  == pl then bn else Double.NaN;



#AddLabel(1, ph + " " + highestall(phbar) + " " + pl + " " + HighestAll(plbar));



def day     = GetDay();

def Month   = if GetMonth() == GetLastMonth() then 1 else 0;

def year    = GetYear();

def lastDay = GetLastDay();

def lastmonth = GetLastMonth();

def lastyear  = GetLastYear();

def isToday   = If(day == lastDay and Month == lastmonth and year == lastyear, 1, 0);



def istodaybarnumber = Highest(if isToday then bn else Double.NaN, length);



def PO    = open(period = aggregationPeriod);

def Today = if GetDay() == GetLastDay() then 1 else 0;

def Week  = if GetWeek() == GetLastWeek() then 1 else 0;



def range = ph - pl;



input fib  = 1.618;

input fib1 = 1.272;

input fib2 = 0.618;

input fib3 = 0.236;

input fib4 = 0.382;

input fib5 = 0.5;

input fib6 = 0.786;

input fib7 = 2.618;



plot h;

plot h1;

plot h2;

plot h3;

plot h4;

plot h5;

plot h6;

plot h7;

plot h8;

plot h9;

plot h10;

plot highofline;

plot lowofline;





if ShowTodayOnly and !IsNaN(close(period = aggregationPeriod)[-1])

then {

    h   = Double.NaN;

    h1  = Double.NaN;

    h2  = Double.NaN;

    h3  = Double.NaN;

    h4  = Double.NaN;

    h5  = Double.NaN;

    h6  = Double.NaN;

    h7  = Double.NaN;

    h8  = Double.NaN;

    h9  = Double.NaN;

    h10 = Double.NaN;

    highofline = Double.NaN;

    lowofline  = Double.NaN;

} else {



    if HighestAll(phbar) > HighestAll(plbar)

    then {

        h  = ph + range * (fib);

        h1 = ph + range * (fib1);

        h2 = ph - range * (fib2);

        h3 = ph - range * (fib3);

        h4 = ph - range * (fib4);

        h5 = ph - range * (fib5);

        h6 = ph - range * (fib6);

        h7 = ph - range * (fib7);

        h8 = pl + range * (fib1);

        h9 = pl + range * (fib);

        h10 = pl + range * (fib7);

        highofline = LowestAll(pl);

        lowofline  = HighestAll(ph);

    }

    else

    {

        h  = ph + range * (fib - 1);

        h1 = ph + range * (fib1 - 1);

        h2 = ph + range * (fib2 - 1);

        h3 = ph + range * (fib3 - 1);

        h4 = ph + range * (fib4 - 1);

        h5 = ph + range * (fib5 - 1);

        h6 = ph + range * (fib6 - 1);

        h7 = ph + range * (fib7 - 1);

        h8 = pl - range * (fib1 - 1);

        h9 = pl - range * (fib - 1);

        h10 = pl - range * (fib7 - 1);

        highofline = HighestAll(ph);

        lowofline  = LowestAll(pl);

    }



}



h.DefineColor("Color", CreateColor(50, 150, 200));

h.AssignValueColor(h.Color("Color"));

h.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);



h1.DefineColor("Color", Color.MAGENTA);

h1.AssignValueColor(h1.Color("Color"));

h1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);



h2.DefineColor("Color", Color.GREEN);

h2.AssignValueColor(h2.Color("Color"));

h2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);



h3.DefineColor("Color", Color.RED);

h3.AssignValueColor(h3.Color("Color"));

h3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);



h4.DefineColor("Color", Color.GREEN);

h4.AssignValueColor(h4.Color("Color"));

h4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);



h5.DefineColor("Color", Color.YELLOW);

h5.AssignValueColor(h5.Color("Color"));

h5.SetPaintingStrategy(PaintingStrategy.DASHES);



h6.DefineColor("Color", Color.RED);

h6.AssignValueColor(h6.Color("Color"));

h6.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);



h7.DefineColor("Color", CreateColor(100, 100, 200));

h7.AssignValueColor(h7.Color("Color"));

h7.SetPaintingStrategy(PaintingStrategy.LINE);



h8.DefineColor("Color", Color.MAGENTA);

h8.AssignValueColor(h8.Color("Color"));

h8.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);



h9.DefineColor("Color", CreateColor(50, 150, 200));

h9.AssignValueColor(h9.Color("Color"));

h9.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);



h10.DefineColor("Color", CreateColor(100, 100, 200));

h10.AssignValueColor(h10.Color("Color"));

h10.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);





highofline.DefineColor("Color", Color.WHITE);

highofline.AssignValueColor(highofline.Color("Color"));

highofline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);





lowofline.DefineColor("Color", Color.WHITE);

lowofline.AssignValueColor(highofline.Color("Color"));

lowofline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);



input n = 3;

def n1 = n + 1;

#AddChartBubble(isnan(close[n]) and  !IsNaN(close[n1]), h4[n], concat(concat(fib4*100,"%") , concat(" $", round(h4[n1], 2))), color.green, yes);

AddChartBubble(IsNaN(close[n]) and  !IsNaN(close[n1]), lowofline[n1], Concat(Concat(0, "%") , Concat(" $", Round(lowofline[n1], 2))), Color.WHITE, yes);

AddChartBubble(  IsNaN(close[n]) and  !IsNaN(close[n1]), highofline[n1], Concat(Concat(100, "%") , Concat(" $", Round(highofline[n1], 2))), Color.WHITE, yes);

AddChartBubble( IsNaN(close[n]) and  !IsNaN(close[n1]), h4[n1], Concat(Concat(fib4 * 100, "%") , Concat(" $", Round(h4[n1], 2))), Color.GREEN, yes);

AddChartBubble( IsNaN(close[n]) and  !IsNaN(close[n1]), h2[n1], Concat(Concat(fib2 * 100, "%") , Concat(" $", Round(h2[n1], 2))), Color.GREEN, yes);

AddChartBubble( IsNaN(close[n]) and  !IsNaN(close[n1]), h3[n1], Concat(Concat(fib3 * 100, "%") , Concat(" $", Round(h3[n1], 2))), Color.RED, yes);

AddChartBubble( IsNaN(close[n]) and  !IsNaN(close[n1]), h5[n1], Concat(Concat(fib5 * 100, "%") , Concat(" $", Round(h5[n1], 2))), Color.YELLOW, yes);

AddChartBubble( IsNaN(close[n]) and  !IsNaN(close[n1]), h6[n1], Concat(Concat(fib6 * 100, "%") , Concat(" $", Round(h6[n1], 2))), Color.RED, yes);

As I did not write the original code, and only made adjustments to specific requests, I did not analyze it beyond those adjustments.

Only the current day's display is usable as it represents the Highest(highs) and Lowest(lows) of the range defined by length.

The reason to set showtodayonly to no, allows one (and me when testing the adjustments) to verify that the highest(highs) and lowest(lows) are being properly used for the length. The fib lines for when showtodayonly to no for days other than the current day would probably best not being displayed, as only the highest(highs) and lowest(lows) for the length are relevant for current days fibs.

Sorry, if this created any confusion as to the usefulness to those fib lines for the other days than the current days.
 
This indicator for ThinkorSwim takes the high and low of the previous week or month and add Fibonacci retracement levels to create potential support and resistance levels. You can also use the high and low of the previous day, yearly, etc. Up to you to select the right timeframe to help your trading style.

2qzTplK.png


thinkScript Code

Rich (BB code):
#
# WalkingBallista & BenTen
# https://usethinkscript.com/d/153-high-and-low-with-fibonacci-retracement-indicator-for-thinkorswim
input aggregationPeriod = AggregationPeriod.DAY;
input ShowTodayOnly = yes;

def PH = high(period = aggregationPeriod)[1];
def PL = low(period = aggregationPeriod)[1];
def PO = open(period = aggregationPeriod);
def Today = if GetDay() == GetLastDay() then 1 else 0;
def Week = if GetWeek() == GetLastWeek() then 1 else 0;

def range = PH - PL;
input fib = 1.618;
input fib1 = 1.272;
input fib2 = 0.618;
input fib3 = 0.236;
input fib4 = 0.382;
input fib5 = 0.5;
input fib6 = 0.786;
input fib7 = 2.618;

plot h = if ShowTodayOnly and !Today and aggregationPeriod == AggregationPeriod.Day then Double.NaN else if ShowTodayOnly and !Week and aggregationPeriod == AggregationPeriod.Week then Double.NaN else PH + range*(fib-1);
plot h1 = if ShowTodayOnly and !Today and aggregationPeriod == AggregationPeriod.Day then Double.NaN else if ShowTodayOnly and !Week and aggregationPeriod == AggregationPeriod.Week then Double.NaN else PH + range*(fib1-1);
plot h2 = if ShowTodayOnly and !Today and aggregationPeriod == AggregationPeriod.Day then Double.NaN else if ShowTodayOnly and !Week and aggregationPeriod == AggregationPeriod.Week then Double.NaN else PH + range*(fib2-1);
plot h3 = if ShowTodayOnly and !Today and aggregationPeriod == AggregationPeriod.Day then Double.NaN else if ShowTodayOnly and !Week and aggregationPeriod == AggregationPeriod.Week then Double.NaN else PH + range*(fib3-1);
plot h4 = if ShowTodayOnly and !Today and aggregationPeriod == AggregationPeriod.Day then Double.NaN else if ShowTodayOnly and !Week and aggregationPeriod == AggregationPeriod.Week then Double.NaN else PH + range*(fib4-1);
plot h5 = if ShowTodayOnly and !Today and aggregationPeriod == AggregationPeriod.Day then Double.NaN else if ShowTodayOnly and !Week and aggregationPeriod == AggregationPeriod.Week then Double.NaN else PH + range*(fib5-1);
plot h6 = if ShowTodayOnly and !Today and aggregationPeriod == AggregationPeriod.Day then Double.NaN else if ShowTodayOnly and !Week and aggregationPeriod == AggregationPeriod.Week then Double.NaN else PH + range*(fib6-1);
plot h7 = if ShowTodayOnly and !Today and aggregationPeriod == AggregationPeriod.Day then Double.NaN else if ShowTodayOnly and !Week and aggregationPeriod == AggregationPeriod.Week then Double.NaN else PH + range*(fib7-1);
plot h8 = if ShowTodayOnly and !Today and aggregationPeriod == AggregationPeriod.Day then Double.NaN else if ShowTodayOnly and !Week and aggregationPeriod == AggregationPeriod.Week then Double.NaN else PL - range*(fib1-1);
plot h9 = if ShowTodayOnly and !Today and aggregationPeriod == AggregationPeriod.Day then Double.NaN else if ShowTodayOnly and !Week and aggregationPeriod == AggregationPeriod.Week then Double.NaN else PL - range*(fib-1);
plot h10 = if ShowTodayOnly and !Today and aggregationPeriod == AggregationPeriod.Day then Double.NaN else if ShowTodayOnly and !Week and aggregationPeriod == AggregationPeriod.Week then Double.NaN else PL - range*(fib7-1);

h.DefineColor("Color", Color.DARK_RED);
h.AssignValueColor(h.color("Color"));
h.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

h1.DefineColor("Color", Color.RED);
h1.AssignValueColor(h1.color("Color"));
h1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

h2.DefineColor("Color", Color.RED);
h2.AssignValueColor(h2.color("Color"));
h2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

h3.DefineColor("Color", Color.RED);
h3.AssignValueColor(h3.color("Color"));
h3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

h4.DefineColor("Color", Color.RED);
h4.AssignValueColor(h3.color("Color"));
h4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

h5.DefineColor("Color", Color.RED);
h5.AssignValueColor(h3.color("Color"));
h5.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

h6.DefineColor("Color", Color.RED);
h6.AssignValueColor(h3.color("Color"));
h6.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

h7.DefineColor("Color", Color.RED);
h7.AssignValueColor(h3.color("Color"));
h7.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

h8.DefineColor("Color", Color.RED);
h8.AssignValueColor(h3.color("Color"));
h8.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

h9.DefineColor("Color", Color.RED);
h9.AssignValueColor(h3.color("Color"));
h9.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

h10.DefineColor("Color", Color.RED);
h10.AssignValueColor(h3.color("Color"));
h10.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

plot highofline = if ShowTodayOnly and !Today and aggregationPeriod == AggregationPeriod.Day then Double.NaN else if ShowTodayOnly and !Week and aggregationPeriod == AggregationPeriod.Week then Double.NaN else PH;
highofline.DefineColor("Color", Color.YELLOW);
highofline.AssignValueColor(highofline.color("Color"));
highofline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

plot lowofline = if ShowTodayOnly and !Today and aggregationPeriod == AggregationPeriod.Day then Double.NaN else if ShowTodayOnly and !Week and aggregationPeriod == AggregationPeriod.Week then Double.NaN else PL;
lowofline.DefineColor("Color", Color.YELLOW);
lowofline.AssignValueColor(highofline.color("Color"));
lowofline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

Shareable Link

https://tos.mx/UoIukn

Video Tutorial


Using your code from post #1, do you have any ideas on how to flip the fib based on the trend of the stock? This discussion started in post #15, but we are currently stuck. I am not sure if this is possible or if this will only work on the current day.
 
Last edited by a moderator:
Using your code from post #1, do you have any ideas on how to flip the fib based on the trend of the stock? This discussion started in post #15, but we are currently stuck. I am not sure if this is possible or if this will only work on the current day.

As the last code was not set up to do your request, the following should now flip the previous period selected fibs on the following day using the previous day's high/low fib orientation there.

Screenshot-2022-12-23-091851.png
Ruby:
#Auto_FibPrevDay_plots_previous_day_and_fib_levels_based_thereon
input ShowTodayOnly = { default "No", "Yes"};
input showbubbles   = yes;
input aggperiod     = {default "DAY", "WEEK", "MONTH", "YEAR"};
input displace      = 1;

plot ORH = if ShowTodayOnly and !IsNaN(close(period = aggperiod)[-1]) then Double.NaN else high(period = aggperiod)[displace];
plot ORL = if ShowTodayOnly and !IsNaN(close(period = aggperiod)[-1]) then Double.NaN else  low(period = aggperiod)[displace];

ORH.SetDefaultColor(Color.WHITE);
ORH.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ORH.SetLineWeight(2);

ORL.SetDefaultColor(Color.WHITE);
ORL.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ORL.SetLineWeight(2);

def bn = BarNumber();
def hbar = if high == high(period = aggperiod) then bn else hbar[1];
def lbar = if low == low(period = aggperiod) then bn else lbar[1];


input F1 = -1.00;
input F2 = -0.62;
input F3 = -0.27;
input F4 = 0.236;
input F5 = 0.382;
input F6 = 0.500;
input F7 = 0.618;
input F8 = 0.786;
input F10 = 1.27;
input F11 = 1.618;
input F12 = 2.00;

def rHi =  ORH;
def rLo =  ORL;

def range  = rHi - rLo;
plot FF1   = rLo + (range * F1);
plot FF2   = rLo + (range * F2);
plot FF3   = rLo + (range * F3);
plot FF4   = rLo + (range * F4);
plot FF5   = rLo + (range * F5);
plot FF6   = rLo + (range * F6);
plot FF7   = rLo + (range * F7);
plot FF8   = rLo + (range * F8);
plot FF10  = rLo + (range * F10);
plot FF11  = rLo + (range * F11);
plot FF12  = rLo + (range * F12);

def h = high(period = AggregationPeriod.DAY);
def l = low(period = AggregationPeriod.DAY);

FF1.SetDefaultColor(Color.GREEN);
FF1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF1.SetLineWeight(1);

FF2.SetDefaultColor(Color.GREEN);
FF2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF2.SetLineWeight(1);

FF3.SetDefaultColor(Color.GREEN);
FF3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF3.SetLineWeight(1);

FF4.SetDefaultColor(Color.CYAN);
FF4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF4.SetLineWeight(1);

FF5.SetDefaultColor(Color.YELLOW);
FF5.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF5.SetLineWeight(1);

FF6.SetDefaultColor(Color.WHITE);
FF6.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF6.SetLineWeight(1);

FF7.SetDefaultColor(Color.YELLOW);
FF7.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF7.SetLineWeight(1);

FF8.SetDefaultColor(Color.CYAN);
FF8.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF8.SetLineWeight(1);

FF10.SetDefaultColor(Color.GREEN);
FF10.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF10.SetLineWeight(1);

FF11.SetDefaultColor(Color.GREEN);
FF11.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF11.SetLineWeight(1);

FF12.SetDefaultColor(Color.GREEN);
FF12.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF12.SetLineWeight(1);

def basis;
switch (aggperiod) {
case "DAY":
    basis = GetDay() != GetDay()[1];
case "WEEK":
    basis = GetWeek() != GetWeek()[1];
case "MONTH":
    basis = GetMonth() != GetMonth()[1];
case "YEAR":
    basis = GetYear() != GetYear()[1];
}
AddChartBubble(showbubbles and basis, ORH, if lbar > hbar then 100 else 0, if lbar > hbar then Color.GREEN else Color.RED);
AddChartBubble(showbubbles and basis, ORL, if lbar > hbar then 0 else 100, if lbar > hbar then Color.RED else Color.GREEN, no);
AddChartBubble(showbubbles and basis, FF1, if lbar > hbar then F1 else F12, FF1.TakeValueColor());
AddChartBubble(showbubbles and basis, FF2, if lbar > hbar then F2 else F11, FF2.TakeValueColor());
AddChartBubble(showbubbles and basis, FF3, if lbar > hbar then F3 else F10, FF3.TakeValueColor());
AddChartBubble(showbubbles and basis, FF4, if lbar > hbar then F4 else F8, FF4.TakeValueColor());
AddChartBubble(showbubbles and basis, FF5, if lbar > hbar then F5 else F7, FF5.TakeValueColor());
AddChartBubble(showbubbles and basis, FF6, if lbar > hbar then F6 else F6, FF6.TakeValueColor());
AddChartBubble(showbubbles and basis, FF7, if lbar > hbar then F7 else F5, FF7.TakeValueColor());
AddChartBubble(showbubbles and basis, FF8, if lbar > hbar then F8 else F4, FF8.TakeValueColor());
AddChartBubble(showbubbles and basis, FF10, if lbar > hbar then F10 else F3, FF10.TakeValueColor());
AddChartBubble(showbubbles and basis, FF11, if lbar > hbar then F11 else F2, FF11.TakeValueColor());
AddChartBubble(showbubbles and basis, FF12, if lbar > hbar then F12 else F1, FF12.TakeValueColor());
;
;
 
Last edited:
As the last code was not set up to do your request, the following should now flip the previous period selected fibs on the following day using the previous day's high/low fib orientation there.
This is great @SleepyZ and I like how you simplified the code! However, I did want to include the ability to select lower timeframes of less than a Day. How would I update this switch statement if I wanted to select any timeframe?

def basis;
switch (aggperiod) {
case "DAY":
basis = GetDay() != GetDay()[1];
case "WEEK":
basis = GetWeek() != GetWeek()[1];
case "MONTH":
basis = GetMonth() != GetMonth()[1];
case "YEAR":
basis = GetYear() != GetYear()[1];
}
 
This is great @SleepyZ and I like how you simplified the code! However, I did want to include the ability to select lower timeframes of less than a Day. How would I update this switch statement if I wanted to select any timeframe?

def basis;
switch (aggperiod) {
case "DAY":
basis = GetDay() != GetDay()[1];
case "WEEK":
basis = GetWeek() != GetWeek()[1];
case "MONTH":
basis = GetMonth() != GetMonth()[1];
case "YEAR":
basis = GetYear() != GetYear()[1];
}

This now has some intraday periods added to select.

Screenshot-2022-12-30-104149.png
Ruby:
#Auto_FibPrevDay_plots_previous_day_and_flips_fib_levels_including_intraday_timeframes
input ShowTodayOnly = { default "No", "Yes"};
input showbubbles   = yes;
input aggperiod     = {"1 MIN", "2 MIN", "3 MIN", "5 MIN", "10 MIN", "15 MIN", "30 MIN", "1 HOUR", default "DAY", "WEEK", "MONTH", "YEAR"};
input displace      = 1;

def period;
def yyyymmdd = GetYYYYMMDD();
def seconds = SecondsFromTime(0);
def month = GetYear() * 12 + GetMonth();
def day_number = DaysFromDate(First(yyyymmdd)) + GetDayOfWeek(First(yyyymmdd));
def dom = GetDayOfMonth(yyyymmdd);
def dow = GetDayOfWeek(yyyymmdd - dom + 1);

switch (aggperiod) {
case "1 MIN":
    period = Floor(seconds / 60 + day_number * 24 * 60);
case "2 MIN":
    period = Floor(seconds / 120 + day_number * 24);
case "3 MIN":
    period = Floor(seconds / 180 + day_number * 24);
case "5 MIN":
    period = Floor(seconds / 300 + day_number * 24);
case "10 MIN":
    period = Floor(seconds / 600 + day_number * 24);
case "15 MIN":
    period = Floor(seconds / 900 + day_number * 24);
case "30 MIN":
    period = Floor(seconds / 1800 + day_number * 24);
case "1 HOUR":
    period = Floor(seconds / 3600 + day_number * 24);
case "DAY":
    period = CountTradingDays(Min(First(yyyymmdd), yyyymmdd), yyyymmdd) - 1;
case "WEEK":
    period = Floor(day_number / 7);
case "MONTH":
    period = Floor(month - First(month));
case "YEAR":
    period = getyear();
}

plot ORH = if ShowTodayOnly and !IsNaN(close(period = aggperiod)[-1]) then Double.NaN else high(period = aggperiod)[displace];
plot ORL = if ShowTodayOnly and !IsNaN(close(period = aggperiod)[-1]) then Double.NaN else  low(period = aggperiod)[displace];

ORH.SetDefaultColor(Color.WHITE);
ORH.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ORH.SetLineWeight(2);

ORL.SetDefaultColor(Color.WHITE);
ORL.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ORL.SetLineWeight(2);

def bn = BarNumber();
def hbar = if high == high(period = aggperiod) then bn else hbar[1];
def lbar = if low  == low(period = aggperiod)  then bn else lbar[1];

input F1 = -1.00;
input F2 = -0.62;
input F3 = -0.27;
input F4 = 0.236;
input F5 = 0.382;
input F6 = 0.500;
input F7 = 0.618;
input F8 = 0.786;
input F10 = 1.27;
input F11 = 1.618;
input F12 = 2.00;

def rHi =  ORH;
def rLo =  ORL;

def range  = rHi - rLo;
plot FF1   = rLo + (range * F1);
plot FF2   = rLo + (range * F2);
plot FF3   = rLo + (range * F3);
plot FF4   = rLo + (range * F4);
plot FF5   = rLo + (range * F5);
plot FF6   = rLo + (range * F6);
plot FF7   = rLo + (range * F7);
plot FF8   = rLo + (range * F8);
plot FF10  = rLo + (range * F10);
plot FF11  = rLo + (range * F11);
plot FF12  = rLo + (range * F12);

FF1.SetDefaultColor(Color.GREEN);
FF1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF1.SetLineWeight(1);

FF2.SetDefaultColor(Color.GREEN);
FF2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF2.SetLineWeight(1);

FF3.SetDefaultColor(Color.GREEN);
FF3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF3.SetLineWeight(1);

FF4.SetDefaultColor(Color.CYAN);
FF4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF4.SetLineWeight(1);

FF5.SetDefaultColor(Color.YELLOW);
FF5.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF5.SetLineWeight(1);

FF6.SetDefaultColor(Color.WHITE);
FF6.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF6.SetLineWeight(1);

FF7.SetDefaultColor(Color.YELLOW);
FF7.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF7.SetLineWeight(1);

FF8.SetDefaultColor(Color.CYAN);
FF8.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF8.SetLineWeight(1);

FF10.SetDefaultColor(Color.GREEN);
FF10.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF10.SetLineWeight(1);

FF11.SetDefaultColor(Color.GREEN);
FF11.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF11.SetLineWeight(1);

FF12.SetDefaultColor(Color.GREEN);
FF12.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF12.SetLineWeight(1);

def basis;
def lowbar;
def highbar;
switch (aggperiod) {
case "1 MIN":
    basis = period != period[1];
lowbar=lbar[1];
highbar=hbar[1];
case "2 MIN":
    basis = period != period[1];
lowbar=lbar[1];
highbar=hbar[1];
case "3 MIN":
    basis = period != period[1];
lowbar=lbar[1];
highbar=hbar[1];
case "5 MIN":
    basis = period != period[1];
lowbar=lbar[1];
highbar=hbar[1];
case "10 MIN":
    basis = period != period[1];
lowbar=lbar[1];
highbar=hbar[1];
case "15 MIN":
    basis = period != period[1];
lowbar=lbar[1];
highbar=hbar[1];
case "30 MIN":
    basis = period != period[1];
lowbar=lbar[1];
highbar=hbar[1];
case "1 HOUR":
    basis = period != period[1];
lowbar=lbar[1];
highbar=hbar[1];
case "DAY":
    basis = GetDay() != GetDay()[1];
lowbar=lbar;
highbar=hbar;
case "WEEK":
    basis = GetWeek() != GetWeek()[1];
lowbar=lbar;
highbar=hbar;
case "MONTH":
    basis = GetMonth() != GetMonth()[1];
lowbar=lbar;
highbar=hbar;
case "YEAR":
    basis = getyear() != getyear()[1];
lowbar=lbar;
highbar=hbar;
}

AddChartBubble(showbubbles and basis, ORH, (if lowbar >highbar then 100 else 0), if lowbar > highbar then Color.GREEN else Color.RED);
AddChartBubble(showbubbles and basis, ORL, (if lowbar > highbar then 0 else 100), if lowbar > highbar then Color.RED else Color.GREEN, no);
AddChartBubble(showbubbles and basis, FF1, if lowbar > highbar then F1 else F12, FF1.TakeValueColor());
AddChartBubble(showbubbles and basis, FF2, if lowbar > highbar then F2 else F11, FF2.TakeValueColor());
AddChartBubble(showbubbles and basis, FF3, if lowbar > highbar then F3 else F10, FF3.TakeValueColor());
AddChartBubble(showbubbles and basis, FF4, if lowbar > highbar then F4 else F8, FF4.TakeValueColor());
AddChartBubble(showbubbles and basis, FF5, if lowbar > highbar then F5 else F7, FF5.TakeValueColor());
AddChartBubble(showbubbles and basis, FF6, if lowbar > highbar then F6 else F6, FF6.TakeValueColor());
AddChartBubble(showbubbles and basis, FF7, if lowbar > highbar then F7 else F5, FF7.TakeValueColor());
AddChartBubble(showbubbles and basis, FF8, if lowbar > highbar then F8 else F4, FF8.TakeValueColor());
AddChartBubble(showbubbles and basis, FF10, if lowbar > highbar then F10 else F3, FF10.TakeValueColor());
AddChartBubble(showbubbles and basis, FF11, if lowbar > highbar then F11 else F2, FF11.TakeValueColor());
AddChartBubble(showbubbles and basis, FF12, if lowbar > highbar then F12 else F1, FF12.TakeValueColor());
;
;
 
anyway to get similar script for Fib Extensions based on high/low of prev day. Thanks

[Edit: The code in post #31 is a perhaps a better version of the Auto Fib Extension than below. It does not repaint as below does]

This will provide auto Fib Extensions vs Fib Retracements.

There is an overlay, using the TOS fib extension tool (color red), of the auto drawn fibs using the high/low of the previous aggperiod and the developing extension into the current aggperiod.

You can limit the display of the fibs at the input screen.

Screenshot-2023-01-14-180928.png
Ruby:
#Auto_FibExtensions_flips_fib_levels_including_intraday_timeframes

input limit_display = yes;
input display_x     = 1;
input aggperiod     = {"1 MIN", "2 MIN", "3 MIN", "5 MIN", "10 MIN", "15 MIN", "30 MIN", "1 HOUR", default "DAY", "WEEK", "MONTH", "YEAR"};
input showbubbles   = yes;
input bubblemover   = -1;

def bm  = bubblemover;
def period;
def yyyymmdd = GetYYYYMMDD();
def seconds = SecondsFromTime(0);
def month = GetYear() * 12 + GetMonth();
def day_number = DaysFromDate(First(yyyymmdd)) + GetDayOfWeek(First(yyyymmdd));
def dom = GetDayOfMonth(yyyymmdd);
def dow = GetDayOfWeek(yyyymmdd - dom + 1);

switch (aggperiod) {
case "1 MIN":
    period = Floor(seconds / 60 + day_number * 24 * 60);
case "2 MIN":
    period = Floor(seconds / 120 + day_number * 24);
case "3 MIN":
    period = Floor(seconds / 180 + day_number * 24);
case "5 MIN":
    period = Floor(seconds / 300 + day_number * 24);
case "10 MIN":
    period = Floor(seconds / 600 + day_number * 24);
case "15 MIN":
    period = Floor(seconds / 900 + day_number * 24);
case "30 MIN":
    period = Floor(seconds / 1800 + day_number * 24);
case "1 HOUR":
    period = Floor(seconds / 3600 + day_number * 24);
case "DAY":
    period = CountTradingDays(Min(First(yyyymmdd), yyyymmdd), yyyymmdd) - 1;
case "WEEK":
    period = Floor(day_number / 7);
case "MONTH":
    period = Floor(month - First(month));
case "YEAR":
    period = GetYear();
}

def na = Double.NaN;
def bn = BarNumber();
def H  = high(period=aggperiod);
def L  = low(period=aggperiod);

def hbar = if high == H then bn else hbar[1];
def lbar = if low == L  then bn else lbar[1];

def basis;
switch (aggperiod) {
case "1 MIN":
    basis = period != period[1];
case "2 MIN":
    basis = period != period[1];
case "3 MIN":
    basis = period != period[1];
case "5 MIN":
    basis = period != period[1];
case "10 MIN":
    basis = period != period[1];
case "15 MIN":
    basis = period != period[1];
case "30 MIN":
    basis = period != period[1];
case "1 HOUR":
    basis = period != period[1];
case "DAY":
    basis = GetDay() != GetDay()[1];
case "WEEK":
    basis = GetWeek() != GetWeek()[1];
case "MONTH":
    basis = GetMonth() != GetMonth()[1];
case "YEAR":
    basis = GetYear() != GetYear()[1];
}

input F4 = 0.236;
input F5 = 0.382;
input F6 = 0.500;
input F7 = 0.618;
input F8 = 0.764;
input F9 = 1.00;
input F10 = 1.27;
input F11 = 1.618;
input F12 = 2.00;

def rz         = if period != period[1]
                 then if hbar[1] < lbar[1]
                      then H
                      else L
                 else rz[1];
def rplusminus = if period != period[1]
                 then if hbar[1] < lbar[1]
                      then 0
                      else 1
                 else rplusminus[1];

def Count  = CompoundValue(1, if (H+L!=H[1]+L[1]) then Count[1] + 1 else Count[1], 0);

plot rzero = if limit_display and HighestAll(Count) - Count <= (display_x - 1)
             then rz
             else if !limit_display
             then rz else na;


def range  = AbsValue(H[1] - L[1]);

plot FF4   = if rplusminus == 1 then rzero + (range * F4) else rzero - (range * F4);
plot FF5   = if rplusminus == 1 then rzero + (range * F5) else rzero - (range * F5);
plot FF6   = if rplusminus == 1 then rzero + (range * F6) else rzero - (range * F6);
plot FF7   = if rplusminus == 1 then rzero + (range * F7) else rzero - (range * F7);
plot FF8   = if rplusminus == 1 then rzero + (range * F8) else rzero - (range * F8);
plot FF9   = if rplusminus == 1 then rzero + (range * F9) else rzero - (range * F9);
plot FF10  = if rplusminus == 1 then rzero + (range * F10) else rzero - (range * F10);
plot FF11  = if rplusminus == 1 then rzero + (range * F11) else rzero - (range * F11);

rzero.SetDefaultColor(Color.WHITE);
rzero.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
rzero.SetLineWeight(1);

FF4.SetDefaultColor(Color.CYAN);
FF4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF4.SetLineWeight(1);


FF4.SetDefaultColor(Color.CYAN);
FF4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF4.SetLineWeight(1);

FF5.SetDefaultColor(Color.YELLOW);
FF5.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF5.SetLineWeight(1);

FF6.SetDefaultColor(Color.WHITE);
FF6.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF6.SetLineWeight(1);

FF7.SetDefaultColor(Color.YELLOW);
FF7.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF7.SetLineWeight(1);

FF8.SetDefaultColor(Color.CYAN);
FF8.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF8.SetLineWeight(1);

FF9.SetDefaultColor(Color.WHITE);
FF9.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF9.SetLineWeight(1);

FF10.SetDefaultColor(Color.CYAN);
FF10.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF10.SetLineWeight(1);

FF11.SetDefaultColor(Color.YELLOW);
FF11.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF11.SetLineWeight(1);


AddChartBubble(if IsNaN(rzero[-1]) then Double.NaN else showbubbles and basis[bm] , rzero[bm], 0, if rplusminus[bm] == 1 then Color.GREEN else Color.RED);
AddChartBubble(if IsNaN(FF4[-1]) then Double.NaN else showbubbles and basis[bm], FF4[bm], F4, FF4.TakeValueColor());
AddChartBubble(if IsNaN(FF5[-1]) then Double.NaN else showbubbles and basis[bm], FF5[bm],  F5, FF5.TakeValueColor());
AddChartBubble(if IsNaN(FF6[-1]) then Double.NaN else showbubbles and basis[bm], FF6[bm], F6, FF6.TakeValueColor());
AddChartBubble(if IsNaN(FF7[-1]) then Double.NaN else showbubbles and basis[bm], FF7[bm], F7 , FF7.TakeValueColor());
AddChartBubble(if IsNaN(FF8[-1]) then Double.NaN else showbubbles and basis[bm], FF8[bm],  F8, FF8.TakeValueColor());
AddChartBubble(if IsNaN(FF9[-1]) then Double.NaN else showbubbles and basis[bm], FF9[bm],  F9, if rplusminus == 1 then Color.GREEN else Color.RED);
AddChartBubble(if IsNaN(FF10[-1]) then Double.NaN else showbubbles and basis[bm], FF10[bm],  F10, FF10.TakeValueColor());
AddChartBubble(if IsNaN(FF11[-1]) then Double.NaN else showbubbles and basis[bm], FF11[bm],  F11, FF11.TakeValueColor());

;
 
Last edited:
This will provide auto Fib Extensions vs Fib Retracements.

There is an overlay, using the TOS fib extension tool (color red), of the auto drawn fibs using the high/low of the previous aggperiod and the developing extension into the current aggperiod.

You can limit the display of the fibs at the input screen.

Here is an alternate method to the Auto Fib Extensions above. Rather than using the Developing High or Low of the current period as the Base for the Extension. use the Open of the current period instead.

Optional Vertical lines have been added which can be limited as are the fib extensions.

The image shows a red colored drawing using the TOS Fib Extension tool to show how the Fib Extensions are derived.

Screenshot-2023-01-15-110808.png
Ruby:
#Auto_FibExtensions_flips_fib_levels_including_intraday_timeframes_using_Open_as_ZeroBase

input limit_display = yes;
input display_x     = 1;
input aggperiod     = {"1 MIN", "2 MIN", "3 MIN", "5 MIN", "10 MIN", "15 MIN", "30 MIN", "1 HOUR", default "DAY", "WEEK", "MONTH", "YEAR"};
input showbubbles   = yes;
input bubblemover   = -1;
input showverticals = yes;

def bm  = bubblemover;
def period;
def yyyymmdd = GetYYYYMMDD();
def seconds = SecondsFromTime(0);
def month = GetYear() * 12 + GetMonth();
def day_number = DaysFromDate(First(yyyymmdd)) + GetDayOfWeek(First(yyyymmdd));
def dom = GetDayOfMonth(yyyymmdd);
def dow = GetDayOfWeek(yyyymmdd - dom + 1);

switch (aggperiod) {
case "1 MIN":
    period = Floor(seconds / 60 + day_number * 24 * 60);
case "2 MIN":
    period = Floor(seconds / 120 + day_number * 24);
case "3 MIN":
    period = Floor(seconds / 180 + day_number * 24);
case "5 MIN":
    period = Floor(seconds / 300 + day_number * 24);
case "10 MIN":
    period = Floor(seconds / 600 + day_number * 24);
case "15 MIN":
    period = Floor(seconds / 900 + day_number * 24);
case "30 MIN":
    period = Floor(seconds / 1800 + day_number * 24);
case "1 HOUR":
    period = Floor(seconds / 3600 + day_number * 24);
case "DAY":
    period = CountTradingDays(Min(First(yyyymmdd), yyyymmdd), yyyymmdd) - 1;
case "WEEK":
    period = Floor(day_number / 7);
case "MONTH":
    period = Floor(month - First(month));
case "YEAR":
    period = GetYear();
}

def na = Double.NaN;
def bn = BarNumber();
def H  = high(period = aggperiod);
def L  = low(period = aggperiod);
def O  = open(period = aggperiod);

def hbar = if high == H then bn else hbar[1];
def lbar = if low == L  then bn else lbar[1];

def basis;
switch (aggperiod) {
case "1 MIN":
    basis = period != period[1];
case "2 MIN":
    basis = period != period[1];
case "3 MIN":
    basis = period != period[1];
case "5 MIN":
    basis = period != period[1];
case "10 MIN":
    basis = period != period[1];
case "15 MIN":
    basis = period != period[1];
case "30 MIN":
    basis = period != period[1];
case "1 HOUR":
    basis = period != period[1];
case "DAY":
    basis = GetDay() != GetDay()[1];
case "WEEK":
    basis = GetWeek() != GetWeek()[1];
case "MONTH":
    basis = GetMonth() != GetMonth()[1];
case "YEAR":
    basis = GetYear() != GetYear()[1];
}
input F1 = -1.00;
input F2 = -.618;
input F3 = -.27;
input F4 = 0.236;
input F5 = 0.382;
input F6 = 0.500;
input F7 = 0.618;
input F8 = 0.764;
input F9 = 1.00;
input F10 = 1.27;
input F11 = 1.618;
input F12 = 2.00;

def rz         = if period != period[1]
                 then if hbar[1] < lbar[1]
                      then O
                      else O
                 else rz[1];
def rplusminus = if period != period[1]
                 then if hbar[1] < lbar[1]
                      then 0
                      else 1
                 else rplusminus[1];

def Count  = CompoundValue(1, if (H + L != H[1] + L[1]) then Count[1] + 1 else Count[1], 0);

plot rzero = if limit_display and HighestAll(Count) - Count <= (display_x - 1)
             then rz
             else if !limit_display
             then rz else na;


def range  = AbsValue(H[1] - L[1]);

plot FF1   = if rplusminus == 1 then rzero + (range * F1) else rzero - (range * F1);
plot FF2   = if rplusminus == 1 then rzero + (range * F2) else rzero - (range * F2);
plot FF3   = if rplusminus == 1 then rzero + (range * F3) else rzero - (range * F3);
plot FF4   = if rplusminus == 1 then rzero + (range * F4) else rzero - (range * F4);
plot FF5   = if rplusminus == 1 then rzero + (range * F5) else rzero - (range * F5);
plot FF6   = if rplusminus == 1 then rzero + (range * F6) else rzero - (range * F6);
plot FF7   = if rplusminus == 1 then rzero + (range * F7) else rzero - (range * F7);
plot FF8   = if rplusminus == 1 then rzero + (range * F8) else rzero - (range * F8);
plot FF9   = if rplusminus == 1 then rzero + (range * F9) else rzero - (range * F9);
plot FF10  = if rplusminus == 1 then rzero + (range * F10) else rzero - (range * F10);
plot FF11  = if rplusminus == 1 then rzero + (range * F11) else rzero - (range * F11);
plot FF12  = if rplusminus == 1 then rzero + (range * F12) else rzero - (range * F12);

rzero.SetDefaultColor(Color.WHITE);
rzero.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
rzero.SetLineWeight(1);

FF1.SetDefaultColor(Color.WHITE);
FF1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF1.SetLineWeight(1);

FF2.SetDefaultColor(Color.YELLOW);
FF2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF2.SetLineWeight(1);

FF3.SetDefaultColor(Color.CYAN);
FF3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF3.SetLineWeight(1);

FF4.SetDefaultColor(Color.CYAN);
FF4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF4.SetLineWeight(1);

FF4.SetDefaultColor(Color.CYAN);
FF4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF4.SetLineWeight(1);

FF4.SetDefaultColor(Color.CYAN);
FF4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF4.SetLineWeight(1);

FF5.SetDefaultColor(Color.YELLOW);
FF5.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF5.SetLineWeight(1);

FF6.SetDefaultColor(Color.WHITE);
FF6.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF6.SetLineWeight(1);

FF7.SetDefaultColor(Color.YELLOW);
FF7.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF7.SetLineWeight(1);

FF8.SetDefaultColor(Color.CYAN);
FF8.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF8.SetLineWeight(1);

FF9.SetDefaultColor(Color.WHITE);
FF9.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF9.SetLineWeight(1);

FF10.SetDefaultColor(Color.CYAN);
FF10.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF10.SetLineWeight(1);

FF11.SetDefaultColor(Color.YELLOW);
FF11.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF11.SetLineWeight(1);

FF12.SetDefaultColor(Color.WHITE);
FF12.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF12.SetLineWeight(1);

AddChartBubble(if IsNaN(rzero[-1]) then Double.NaN else showbubbles and basis[bm] , rzero[bm], 0, if rplusminus[bm] == 1 then Color.GREEN else Color.RED);
AddChartBubble(if IsNaN(FF1[-1]) then Double.NaN else showbubbles and basis[bm], FF1[bm], F1, FF1.TakeValueColor());
AddChartBubble(if IsNaN(FF2[-1]) then Double.NaN else showbubbles and basis[bm], FF2[bm], F2, FF2.TakeValueColor());
AddChartBubble(if IsNaN(FF3[-1]) then Double.NaN else showbubbles and basis[bm], FF3[bm], F3, FF3.TakeValueColor());
AddChartBubble(if IsNaN(FF4[-1]) then Double.NaN else showbubbles and basis[bm], FF4[bm], F4, FF4.TakeValueColor());
AddChartBubble(if IsNaN(FF5[-1]) then Double.NaN else showbubbles and basis[bm], FF5[bm],  F5, FF5.TakeValueColor());
AddChartBubble(if IsNaN(FF6[-1]) then Double.NaN else showbubbles and basis[bm], FF6[bm], F6, FF6.TakeValueColor());
AddChartBubble(if IsNaN(FF7[-1]) then Double.NaN else showbubbles and basis[bm], FF7[bm], F7 , FF7.TakeValueColor());
AddChartBubble(if IsNaN(FF8[-1]) then Double.NaN else showbubbles and basis[bm], FF8[bm],  F8, FF8.TakeValueColor());
AddChartBubble(if IsNaN(FF9[-1]) then Double.NaN else showbubbles and basis[bm], FF9[bm],  F9, if rplusminus == 1 then Color.GREEN else Color.RED);
AddChartBubble(if IsNaN(FF10[-1]) then Double.NaN else showbubbles and basis[bm], FF10[bm],  F10, FF10.TakeValueColor());
AddChartBubble(if IsNaN(FF11[-1]) then Double.NaN else showbubbles and basis[bm], FF11[bm],  F11, FF11.TakeValueColor());
AddChartBubble(if IsNaN(FF12[-1]) then Double.NaN else showbubbles and basis[bm], FF12[bm], F12, FF12.TakeValueColor());

AddVerticalLine(if limit_display and HighestAll(Count) - Count <= (display_x - 1)  and period!=period[1] then 1 else if !limit_display and showverticals and period != period[1] then 1 else na, " ", Color.WHITE );
;
 
Here is an alternate method to the Auto Fib Extensions above. Rather than using the Developing High or Low of the current period as the Base for the Extension. use the Open of the current period instead.

Optional Vertical lines have been added which can be limited as are the fib extensions.

The image shows a red colored drawing using the TOS Fib Extension tool to show how the Fib Extensions are derived.
Hi SleepyZ, somehow on 1 min chart I don't see what you are showing
can this be revised to show daily chart?
 
Hi SleepyZ, I copied the codes and pasted as study, but I don't see the same chart that is shown. Not sure what I am doing wrong.

r8BlO2O.jpg
Post a share link of your chart and an explanation of what you were wanting
 
This now has some intraday periods added to select.
@SleepyZ , this might be a stretch but is there a way to extend the lines from the prior periods to the end of the chart? I found a post you did from Aug 2021 where you were able to do something similar but I wasn't able to get this code to work with this scenario. I liked your idea of using plot_limit.

https://usethinkscript.com/threads/extend-target-lines.321/post-72099

I am using the code you posted in post 27. Below is what I have using the value FF1 as an example and your prior post.

def counth = if IsNaN(FF1) and !IsNaN(FF1[1]) then 1 else counth[1] + 1;
plot FF1text = if IsNaN(FF1) then GetValue(FF1, counth) else Double.NaN;
FF1text.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF1text.SetDefaultColor(Color.GREEN);

Below is the full code.


Code:
#Auto_FibPrevDay_plots_previous_day_and_flips_fib_levels_including_intraday_timeframes
input ShowTodayOnly = { default "No", "Yes"};
input showbubbles   = yes;
input aggperiod     = {"1 MIN", "2 MIN", "3 MIN", "5 MIN", "10 MIN", "15 MIN", "30 MIN", "1 HOUR", default "DAY", "WEEK", "MONTH", "YEAR"};
input displace      = 1;

def period;
def yyyymmdd = GetYYYYMMDD();
def seconds = SecondsFromTime(0);
def month = GetYear() * 12 + GetMonth();
def day_number = DaysFromDate(First(yyyymmdd)) + GetDayOfWeek(First(yyyymmdd));
def dom = GetDayOfMonth(yyyymmdd);
def dow = GetDayOfWeek(yyyymmdd - dom + 1);

switch (aggperiod) {
case "1 MIN":
    period = Floor(seconds / 60 + day_number * 24 * 60);
case "2 MIN":
    period = Floor(seconds / 120 + day_number * 24);
case "3 MIN":
    period = Floor(seconds / 180 + day_number * 24);
case "5 MIN":
    period = Floor(seconds / 300 + day_number * 24);
case "10 MIN":
    period = Floor(seconds / 600 + day_number * 24);
case "15 MIN":
    period = Floor(seconds / 900 + day_number * 24);
case "30 MIN":
    period = Floor(seconds / 1800 + day_number * 24);
case "1 HOUR":
    period = Floor(seconds / 3600 + day_number * 24);
case "DAY":
    period = CountTradingDays(Min(First(yyyymmdd), yyyymmdd), yyyymmdd) - 1;
case "WEEK":
    period = Floor(day_number / 7);
case "MONTH":
    period = Floor(month - First(month));
case "YEAR":
    period = getyear();
}

plot ORH = if ShowTodayOnly and !IsNaN(close(period = aggperiod)[-1]) then Double.NaN else high(period = aggperiod)[displace];
plot ORL = if ShowTodayOnly and !IsNaN(close(period = aggperiod)[-1]) then Double.NaN else  low(period = aggperiod)[displace];

ORH.SetDefaultColor(Color.WHITE);
ORH.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ORH.SetLineWeight(2);

ORL.SetDefaultColor(Color.WHITE);
ORL.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ORL.SetLineWeight(2);

def bn = BarNumber();
def hbar = if high == high(period = aggperiod) then bn else hbar[1];
def lbar = if low  == low(period = aggperiod)  then bn else lbar[1];

input F1 = -1.00;
input F2 = -0.62;
input F3 = -0.27;
input F4 = 0.236;
input F5 = 0.382;
input F6 = 0.500;
input F7 = 0.618;
input F8 = 0.786;
input F10 = 1.27;
input F11 = 1.618;
input F12 = 2.00;

def rHi =  ORH;
def rLo =  ORL;

# Assign values
def range  = rHi - rLo;
plot FF1   = rLo + (range * F1);
plot FF2   = rLo + (range * F2);
plot FF3   = rLo + (range * F3);
plot FF4   = rLo + (range * F4);
plot FF5   = rLo + (range * F5);
plot FF6   = rLo + (range * F6);
plot FF7   = rLo + (range * F7);
plot FF8   = rLo + (range * F8);
plot FF10  = rLo + (range * F10);
plot FF11  = rLo + (range * F11);
plot FF12  = rLo + (range * F12);

# Assign colors
FF1.SetDefaultColor(Color.GREEN);
FF1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF1.SetLineWeight(1);

def counth = if IsNaN(FF1) and !IsNaN(FF1[1]) then 1 else counth[1] + 1;
plot FF1text = if IsNaN(FF1) then GetValue(FF1, counth) else Double.NaN;
FF1text.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF1text.SetDefaultColor(Color.GREEN);

FF2.SetDefaultColor(Color.GREEN);
FF2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF2.SetLineWeight(1);

FF3.SetDefaultColor(Color.GREEN);
FF3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF3.SetLineWeight(1);

FF4.SetDefaultColor(Color.CYAN);
FF4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF4.SetLineWeight(1);

FF5.SetDefaultColor(Color.YELLOW);
FF5.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF5.SetLineWeight(1);

FF6.SetDefaultColor(Color.WHITE);
FF6.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF6.SetLineWeight(1);

FF7.SetDefaultColor(Color.YELLOW);
FF7.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF7.SetLineWeight(1);

FF8.SetDefaultColor(Color.CYAN);
FF8.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF8.SetLineWeight(1);

FF10.SetDefaultColor(Color.GREEN);
FF10.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF10.SetLineWeight(1);

FF11.SetDefaultColor(Color.GREEN);
FF11.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF11.SetLineWeight(1);

FF12.SetDefaultColor(Color.GREEN);
FF12.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF12.SetLineWeight(1);

def basis;
def lowbar;
def highbar;
switch (aggperiod) {
case "1 MIN":
    basis = period != period[1];
    lowbar=lbar[1];
    highbar=hbar[1];
case "2 MIN":
    basis = period != period[1];
    lowbar=lbar[1];
    highbar=hbar[1];
case "3 MIN":
    basis = period != period[1];
    lowbar=lbar[1];
    highbar=hbar[1];
case "5 MIN":
    basis = period != period[1];
    lowbar=lbar[1];
    highbar=hbar[1];
case "10 MIN":
    basis = period != period[1];
    lowbar=lbar[1];
    highbar=hbar[1];
case "15 MIN":
    basis = period != period[1];
    lowbar=lbar[1];
    highbar=hbar[1];
case "30 MIN":
    basis = period != period[1];
    lowbar=lbar[1];
    highbar=hbar[1];
case "1 HOUR":
    basis = period != period[1];
    lowbar=lbar[1];
    highbar=hbar[1];
case "DAY":
    basis = GetDay() != GetDay()[1];
    lowbar=lbar;
    highbar=hbar;
case "WEEK":
    basis = GetWeek() != GetWeek()[1];
    lowbar=lbar;
    highbar=hbar;
case "MONTH":
    basis = GetMonth() != GetMonth()[1];
    lowbar=lbar;
    highbar=hbar;
case "YEAR":
    basis = getyear() != getyear()[1];
    lowbar=lbar;
    highbar=hbar;
}

AddChartBubble(showbubbles and basis, ORH, (if lowbar >highbar then 100 else 0), if lowbar > highbar then Color.GREEN else Color.RED);
AddChartBubble(showbubbles and basis, ORL, (if lowbar > highbar then 0 else 100), if lowbar > highbar then Color.RED else Color.GREEN, no);
AddChartBubble(showbubbles and basis, FF1, if lowbar > highbar then F1 else F12, FF1.TakeValueColor());
AddChartBubble(showbubbles and basis, FF2, if lowbar > highbar then F2 else F11, FF2.TakeValueColor());
AddChartBubble(showbubbles and basis, FF3, if lowbar > highbar then F3 else F10, FF3.TakeValueColor());
AddChartBubble(showbubbles and basis, FF4, if lowbar > highbar then F4 else F8, FF4.TakeValueColor());
AddChartBubble(showbubbles and basis, FF5, if lowbar > highbar then F5 else F7, FF5.TakeValueColor());
AddChartBubble(showbubbles and basis, FF6, if lowbar > highbar then F6 else F6, FF6.TakeValueColor());
AddChartBubble(showbubbles and basis, FF7, if lowbar > highbar then F7 else F5, FF7.TakeValueColor());
AddChartBubble(showbubbles and basis, FF8, if lowbar > highbar then F8 else F4, FF8.TakeValueColor());
AddChartBubble(showbubbles and basis, FF10, if lowbar > highbar then F10 else F3, FF10.TakeValueColor());
AddChartBubble(showbubbles and basis, FF11, if lowbar > highbar then F11 else F2, FF11.TakeValueColor());
AddChartBubble(showbubbles and basis, FF12, if lowbar > highbar then F12 else F1, FF12.TakeValueColor());
 
Last edited:
@SleepyZ , this might be a stretch but is there a way to extend the lines from the prior periods to the end of the chart? I found a post you did from Aug 2021 where you were able to do something similar but I wasn't able to get this code to work with this scenario. I liked your idea of using plot_limit.

https://usethinkscript.com/threads/extend-target-lines.321/post-72099

I am using the code you posted in post 27. Below is what I have using the value FF1 as an example and your prior post.

def counth = if IsNaN(FF1) and !IsNaN(FF1[1]) then 1 else counth[1] + 1;
plot FF1text = if IsNaN(FF1) then GetValue(FF1, counth) else Double.NaN;
FF1text.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF1text.SetDefaultColor(Color.GREEN);

Below is the full code.


Code:
#Auto_FibPrevDay_plots_previous_day_and_flips_fib_levels_including_intraday_timeframes
input ShowTodayOnly = { default "No", "Yes"};
input showbubbles   = yes;
input aggperiod     = {"1 MIN", "2 MIN", "3 MIN", "5 MIN", "10 MIN", "15 MIN", "30 MIN", "1 HOUR", default "DAY", "WEEK", "MONTH", "YEAR"};
input displace      = 1;

def period;
def yyyymmdd = GetYYYYMMDD();
def seconds = SecondsFromTime(0);
def month = GetYear() * 12 + GetMonth();
def day_number = DaysFromDate(First(yyyymmdd)) + GetDayOfWeek(First(yyyymmdd));
def dom = GetDayOfMonth(yyyymmdd);
def dow = GetDayOfWeek(yyyymmdd - dom + 1);

switch (aggperiod) {
case "1 MIN":
    period = Floor(seconds / 60 + day_number * 24 * 60);
case "2 MIN":
    period = Floor(seconds / 120 + day_number * 24);
case "3 MIN":
    period = Floor(seconds / 180 + day_number * 24);
case "5 MIN":
    period = Floor(seconds / 300 + day_number * 24);
case "10 MIN":
    period = Floor(seconds / 600 + day_number * 24);
case "15 MIN":
    period = Floor(seconds / 900 + day_number * 24);
case "30 MIN":
    period = Floor(seconds / 1800 + day_number * 24);
case "1 HOUR":
    period = Floor(seconds / 3600 + day_number * 24);
case "DAY":
    period = CountTradingDays(Min(First(yyyymmdd), yyyymmdd), yyyymmdd) - 1;
case "WEEK":
    period = Floor(day_number / 7);
case "MONTH":
    period = Floor(month - First(month));
case "YEAR":
    period = getyear();
}

plot ORH = if ShowTodayOnly and !IsNaN(close(period = aggperiod)[-1]) then Double.NaN else high(period = aggperiod)[displace];
plot ORL = if ShowTodayOnly and !IsNaN(close(period = aggperiod)[-1]) then Double.NaN else  low(period = aggperiod)[displace];

ORH.SetDefaultColor(Color.WHITE);
ORH.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ORH.SetLineWeight(2);

ORL.SetDefaultColor(Color.WHITE);
ORL.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ORL.SetLineWeight(2);

def bn = BarNumber();
def hbar = if high == high(period = aggperiod) then bn else hbar[1];
def lbar = if low  == low(period = aggperiod)  then bn else lbar[1];

input F1 = -1.00;
input F2 = -0.62;
input F3 = -0.27;
input F4 = 0.236;
input F5 = 0.382;
input F6 = 0.500;
input F7 = 0.618;
input F8 = 0.786;
input F10 = 1.27;
input F11 = 1.618;
input F12 = 2.00;

def rHi =  ORH;
def rLo =  ORL;

# Assign values
def range  = rHi - rLo;
plot FF1   = rLo + (range * F1);
plot FF2   = rLo + (range * F2);
plot FF3   = rLo + (range * F3);
plot FF4   = rLo + (range * F4);
plot FF5   = rLo + (range * F5);
plot FF6   = rLo + (range * F6);
plot FF7   = rLo + (range * F7);
plot FF8   = rLo + (range * F8);
plot FF10  = rLo + (range * F10);
plot FF11  = rLo + (range * F11);
plot FF12  = rLo + (range * F12);

# Assign colors
FF1.SetDefaultColor(Color.GREEN);
FF1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF1.SetLineWeight(1);

def counth = if IsNaN(FF1) and !IsNaN(FF1[1]) then 1 else counth[1] + 1;
plot FF1text = if IsNaN(FF1) then GetValue(FF1, counth) else Double.NaN;
FF1text.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF1text.SetDefaultColor(Color.GREEN);

FF2.SetDefaultColor(Color.GREEN);
FF2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF2.SetLineWeight(1);

FF3.SetDefaultColor(Color.GREEN);
FF3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF3.SetLineWeight(1);

FF4.SetDefaultColor(Color.CYAN);
FF4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF4.SetLineWeight(1);

FF5.SetDefaultColor(Color.YELLOW);
FF5.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF5.SetLineWeight(1);

FF6.SetDefaultColor(Color.WHITE);
FF6.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF6.SetLineWeight(1);

FF7.SetDefaultColor(Color.YELLOW);
FF7.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF7.SetLineWeight(1);

FF8.SetDefaultColor(Color.CYAN);
FF8.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF8.SetLineWeight(1);

FF10.SetDefaultColor(Color.GREEN);
FF10.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF10.SetLineWeight(1);

FF11.SetDefaultColor(Color.GREEN);
FF11.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF11.SetLineWeight(1);

FF12.SetDefaultColor(Color.GREEN);
FF12.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF12.SetLineWeight(1);

def basis;
def lowbar;
def highbar;
switch (aggperiod) {
case "1 MIN":
    basis = period != period[1];
    lowbar=lbar[1];
    highbar=hbar[1];
case "2 MIN":
    basis = period != period[1];
    lowbar=lbar[1];
    highbar=hbar[1];
case "3 MIN":
    basis = period != period[1];
    lowbar=lbar[1];
    highbar=hbar[1];
case "5 MIN":
    basis = period != period[1];
    lowbar=lbar[1];
    highbar=hbar[1];
case "10 MIN":
    basis = period != period[1];
    lowbar=lbar[1];
    highbar=hbar[1];
case "15 MIN":
    basis = period != period[1];
    lowbar=lbar[1];
    highbar=hbar[1];
case "30 MIN":
    basis = period != period[1];
    lowbar=lbar[1];
    highbar=hbar[1];
case "1 HOUR":
    basis = period != period[1];
    lowbar=lbar[1];
    highbar=hbar[1];
case "DAY":
    basis = GetDay() != GetDay()[1];
    lowbar=lbar;
    highbar=hbar;
case "WEEK":
    basis = GetWeek() != GetWeek()[1];
    lowbar=lbar;
    highbar=hbar;
case "MONTH":
    basis = GetMonth() != GetMonth()[1];
    lowbar=lbar;
    highbar=hbar;
case "YEAR":
    basis = getyear() != getyear()[1];
    lowbar=lbar;
    highbar=hbar;
}

AddChartBubble(showbubbles and basis, ORH, (if lowbar >highbar then 100 else 0), if lowbar > highbar then Color.GREEN else Color.RED);
AddChartBubble(showbubbles and basis, ORL, (if lowbar > highbar then 0 else 100), if lowbar > highbar then Color.RED else Color.GREEN, no);
AddChartBubble(showbubbles and basis, FF1, if lowbar > highbar then F1 else F12, FF1.TakeValueColor());
AddChartBubble(showbubbles and basis, FF2, if lowbar > highbar then F2 else F11, FF2.TakeValueColor());
AddChartBubble(showbubbles and basis, FF3, if lowbar > highbar then F3 else F10, FF3.TakeValueColor());
AddChartBubble(showbubbles and basis, FF4, if lowbar > highbar then F4 else F8, FF4.TakeValueColor());
AddChartBubble(showbubbles and basis, FF5, if lowbar > highbar then F5 else F7, FF5.TakeValueColor());
AddChartBubble(showbubbles and basis, FF6, if lowbar > highbar then F6 else F6, FF6.TakeValueColor());
AddChartBubble(showbubbles and basis, FF7, if lowbar > highbar then F7 else F5, FF7.TakeValueColor());
AddChartBubble(showbubbles and basis, FF8, if lowbar > highbar then F8 else F4, FF8.TakeValueColor());
AddChartBubble(showbubbles and basis, FF10, if lowbar > highbar then F10 else F3, FF10.TakeValueColor());
AddChartBubble(showbubbles and basis, FF11, if lowbar > highbar then F11 else F2, FF11.TakeValueColor());
AddChartBubble(showbubbles and basis, FF12, if lowbar > highbar then F12 else F1, FF12.TakeValueColor());
It would not be very practicable as all horizontal lines per period would require a separate plot to extend them to the right edge of the chart. I have done this with the script function where there a few lines (for example, volumeprofile's poc, vah and val). However, this script has too many horizontal line plots and corresponding look and feel, coding including bubbles.
 
[Edit: The code in post #31 is a perhaps a better version of the Auto Fib Extension than below. It does not repaint as below does]

This will provide auto Fib Extensions vs Fib Retracements.

There is an overlay, using the TOS fib extension tool (color red), of the auto drawn fibs using the high/low of the previous aggperiod and the developing extension into the current aggperiod.

You can limit the display of the fibs at the input screen.
Is it possible to draw both low to high and high to low FIb extensions? Currently it shows the lower extensions only..how can I get the opposite extensions?
 
Is it possible to draw both low to high and high to low FIb extensions? Currently it shows the lower extensions only..how can I get the opposite extensions?
The code in post #31 draws both low to high and high to low dependent on whether the low or high of prior period selected is first. In this version, the last anchor of the fib extension is based upon the open of the next period. The bubbles 0 to 100 will flip dependent on the low to high or high to low comparision of the prior period.
 
Here is an alternate method to the Auto Fib Extensions above. Rather than using the Developing High or Low of the current period as the Base for the Extension. use the Open of the current period instead.

Optional Vertical lines have been added which can be limited as are the fib extensions.

The image shows a red colored drawing using the TOS Fib Extension tool to show how the Fib Extensions are derived.
hi @SleepyZ thanks for sharing this script. i have a question i see that one the anchors is the open but where is the other point being anchored? what am i missing? does not seem to line up with any hi or low, sorry i am very new to all this. and thanks again.
 

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

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
422 Online
Create Post

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