Change Color for Horizontal Line Thinkscript

eugchen

Member
Hello, im creating a study where I simply draw horizontal lines for each stock. So each stock I list has a different value.

So as you can see, I will have a list of prices for each stock to draw specific horizontal lines on many charts. I just included 3 stocks for simplicity sake.

But my main question is this. Right now the line is shown to be dotted Orange line for all stocks. Is there anyway to color code the line to show up as ORANGE when P1>current stock close, and GREEN when P1<current stock close?

So for AAON, P1 is 48.95. current price for AAON is 72.74. so the line should be Green. And if the current price was 40, then it would be orange. Not sure how to get this in the code.

Basically im trying to distinguish which is higher, P1, or current price. If I cant change the color of the line, then maybe a chart label? Something to make it more obvious.

thanks in advance!

Code:
AddLabel(No, GetSymbol());
plot p1;
p1.SetDefaultColor(Color.orange);
p1.SetLineWeight(5);
p1.SetStyle(Curve.LONG_DASH);
def nan = Double.NaN;
if (GetSymbol() == "AAON")then {p1 = 48.95;} else
if (GetSymbol() == "AAPL")then {p1 = 85;} else
if (GetSymbol() == "ZTS")then {p1 = 125;} else
{ p1 = nan; ;}
 

BenTen

Administrative
Staff
VIP
You can use the if-else statement. Here's an example:

Code:
plot Data = 350;
Data.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Data.AssignValueColor(if close > Data then color.CYAN else color.MAGENTA);

O70cn1U.png
 

eugchen

Member
Wow, thanks, but how do I incorporate yours? When I try the below, thinkscript is giving me an error on the p1 that I bolded.

Code:
AddLabel(No, GetSymbol());
plot p1;
p1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
p1.AssignValueColor(if close > p1 then color.CYAN else color.MAGENTA);
p1.SetLineWeight(5);
p1.SetStyle(Curve.LONG_DASH);
def nan = Double.NaN;

if (GetSymbol() == "AAON")then {p1 = 48.95;} else
if (GetSymbol() == "AAPL")then {p1 = 85;} else
if (GetSymbol() == "ZTS")then {p1 = 125;} else
{    p1 = nan;    ;}
 

BenTen

Administrative
Staff
VIP
@eugchen Where are you trying to plot "p1" on your chart? You need to specify the level. Look at my example above.

Code:
plot Data = 350;

I'm plotting the line at $350 price level on my chart.
 

eugchen

Member
Sorry forgive me, I'm a total noobie...i got this code from researching a bit online.

What I believe the code I'm using is to plot p1.

for AAON, p1 = 48.95 (draw a horizontal line at 48.95)
for AAPL, p1 = 85 (draw a horizontal line at 85)

and so forth.

I do see you are plotting a horizontal line at 350, but my price level will change for different stocks.
 

BenTen

Administrative
Staff
VIP
@eugchen The code you provided was not in order. In other words, you were trying to plot "p1" before defining its location.

This should work:

Code:
plot p1 = if (GetSymbol() == "AAON") then 48.95 else if (GetSymbol() == "AAPL") then 85 else if (GetSymbol() == "ZTS") then 125 else double.nan;
p1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
p1.AssignValueColor(if close > p1 then color.CYAN else color.MAGENTA);
 

eugchen

Member
Benten---THANK YOU!!!!! so helpful!!!

one last thing....on my charts, i like to see some white space to the right. often 20-30 bars of blank space. well in that blank space area, the line is showing up always at cyan. is there anyway to have the entire line either show up as cyan or magenta based on the current price and P1?

so for instance, if P1 = 80 and Last price on stock is 90, then the entire line should show up as Cyan and vice versa if stock less than 80.
 

eugchen

Member
sorry I wasn't clear. I already have the expansion area.

here's a screenshot of what I'm looking at. since i have the expansion area, its assuming the price in that expansion area is 0 and will always show a cyan line. id like the entire line to be the same as the part of the line to the left, not different colors. hope this clarifies a bit.

Capture1.jpg
 

rad14733

Well-known member
VIP
Rad14733- will this hullmovingavg make my entire horizontal line one color? That's what I'm looking for.

Ok... Got it... If SetDefaultColor() won't do it then it probably can't be done... The only other thing that "might" work would be trying to use some type of loop to paint ahead... For that you would probably use something like the code below and then set the color as you would for a normal plot... Just spitballing... Perhaps some else knows a better way though...

Ruby:
<loop code>
plot data[-x] = <whatever>;
<color code>
<loop code>
 

eugchen

Member
Or I'll be ok if the line stops at the current price (where the maroon meets the blue). Is that easier?

Here's the current code btw...I'm sorta confused as to why the line in that blank area is blue?

Code:
plot p1 =

if (GetSymbol() == "AAON") then 48.95 else
if (GetSymbol() == "AAPL") then 85 else
if (GetSymbol() == "ABBV") then 97 else

double.nan;

p1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
p1.SetLineWeight(5);
p1.SetStyle(Curve.POINTS);
p1.AssignValueColor(if close > p1 then color.Dark_red else color.Dark_orange);
 

XeoNoX

Well-known member
VIP
@eugchen

Code:
input start_time = 0930;
input endtime = 1600;

def selected_time = if GetLastDay() == GetDay() and SecondsFromTime(start_time) >= 0 and SecondsFromTime(endtime) <
0 then 1 else 0;
plot p1 =

if (GetSymbol() == "AAON") and selected_time then 48.95 else
if (GetSymbol() == "AAPL") and selected_time then 85 else
if (GetSymbol() == "ABBV") and selected_time then 97 else

double.nan;

p1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
p1.SetLineWeight(5);
p1.SetStyle(Curve.POINTS);
p1.AssignValueColor(if close > p1 then color.Dark_red else color.Dark_orange);

if you found this post useful, dont forget to hit the thumbs up!
 

rad14733

Well-known member
VIP
ic. i sorta figured that, but didnt see it in the code.

but does anyone know how to make the line stop at todays price and not go into the expansion area?

Not if you use expansion area... Is this an OCD issue or what is the problem with the cyan color...???

@XeoNoX I don't think that code will help during trading hours, only afterhours...
 

eugchen

Member
not an OCD issue. and i do like using the expansion area.

since i cycle through hundreds of stocks a day, i want to see an easy way if the current price is above or below the price line. and i have a color scheme that i like where if i see a certain color, it means one thing. opposite for other color. the cyan just distracts me and annoys me.

and yea, i think that code only works on an intraday chart, not a 6month daily chart like i have :(
 

XeoNoX

Well-known member
VIP
@eugchen say go Tampabay! if it works
Code:
plot p1 =
if (GetSymbol() == "AAON") and close  then 48.95 else
if (GetSymbol() == "AAPL") and close  then 85 else
if (GetSymbol() == "ABBV") and close then 97 else
double.nan;

p1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
p1.SetLineWeight(5);
p1.SetStyle(Curve.POINTS);
p1.AssignValueColor(if close > p1 then color.Dark_red else color.Dark_orange);
 

Similar threads

Top