Price percent from EMA with background color


Category:
0
0

Hi Pete,

Can this code show background color on the watchlist when the following parameters are met? This was taken from this question: https://www.hahn-tech.com/ans/price-from-ema/

I want to assign background color green when the watchlist filter is showing positive values between a value of 0.0 to 0.35

To give you a specific example, i implemented this code and there’s multiple stocks showing different values.

For example:
-TMUS= 0.06
– CHK= – 0.12
– DINO= -0.03
-LDOS= 0.31

Based on this result, the watchlist filter background should be green for LDOS and TMUS.

input maLengthOne = 21;
input maTypeOne = AverageType.EXPONENTIAL;
input maPriceOne = close;
def maOne = MovingAverage(maTypeOne, maPriceOne, maLengthOne);
plot percentFrom = 10 * (close / maOne – 1);

 

Marked as spam
Posted by (Questions: 1, Answers: 2)
Asked on November 29, 2022 6:45 pm
119 views
0
Private answer

Since you logged into the forum using your Twitter account we don't have a valid email address for you. This means you are not getting email notifications when you post new content on our website. Don't post your email address here or you will get spammed. Contact me directly and we can get that updated.

Here is the solution you requested:

input minimumValue = 0.0;
input maximumValue = 0.35;
input maLengthOne = 21;
input maTypeOne = AverageType.EXPONENTIAL;
input maPriceOne = close;
def maOne = MovingAverage(maTypeOne, maPriceOne, maLengthOne);
plot percentFrom = 10 * (close / maOne – 1);
percentFrom.AssignValueColor(if percentFrom > minimumValue and percentFrom < maximumValue then Color.BLACK else Color.CURRENT);
AssignBackgroundColor(if percentFrom > minimumValue and percentFrom < maximumValue then Color.GREEN else Color.CURRENT);

I included user inputs for min/max values so the rest of our viewers can use this for their own preferences.

And while I was working this I realized the percentFrom variable is not computed correctly. That line of code should be as follows:

plot percentFrom = 100 * (close / maOne – 1);

But changing this would also require you to update the min/max value inputs to account for the adjusted value. I am providing this correction for the benefit of those who come across this post at a later date. And I will updated the solution in the original post as well.

Edit: Someone requested this to be converted to a chart label. Since the solution to create a chart label will also work as a watchlist column I will provide that solution here instead of requiring that request be posted as a new question. When we use the AddLabel() statement to display values in a watchlist column, those values get converted to text. Which means we loose the ability to sort by the values of that column. But we can do neat things with this, such as add the "%" sign to the displayed value:

input minimumValue = 0.0;
input maximumValue = 0.35;
input maLengthOne = 21;
input maTypeOne = AverageType.EXPONENTIAL;
input maPriceOne = close;
def maOne = MovingAverage(maTypeOne, maPriceOne, maLengthOne);
def percentFrom = 100 * (close / maOne – 1);
AddLabel(yes, Concat(percentFrom, "%"), if percentFrom > minimumValue and percentFrom < maximumValue then Color.GREEN else Color.GRAY);

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on November 29, 2022 7:48 pm
0
Perfect Pete, this works very well. Thanks for the help!!
( at November 29, 2022 7:58 pm)
0
Hello Hanh, is there any way to create a chart label for this? Im just interested in seeing the label showing the EMA % from the current price. Colors are not important for the label. thanks for the help, Luis
( at March 24, 2023 7:43 am)
0
I have updated my solution to include a version which uses the AddLabel() function. This solution will work for a custom watchlist column as well as a chart. If you want to display the moving average on the chart, just change "def maOne" to "plot maOne".
( at March 24, 2023 11:09 am)