# % change from yesterday’s close to today’s open

Category:
0
0

Hello there Mr. Hahn,

I was able to write the following code for a custom watchlist to give me the dollar difference between yesterday’s close and today’s open:

def o = open;
def c = close[1];

def gu = o > c;
def gd = o < c;

def diff = o – c;
plot gap = diff;

AssignBackgroundColor(if gap > 0 then color.dark_green else if gap < 0 then color.dark_red else color.gray);

The code works perfectly on the watchlist but when I try to adjust the code to show me the percentage difference, the percentages are always off. I am sure that what i’m missing is very simple but I can’t seem to figure it out. I would very much appreciate your assistance with putting this simple code together. Thanks in advance.

Regards,

Marked as spam
Posted by (Questions: 3, Answers: 4)
Asked on October 10, 2020 4:15 pm
162 views
0

Please don't ever do this again:

```def o = open; def c = close[1];```

That is just a complete waste. I hate it when I see folks doing this in their code. There is never a scenario where that is necessary and it makes the code extremely difficult to read and maintain.

And please don't ever do this either:

```def gu = o > c; def gd = o < c;```

That really is useless (variable names should be spelled out so that they explain exactly what data they contain). I wish I could find out where folks are learning to do this sort of thing and shut it down. Sorry, the longer I do this the more annoyed I get when I see folks learning how to do things the wrong way. My only way to combat this is to call it out when I see it, so the rest of our viewers learn how to do things the right way. Now I will dismount my soapbox and provide my own solution:

```plot percentChange = 100 * (open / close[1] - 1); AssignBackgroundColor(if percentChange > 0 then Color.DARK_GREEN else if percentChange < 0 then Color.DARK_RED else Color.GRAY); ```

Two lines of code and it adds the functionality you requested. Our company motto: "Maximum Precision Through Intelligent Use of Minimal Resources"

Marked as spam
Posted by (Questions: 37, Answers: 4052)
Answered on October 10, 2020 5:36 pm
 ▲ 0 ▼ Hey Pete, Thank you for your help and your feedback. I came up with that code with the help of some "experts" on the usethinkscript.com forum. The code you provided is obviously much more streamlined. So how would you re-write my original code and clean it up? (Leo Ortega at October 10, 2020 6:02 pm)
 ▲ 0 ▼ How would I rewrite your original code and clean it up? that is precisely what I did. (Pete Hahn at October 10, 2020 6:04 pm)