# Horizontal Lines In ThinkOrSwim

thank you for the detailed explanation, so the code if !IsNaN(close) and IsNaN(close[-1]) returns either 0 or 1, in this case since there's only one last bar, so the "highest" value is 1, then the code process the second line with the close price of last bar.

I wrote another similar code with labels to show the value of the variables, both "MA" and "something" and returning the same value, how come when I plot something, it does not give me the same results as MA? I don't see any lines being plotted unless I add highestall(something);

almost, but no.
it evaluates the if-then and sends that result on to highestall.

highestall( )
on every bar, what is in the parenthesis is evaluated.

every time highestall( ) is evaluated,
it creates a temporary array of all the values for the variable or formula.

highestall(close)
for every bar on the chart, this will look at all of the close values from all the bars, and find the highest close price.

when the code
if ( !IsNaN(close) and IsNaN(close[-1]) )
is true ( 1 )
then the middle parameter of the if-then , the average, is processed.

if the first line is false, then the 3rd parameter of the if-then is processed.

the result of the if-then is passed on to the highestall. either the average at the last bar or a nan value.
so for each pass over the bars, highestall will produce the same number, in all bars

Last edited:
Its because thinkScript is iterated on a bar by bar basis, it is run over and over, once per each bar. It has to figure out where to draw the line at each bar as it passes from left to right. HighestAll contains a nested loop which iterates through the entire chart, at each of these primary iterations.

Your something variable, by itself, returns double.nan at each bar, except for when its on the final bar. HighstAll will return the highest value from the whole chart at each bar, in this case the value of the moving average.

It works by using a logical statement within highest all - if the nested iteration encapsulated by highest all is on its own final bar, then it will return the moving average. Otherwise, it will return double.nan.

So essentially it goes. ..nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, moving average. The moving average will always be the highest value. It does this for each bar on the chart, and that is how it figures out the value at this bar, which ever bar that may be.

If you're a programmer, this is where ThinkScript can get a little wonky for programmers of other languages. I guess, in a way, it speaks more to the proprietary nature of how the price chart itself operates, more so than code or syntax, if that makes sense.

thank you for the detailed explanation, so the code if !IsNaN(close) and IsNaN(close[-1]) returns either 0 or 1, in this case since there's only one last bar, so the "highest" value is 1, then the code process the second line with the close price of last bar.

I wrote another similar code with labels to show the value of the variables, both "MA" and "something" and returning the same value, how come when I plot something, it does not give me the same results as MA? I don't see any lines being plotted unless I add highestall(something);

without highestall, nothing will be plotted.
something = ... has a value only on 1 bar.

for a line to be plotted, there has to be 2+ consecutive bars, with a variable with values.
need 2 points to make a line.

it might help to see it all plotted visually

Ruby:
``````def MA =
HighestAll(
if !IsNaN(close) and IsNaN(close[-1])
then simplemovingAvg(close,5)
else Double.NaN
)
;
plot MALine =
MA
;

def LastBar = !IsNaN(close) and IsNaN(close[-1]);
def Something = if LastBar then SimpleMovingAvg(close,5) else Double.NaN;
plot x = Something;
x.setPaintingStrategy(paintingStrategy.HORIZONTAL);
x.setlineWeight(5);
x.setdefaultColor(color.magenta);

plot y = simpleMovingAvg(close,5);``````

I still need time to test all these, but I get the idea.
Thank you both!

This site has a very good search engine. After searching, here is a result that has an example for you in the code https://usethinkscript.com/threads/opening-range-breakout-indicator-for-thinkorswim.16/post-75429 . Just change the following to your requested time and label it how you want in place of "Open"

is there a way to plot price a horizontal level at a given time, lets say for example the close or open of price at 1030(or any given time)

is there a way to plot price a horizontal level at a given time, lets say for example the close or open of price at 1030(or any given time)

Ruby:
``````AddVerticalLine(SecondsFromTime(1030) == 0, "                                                      1030", Color.CYAN, Curve.SHORT_DASH);
def h1030     = if SecondsFromTime(1030) == 0 then high else h1030[1];
plot high1030 = h1030;
high1030.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);``````

Here we go, horizontal line at HOD, use this on an intraday chart

Code:
``````plot highLine = highestAll(if isNaN(close[-1])
then high(period = "Day")
else Double.NaN);``````
Is it possible to get one for the just the PRE MARKET HIGH as well?

Is it possible to get one for the just the PRE MARKET HIGH as well?
Try this
Ruby:
``````def ymd           = GetYYYYMMDD();
def rthstart      = GetTime() < RegularTradingStart(GetYYYYMMDD());
def PreMarketHigh = if ymd != ymd[1] and rthstart
then high
else if rthstart and high > PreMarketHigh[1]
then high
else PreMarketHigh[1];
plot prehigh      = highestall(if isnan(close[-1]) then premarkethigh else double.nan);``````

Can any of you smart people help me with a code? I need a horizontal line drawn at 9:45 EST at the average of the 1st 15 minutes (9:30-9:45) high and low (SPX).

If you would like to add to this, I could also use an arrow up or down when it crosses this line. This cross can be alone or with a trend indicator such as EZ Trend for agreement.

Can any of you smart people help me with a code? I need a horizontal line drawn at 9:45 EST at the average of the 1st 15 minutes (9:30-9:45) high and low (SPX).

If you would like to add to this, I could also use an arrow up or down when it crosses this line. This cross can be alone or with a trend indicator such as EZ Trend for agreement.

This might get you started.

Ruby:
``````def spxhl2 = if SecondsFromTime(0930) == 0 then hl2("SPX", AggregationPeriod.FIFTEEN_MIN) else spxhl2[1];
plot x = spxhl2;
x.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);``````

This might get you started.

Ruby:
``````def spxhl2 = if SecondsFromTime(0930) == 0 then hl2("SPX", AggregationPeriod.FIFTEEN_MIN) else spxhl2[1];
plot x = spxhl2;
x.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);``````
SleepyZ,
Are there any inputs I need to add to this? I can't get this to work.

Thanks,
R

Last edited:
SleepyZ,
Are there any inputs I need to add to this? I can't get this to work.

Thanks,
R
It will plot without any adjustment on an instrument like /ES that has values similart to SPX. Anything else you will have to check at the input screen, 'left axis'.

It will plot without any adjustment on an instrument like /ES that has values similart to SPX. Anything else you will have to check at the input screen, 'left axis'.
OK...but mine will not even save as a script. Not sure why.

OK...but mine will not even save as a script. Not sure why.
Here it is pasted as coded above into a study, displayed on 1d1m chart of GME, with left axis checked.

Here it is pasted as coded above into a study, displayed on 1d1m chart of GME, with left axis checked.
Here is the same code in a study on 15m SPX chart to show it is the average (hl2) of the 1st 15m candle

is there a way to plot the highest high and lowest low of yesterdays after hours and today premarket, so basically after hours before the market opens.

is there a way to plot the highest high and lowest low of yesterdays after hours and today premarket, so basically after hours before the market opens.

Adjust the times if necessary, as this will primarily set to work with stocks. As the times in the 'aftermarket script' were set for the postmarket timeframe, no adjustment was needed when plotting the postmarket highs/lows. Otherwise, the adjustment is made for times as was done within the premarket highs/lows.

Ruby:
``````script aftermarket {
input openingTime = 1600;
input closingTime = 0359;

def sec1    = SecondsFromTime(openingTime);
def sec2    = SecondsFromTime(closingTime);
def isTime1 = (sec1 >= 0 and sec1[1] < 0) or (sec1 < sec1[1] and sec1 >= 0);
def isTime2 = (sec2 >= 0 and sec2[1] < 0) or (sec2 < sec2[1] and sec2 >= 0);
def inRange = CompoundValue(1, if isTime1 then 1 else if isTime2 then 0 else inRange[1], 0);

def rhi        = if inRange and !inRange[1]
then high
else if inRange[1] and high > rhi[1]
then high else rhi[1];
def rHighBar   = if inRange and high == rhi then BarNumber() else Double.NaN;
def rHighest   = if BarNumber() == HighestAll(rHighBar)  then rhi else rHighest[1];

plot rangehigh = if (rHighest > 0) then rHighest else Double.NaN;
rangehigh.setpaintingStrategy(paintingStrategy.HORIZONTAL);
rangehigh.setlineWeight(2);

def rlow       = if inRange and !inRange[1]
then low
else if inRange[1] and low < rlow[1]
then low else rlow[1];
def rLowBar    = if inRange and low == rlow then BarNumber() else Double.NaN;
def rlowest    = if BarNumber() == HighestAll(rLowBar) then rlow else rlowest[1];

plot rangelow  = if (rlowest > 0) then rlowest else Double.NaN;
rangelow.setpaintingStrategy(paintingStrategy.HORIZONTAL);
rangelow.setlineWeight(2);

}

plot postmarketHigh = aftermarket();
plot postmarketLow  = aftermarket().rangelow;
plot premarketHigh  = aftermarket("opening time" = 400, "closing time" = 929);
plot premarketLow   = aftermarket("opening time" = 400, "closing time" = 929).rangelow;

postmarketHigh.setdefaultColor(color.cyan);
postmarketLow.setdefaultColor(color.cyan);
premarketHigh.setdefaultColor(color.magenta);
premarketLow.setdefaultColor(color.magenta);``````

Here is the same code in a study on 15m SPX chart to show it is the average (hl2) of the 1st 15m candle
SleepyZ,
After rebooting Tos...everything started working. Thanks!

@SleepyZ How can I plot a simple horizontal line representing the average price over a 52-week period? Thanks in advance.

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