# Highest Volume Bar In Premarket For Use As S&R

#### Stoynks

##### Member
I am trying to find the highest volume in the premarket and then plotting a horizontal line.

Code:
``````# Plots High/Low Price at Highest Volume
# tomsk
# 12.4.2019
input barsago = 4;
#
def time = if GetLastDay() == GetDay() and SecondsFromTime(0430) >= 0 and SecondsTillTime(0929) >= 0 then 1 else 0;
def bar = BarNumber();
def HighVolBar = if time and ((volume - Lowest(volume, barsago)) /
(Highest(volume, barsago) - Lowest(volume, barsago))) >= 1
then bar
else Double.NaN;
def VolHighLevel = if!IsNaN(HighVolBar) then high else VolHighLevel[1];
def VolLowLevel = if !IsNaN(HighVolBar) then low else VolLowLevel[1];

plot HighLine = if bar >= HighestAll(HighVolBar) then VolHighLevel else Double.NaN;
plot LowLine = if bar >= HighestAll(HighVolBar) then VolLowLevel else Double.NaN;

HighLine.SetDefaultColor(Color.CYAN);
HighLine.SetLineWeight(3);
LowLine.SetDefaultColor(Color.YELLOW);
LowLine.SetLineWeight(3);
# End Plots High/Low Price at Highest Volume``````

I change "def time = if GetLastDay() == GetDay() and SecondsFromTime(0430) >= 0 and SecondsTillTime(0929) >= 0 then 1 else 0;" that way it only reads from premarket. but one problem is, when I change "input barsago", it's a a little wonky because on some charts the horizontal line is there sometimes it isn't at 1 min aggregation period.

Then I started digging even more so instead of using "input barsago" to find the highest volume bar. I use this code.

Code:
``````declare on_volume;
def NA = Double.NaN;
input length = 5;
def aggcheck = GetAggregationPeriod() == AggregationPeriod.DAY;
def v = volume;
def vt = v == Highest(v,length);
plot Vtest = if aggcheck and vt then v else NA;

vtest.SetDefaultColor(CreateColor(0,100,200));
vtest.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);``````

Code:
``````declare on_volume;
def NA = Double.NaN;
def aggcheck = GetLastDay() == GetDay()and SecondsFromTime(0931) >= 0 and SecondsTillTime(1600) >= 0;
def v = volume;
def vt = v == Highestall(v);
plot Vtest = if aggcheck then vt and v else NA;

vtest.SetDefaultColor(CreateColor(0,100,200));
vtest.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);``````

Good news. From trading hours, it was able to calculate the highest volume from that day using "Highestall(v), and then plotting only the highest volume. But when I change it to premarket, it disappeared. I am trying to combine the 2nd code to find the highest volume in premarket and then add in the 1st first code to plot the horizontal line.

Thank you for your talents and I hope you guys' have a wonderful weekend.

I am trying to find the highest volume in the premarket and then plotting a horizontal line.

Code:
``````# Plots High/Low Price at Highest Volume
# tomsk
# 12.4.2019
input barsago = 4;
#
def time = if GetLastDay() == GetDay() and SecondsFromTime(0430) >= 0 and SecondsTillTime(0929) >= 0 then 1 else 0;
def bar = BarNumber();
def HighVolBar = if time and ((volume - Lowest(volume, barsago)) /
(Highest(volume, barsago) - Lowest(volume, barsago))) >= 1
then bar
else Double.NaN;
def VolHighLevel = if!IsNaN(HighVolBar) then high else VolHighLevel[1];
def VolLowLevel = if !IsNaN(HighVolBar) then low else VolLowLevel[1];

plot HighLine = if bar >= HighestAll(HighVolBar) then VolHighLevel else Double.NaN;
plot LowLine = if bar >= HighestAll(HighVolBar) then VolLowLevel else Double.NaN;

HighLine.SetDefaultColor(Color.CYAN);
HighLine.SetLineWeight(3);
LowLine.SetDefaultColor(Color.YELLOW);
LowLine.SetLineWeight(3);
# End Plots High/Low Price at Highest Volume``````

I change "def time = if GetLastDay() == GetDay() and SecondsFromTime(0430) >= 0 and SecondsTillTime(0929) >= 0 then 1 else 0;" that way it only reads from premarket. but one problem is, when I change "input barsago", it's a a little wonky because on some charts the horizontal line is there sometimes it isn't at 1 min aggregation period.

Then I started digging even more so instead of using "input barsago" to find the highest volume bar. I use this code.

Code:
``````declare on_volume;
def NA = Double.NaN;
input length = 5;
def aggcheck = GetAggregationPeriod() == AggregationPeriod.DAY;
def v = volume;
def vt = v == Highest(v,length);
plot Vtest = if aggcheck and vt then v else NA;

vtest.SetDefaultColor(CreateColor(0,100,200));
vtest.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);``````

Code:
``````declare on_volume;
def NA = Double.NaN;
def aggcheck = GetLastDay() == GetDay()and SecondsFromTime(0931) >= 0 and SecondsTillTime(1600) >= 0;
def v = volume;
def vt = v == Highestall(v);
plot Vtest = if aggcheck then vt and v else NA;

vtest.SetDefaultColor(CreateColor(0,100,200));
vtest.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);``````

Good news. From trading hours, it was able to calculate the highest volume from that day using "Highestall(v), and then plotting only the highest volume. But when I change it to premarket, it disappeared. I am trying to combine the 2nd code to find the highest volume in premarket and then add in the 1st first code to plot the horizontal line.

Thank you for your talents and I hope you guys' have a wonderful weekend.

If I understand what you want correctly, then this hopefully plots a colored histogram when on intraday charts, when the current day's volume bars are higher than the prior 'x' number of days highest volume bar.

Ruby:
``````declare on_volume;
input days = 5;
def NA = Double.NaN;
def ymd = GetYYYYMMDD();
def candles  = if IsNaN(close) then candles[1] else 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) ;

#--------------#
#Highest Volume Last x days

def  v  = if thisDay == days
then volume
else if Between(thisDay, 1, days)
then Max(volume, v[1]) else v[1];
plot xv = if volume == HighestAll(v)
then volume else NA;
xv.SetDefaultColor(Color.MAGENTA);
xv.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);

#--------------#
#Current Days Volume Greater Than Highest Volume Last x days

def vt     = if  volume > v then volume else NA;
plot Vtest = if thisDay == 0 then vt else NA;
Vtest.SetDefaultColor(Color.BLACK);
Vtest.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);

Code:
``````# Plots High/Low Price at Highest Volume
# tomsk
# 12.4.2019
input barsago = 240;
#
def time = if GetLastDay() == GetDay() and SecondsFromTime(0430) >= 0 and SecondsTillTime(0929) >= 0 then 1 else 0;
def bar = BarNumber();
def HighVolBar = if time and ((volume - Lowest(volume, barsago)) /
(Highest(volume, barsago) - Lowest(volume, barsago))) >= 1
then bar
else Double.NaN;
def VolHighLevel = if!IsNaN(HighVolBar) then high else VolHighLevel[1];
def VolLowLevel = if !IsNaN(HighVolBar) then low else VolLowLevel[1];

plot HighLine = if bar >= HighestAll(HighVolBar) then VolHighLevel else Double.NaN;
plot LowLine = if bar >= HighestAll(HighVolBar) then VolLowLevel else Double.NaN;

HighLine.SetDefaultColor(Color.CYAN);
HighLine.SetLineWeight(3);
LowLine.SetDefaultColor(Color.YELLOW);
LowLine.SetLineWeight(3);
# End Plots High/Low Price at Highest Volume``````

Don't really know how to explain it. But this code is similar to what I want:

1. "SecondsFromTime(0430) >= 0 and SecondsTillTime(0929) >= 0" I want to find the time for premarket.
2. "def v = highestall(volume);" Then find the highest volume in premarket hours.
3. "plot HighLine = if bar >= HighestAll(HighVolBar) then VolHighLevel else Double.NaN;" then plot a horizontal line on the top(high) of the highest volume bar in premarket hours.

Last edited:
Code:
``````# Plots High/Low Price at Highest Volume
# tomsk
# 12.4.2019
input barsago = 240;
#
def time = if GetLastDay() == GetDay() and SecondsFromTime(0430) >= 0 and SecondsTillTime(0929) >= 0 then 1 else 0;
def bar = BarNumber();
def HighVolBar = if time and ((volume - Lowest(volume, barsago)) /
(Highest(volume, barsago) - Lowest(volume, barsago))) >= 1
then bar
else Double.NaN;
def VolHighLevel = if!IsNaN(HighVolBar) then high else VolHighLevel[1];
def VolLowLevel = if !IsNaN(HighVolBar) then low else VolLowLevel[1];

plot HighLine = if bar >= HighestAll(HighVolBar) then VolHighLevel else Double.NaN;
plot LowLine = if bar >= HighestAll(HighVolBar) then VolLowLevel else Double.NaN;

HighLine.SetDefaultColor(Color.CYAN);
HighLine.SetLineWeight(3);
LowLine.SetDefaultColor(Color.YELLOW);
LowLine.SetLineWeight(3);
# End Plots High/Low Price at Highest Volume``````

Don't really know how to explain it. But this code is similar to what I want:

1. "SecondsFromTime(0430) >= 0 and SecondsTillTime(0929) >= 0" I want to find the time for premarket.
2. "def v = highestall(volume);" Then find the highest volume in premarket hours.
3. "plot HighLine = if bar >= HighestAll(HighVolBar) then VolHighLevel else Double.NaN;" then plot a horizontal line on the top(high) of the highest volume bar in premarket hours.
Sorry, but perhaps someone else can understand what you want and can help you.

All of the premarket volume bars are in the above example and my observations are almost always lower than almost every bar during regular trading hours, making what happens in premarket insignificant. Also, how many premarket days are you looking to test? and then compare to the current days regular trading hours or something else?

I want to find the premarket highest volume for the current day and use it as a support or resistance level to determine a price level. It’s basically tomsk’s code but his code is during market hours but I want to plot the highest volume in premarket hours. So it’s his code which is during market hours but just change it during premarket hours.

@SleepyZ

With BenTen's code, instead of plotting high, how would I plot highestall(volume)?

Last edited:
I really don't understand what you're really asking either. Consider these a test just trying to figure that out, do not consider this to be the actual solution. Tell me which one is more similar to what you're trying to do. Also, please differentiate between pre-market, post-market, or extended hours on the whole. I am not sure what you're asking on that front either.

Try this on an upper chart study:

Ruby:
``````def Line = highestall(
if     getday() == getlastday()
and    secondstillTime(0930) > 0
then   volume
else   double.nan
);
def Iterate =
if     getday() == getlastday()
and    secondstillTime(0930) > 0
and    volume == line
then   high
else   Iterate[1]
;

plot Result = Iterate;
Result.setpaintingStrategy(paintingStrategy.hORIZONTAL);``````

Try this on a volume panel study:

Ruby:
``````plot Line = highestall(
if     getday() == getlastday()
and    secondstillTime(0930) > 0
then   volume
else   double.nan
);``````

I think what he wants is simply to plot a horizontal line starting 09:30AM based on the highest volume bar traded between 04:00-09:29AM using the high of that candle for the plot.

Am i right? @Stoynks

I really don't understand what you're really asking either. Consider these a test just trying to figure that out, do not consider this to be the actual solution. Tell me which one is more similar to what you're trying to do. Also, please differentiate between pre-market, post-market, or extended hours on the whole. I am not sure what you're asking on that front either.

Try this on an upper chart study:

Ruby:
``````def Line = highestall(
if     getday() == getlastday()
and    secondstillTime(0930) > 0
then   volume
else   double.nan
);
def Iterate =
if     getday() == getlastday()
and    secondstillTime(0930) > 0
and    volume == line
then   high
else   Iterate[1]
;

plot Result = Iterate;
Result.setpaintingStrategy(paintingStrategy.hORIZONTAL);``````

Try this on a volume panel study:

Ruby:
``````plot Line = highestall(
if     getday() == getlastday()
and    secondstillTime(0930) > 0
then   volume
else   double.nan
);``````
This is exactly what I wanted, you plotted the highest volume bar during premarket with a horizontal line on top of the bar. How would I reoccur this every day not just for today? Just like how VWAP reoccurs every day. I wish to understand the code behind this but it is so difficult to comprehend. Thank you.

I think what he wants is simply to plot a horizontal line starting 09:30AM based on the highest volume bar traded between 04:00-09:29AM using the high of that candle for the plot.

Am i right? @Stoynks
That's exactly right, but the horizontal line could start at 4:00am as well. The main thing I just wanted to find was the highest volume bar only during premarket.

I think what he wants is simply to plot a horizontal line starting 09:30AM based on the highest volume bar traded between 04:00-09:29AM using the high of that candle for the plot.

Am i right? @Stoynks
Ruby:
``````f Line = highestall(
if     getday() == getlastday()
and    secondstillTime(0930) > 0
then   volume
else   double.nan
);
def iHigh; def iLow;
if (getday() == getlastday() and secondstillTime(0930) > 0 and volume == line){
iHigh = high;
iLow = low;
} else {
iHigh = ihigh[1];
iLow = ilow[1];
}
plot vHigh = if secondstilltime(1600) > 0 and secondsfromtime(0930) >= 0 then iHigh else double.nan;
vHigh.setpaintingStrategy(paintingStrategy.hORIZONTAL);
plot vLow = if secondstilltime(1600) > 0 and secondsfromtime(0930) >= 0 then iLow else double.nan;
vLow.setpaintingStrategy(paintingStrategy.hORIZONTAL);``````

I really don't understand what you're really asking either. Consider these a test just trying to figure that out, do not consider this to be the actual solution. Tell me which one is more similar to what you're trying to do. Also, please differentiate between pre-market, post-market, or extended hours on the whole. I am not sure what you're asking on that front either.

Try this on an upper chart study:

Ruby:
``````def Line = highestall(
if     getday() == getlastday()
and    secondstillTime(0930) > 0
then   volume
else   double.nan
);
def Iterate =
if     getday() == getlastday()
and    secondstillTime(0930) > 0
and    volume == line
then   high
else   Iterate[1]
;

plot Result = Iterate;
Result.setpaintingStrategy(paintingStrategy.hORIZONTAL);``````

Try this on a volume panel study:

Ruby:
``````plot Line = highestall(
if     getday() == getlastday()
and    secondstillTime(0930) > 0
then   volume
else   double.nan
);``````

With me trying to comprehend this.
1. "highestall" tells me to find the highest volume bar.
2. "getday() == getlastday()" tells me to find only for the current day.
3. "secondstillTime(0930) > 0" tells me that it finds the highest volume bar during premarket only.
4. "volume" tells TOS to find volume instead of high/low/close/open.
5. "volume == line" tells me that the line will be on the highest volume bar.
6. "high" tells me that that the horizontal line will be on top of the highest volume bar.
Hopefully, I am understanding correctly. Sorry for the lack of my coding skills.

I really don't understand what you're really asking either. Consider these a test just trying to figure that out, do not consider this to be the actual solution. Tell me which one is more similar to what you're trying to do. Also, please differentiate between pre-market, post-market, or extended hours on the whole. I am not sure what you're asking on that front either.

Try this on an upper chart study:

Ruby:
``````def Line = highestall(
if     getday() == getlastday()
and    secondstillTime(0930) > 0
then   volume
else   double.nan
);
def Iterate =
if     getday() == getlastday()
and    secondstillTime(0930) > 0
and    volume == line
then   high
else   Iterate[1]
;

plot Result = Iterate;
Result.setpaintingStrategy(paintingStrategy.hORIZONTAL);``````

Try this on a volume panel study:

Ruby:
``````plot Line = highestall(
if     getday() == getlastday()
and    secondstillTime(0930) > 0
then   volume
else   double.nan
);``````

Yup. This code helped me plot a horizontal line on the highest volume bar in premarket which helps me determine support/resistance level at markethours. Thank you.

With me trying to comprehend this.
1. "highestall" tells me to find the highest volume bar.
2. "getday() == getlastday()" tells me to find only for the current day.
3. "secondstillTime(0930) > 0" tells me that it finds the highest volume bar during premarket only.
4. "volume" tells TOS to find volume instead of high/low/close/open.
5. "volume == line" tells me that the line will be on the highest volume bar.
6. "high" tells me that that the horizontal line will be on top of the highest volume bar.
Hopefully, I am understanding correctly. Sorry for the lack of my coding skills.
If you're actually interested in learning the code, I would be more than happy to explain it in more detail, but I'm out the door fairly soon. Perhaps later. The code itself actually needs some work, but now that I know what you actually want, I'll clean it up and try to explain what's going on.

If you're actually interested in learning the code, I would be more than happy to explain it in more detail, but I'm out the door fairly soon. Perhaps later. The code itself actually needs some work, but now that I know what you actually want, I'll clean it up and try to explain what's going on.
Thank you so much. Yes, I want it in more detail that way I am able to understand what goes through the code and how it works. I like trying to make sense of things and trying to solve the pieces. That way, later on, I can play with the code and customize it.

Edit:
Seems like I was messing around your code and change it:
Code:
``````def Iterate =
if     getday() == getlastday()
and    secondstillTime(0930) > 0
and    volume == line
then   high
else if  Iterate[1] then Iterate[1] else double.nan``````

This enabled me to clean up the unnecessary lines before the horizontal line that is being plotted on the highest volume bar.

Last edited:
Ruby:
``````def HighestVol; def BarHigh; def BarLow;
def isPreMarket = GetTime() < RegularTradingStart(GetYYYYMMDD());
def isPostMarket = GetTime() > RegularTradingEnd(GetYYYYMMDD());
def isOpen = !isPreMarket and !isPostMarket;
if (isPreMarket and volume > HighestVol[1]) {
HighestVol = volume;
BarHigh = high;
BarLow = low;
} else if (isPostMarket) {
HighestVol = 0;
BarHigh = 0;
BarLow = 0;
} else {
HighestVol = HighestVol[1];
BarHigh = BarHigh[1];
BarLow = BarLow[1];
}
plot PreHigh = if IsOpen then BarHigh else Double.NaN;
PreHigh.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PreHigh.SetDefaultColor(Color.CYAN);
plot PreLow = if IsOpen then BarLow else Double.NaN;
PreLow.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PreLow.SetDefaultColor(Color.CYAN);``````

Ruby:
``````def HighestVol; def BarHigh; def BarLow;
def isPreMarket = GetTime() < RegularTradingStart(GetYYYYMMDD());
def isPostMarket = GetTime() > RegularTradingEnd(GetYYYYMMDD());
def isOpen = !isPreMarket and !isPostMarket;
if (isPreMarket and volume > HighestVol[1]) {
HighestVol = volume;
BarHigh = high;
BarLow = low;
} else if (isPostMarket) {
HighestVol = 0;
BarHigh = 0;
BarLow = 0;
} else {
HighestVol = HighestVol[1];
BarHigh = BarHigh[1];
BarLow = BarLow[1];
}
plot PreHigh = if IsOpen then BarHigh else Double.NaN;
PreHigh.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PreHigh.SetDefaultColor(Color.CYAN);
plot PreLow = if IsOpen then BarLow else Double.NaN;
PreLow.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PreLow.SetDefaultColor(Color.CYAN);``````
If you have time, can you explain what each line of code does so I can try to make sense of it? Thank you this is a wonderful code. I am trying to add "Showonlytoday = yes" option, but do not know where to implement it.

Sure, as soon as I have time, I'll return and explain it.

Last edited:
I am trying to add "Showonlytoday = yes" option, but do not know where to implement it.

This quick edit will do that, assuming you don't run your charts 24 hours a day. To get it exactly and always right, I am pretty sure it would require HighestAll(), which could also write later if you want. For now, lets just work with this.

Ruby:
``````input ShowOnlyToday = Yes;
def HighestVol; def BarHigh; def BarLow;
def isLastDay = GetDay() == GetLastDay();
def isPreMarket = GetTime() < RegularTradingStart(GetYYYYMMDD());
def isValidPreMarket =
if ShowOnlyToday then isLastDay and isPreMarket
else isPreMarket;
def isPostMarket = GetTime() > RegularTradingEnd(GetYYYYMMDD());
def isOpen = !isPreMarket and !isPostMarket;
if (isValidPreMarket and volume > HighestVol[1]) {
HighestVol = volume;
BarHigh = high;
BarLow = low;
} else if (isPostMarket) {
HighestVol = 0;
BarHigh = 0;
BarLow = 0;
} else {
HighestVol = HighestVol[1];
BarHigh = BarHigh[1];
BarLow = BarLow[1];
}
plot PreHigh = if IsOpen then BarHigh else Double.NaN;
PreHigh.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PreHigh.SetDefaultColor(Color.CYAN);
plot PreLow = if IsOpen then BarLow else Double.NaN;
PreLow.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PreLow.SetDefaultColor(Color.CYAN);``````

LINE 1 - 2

Alright, so Line 1 is obviously an input, and on Line 2 are just some variables. We're going to be working with these variables multiple times later on, so its best in this situation not to use inline definitions. We'll come back to this later.

LINE 3

Ruby:
``def isLastDay = GetDay() == GetLastDay();``

I guess at this time I should point out to you, or anyone else who may be reading this, that ThinkScript is iterated on a bar by bar basis; the script is run over and over again, one time per each bar.

GetDay() will return the day of year in 365 day format, as per each individual bar, at the time the bar was originally created, as the script is iterated.

GetLastDay() will always return the value of the final day on the chart, regardless of the position within the iteration. When the two are both the same, the iteration is on the last day.

Any variable that is assigned a condition will basically hold a Yes/No value, in this case; isLastDay.

One of the best ways to figure out how something works, or to test and debug as you go, is just to plot it out individually or with a simple condition. Reading the manual is certainly a good idea, but I generally find this faster than reading the manual, which is also full of programmer mumbo jumbo that you may not be familiar with. A single plot will usually suffice, though here I placed four of them for illustration.

LINE 4

Ruby:
``def isPreMarket = GetTime() < RegularTradingStart(GetYYYYMMDD());``

GetTime() returns the number of milliseconds that have passed since some time in the 1970's. The exact date doesn't matter, what does matter is that it is a continuous flow. It does not reset at 12:00AM or anything similar.

RegularTradingStart() returns exact millisecond of the open, at a specified date, working along the same continuous flow as GetTime(). In this case GetYYYYMMDD() supplies the date. The date is what changes at 12:00AM to ensure you're working with the proper opening millisecond.

In this example, we have GetTime() in blue, and RegularTradingStart(GetYYYYMMDD()) in white. When the blue line is under the white line, its premarket. Again, this is just to illustrate the thought process of how to figure these things out on the go.

Ruby:
``````declare lower;
plot x = GetTime();
plot I_Hate_This_Color = Double.NaN;

LINE 5 - 7

Ruby:
``````def isValidPreMarket =
if ShowOnlyToday then isLastDay and isPreMarket
else isPreMarket;``````

Here we check our input, if we only want to work with today, then isLastDay and isPreMarket must both be true. But, If we want to draw our lines on all days, then we only need to know of isPreMarket is true.

If ShowOnlyToday is the same thing as If ShowOnlyToday == Yes | if ShowOnlyToday == 1.

Likewise, !ShowOnlyToday is the same as if ShowOnlyToday == No | if ShowOnlyToday == 0

The same is true of the others.

LINE 8

Ruby:
``def isPostMarket = GetTime() > RegularTradingEnd(GetYYYYMMDD());``

This should be self explanatory by now, I hope. Plot it out as I showed earlier if you don't understand it. However, I should note that we do not need any conditions regarding the ShowOnlyToday input, or anything else like that. We will not be gathering any information during the post-market. Rather, we will be using the post-market as a reset to clear old data.

LINE 9

Ruby:
``def isOpen = !isPreMarket and !isPostMarket;``

Basically, if it is not the premarket, and it is not the post market, what is it? Anyway, this is just pre-defined for later.

LINE 10 - 13

Ruby:
``````if (isValidPreMarket and volume > HighestVol[1]) {
HighestVol = volume;
BarHigh = high;
BarLow = low;``````

Here we are checking isValidPreMarket, This will be true if the iteration is passing through a pre-market period, it also implies that we are actually interested in this pre-market period based on ShowTodayOnly. Additionally, this will check to see if the volume for this bar is greater than highest volume that has been recorded so far during this pre-market.

If it is, we record it. We also do the same for the high and low of the corresponding bar. This is where things get a little jumpy though, the whole if/then structure must be taken into account.

LINE 14 - 17

Ruby:
``````} else if (isPostMarket) {
HighestVol = 0;
BarHigh = 0;
BarLow = 0;``````

This checks to see if regular trading hours are over, and clears the data.

LINE 18 - 22

Ruby:
``````} else {
HighestVol = HighestVol[1];
BarHigh = BarHigh[1];
BarLow = BarLow[1];
}``````

This continuously passes the data from the previous bar to the current bar as the script iterates. The current bar will become the prior bar on the next iteration, and so on. This is what maintains the record. This occurs when the data does not need to be cleared, and when the volume for this bar is not higher than the highest recorded value - it just maintains the recorded values if they don't require updating or clearing.

LINE 23

Ruby:
``plot PreHigh = if IsOpen then BarHigh else Double.NaN;``

Here we finally plot the lines, the line is plotted if the market is open, otherwise its blank. The code for the low works exactly the same, and the rest is just decoration. If you remove the open condition, and just use the line plot PreHigh = BarHigh; and do the same for the low, you will see what is happening a bit better "under the hood."

Hope that helps.

This is amazing. Thank you for your effort and time. Hopefully, I did not take too much of your time. This will help me learn thinkscript tremendously. This free knowledge is why I keep going back for more. Asking for a code is one thing, but understanding the functions and what's behind its capabilities is more intriguing. I will save this post on my notepad just in case it gets deleted. Once again, I admire your immense work and I hope you have an amazing week. Forever thankful @Joshua and everyone that has done good deeds on this website.

This is amazing ! Thank you.

Last edited:

87k+ Posts
276 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

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?