# How to Plot Horizontal Line in thinkScript

#### Tom4235

##### New member
Hey, using thinkscript, how would you draw a horizontal line at high of day (HOD)? Just need the visual on my ThinkorSwim chart.

Update: more examples of how to plot horizontal lines are provided throughout this thread.

#### tomsk

##### Well-known member
VIP
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);``````

#### Branch

##### Member
VIP
How can you take this code (Use to follow price) and change it so that it will provide the RSI Percentage in a Bubble on the Lower (SubChart) with a horizontal line?

Code:
``````input barsBack = 0;
input price= close;

def vClose = close;
def nan = double.NaN;
def bn = BarNumber();
def month = GetMonth();
def monthDay = GetDayOfMonth(GetYYYYMMDD());
def currentBar = HighestAll(if  !IsNaN(price) then bn else Double.NaN);
def highestClose = HighestAll(if IsNaN(vClose[-1]) then vClose else nan);
plot hc = highestClose;
hc.SetPaintingStrategy(PaintingStrategy.DASHES);
hc.SetDefaultColor(Color.CYAN);

# throws an error symbol at top left of chart when condition is false. click on the error symbol to view the error message
# condition  , "text"
assert(barsBack >= 0, "''bars back'' cannot be negative ");
# variables

AddChartBubble(bn == currentBar - barsBack, price,
# This section for text
"Price " +  ( if    price == close  then  "\$ "  else  if    price == open  then  "Open: \$"   else  if    price == low    then  " Low: \$"   else  if    price == high   then  " High: \$"   else  if    price == OHLC4  then  " OHLC4 \$" else  if    price == HLC3  then  " HLC3 \$" else  " Value:  " )+ price,(

# this section for color
if    price == close  then  color.cyan  else  if    price == open  then  color.pink   else  if    price == low  then  color.yellow  else  if price == high then  Color.White
else  if    price == OHLC4 then  Color.Dark_Orange else  if    price == HLC3 then  Color.Magenta else  color.Plum), yes);``````

#### Minefieldhopscotch

##### New member
VIP
Hi, this is a request for help with creating a study. I have simple scripting skills and I haven't been able to figure this out on my own. I would like to be able to plot a horizontal line on a chart from an inputted start date at that day's high. Then on subsequent days when the stock's high is higher, shift the horizontal line higher to that new high value. The line at the highs would move higher as the stock's prior high is exceeded, but the line would never move lower. Below is a link to a mock-up sketch on imgur.com that illustrates what I am trying to do. The way I want to use this is as a way to easily visualize how closely or not a stock is trending upward near its highs. Thanks very much.

#### RobertPayne

##### Member
Ruby:
``````input StartDate = 20191003;
def hh = if GetYYYYMMDD() == StartDate then high else if high > hh[1] then high else hh[1];
plot higherHigh = if GetYYYYMMDD() < StartDate then Double.NaN else hh;
higherHigh.SetPaintingStrategy(12);``````

Last edited:

#### Minefieldhopscotch

##### New member
VIP
Thank you Robert. This is appreciated.

#### twelvester

##### New member
I am trying to draw a horizontal line about 5 bars long. But am only able to draw it at the current bar.

Background:
• To show the break out price level when an Inside Bar occurs
• The Price Line is the Mother Bar High
• The Price Line should start at the Mother Bar and end 3 bars later

Current code:
Code:
``````def RangeMax = Max(high, low);
def RangeMin = Min(high, low);

def IsMotherBarBullish3 = if close[2] > open[2] then 1 else 0;
def IBB3 = if RangeMax[1] < RangeMax[2] and RangeMin[1] > RangeMin[2]
and IsMotherBarBullish3
then 1 else 0;
plot IBB3_ = if IBB3 then high[2] else Double.NaN;
IBB3_.setPaintingStrategy(PaintingStrategy.HORIZONTAL);
IBB3_.setLineWeight(1);
IBB3_.setDefaultColor(Color.ORANGE);``````

Issue:
- The horizontal line is showing only on the 3rd bar

I think it needs to be a recursion but i'm not sure how to make it recursive.

Last edited by a moderator:

#### inthefutures

##### Active member
Code:
``plot IBB3 = if high[2],and high[3] and high[4] else Double.NaN;``

This might fix it.

#### twelvester

##### New member
plot IBB3 = if high[2],and high[3] and high[4] else Double.NaN;

this might fix it
Nope doesn't work. Thanks for trying

##### Active member
2019 Donor
VIP
I'm looking for a little help to fix this script I'm trying to create. I know it will be very simple for you guys as I'm still learning.

What I'm trying to do here is have the script draw 3 horizontal price lines (High, Low, Close) based on the manually inputted levels with a cloud around each level. Depending on the symbol displayed (either SPX or /ES) and depending on the trading session (RTH or ETH), I would also like to have the appropriate info as labels. So for example, if I have a chart of the SPX during Regular Trading Hours then it will have the lines and labels levels for SPX for the regular trading hour session. If I have a chart of the S&P 500 futures during RTH, then it will who the levels for /ES for RTH. If it's during the ETH session, then levels and label for the ETH session are shown instead. I will input the SPX levels for each session and the script will use that to come up with the /ES level based on the offset.

Here is the script. Let me know what I am doing wrong and if you have a better solution, please let me know.

Code:
``````#
# Algo Predicted High Low Close for RTH and ETH sessions #
# These levels are inputed manually and is only valid
# for the specified period!
#
# Offset is the difference between the SPX and S&P 500 Emini Futures in points

def na = Double.NaN;
def timeStrRTH = (SecondsFromTime(930) > 0) and (SecondsFromTime(1700) < 0);
def timeStrETH = ((1700) > 0) and (SecondsFromTime(930) < 0);

input AlgoHigh = 2801;
input AlgoLow = 2689;
input AlgoClose = 2743;
input AlgoHighETH = 2801;
input AlgoLowETH = 2689;
input AlgoCloseETH = 2743;
input offset = 12;
input MarginErrorHigh = 10;
input MarginErrorLow = 14.5;
input MarginErrorClose = 5;

def AHRTH = AlgoHigh - offset;
def ALRTH = AlgoLow - offset;
def ACRTH = AlgoClose - offset;
def AHETH = AlgoHighETH - offset;
def ALETH = AlgoLowETH - offset;
def ACETH = AlgoCloseETH - offset;

plot AH;
plot AL;
plot AC;

if  GetSymbol() == "/ES:XCME" {
AH = if timeStrRTH then AHRTH else if timeStrETH then AHETH else na ;
AL = if timeStrRTH then ALRTH else if timeStrETH then ALETH else na ;
AC = if timeStrRTH then ACRTH else if timeStrETH then ACETH else na ;
} else

if  GetSymbol() == "SPX" {
AH = if timeStrRTH then AlgoHigh else na ;
AL = if timeStrRTH then AlgoLow else na ;
AC = if timeStrRTH then AlgoClose else na ;
} else  {
AH = na;
AL = na;
AC = na;

}

def upperAH = AH + (MarginErrorHigh/2);
def lowerAH = AH - (MarginErrorHigh/2);
def upperAL = AL + (MarginErrorLow/2);
def lowerAL = AL - (MarginErrorLow/2);
def upperAC = AC + (MarginErrorClose/2);
def lowerAC = AC - (MarginErrorClose/2);

AH.SetDefaultColor(Color.RED);
AH.SetLineWeight(2);

AL.SetDefaultColor(Color.GREEN);
AL.SetLineWeight(2);

AC.SetDefaultColor(Color.ORANGE);
AC.SetLineWeight(2);

AddLabel(GetSymbolPart()== "SPX", "Algo High: " + AH, Color.RED);
AddLabel(GetSymbol()== "SPX" and timeStrRTH, "Algo Low: " + AlgoLow, Color.GREEN);
AddLabel(GetSymbol()== "SPX" and timeStrRTH, "Algo Close: " + AlgoClose, Color.ORANGE);

AddLabel(GetSymbol()== "SPX" and timeStrETH, "Algo High: " + AlgoHighETH, Color.RED);
AddLabel(GetSymbol()== "SPX" and timeStrETH, "Algo Low: " + AlgoLowETH, Color.GREEN);
AddLabel(GetSymbol()== "SPX" and timeStrETH, "Algo Close: " + AlgoCloseETH, Color.ORANGE);

AddLabel(GetSymbolPart()== "/ES:XCME" and timeStrRTH, "Algo High: " + AHRTH, Color.RED);
AddLabel(GetSymbolPart()== "/ES:XCME" and timeStrRTH, "Algo Low: " + ALRTH, Color.GREEN);
AddLabel(GetSymbol()== "/ES:XCME" and timeStrRTH, "Algo Close: " + ACRTH, Color.ORANGE);

AddLabel(GetSymbol()== "/ES:XCME" and timeStrETH, "Algo High: " + AHETH, Color.RED);
AddLabel(GetSymbol()== "/ES:XCME" and timeStrETH, "Algo Low: " + ALETH, Color.GREEN);
AddLabel(GetSymbol()== "/ES:XCME" and timeStrETH, "Algo Close: " + ACETH, Color.ORANGE);``````

Last edited:

#### kblaine123

##### New member
Is there a simple script to limit the length of the horizontal line to say 30 or 60 bars rather than the whole length of the chart ?

Code:
``````plot support = D*1.2;
support.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
support.setDefaultColor(Color.Yellow);
support.SetLineWeight(3);``````

#### scriptorswim

##### New member
Pretty much the question folks. I have seen some codes from google for creating horizontal lines for the current price/position (in which they didnt work really) but i tried to mess with them to create this but been unsuccessful. Basically, if we have SPY on our chart, i want to see hortizontal lines at 307,307.5,308,308.5 that are close to the current price (50 instances?) for today, anyone have the script or interested in mocking something up?

Last edited:

#### BenTen

Staff
VIP
Something like this would work

Code:
``````input level = 100;

plot a = level + 5;

plot b = level + 10;``````

#### Len20

##### Member
I'm working on something similar which draws a horizontal line when price goes near an even number relative to the price of the stock.

I modified it to plot the nearest 50 cents above and below the current price, so it adjusts with the current price. It's just two horizontal lines, but I figured it might be useful anyways.

Code:
``````# Even Number Lines by Len20

input step = .50;

def price = close;
def upper = roundUp(price / step , 0) * step;
def lower = roundDown(price/ step , 0) * step;
def upperLine = if !isNaN(price) and isNaN(price[-1]) then upper else if isNan(price) then upperLine[1] else double.NaN;
def lowerLine = if !isNaN(price) and isNaN(price[-1]) then lower else if isNan(price) then lowerLine[1] else double.NaN;

plot above1 = upperLine;
plot below1 = lowerLine;``````

#### Pelonsax

##### Active member
VIP
Hi guys! I am working on a study right now that, among other things, paints these pivots (red and green lines). As you can see, there is a horizontal component and a vertical component. The red line is called PH and the green line is called PL. What I need your help with is a section of code that remembers the value of the last horizontal component so that I can write code that uses that number when comparing to the current horizontal component. Can you help?

Here's a snapshot:

#### scriptorswim

##### New member
How to auto draw a horizontal line in an indicator?

Last edited by a moderator:

#### MrDrB

##### New member
Code:
``plot my_line = 1; #change 1 to whatever static value or variable you want it to plot``

#### scriptorswim

##### New member
plot my_line = 1; #change 1 to whatever static value or variable you want it to plot

how about the value of the first 1m candle each day opening? day.open types dont work

#### Tweak155

##### New member
Code:
``````def mostRecentBar = !IsNaN(close) and IsNaN(close[-1]);
def mostRecentBarNumExtract = If mostRecentBar Then BarNumber() else Double.NaN;
def mostRecentBarNum = HighestAll(mostRecentBarNumExtract);
plot someStuff = If BarNumber() > (mostRecentBarNum - 5) Then high + 2 else Double.NaN;``````

EDIT: I see now this thread is 3+ months old