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);

10-10-2021-2-47-11-PM.png

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:

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

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);

10-10-2021-2-47-11-PM.png

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

wr5XlPP.png

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);
 
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"

AddVerticalLine(SecondsFromTime(0930)==0,"Open",Color.Gray,Curve.SHORT_DASH);
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)


Capture.jpg
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);
 
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:
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.

Capture.jpg
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);
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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