Set background color based on stock price


Category:
0
0

Hi all, I need assistance in correcting my script … Here’s what I am trying to accomplish
Price equal to or greater than $1 … background color Magenta
Price equal to or greater than $10 … background color White
Price is less than $1 … background color grey

The current code below just makes all the background colors magenta

Thank you!

plot x = close;

assignBackgroundColor(
    if x >= 1.00 then COLOR.magenta
    else if x >= 10.00 then COLOR.white
         else if x < 0 then COLOR.gray
              else COLOR.grey);

x.assignValueColor (color.black);
Marked as spam
Posted by (Questions: 3, Answers: 2)
Asked on July 10, 2020 4:24 pm
1165 views
0
Private answer

Before we get to the solution we have a few details to cover to help you learn as much as we can teach you from this post.

  1. The question of the title needs to describe the context of your request. Other viewers will be searching the forum for this solution and when they run across a post that is titled "Price background color help" they will skip right over you post. So I updated the title of your question to include the full context of your request.
  2. Your code is miserable. Don't be offended. The vast majority of folks are writing pure garbage because they have had nothing but miserable examples to learn from. The only way to correct this is to call it out when we see garbage code. So let's get into the details so you understand what you are doing wrong:
    1. NEVER use single character abbreviations for your variable names. This is stupid:
      plot x = close;So stop doing that.
    2. Learn to use proper capitalization. How do we learn the right way? By comparing the following statement from your code assignBackgroundColor to the examples provided in the language reference of Thinkorswim: https://toslc.thinkorswim.com/center/reference/thinkScript/Functions/Look---Feel/AssignBackgroundColor
    3. The keyword "color" should only have the first letter capitalized: https://toslc.thinkorswim.com/center/reference/thinkScript/Functions/Look---Feel/Color
    4. The colors themselves are "Constants" and should be all caps: https://toslc.thinkorswim.com/center/reference/thinkScript/Constants/Color

Ok, I think that is enough for now. Please learn from the examples in the Thinkorswim language reference and NOT from the miserable examples scattered all over the internet. I have made many of these mistakes myself. Only once I started doing this for a living did I realize how important it is to follow these fundamental guidelines.

Now on to your solution. The problem is that you ordered your statement backwards. Your first test is to check if price is greater than 1. Everything greater than 1 will be captured right there and the rest of the statement is ignored. If you want to do this correctly you need to order these statements correctly. So the first check is for the largest value. The second check is for the next largest value. And so on...

plot data = close;
data.AssignValueColor(Color.BLACK);
AssignBackgroundColor(if data >= 10 then Color.WHITE else if data >= 1 then Color.MAGENTA else Color.GRAY);

That right there is some properly structured code for Thinkorswim. Notice there is no need to test if data is less than zero because you are using the same color for anything not greater than or equal to one.

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on July 10, 2020 7:24 pm
0
Tired manipulating and adjusting code to change background colors when Hull Moving Avg switches from going up to going down. Up is when the avg current HMA is (HMA>HMA[1] and down the current HMA is (HMA<HMA[1]
( at November 15, 2020 2:45 pm)
0
I suggest you try the following solution: https://www.hahn-tech.com/ans/wl-for-hull-in-tos/
( at November 15, 2020 3:03 pm)
0
Tried the above code it did not work, just got one background color.
( at November 16, 2020 10:04 am)
0
Works perfectly when I test it.
( at November 16, 2020 11:44 am)