Draw a trendline based on time

bamafamily333

New member
Hey all,
I am looking to draw a simple tend line based on time on intraday charts.
An example might be draw a line from the 10:45 AM close to the 12:15 PM close.
These would show on all the intraday charts for the timeframe chosen. Not just today.

I searched the forums and could not find anyone who had asked this before.
In advance, thanks
Bama
 
Hey all,
I am looking to draw a simple tend line based on time on intraday charts.
An example might be draw a line from the 10:45 AM close to the 12:15 PM close.
These would show on all the intraday charts for the timeframe chosen. Not just today.

I searched the forums and could not find anyone who had asked this before.
In advance, thanks
Bama

To have the trendlines display as lines on each day independently then this requires a plot statement for each line. Otherwise, it will display as a continuous zigzag looking line. So here is one way to do your request is with a script. There are 10 plots. More plots and styles can be made following the pattern used in the code.

Capture.jpg
Ruby:
script tl {
    def ymd      = GetYYYYMMDD();
    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) ;

    input days  = 0;
    input time1 = 1045;
    input time2 = 1215;
    input price = high;

    def begin = if  thisDay == days and SecondsFromTime(time1) == 0 then 1 else 0;
    def end   = if  thisDay == days and SecondsFromTime(time2) == 0 then 1 else 0;
    def beginprice  = if thisDay == days and begin then price else beginprice[1];
    def endprice1   = if thisDay == days and end   then price else endprice1[1];
    plot line = if thisDay == days and begin then beginprice else if thisDay == days and end then endprice1 else Double.NaN;
    line.EnableApproximation();
}

input begin  = 1045;
input end    = 1215;
input weight = 5;

plot tl0 = tl(0, begin, end);
plot tl1 = tl(1, begin, end);
plot tl2 = tl(2, begin, end);
plot tl3 = tl(3, begin, end);
plot tl4 = tl(4, begin, end);
plot tl5 = tl(5, begin, end);
plot tl6 = tl(6, begin, end);
plot tl7 = tl(7, begin, end);
plot tl8 = tl(8, begin, end);
plot tl9 = tl(9, begin, end);

defineglobalColor("TL", color.yellow);
tl0.EnableApproximation();
tl0.SetLineWeight(weight);
tl0.setdefaultColor(globalColor("TL"));
tl1.EnableApproximation();
tl1.SetLineWeight(weight);
tl1.setdefaultColor(globalColor("TL"));
tl2.EnableApproximation();
tl2.SetLineWeight(weight);
tl2.setdefaultColor(globalColor("TL"));
tl3.EnableApproximation();
tl3.SetLineWeight(weight);
tl3.setdefaultColor(globalColor("TL"));
tl4.EnableApproximation();
tl4.SetLineWeight(weight);
tl4.setdefaultColor(globalColor("TL"));
tl5.EnableApproximation();
tl5.SetLineWeight(weight);
tl5.setdefaultColor(globalColor("TL"));
tl6.EnableApproximation();
tl6.SetLineWeight(weight);
tl6.setdefaultColor(globalColor("TL"));
tl7.EnableApproximation();
tl7.SetLineWeight(weight);
tl7.setdefaultColor(globalColor("TL"));
tl8.EnableApproximation();
tl8.SetLineWeight(weight);
tl8.setdefaultColor(globalColor("TL"));
tl9.EnableApproximation();
tl9.SetLineWeight(weight);
tl9.setdefaultColor(globalColor("TL"));
 
To have the trendlines display as lines on each day independently then this requires a plot statement for each line. Otherwise, it will display as a continuous zigzag looking line. So here is one way to do your request is with a script. There are 10 plots. More plots and styles can be made following the pattern used in the code.
This is a great script! Is there anyway to extend the line? For example, Time1 is at 4AM and time2 is at 930AM, but I the horizontal line to extend 20 bars to the right. Thanks in advance!
 
This is a great script! Is there anyway to extend the line? For example, Time1 is at 4AM and time2 is at 930AM, but I the horizontal line to extend 20 bars to the right. Thanks in advance!

This adds a variable line extension per length input ext.

Ruby:
script tl {
    def ymd      = GetYYYYMMDD();
    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) ;

    input days  = 1;
    input time1 = 1045;
    input time2 = 1215;
    input ext   = 20;
    input price = high;

    def begin = if  thisDay == days and SecondsFromTime(time1) == 0 then 1 else 0;
    def end   = if  thisDay == days and SecondsFromTime(time2) == 0 then 1 else 0;
    def beginprice  = if thisDay == days and begin then price else beginprice[1];
    def endprice1   = if thisDay == days and end   then price else endprice1[1];
    plot line = if thisDay == days and begin then beginprice else if thisDay == days and end then endprice1 else Double.NaN;
    line.EnableApproximation();

#Line Extension-----------------------------------------------------------------
    def bn      = BarNumber();
    def beginbn = if  thisDay == days and SecondsFromTime(time1) == 0 then bn else beginbn[1];
    def endbn   = if  thisDay == days and SecondsFromTime(time2) == 0 then bn else endbn[1];

    def y1    = if thisDay == days and bn == HighestAll(beginbn) then beginprice else y1[1];
    def y2    = if thisDay == days and bn == HighestAll(endbn) then endprice1   else y2[1];
    def slope = if thisDay == days and endbn > beginbn
            then (( y2 - y1) / (endbn - beginbn))
            else (( y1 - y2) / (beginbn - endbn));
    rec lext  = if thisDay == days and IsNaN(close[-1])
            then lext[1] + (lext[1] - lext[2])
            else if thisDay == days and endbn > beginbn
            then y1 + ((bn - beginbn) * slope)
            else y2 + ((bn - endbn) * slope);
    plot lextplot = if thisDay == days and Between(bn, HighestAll(endbn) + 1, HighestAll(endbn) + 20)
                then lext[1]
                else Double.NaN;
    lextplot.EnableApproximation();
    lextplot.SetPaintingStrategy(PaintingStrategy.LINE);
    lextplot.SetDefaultColor(Color.WHITE);
    lextplot.SetLineWeight(2);
}

input begin  = 1045;
input end    = 1215;
input ext    = 20;
input weight = 5;

plot tl0 = tl(0, begin, end, ext);
plot tl1 = tl(1, begin, end, ext);
plot tl2 = tl(2, begin, end, ext);
plot tl3 = tl(3, begin, end, ext);
plot tl4 = tl(4, begin, end, ext);
plot tl5 = tl(5, begin, end, ext);
plot tl6 = tl(6, begin, end, ext);
plot tl7 = tl(7, begin, end, ext);
plot tl8 = tl(8, begin, end, ext);
plot tl9 = tl(9, begin, end, ext);

DefineGlobalColor("TL", Color.YELLOW);
tl0.EnableApproximation();
tl0.SetLineWeight(weight);
tl0.SetDefaultColor(GlobalColor("TL"));
tl1.EnableApproximation();
tl1.SetLineWeight(weight);
tl1.SetDefaultColor(GlobalColor("TL"));
tl2.EnableApproximation();
tl2.SetLineWeight(weight);
tl2.SetDefaultColor(GlobalColor("TL"));
tl3.EnableApproximation();
tl3.SetLineWeight(weight);
tl3.SetDefaultColor(GlobalColor("TL"));
tl4.EnableApproximation();
tl4.SetLineWeight(weight);
tl4.SetDefaultColor(GlobalColor("TL"));
tl5.EnableApproximation();
tl5.SetLineWeight(weight);
tl5.SetDefaultColor(GlobalColor("TL"));
tl6.EnableApproximation();
tl6.SetLineWeight(weight);
tl6.SetDefaultColor(GlobalColor("TL"));
tl7.EnableApproximation();
tl7.SetLineWeight(weight);
tl7.SetDefaultColor(GlobalColor("TL"));
tl8.EnableApproximation();
tl8.SetLineWeight(weight);
tl8.SetDefaultColor(GlobalColor("TL"));
tl9.EnableApproximation();
tl9.SetLineWeight(weight);
tl9.SetDefaultColor(GlobalColor("TL"));

plot tlext0 = tl(0, begin, end, ext).lextplot;
plot tlext1 = tl(1, begin, end, ext).lextplot;
plot tlext2 = tl(2, begin, end, ext).lextplot;
plot tlext3 = tl(3, begin, end, ext).lextplot;
plot tlext4 = tl(4, begin, end, ext).lextplot;
plot tlext5 = tl(5, begin, end, ext).lextplot;
plot tlext6 = tl(6, begin, end, ext).lextplot;
plot tlext7 = tl(7, begin, end, ext).lextplot;
plot tlext8 = tl(8, begin, end, ext).lextplot;
plot tlext9 = tl(9, begin, end, ext).lextplot;

tlext0.EnableApproximation();
tlext0.SetLineWeight(weight);
tlext0.SetDefaultColor(GlobalColor("TL"));
tlext1.EnableApproximation();
tlext1.SetLineWeight(weight);
tlext1.SetDefaultColor(GlobalColor("TL"));
tlext2.EnableApproximation();
tlext2.SetLineWeight(weight);
tlext2.SetDefaultColor(GlobalColor("TL"));
tlext3.EnableApproximation();
tlext3.SetLineWeight(weight);
tlext3.SetDefaultColor(GlobalColor("TL"));
tlext4.EnableApproximation();
tlext4.SetLineWeight(weight);
tlext4.SetDefaultColor(GlobalColor("TL"));
tlext5.EnableApproximation();
tlext5.SetLineWeight(weight);
tlext5.SetDefaultColor(GlobalColor("TL"));
tlext6.EnableApproximation();
tlext6.SetLineWeight(weight);
tlext6.SetDefaultColor(GlobalColor("TL"));
tlext7.EnableApproximation();
tlext7.SetLineWeight(weight);
tlext7.SetDefaultColor(GlobalColor("TL"));
tlext8.EnableApproximation();
tlext8.SetLineWeight(weight);
tlext8.SetDefaultColor(GlobalColor("TL"));
tlext9.EnableApproximation();
tlext9.SetLineWeight(weight);
tlext9.SetDefaultColor(GlobalColor("TL"));
 
Thanks! There is a gab between the two lines. Is there anyway to get rid of that or have it more alignedExample here?
Also, is there a way to have the line start at the pre-market high instead of a specific time Example here?

I believe that I have fixed that gap.

Capture.jpg
Ruby:
script tl {
    def ymd      = GetYYYYMMDD();
    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) ;

    input days  = 1;
    input time1 = 1045;
    input time2 = 1215;
    input ext   = 20;
    input price = high;

    def begin = if  thisDay == days and SecondsFromTime(time1) == 0 then 1 else 0;
    def end   = if  thisDay == days and SecondsFromTime(time2) == 0 then 1 else 0;
    def beginprice  = if thisDay == days and begin then price else beginprice[1];
    def endprice1   = if thisDay == days and end   then price else endprice1[1];
    plot line = if thisDay == days and begin then beginprice else if thisDay == days and end then endprice1 else Double.NaN;
    line.EnableApproximation();

#Line Extension-----------------------------------------------------------------
    def bn      = BarNumber();
    def beginbn = if  thisDay == days and SecondsFromTime(time1) == 0 then bn else beginbn[1];
    def endbn   = if  thisDay == days and SecondsFromTime(time2) == 0 then bn else endbn[1];

    def y1    = if thisDay == days and bn == HighestAll(beginbn) then beginprice else y1[1];
    def y2    = if thisDay == days and bn == HighestAll(endbn) then endprice1   else y2[1];
    def slope = if thisDay == days and endbn > beginbn
            then (( y2 - y1) / (endbn - beginbn))
            else (( y1 - y2) / (beginbn - endbn));
    rec lext  = if thisDay == days and IsNaN(close[-1])
            then lext[1] + (lext[1] - lext[2])
            else if thisDay == days and endbn > beginbn
            then y1 + ((bn - beginbn) * slope)
            else y2 + ((bn - endbn) * slope);
    plot lextplot = if thisDay == days and Between(bn, HighestAll(endbn) , HighestAll(endbn) + ext)
                then lext
                else Double.NaN;
    lextplot.EnableApproximation();
    lextplot.SetPaintingStrategy(PaintingStrategy.LINE);
    lextplot.SetDefaultColor(Color.WHITE);
    lextplot.SetLineWeight(2);
}

input begin  = 1045;
input end    = 1215;
input ext    = 20;
input weight = 5;

plot tl0 = tl(0, begin, end, ext);
plot tl1 = tl(1, begin, end, ext);
plot tl2 = tl(2, begin, end, ext);
plot tl3 = tl(3, begin, end, ext);
plot tl4 = tl(4, begin, end, ext);
plot tl5 = tl(5, begin, end, ext);
plot tl6 = tl(6, begin, end, ext);
plot tl7 = tl(7, begin, end, ext);
plot tl8 = tl(8, begin, end, ext);
plot tl9 = tl(9, begin, end, ext);

DefineGlobalColor("TL", Color.YELLOW);
tl0.EnableApproximation();
tl0.SetLineWeight(weight);
tl0.SetDefaultColor(GlobalColor("TL"));
tl1.EnableApproximation();
tl1.SetLineWeight(weight);
tl1.SetDefaultColor(GlobalColor("TL"));
tl2.EnableApproximation();
tl2.SetLineWeight(weight);
tl2.SetDefaultColor(GlobalColor("TL"));
tl3.EnableApproximation();
tl3.SetLineWeight(weight);
tl3.SetDefaultColor(GlobalColor("TL"));
tl4.EnableApproximation();
tl4.SetLineWeight(weight);
tl4.SetDefaultColor(GlobalColor("TL"));
tl5.EnableApproximation();
tl5.SetLineWeight(weight);
tl5.SetDefaultColor(GlobalColor("TL"));
tl6.EnableApproximation();
tl6.SetLineWeight(weight);
tl6.SetDefaultColor(GlobalColor("TL"));
tl7.EnableApproximation();
tl7.SetLineWeight(weight);
tl7.SetDefaultColor(GlobalColor("TL"));
tl8.EnableApproximation();
tl8.SetLineWeight(weight);
tl8.SetDefaultColor(GlobalColor("TL"));
tl9.EnableApproximation();
tl9.SetLineWeight(weight);
tl9.SetDefaultColor(GlobalColor("TL"));

plot tlext0 = tl(0, begin, end, ext).lextplot;
plot tlext1 = tl(1, begin, end, ext).lextplot;
plot tlext2 = tl(2, begin, end, ext).lextplot;
plot tlext3 = tl(3, begin, end, ext).lextplot;
plot tlext4 = tl(4, begin, end, ext).lextplot;
plot tlext5 = tl(5, begin, end, ext).lextplot;
plot tlext6 = tl(6, begin, end, ext).lextplot;
plot tlext7 = tl(7, begin, end, ext).lextplot;
plot tlext8 = tl(8, begin, end, ext).lextplot;
plot tlext9 = tl(9, begin, end, ext).lextplot;

tlext0.EnableApproximation();
tlext0.SetLineWeight(weight);
tlext0.SetDefaultColor(GlobalColor("TL"));
tlext1.EnableApproximation();
tlext1.SetLineWeight(weight);
tlext1.SetDefaultColor(GlobalColor("TL"));
tlext2.EnableApproximation();
tlext2.SetLineWeight(weight);
tlext2.SetDefaultColor(GlobalColor("TL"));
tlext3.EnableApproximation();
tlext3.SetLineWeight(weight);
tlext3.SetDefaultColor(GlobalColor("TL"));
tlext4.EnableApproximation();
tlext4.SetLineWeight(weight);
tlext4.SetDefaultColor(GlobalColor("TL"));
tlext5.EnableApproximation();
tlext5.SetLineWeight(weight);
tlext5.SetDefaultColor(GlobalColor("TL"));
tlext6.EnableApproximation();
tlext6.SetLineWeight(weight);
tlext6.SetDefaultColor(GlobalColor("TL"));
tlext7.EnableApproximation();
tlext7.SetLineWeight(weight);
tlext7.SetDefaultColor(GlobalColor("TL"));
tlext8.EnableApproximation();
tlext8.SetLineWeight(weight);
tlext8.SetDefaultColor(GlobalColor("TL"));
tlext9.EnableApproximation();
tlext9.SetLineWeight(weight);
tlext9.SetDefaultColor(GlobalColor("TL"));
 
Thanks so much SleepyZ!!! Exactly what I was asking for…
I really appreciate it…
Is there a way to set a date in the past that would show the 10 dates from that point?
If it’s too much trouble, don’t worry about it….thx
 
Thanks so much SleepyZ!!! Exactly what I was asking for…
I really appreciate it…
Is there a way to set a date in the past that would show the 10 dates from that point?
If it’s too much trouble, don’t worry about it….thx

See if this works the way you want. It allows you to input a choice between your input of a number of Days or a Date to lookback to start the plotting of the trendlines.

[Edit with new code for premarket and corrected lookback from lookback - to lookback + , eg: plot tl0 = tl(AbsValue(lookback + 0), method, time1, end, ext);
Ruby:
    def bn      = BarNumber();
script tl {
    def ymd      = GetYYYYMMDD();
    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) ;
    def bn      = BarNumber();

    input days  = 0;
    def prehigh = CompoundValue(1, if thisDay == days and thisDay == days + 1
                  then high
                  else if thisDay == days and GetTime() < RegularTradingStart(GetYYYYMMDD())
                  then Max(high, prehigh[1])
                  else prehigh[1], thisDay == days and high);

    def prelow = CompoundValue(1, if thisDay == days and thisDay == days + 1
                 then low
                 else if thisDay == days and GetTime() < RegularTradingStart(GetYYYYMMDD())
                 then Min(low, prelow[1])
                 else prelow[1], low);

    def prehighbn = if thisDay == days and GetTime() < RegularTradingStart(GetYYYYMMDD()) and  Round(high) == Round(HighestAll(prehigh))
                    then bn else prehighbn[1];

    def prelowbn = if thisDay == days and GetTime() < RegularTradingStart(GetYYYYMMDD()) and low == prelow
                   then bn else prelowbn[1];
    input method = {default Time, PremarketHigh, PremarketLow};
    input time1 = 1045;
    input time2 = 0930;
    input ext   = 20;

    def begin;
    def pricebegin;
    def priceend;
    def beginbn ;
    def end;
    def endbn;
    switch (method) {
    case Time:
        begin = if  thisDay == days and SecondsFromTime(time1) == 0 then 1 else 0;
        pricebegin = high;
        priceend = high;
        beginbn = if  thisDay == days and SecondsFromTime(time1) == 0 then bn else beginbn[1];
        end     = if  thisDay == days and SecondsFromTime(time2) == 0 then 1 else 0;
        endbn   = if  thisDay == days and SecondsFromTime(time2) == 0 then bn else endbn[1];
    case PremarketHigh:
        begin = if thisDay == days and BarNumber() == HighestAll(prehighbn) then 1 else 0;
        pricebegin = prehigh;
        priceend = if thisDay == days and GetTime() crosses above RegularTradingStart(GetYYYYMMDD()) then low else priceend[1];
        beginbn = HighestAll(prehighbn);
        end = if thisDay == days and GetTime() crosses above RegularTradingStart(GetYYYYMMDD()) then 1 else 0;
        endbn = if thisDay == days and GetTime() crosses above RegularTradingStart(GetYYYYMMDD()) then bn else endbn[1];
    case PremarketLow:
        begin = if thisDay == days and BarNumber() == HighestAll(prelowbn) then 1 else 0;
        pricebegin = prelow;
        priceend = if thisDay == days and GetTime() crosses above RegularTradingStart(GetYYYYMMDD()) then high else priceend[1];
        beginbn = prelowbn;
        end = if thisDay == days and GetTime() crosses above RegularTradingStart(GetYYYYMMDD()) then 1 else 0;
        endbn = if thisDay == days and GetTime() crosses above RegularTradingStart(GetYYYYMMDD()) then bn else endbn[1];
}


    def beginprice  = if thisDay == days and begin then pricebegin else beginprice[1];
    def endprice1   = if thisDay == days and end   then priceend else endprice1[1];
    plot line = if thisDay == days and begin then beginprice else if thisDay == days and end then endprice1 else Double.NaN;
    line.EnableApproximation();

#Line Extension-----------------------------------------------------------------

    def y1    = if thisDay == days and bn == HighestAll(beginbn) then beginprice else y1[1];
    def y2    = if thisDay == days and bn == HighestAll(endbn) then endprice1   else y2[1];
    def slope = if thisDay == days and endbn > beginbn
            then (( y2 - y1) / (endbn - beginbn))
            else (( y1 - y2) / (beginbn - endbn));
    rec lext  = if thisDay == days and IsNaN(close[-1])
            then lext[1] + (lext[1] - lext[2])
            else if thisDay == days and endbn > beginbn
            then y1 + ((bn - beginbn) * slope)
            else y2 + ((bn - endbn) * slope);
    plot lextplot = if thisDay == days and Between(bn, HighestAll(endbn) , HighestAll(endbn) + ext)
                then lext
                else Double.NaN;
    lextplot.EnableApproximation();
    lextplot.SetPaintingStrategy(PaintingStrategy.LINE);
    lextplot.SetDefaultColor(Color.WHITE);
    lextplot.SetLineWeight(2);
}

def today = HighestAll(if IsNaN(close[-1]) then GetYYYYMMDD() else 0);

input startlookback = {default Days, Date};
input lookbackdays  = 0;
input lookbackdate  = 20211126;
def   lookback      = if startlookback == startlookback.Days
                      then lookbackdays
                      else CountTradingDays(lookbackdate, today) - 1;

input method   = {default Time, PremarketHigh, PremarketLow};
input time1    = 1045;
input end      = 1215;
input ext      = 20;
input weight   = 5;

plot tl0 = tl(AbsValue(lookback + 0), method, time1, end, ext);
plot tl1 = tl(AbsValue(lookback + 1), method, time1, end, ext);
plot tl2 = tl(AbsValue(lookback + 2), method, time1, end, ext);
plot tl3 = tl(AbsValue(lookback + 3), method, time1, end, ext);
plot tl4 = tl(AbsValue(lookback + 4), method, time1, end, ext);
plot tl5 = tl(AbsValue(lookback + 5), method, time1, end, ext);
plot tl6 = tl(AbsValue(lookback + 6), method, time1, end, ext);
plot tl7 = tl(AbsValue(lookback + 7), method, time1, end, ext);
plot tl8 = tl(AbsValue(lookback + 8), method, time1, end, ext);
plot tl9 = tl(AbsValue(lookback + 9), method, time1, end, ext);

DefineGlobalColor("TL", Color.YELLOW);
tl0.EnableApproximation();
tl0.SetLineWeight(weight);
tl0.SetDefaultColor(GlobalColor("TL"));
tl1.EnableApproximation();
tl1.SetLineWeight(weight);
tl1.SetDefaultColor(GlobalColor("TL"));
tl2.EnableApproximation();
tl2.SetLineWeight(weight);
tl2.SetDefaultColor(GlobalColor("TL"));
tl3.EnableApproximation();
tl3.SetLineWeight(weight);
tl3.SetDefaultColor(GlobalColor("TL"));
tl4.EnableApproximation();
tl4.SetLineWeight(weight);
tl4.SetDefaultColor(GlobalColor("TL"));
tl5.EnableApproximation();
tl5.SetLineWeight(weight);
tl5.SetDefaultColor(GlobalColor("TL"));
tl6.EnableApproximation();
tl6.SetLineWeight(weight);
tl6.SetDefaultColor(GlobalColor("TL"));
tl7.EnableApproximation();
tl7.SetLineWeight(weight);
tl7.SetDefaultColor(GlobalColor("TL"));
tl8.EnableApproximation();
tl8.SetLineWeight(weight);
tl8.SetDefaultColor(GlobalColor("TL"));
tl9.EnableApproximation();
tl9.SetLineWeight(weight);
tl9.SetDefaultColor(GlobalColor("TL"));

plot tlext0 = tl(AbsValue(lookback + 0), method, time1, end, ext).lextplot;
plot tlext1 = tl(AbsValue(lookback + 1), method, time1, end, ext).lextplot;
plot tlext2 = tl(AbsValue(lookback + 2), method, time1, end, ext).lextplot;
plot tlext3 = tl(AbsValue(lookback + 3), method, time1, end, ext).lextplot;
plot tlext4 = tl(AbsValue(lookback + 4), method, time1, end, ext).lextplot;
plot tlext5 = tl(AbsValue(lookback + 5), method, time1, end, ext).lextplot;
plot tlext6 = tl(AbsValue(lookback + 6), method, time1, end, ext).lextplot;
plot tlext7 = tl(AbsValue(lookback + 7), method, time1, end, ext).lextplot;
plot tlext8 = tl(AbsValue(lookback + 8), method, time1, end, ext).lextplot;
plot tlext9 = tl(AbsValue(lookback + 9), method, time1, end, ext).lextplot;

tlext0.EnableApproximation();
tlext0.SetLineWeight(weight);
tlext0.SetDefaultColor(GlobalColor("TL"));
tlext1.EnableApproximation();
tlext1.SetLineWeight(weight);
tlext1.SetDefaultColor(GlobalColor("TL"));
tlext2.EnableApproximation();
tlext2.SetLineWeight(weight);
tlext2.SetDefaultColor(GlobalColor("TL"));
tlext3.EnableApproximation();
tlext3.SetLineWeight(weight);
tlext3.SetDefaultColor(GlobalColor("TL"));
tlext4.EnableApproximation();
tlext4.SetLineWeight(weight);
tlext4.SetDefaultColor(GlobalColor("TL"));
tlext5.EnableApproximation();
tlext5.SetLineWeight(weight);
tlext5.SetDefaultColor(GlobalColor("TL"));
tlext6.EnableApproximation();
tlext6.SetLineWeight(weight);
tlext6.SetDefaultColor(GlobalColor("TL"));
tlext7.EnableApproximation();
tlext7.SetLineWeight(weight);
tlext7.SetDefaultColor(GlobalColor("TL"));
tlext8.EnableApproximation();
tlext8.SetLineWeight(weight);
tlext8.SetDefaultColor(GlobalColor("TL"));
tlext9.EnableApproximation();
tlext9.SetLineWeight(weight);
tlext9.SetDefaultColor(GlobalColor("TL"));
 
Last edited:
Is there a way to have the line start at the pre-market high or low instead of a specific time?

Here is the script with premarket highs/lows in addition to the time, days, date basis. I have coded the premarket highs to go to the low of RTH opening bar and premarket lows to anchor to the high of that bar.

Here is a chart with a script set to premarket high and anothe copy set to premarket low together

Capture.jpg

Ruby:
    def bn      = BarNumber();
script tl {
    def ymd      = GetYYYYMMDD();
    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) ;
    def bn      = BarNumber();

    input days  = 0;
    def prehigh = CompoundValue(1, if thisDay == days and thisDay[1] == days + 1
                  then high
                  else if thisDay == days and GetTime() < RegularTradingStart(GetYYYYMMDD())
                  then Max(high, prehigh[1])
                  else prehigh[1], thisDay == days and high);

    def prelow = CompoundValue(1, if thisDay == days and thisDay[1] == days + 1
                 then low
                 else if thisDay == days and GetTime() < RegularTradingStart(GetYYYYMMDD())
                 then Min(low, prelow[1])
                 else prelow[1], low);

    def prehighbn = if thisDay == days and GetTime() < RegularTradingStart(GetYYYYMMDD()) and  Round(high) == Round(HighestAll(prehigh))
                    then bn else prehighbn[1];

    def prelowbn = if thisDay == days and GetTime() < RegularTradingStart(GetYYYYMMDD()) and low == prelow
                   then bn else prelowbn[1];
    input method = {default Time, PremarketHigh, PremarketLow};
    input time1  = 1045;
    input time2  = 0930;
    input ext    = 20;

    def begin;
    def pricebegin;
    def priceend;
    def beginbn ;
    def end;
    def endbn;
    switch (method) {
    case Time:
        begin      = if  thisDay == days and SecondsFromTime(time1) == 0 then 1 else 0;
        pricebegin = high;
        priceend   = high;
        beginbn    = if  thisDay == days and SecondsFromTime(time1) == 0 then bn else beginbn[1];
        end        = if  thisDay == days and SecondsFromTime(time2) == 0 then 1 else 0;
        endbn      = if  thisDay == days and SecondsFromTime(time2) == 0 then bn else endbn[1];
    case PremarketHigh:
        begin      = if thisDay == days and BarNumber() == HighestAll(prehighbn) then 1 else 0;
        pricebegin = prehigh;
        priceend   = if thisDay == days and GetTime() crosses above RegularTradingStart(GetYYYYMMDD()) then low else priceend[1];
        beginbn    = HighestAll(prehighbn);
        end        = if thisDay == days and GetTime() crosses above RegularTradingStart(GetYYYYMMDD()) then 1 else 0;
        endbn      = if thisDay == days and GetTime() crosses above RegularTradingStart(GetYYYYMMDD()) then bn else endbn[1];
    case PremarketLow:
        begin      = if thisDay == days and BarNumber() == HighestAll(prelowbn)
                     then 1 else 0;
        pricebegin = prelow;
        priceend   = if thisDay == days and GetTime() crosses above RegularTradingStart(GetYYYYMMDD()) then high else priceend[1];
        beginbn    = prelowbn;
        end        = if thisDay == days and GetTime() crosses above RegularTradingStart(GetYYYYMMDD()) then 1 else 0;
        endbn      = if thisDay == days and GetTime() crosses above RegularTradingStart(GetYYYYMMDD()) then bn else endbn[1];
}


    def beginprice  = if thisDay == days and begin then pricebegin else beginprice[1];
    def endprice1   = if thisDay == days and end   then priceend else endprice1[1];
    plot line       = if thisDay == days and begin
                      then beginprice
                      else if thisDay == days and end
                      then endprice1 else Double.NaN;
    line.EnableApproximation();

#Line Extension-----------------------------------------------------------------

    def y1    = if thisDay == days and bn == HighestAll(beginbn) then beginprice else y1[1];
    def y2    = if thisDay == days and bn == HighestAll(endbn) then endprice1   else y2[1];
    def slope = if thisDay == days and endbn > beginbn
            then (( y2 - y1) / (endbn - beginbn))
            else (( y1 - y2) / (beginbn - endbn));
    rec lext  = if thisDay == days and IsNaN(close[-1])
            then lext[1] + (lext[1] - lext[2])
            else if thisDay == days and endbn > beginbn
            then y1 + ((bn - beginbn) * slope)
            else y2 + ((bn - endbn) * slope);
    plot lextplot = if thisDay == days and Between(bn, HighestAll(endbn) , HighestAll(endbn) + ext)
                then lext
                else Double.NaN;
    lextplot.EnableApproximation();
    lextplot.SetPaintingStrategy(PaintingStrategy.LINE);
    lextplot.SetDefaultColor(Color.WHITE);
    lextplot.SetLineWeight(2);
}

def today = HighestAll(if IsNaN(close[-1]) then GetYYYYMMDD() else 0);

input startlookback = {default Days, Date};
input lookbackdays  = 0;
input lookbackdate  = 20211126;
def   lookback      = if startlookback == startlookback.Days
                      then lookbackdays
                      else CountTradingDays(lookbackdate, today) - 1;

input method   = {default Time, PremarketHigh, PremarketLow};
input time1    = 1045;
input end      = 1215;
input ext      = 20;
input weight   = 5;

plot tl0 = tl(AbsValue(lookback + 0), method, time1, end, ext);
plot tl1 = tl(AbsValue(lookback + 1), method, time1, end, ext);
plot tl2 = tl(AbsValue(lookback + 2), method, time1, end, ext);
plot tl3 = tl(AbsValue(lookback + 3), method, time1, end, ext);
plot tl4 = tl(AbsValue(lookback + 4), method, time1, end, ext);
plot tl5 = tl(AbsValue(lookback + 5), method, time1, end, ext);
plot tl6 = tl(AbsValue(lookback + 6), method, time1, end, ext);
plot tl7 = tl(AbsValue(lookback + 7), method, time1, end, ext);
plot tl8 = tl(AbsValue(lookback + 8), method, time1, end, ext);
plot tl9 = tl(AbsValue(lookback + 9), method, time1, end, ext);

DefineGlobalColor("TL", Color.YELLOW);
tl0.EnableApproximation();
tl0.SetLineWeight(weight);
tl0.SetDefaultColor(GlobalColor("TL"));
tl1.EnableApproximation();
tl1.SetLineWeight(weight);
tl1.SetDefaultColor(GlobalColor("TL"));
tl2.EnableApproximation();
tl2.SetLineWeight(weight);
tl2.SetDefaultColor(GlobalColor("TL"));
tl3.EnableApproximation();
tl3.SetLineWeight(weight);
tl3.SetDefaultColor(GlobalColor("TL"));
tl4.EnableApproximation();
tl4.SetLineWeight(weight);
tl4.SetDefaultColor(GlobalColor("TL"));
tl5.EnableApproximation();
tl5.SetLineWeight(weight);
tl5.SetDefaultColor(GlobalColor("TL"));
tl6.EnableApproximation();
tl6.SetLineWeight(weight);
tl6.SetDefaultColor(GlobalColor("TL"));
tl7.EnableApproximation();
tl7.SetLineWeight(weight);
tl7.SetDefaultColor(GlobalColor("TL"));
tl8.EnableApproximation();
tl8.SetLineWeight(weight);
tl8.SetDefaultColor(GlobalColor("TL"));
tl9.EnableApproximation();
tl9.SetLineWeight(weight);
tl9.SetDefaultColor(GlobalColor("TL"));

plot tlext0 = tl(AbsValue(lookback + 0), method, time1, end, ext).lextplot;
plot tlext1 = tl(AbsValue(lookback + 1), method, time1, end, ext).lextplot;
plot tlext2 = tl(AbsValue(lookback + 2), method, time1, end, ext).lextplot;
plot tlext3 = tl(AbsValue(lookback + 3), method, time1, end, ext).lextplot;
plot tlext4 = tl(AbsValue(lookback + 4), method, time1, end, ext).lextplot;
plot tlext5 = tl(AbsValue(lookback + 5), method, time1, end, ext).lextplot;
plot tlext6 = tl(AbsValue(lookback + 6), method, time1, end, ext).lextplot;
plot tlext7 = tl(AbsValue(lookback + 7), method, time1, end, ext).lextplot;
plot tlext8 = tl(AbsValue(lookback + 8), method, time1, end, ext).lextplot;
plot tlext9 = tl(AbsValue(lookback + 9), method, time1, end, ext).lextplot;

tlext0.EnableApproximation();
tlext0.SetLineWeight(weight);
tlext0.SetDefaultColor(GlobalColor("TL"));
tlext1.EnableApproximation();
tlext1.SetLineWeight(weight);
tlext1.SetDefaultColor(GlobalColor("TL"));
tlext2.EnableApproximation();
tlext2.SetLineWeight(weight);
tlext2.SetDefaultColor(GlobalColor("TL"));
tlext3.EnableApproximation();
tlext3.SetLineWeight(weight);
tlext3.SetDefaultColor(GlobalColor("TL"));
tlext4.EnableApproximation();
tlext4.SetLineWeight(weight);
tlext4.SetDefaultColor(GlobalColor("TL"));
tlext5.EnableApproximation();
tlext5.SetLineWeight(weight);
tlext5.SetDefaultColor(GlobalColor("TL"));
tlext6.EnableApproximation();
tlext6.SetLineWeight(weight);
tlext6.SetDefaultColor(GlobalColor("TL"));
tlext7.EnableApproximation();
tlext7.SetLineWeight(weight);
tlext7.SetDefaultColor(GlobalColor("TL"));
tlext8.EnableApproximation();
tlext8.SetLineWeight(weight);
tlext8.SetDefaultColor(GlobalColor("TL"));
tlext9.EnableApproximation();
tlext9.SetLineWeight(weight);
tlext9.SetDefaultColor(GlobalColor("TL"));
 
Last edited:
Here is the script with premarket highs/lows in addition to the time, days, date basis. I have coded the premarket highs to go to the low of RTH opening bar and premarket lows to anchor to the high of that bar.

Here is a chart with a script set to premarket high and anothe copy set to premarket low together
I'm absolutely subscribing again next year. You're amazing!!!
 
You are welcome!

In further testing of the script in post https://usethinkscript.com/threads/draw-a-trendline-based-on-time.9260/post-84508
it is revised to reflect where I corrected the prehigh and prelow portion of these codes to add [1]:
if thisDay == days and thisDay[1] == days + 1
Thanks for the update! Could the same script also be used to plot the two lowest points during pre-market? So instead of the beginning of the line marking pre-market high and the end of the line marking pre-market low... the beginning of the line marks a low and the end of the line marks another low.
 
Thanks for the update! Could the same script also be used to plot the two lowest points during pre-market? So instead of the beginning of the line marking pre-market high and the end of the line marking pre-market low... the beginning of the line marks a low and the end of the line marks another low.

This will give you the option for premarket's to choose the high/low or other price at the beginning of RTHs for the end/to price.

Ruby:
    def bn      = BarNumber();
script tl {
    def ymd      = GetYYYYMMDD();
    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) ;
    def bn      = BarNumber();

    input days  = 0;
    def prehigh = CompoundValue(1, if thisDay == days and thisDay[1] == days + 1
                  then high
                  else if thisDay == days and GetTime() < RegularTradingStart(GetYYYYMMDD())
                  then Max(high, prehigh[1])
                  else prehigh[1], thisDay == days and high);

    def prelow = CompoundValue(1, if thisDay == days and thisDay[1] == days + 1
                 then low
                 else if thisDay == days and GetTime() < RegularTradingStart(GetYYYYMMDD())
                 then Min(low, prelow[1])
                 else prelow[1], low);

    def prehighbn = if thisDay == days and GetTime() < RegularTradingStart(GetYYYYMMDD()) and  Round(high) == Round(HighestAll(prehigh))
                    then bn else prehighbn[1];

    def prelowbn = if thisDay == days and GetTime() < RegularTradingStart(GetYYYYMMDD()) and low == prelow
                   then bn else prelowbn[1];
    input method      = {default Time, PremarketHigh, PremarketLow};
    input pmHighPrice = low;
    input pmLowPrice  = high;
    input time1       = 1045;
    input time2       = 0930;
    input ext         = 20;

    def begin;
    def pricebegin;
    def priceend;
    def beginbn ;
    def end;
    def endbn;
    switch (method) {
    case Time:
        begin      = if  thisDay == days and SecondsFromTime(time1) == 0 then 1 else 0;
        pricebegin = high;
        priceend   = high;
        beginbn    = if  thisDay == days and SecondsFromTime(time1) == 0 then bn else beginbn[1];
        end        = if  thisDay == days and SecondsFromTime(time2) == 0 then 1 else 0;
        endbn      = if  thisDay == days and SecondsFromTime(time2) == 0 then bn else endbn[1];
    case PremarketHigh:
        begin      = if thisDay == days and BarNumber() == HighestAll(prehighbn) then 1 else 0;
        pricebegin = prehigh;
        priceend   = if thisDay == days and GetTime() crosses above RegularTradingStart(GetYYYYMMDD()) then pmHighPrice else priceend[1];
        beginbn    = HighestAll(prehighbn);
        end        = if thisDay == days and GetTime() crosses above RegularTradingStart(GetYYYYMMDD()) then 1 else 0;
        endbn      = if thisDay == days and GetTime() crosses above RegularTradingStart(GetYYYYMMDD()) then bn else endbn[1];
    case PremarketLow:
        begin      = if thisDay == days and BarNumber() == HighestAll(prelowbn)
                     then 1 else 0;
        pricebegin = prelow;
        priceend   = if thisDay == days and GetTime() crosses above RegularTradingStart(GetYYYYMMDD()) then PmLowPrice else priceend[1];
        beginbn    = prelowbn;
        end        = if thisDay == days and GetTime() crosses above RegularTradingStart(GetYYYYMMDD()) then 1 else 0;
        endbn      = if thisDay == days and GetTime() crosses above RegularTradingStart(GetYYYYMMDD()) then bn else endbn[1];
}


    def beginprice  = if thisDay == days and begin then pricebegin else beginprice[1];
    def endprice1   = if thisDay == days and end   then priceend else endprice1[1];
    plot line       = if thisDay == days and begin
                      then beginprice
                      else if thisDay == days and end
                      then endprice1 else Double.NaN;
    line.EnableApproximation();

#Line Extension-----------------------------------------------------------------

    def y1    = if thisDay == days and bn == HighestAll(beginbn) then beginprice else y1[1];
    def y2    = if thisDay == days and bn == HighestAll(endbn) then endprice1   else y2[1];
    def slope = if thisDay == days and endbn > beginbn
            then (( y2 - y1) / (endbn - beginbn))
            else (( y1 - y2) / (beginbn - endbn));
    rec lext  = if thisDay == days and IsNaN(close[-1])
            then lext[1] + (lext[1] - lext[2])
            else if thisDay == days and endbn > beginbn
            then y1 + ((bn - beginbn) * slope)
            else y2 + ((bn - endbn) * slope);
    plot lextplot = if thisDay == days and Between(bn, HighestAll(endbn) , HighestAll(endbn) + ext)
                then lext
                else Double.NaN;
    lextplot.EnableApproximation();
    lextplot.SetPaintingStrategy(PaintingStrategy.LINE);
    lextplot.SetDefaultColor(Color.WHITE);
    lextplot.SetLineWeight(2);
}

def today = HighestAll(if IsNaN(close[-1]) then GetYYYYMMDD() else 0);

input startlookback = {default Days, Date};
input lookbackdays  = 0;
input lookbackdate  = 20211126;
def   lookback      = if startlookback == startlookback.Days
                      then lookbackdays
                      else CountTradingDays(lookbackdate, today) - 1;

input method   = {default Time, PremarketHigh, PremarketLow};
input pmhighPrice = low;
input pmlowprice  = high;
input time1    = 1045;
input end      = 1215;
input ext      = 20;
input weight   = 5;

plot tl0 = tl(AbsValue(lookback + 0), method, pmhighPrice, pmlowPrice, time1, end, ext);
plot tl1 = tl(AbsValue(lookback + 1), method, pmhighPrice, pmlowPrice, time1, end, ext);
plot tl2 = tl(AbsValue(lookback + 2), method, pmhighPrice, pmlowPrice, time1, end, ext);
plot tl3 = tl(AbsValue(lookback + 3), method, pmhighPrice, pmlowPrice, time1, end, ext);
plot tl4 = tl(AbsValue(lookback + 4), method, pmhighPrice, pmlowPrice, time1, end, ext);
plot tl5 = tl(AbsValue(lookback + 5), method, pmhighPrice, pmlowPrice, time1, end, ext);
plot tl6 = tl(AbsValue(lookback + 6), method, pmhighPrice, pmlowPrice, time1, end, ext);
plot tl7 = tl(AbsValue(lookback + 7), method, pmhighPrice, pmlowPrice, time1, end, ext);
plot tl8 = tl(AbsValue(lookback + 8), method, pmhighPrice, pmlowPrice, time1, end, ext);
plot tl9 = tl(AbsValue(lookback + 9), method, pmhighPrice, pmlowPrice, time1, end, ext);

DefineGlobalColor("TL", Color.YELLOW);
tl0.EnableApproximation();
tl0.SetLineWeight(weight);
tl0.SetDefaultColor(GlobalColor("TL"));
tl1.EnableApproximation();
tl1.SetLineWeight(weight);
tl1.SetDefaultColor(GlobalColor("TL"));
tl2.EnableApproximation();
tl2.SetLineWeight(weight);
tl2.SetDefaultColor(GlobalColor("TL"));
tl3.EnableApproximation();
tl3.SetLineWeight(weight);
tl3.SetDefaultColor(GlobalColor("TL"));
tl4.EnableApproximation();
tl4.SetLineWeight(weight);
tl4.SetDefaultColor(GlobalColor("TL"));
tl5.EnableApproximation();
tl5.SetLineWeight(weight);
tl5.SetDefaultColor(GlobalColor("TL"));
tl6.EnableApproximation();
tl6.SetLineWeight(weight);
tl6.SetDefaultColor(GlobalColor("TL"));
tl7.EnableApproximation();
tl7.SetLineWeight(weight);
tl7.SetDefaultColor(GlobalColor("TL"));
tl8.EnableApproximation();
tl8.SetLineWeight(weight);
tl8.SetDefaultColor(GlobalColor("TL"));
tl9.EnableApproximation();
tl9.SetLineWeight(weight);
tl9.SetDefaultColor(GlobalColor("TL"));

plot tlext0 = tl(AbsValue(lookback + 0), method, pmhighPrice, pmlowPrice, time1, end, ext).lextplot;
plot tlext1 = tl(AbsValue(lookback + 1), method, pmhighPrice, pmlowPrice, time1, end, ext).lextplot;
plot tlext2 = tl(AbsValue(lookback + 2), method, pmhighPrice, pmlowPrice, time1, end, ext).lextplot;
plot tlext3 = tl(AbsValue(lookback + 3), method, pmhighPrice, pmlowPrice, time1, end, ext).lextplot;
plot tlext4 = tl(AbsValue(lookback + 4), method, pmhighPrice, pmlowPrice, time1, end, ext).lextplot;
plot tlext5 = tl(AbsValue(lookback + 5), method, pmhighPrice, pmlowPrice, time1, end, ext).lextplot;
plot tlext6 = tl(AbsValue(lookback + 6), method, pmhighPrice, pmlowPrice, time1, end, ext).lextplot;
plot tlext7 = tl(AbsValue(lookback + 7), method, pmhighPrice, pmlowPrice, time1, end, ext).lextplot;
plot tlext8 = tl(AbsValue(lookback + 8), method, pmhighPrice, pmlowPrice, time1, end, ext).lextplot;
plot tlext9 = tl(AbsValue(lookback + 9), method, pmhighPrice, pmlowPrice, time1, end, ext).lextplot;

tlext0.EnableApproximation();
tlext0.SetLineWeight(weight);
tlext0.SetDefaultColor(GlobalColor("TL"));
tlext1.EnableApproximation();
tlext1.SetLineWeight(weight);
tlext1.SetDefaultColor(GlobalColor("TL"));
tlext2.EnableApproximation();
tlext2.SetLineWeight(weight);
tlext2.SetDefaultColor(GlobalColor("TL"));
tlext3.EnableApproximation();
tlext3.SetLineWeight(weight);
tlext3.SetDefaultColor(GlobalColor("TL"));
tlext4.EnableApproximation();
tlext4.SetLineWeight(weight);
tlext4.SetDefaultColor(GlobalColor("TL"));
tlext5.EnableApproximation();
tlext5.SetLineWeight(weight);
tlext5.SetDefaultColor(GlobalColor("TL"));
tlext6.EnableApproximation();
tlext6.SetLineWeight(weight);
tlext6.SetDefaultColor(GlobalColor("TL"));
tlext7.EnableApproximation();
tlext7.SetLineWeight(weight);
tlext7.SetDefaultColor(GlobalColor("TL"));
tlext8.EnableApproximation();
tlext8.SetLineWeight(weight);
tlext8.SetDefaultColor(GlobalColor("TL"));
tlext9.EnableApproximation();
tlext9.SetLineWeight(weight);
tlext9.SetDefaultColor(GlobalColor("TL"));
 
For some reason the the first bar of the day is being counted in thee script. I only want the calculations for pre-market (so until 9:29am). Is this doable? Something like this...


declare hide_on_daily;
input PlotOverNightExtremes = yes;
input DisplayPriceBubbleOnHiLowBar = yes;
input DisplayPriceBubbleOnRightEdge = yes; #Haven't figured this out yet..JQ

def o = open;
def h = high;
def l = low;
def c = close;
def v = volume;
def bar = BarNumber();
def GlobeX = GetTime() < RegularTradingStart(GetYYYYMMDD());

def vol = if GlobeX and !GlobeX[1]
then v
else if GlobeX
then vol[1] + v
else Double.NaN;

def GlobeX_Volume = vol;

def ONhigh = if GlobeX and !GlobeX[1]
then h
else if GlobeX and
h > ONhigh[1]
then h
else ONhigh[1];

def ONhighBar = if GlobeX and h == ONhigh
then bar
else Double.NaN;

def ONlow = if GlobeX and !GlobeX[1]
then l
else if GlobeX and
l < ONlow[1]
then l
else ONlow[1];

def ONlowBar = if GlobeX and l == ONlow
then bar
else Double.NaN;

def OverNightHigh = if BarNumber() == HighestAll(ONhighBar)
then ONhigh
else OverNightHigh[1];

def OverNightLow = if BarNumber() == HighestAll(ONlowBar)
then ONlow
else OverNightLow[1];


#
plot ONH = if OverNightHigh > 0
then OverNightHigh
else Double.NaN;

ONH.SetHiding(!PlotOverNightExtremes);
#ONH.SetPaintingStrategy(PaintingStrategy.SQUARES);
#ONH.SetDefaultColor(Color.BLUE);
#ONH.HideBubble();
#ONH.HideTitle();

#
plot ONL = if OverNightLow > 0
then OverNightLow
else Double.NaN;

ONL.SetHiding(!PlotOverNightExtremes);
 
For some reason the the first bar of the day is being counted in thee script. I only want the calculations for pre-market (so until 9:29am). Is this doable? Something like this...


declare hide_on_daily;
input PlotOverNightExtremes = yes;
input DisplayPriceBubbleOnHiLowBar = yes;
input DisplayPriceBubbleOnRightEdge = yes; #Haven't figured this out yet..JQ

def o = open;
def h = high;
def l = low;
def c = close;
def v = volume;
def bar = BarNumber();
def GlobeX = GetTime() < RegularTradingStart(GetYYYYMMDD());

def vol = if GlobeX and !GlobeX[1]
then v
else if GlobeX
then vol[1] + v
else Double.NaN;

def GlobeX_Volume = vol;

def ONhigh = if GlobeX and !GlobeX[1]
then h
else if GlobeX and
h > ONhigh[1]
then h
else ONhigh[1];

def ONhighBar = if GlobeX and h == ONhigh
then bar
else Double.NaN;

def ONlow = if GlobeX and !GlobeX[1]
then l
else if GlobeX and
l < ONlow[1]
then l
else ONlow[1];

def ONlowBar = if GlobeX and l == ONlow
then bar
else Double.NaN;

def OverNightHigh = if BarNumber() == HighestAll(ONhighBar)
then ONhigh
else OverNightHigh[1];

def OverNightLow = if BarNumber() == HighestAll(ONlowBar)
then ONlow
else OverNightLow[1];


#
plot ONH = if OverNightHigh > 0
then OverNightHigh
else Double.NaN;

ONH.SetHiding(!PlotOverNightExtremes);
#ONH.SetPaintingStrategy(PaintingStrategy.SQUARES);
#ONH.SetDefaultColor(Color.BLUE);
#ONH.HideBubble();
#ONH.HideTitle();

#
plot ONL = if OverNightLow > 0
then OverNightLow
else Double.NaN;

ONL.SetHiding(!PlotOverNightExtremes);

If you are referring to the script above, I coded it to anchor the premarket high/lows to the first bar of RTH's high/low. The following code should use the last bar of premarket's period for the anchor. You can still choose what price on that bar to use as the anchor price.

Capture.jpg
Ruby:
    def bn      = BarNumber();
script tl {
    def ymd      = GetYYYYMMDD();
    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) ;
    def bn      = BarNumber();

    input days  = 0;
    def prehigh = CompoundValue(1, if thisDay == days and thisDay[1] == days + 1
                  then high
                  else if thisDay == days and GetTime() < RegularTradingStart(GetYYYYMMDD())
                  then Max(high, prehigh[1])
                  else prehigh[1], thisDay == days and high);

    def prelow = CompoundValue(1, if thisDay == days and thisDay[1] == days + 1
                 then low
                 else if thisDay == days and GetTime() < RegularTradingStart(GetYYYYMMDD())
                 then Min(low, prelow[1])
                 else prelow[1], low);

    def prehighbn = if thisDay == days and GetTime() < RegularTradingStart(GetYYYYMMDD()) and  Round(high) == Round(HighestAll(prehigh))
                    then bn else prehighbn[1];

    def prelowbn = if thisDay == days and GetTime() < RegularTradingStart(GetYYYYMMDD()) and low == prelow
                   then bn else prelowbn[1];
    input method      = {default Time, PremarketHigh, PremarketLow};
    input pmHighPrice = low;
    input pmLowPrice  = high;
    input time1       = 1045;
    input time2       = 0930;
    input ext         = 20;

    def begin;
    def pricebegin;
    def priceend;
    def beginbn ;
    def end;
    def endbn;
    switch (method) {
    case Time:
        begin      = if  thisDay == days and SecondsFromTime(time1) == 0 then 1 else 0;
        pricebegin = high;
        priceend   = high;
        beginbn    = if  thisDay == days and SecondsFromTime(time1) == 0 then bn else beginbn[1];
        end        = if  thisDay == days and SecondsFromTime(time2) == 0 then 1 else 0;
        endbn      = if  thisDay == days and SecondsFromTime(time2) == 0 then bn else endbn[1];
    case PremarketHigh:
        begin      = if thisDay == days and BarNumber() == HighestAll(prehighbn) then 1 else 0;
        pricebegin = prehigh;
        priceend   = if thisDay == days and GetTime()[-1] crosses above RegularTradingStart(GetYYYYMMDD()) then pmHighPrice else priceend[1];
        beginbn    = HighestAll(prehighbn);
        end        = if thisDay == days and GetTime()[-1] crosses above RegularTradingStart(GetYYYYMMDD()) then 1 else 0;
        endbn      = if thisDay == days and GetTime()[-1] crosses above RegularTradingStart(GetYYYYMMDD()) then bn else endbn[1];
    case PremarketLow:
        begin      = if thisDay == days and BarNumber() == HighestAll(prelowbn)
                     then 1 else 0;
        pricebegin = prelow;
        priceend   = if thisDay == days and GetTime()[-1] crosses above RegularTradingStart(GetYYYYMMDD()) then PmLowPrice else priceend[1];
        beginbn    = prelowbn;
        end        = if thisDay == days and GetTime()[-1] crosses above RegularTradingStart(GetYYYYMMDD()) then 1 else 0;
        endbn      = if thisDay == days and GetTime()[-1] crosses above RegularTradingStart(GetYYYYMMDD()) then bn else endbn[1];
}


    def beginprice  = if thisDay == days and begin then pricebegin else beginprice[1];
    def endprice1   = if thisDay == days and end   then priceend else endprice1[1];
    plot line       = if thisDay == days and begin
                      then beginprice
                      else if thisDay == days and end
                      then endprice1 else Double.NaN;
    line.EnableApproximation();

#Line Extension-----------------------------------------------------------------

    def y1    = if thisDay == days and bn == HighestAll(beginbn) then beginprice else y1[1];
    def y2    = if thisDay == days and bn == HighestAll(endbn) then endprice1   else y2[1];
    def slope = if thisDay == days and endbn > beginbn
            then (( y2 - y1) / (endbn - beginbn))
            else (( y1 - y2) / (beginbn - endbn));
    rec lext  = if thisDay == days and IsNaN(close[-1])
            then lext[1] + (lext[1] - lext[2])
            else if thisDay == days and endbn > beginbn
            then y1 + ((bn - beginbn) * slope)
            else y2 + ((bn - endbn) * slope);
    plot lextplot = if thisDay == days and Between(bn, HighestAll(endbn) , HighestAll(endbn) + ext)
                then lext
                else Double.NaN;
    lextplot.EnableApproximation();
    lextplot.SetPaintingStrategy(PaintingStrategy.LINE);
    lextplot.SetDefaultColor(Color.WHITE);
    lextplot.SetLineWeight(2);
}

def today = HighestAll(if IsNaN(close[-1]) then GetYYYYMMDD() else 0);

input startlookback = {default Days, Date};
input lookbackdays  = 0;
input lookbackdate  = 20211126;
def   lookback      = if startlookback == startlookback.Days
                      then lookbackdays
                      else CountTradingDays(lookbackdate, today) - 1;

input method   = {default Time, PremarketHigh, PremarketLow};
input pmhighPrice = low;
input pmlowprice  = high;
input time1    = 1045;
input end      = 1215;
input ext      = 20;
input weight   = 5;

plot tl0 = tl(AbsValue(lookback + 0), method, pmhighPrice, pmlowPrice, time1, end, ext);
plot tl1 = tl(AbsValue(lookback + 1), method, pmhighPrice, pmlowPrice, time1, end, ext);
plot tl2 = tl(AbsValue(lookback + 2), method, pmhighPrice, pmlowPrice, time1, end, ext);
plot tl3 = tl(AbsValue(lookback + 3), method, pmhighPrice, pmlowPrice, time1, end, ext);
plot tl4 = tl(AbsValue(lookback + 4), method, pmhighPrice, pmlowPrice, time1, end, ext);
plot tl5 = tl(AbsValue(lookback + 5), method, pmhighPrice, pmlowPrice, time1, end, ext);
plot tl6 = tl(AbsValue(lookback + 6), method, pmhighPrice, pmlowPrice, time1, end, ext);
plot tl7 = tl(AbsValue(lookback + 7), method, pmhighPrice, pmlowPrice, time1, end, ext);
plot tl8 = tl(AbsValue(lookback + 8), method, pmhighPrice, pmlowPrice, time1, end, ext);
plot tl9 = tl(AbsValue(lookback + 9), method, pmhighPrice, pmlowPrice, time1, end, ext);

DefineGlobalColor("TL", Color.YELLOW);
tl0.EnableApproximation();
tl0.SetLineWeight(weight);
tl0.SetDefaultColor(GlobalColor("TL"));
tl1.EnableApproximation();
tl1.SetLineWeight(weight);
tl1.SetDefaultColor(GlobalColor("TL"));
tl2.EnableApproximation();
tl2.SetLineWeight(weight);
tl2.SetDefaultColor(GlobalColor("TL"));
tl3.EnableApproximation();
tl3.SetLineWeight(weight);
tl3.SetDefaultColor(GlobalColor("TL"));
tl4.EnableApproximation();
tl4.SetLineWeight(weight);
tl4.SetDefaultColor(GlobalColor("TL"));
tl5.EnableApproximation();
tl5.SetLineWeight(weight);
tl5.SetDefaultColor(GlobalColor("TL"));
tl6.EnableApproximation();
tl6.SetLineWeight(weight);
tl6.SetDefaultColor(GlobalColor("TL"));
tl7.EnableApproximation();
tl7.SetLineWeight(weight);
tl7.SetDefaultColor(GlobalColor("TL"));
tl8.EnableApproximation();
tl8.SetLineWeight(weight);
tl8.SetDefaultColor(GlobalColor("TL"));
tl9.EnableApproximation();
tl9.SetLineWeight(weight);
tl9.SetDefaultColor(GlobalColor("TL"));

plot tlext0 = tl(AbsValue(lookback + 0), method, pmhighPrice, pmlowPrice, time1, end, ext).lextplot;
plot tlext1 = tl(AbsValue(lookback + 1), method, pmhighPrice, pmlowPrice, time1, end, ext).lextplot;
plot tlext2 = tl(AbsValue(lookback + 2), method, pmhighPrice, pmlowPrice, time1, end, ext).lextplot;
plot tlext3 = tl(AbsValue(lookback + 3), method, pmhighPrice, pmlowPrice, time1, end, ext).lextplot;
plot tlext4 = tl(AbsValue(lookback + 4), method, pmhighPrice, pmlowPrice, time1, end, ext).lextplot;
plot tlext5 = tl(AbsValue(lookback + 5), method, pmhighPrice, pmlowPrice, time1, end, ext).lextplot;
plot tlext6 = tl(AbsValue(lookback + 6), method, pmhighPrice, pmlowPrice, time1, end, ext).lextplot;
plot tlext7 = tl(AbsValue(lookback + 7), method, pmhighPrice, pmlowPrice, time1, end, ext).lextplot;
plot tlext8 = tl(AbsValue(lookback + 8), method, pmhighPrice, pmlowPrice, time1, end, ext).lextplot;
plot tlext9 = tl(AbsValue(lookback + 9), method, pmhighPrice, pmlowPrice, time1, end, ext).lextplot;

tlext0.EnableApproximation();
tlext0.SetLineWeight(weight);
tlext0.SetDefaultColor(GlobalColor("TL"));
tlext1.EnableApproximation();
tlext1.SetLineWeight(weight);
tlext1.SetDefaultColor(GlobalColor("TL"));
tlext2.EnableApproximation();
tlext2.SetLineWeight(weight);
tlext2.SetDefaultColor(GlobalColor("TL"));
tlext3.EnableApproximation();
tlext3.SetLineWeight(weight);
tlext3.SetDefaultColor(GlobalColor("TL"));
tlext4.EnableApproximation();
tlext4.SetLineWeight(weight);
tlext4.SetDefaultColor(GlobalColor("TL"));
tlext5.EnableApproximation();
tlext5.SetLineWeight(weight);
tlext5.SetDefaultColor(GlobalColor("TL"));
tlext6.EnableApproximation();
tlext6.SetLineWeight(weight);
tlext6.SetDefaultColor(GlobalColor("TL"));
tlext7.EnableApproximation();
tlext7.SetLineWeight(weight);
tlext7.SetDefaultColor(GlobalColor("TL"));
tlext8.EnableApproximation();
tlext8.SetLineWeight(weight);
tlext8.SetDefaultColor(GlobalColor("TL"));
tlext9.EnableApproximation();
tlext9.SetLineWeight(weight);
tlext9.SetDefaultColor(GlobalColor("TL"));
 
Last edited:
If you are referring to the script above, I coded it to anchor the premarket high/lows to the first bar of RTH's high/low. The following code should use the last bar of premarket's period for the anchor. You can still choose what price on that bar to use as the anchor price.
Hi there! This script as been working so well... thanks again! So well that I'm thinking about using a variation. Is it possible to have it be plotted on a weekly, daily, 4hr, etc. chart? I hope this isn't too difficult. Thanks again!
 
Would love help with this code. I want to plot the highest accumulation and lowest distribution point. Currently, it only plots the high in some time frames. I've included a screenshot. Thanks in advance.

0TOoW5B.png



Code:
def data = if close > close[1] then close - Min(close[1], low) else if close < close[1] then close - Max(close[1], high) else 0;

plot AccDist = TotalSum(data);

declare hide_on_daily;
input PlotOverNightExtremes = yes;
input DisplayPriceBubbleOnHiLowBar = yes;
input DisplayPriceBubbleOnRightEdge = yes;  #Haven't figured this out yet..JQ

def o = AccDist;
def h = AccDist;
def l = AccDist;
def c = AccDist;
def v = volume;
def bar = BarNumber();
def GlobeX = GetTime() < RegularTradingStart(GetYYYYMMDD());

def vol = if GlobeX and !GlobeX[1]
then v
else if GlobeX
then vol[1] + v
else Double.NaN;

def GlobeX_Volume = vol;

def ONhigh = if GlobeX and !GlobeX[1]
then h
else if GlobeX and
h > ONhigh[1]
then h
else ONhigh[1];

def ONhighBar = if GlobeX and h == ONhigh
then bar
else Double.NaN;

def ONlow = if GlobeX and !GlobeX[1]
then l
else if GlobeX and
l < ONlow[1]
then l
else ONlow[1];

def ONlowBar = if GlobeX and l == ONlow
then bar
else Double.NaN;

def OverNightHigh = if BarNumber() == HighestAll(ONhighBar)
then ONhigh
else OverNightHigh[1];

def OverNightLow = if BarNumber() == HighestAll(ONlowBar)
then ONlow
else OverNightLow[1];


#
plot ONH = if OverNightHigh > 0
then OverNightHigh
else Double.NaN;

ONH.SetHiding(!PlotOverNightExtremes);
#ONH.SetPaintingStrategy(PaintingStrategy.SQUARES);
#ONH.SetDefaultColor(Color.BLUE);
#ONH.HideBubble();
#ONH.HideTitle();

#
plot ONL = if OverNightLow > 0
then OverNightLow
else Double.NaN;

ONL.SetHiding(!PlotOverNightExtremes);
#ONL.SetPaintingStrategy(PaintingStrategy.SQUARES);
#ONL.SetDefaultColor(Color.LIGHT_GRAY);
#ONL.HideBubble();
#ONL.HideTitle();
#AddChartBubble(OverNightHigh == high(period = "DAY"), OverNightHigh, "ONH: " + OverNightHigh, Color.green, yes);
#AddChartBubble(OverNightLow == low(period = "DAY"), OverNightLow, "ONL: " + OverNightLow, Color.green, no);


#Bubble code
#AddChartBubble(bar == ONhighBar and PlotOverNightExtremes and DisplayPriceBubbleOnHiLowBar, ONH, "PMH: " + ONH, CreateColor(100, 200, 100));
#AddChartBubble(bar == ONlowBar and PlotOverNightExtremes and DisplayPriceBubbleOnHiLowBar, ONL, "PML: " + ONL, CreateColor(100, 200, 100), no);  #designated that the bubble be painted below the ONL line.  2018-04-07 JQ


#AddChartBubble(BarNumber() == HighestAll(BarNumber()) and  PlotOverNightExtremes, ONH, "PMH: " + ONH, CreateColor(204, 204, 255));
#AddChartBubble(BarNumber() == HighestAll(BarNumber()) and #PlotOverNightExtremes, ONL, "PML: " + ONL, CreateColor(204, 204, 255), no);
# End Code GlobeX or Overnight High / Low without Fibonacci Values

####################################################
#input addcloud = yes;
#AddCloud(ONH, ONL, color.gray, color.gray);
####################################################
 
Last edited by a moderator:
@SJP07 Try this:

Code:
#
# TD Ameritrade IP Company, Inc. (c) 2009-2022
#

declare lower;

plot AccDist = TotalSum(volume * CloseLocationValue());
plot ZeroLine = 0;

AccDist.SetDefaultColor(GetColor(1));
ZeroLine.SetDefaultColor(GetColor(5));

plot highestAD = Highest(AccDist);
plot lowestAD = Lowest(AccDist);
 
@SJP07 Try this:

Code:
#
# TD Ameritrade IP Company, Inc. (c) 2009-2022
#

declare lower;

plot AccDist = TotalSum(volume * CloseLocationValue());
plot ZeroLine = 0;

AccDist.SetDefaultColor(GetColor(1));
ZeroLine.SetDefaultColor(GetColor(5));

plot highestAD = Highest(AccDist);
plot lowestAD = Lowest(AccDist);
Thanks BenTen! I only want it to show the high and low for premarket hours, with the option of seeking previous days.
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
537 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