Color chart background based on two moving averages


Category:
0
0

Hello,  I have found some answers that pertain to my desired outcome but have been unable to put them all together in a way that is accepted.

Working on a 1 minute chart… when price closes higher than 9 EMA

and

higher than HMA 30 length then background changes to dark green

 

when price closes lower than 9 EMA

and

lower than HMA 30 length then background changes to dark red

 

when price is not above or below BOTH the 9 EMA and the HMA 30 length then background is current  (which in my case is black).

I have tried piecing together various bits of code to accomplish this task but have not been successful.  Thanks for any help!

Marked as spam
Posted by (Questions: 7, Answers: 1)
Asked on June 6, 2022 7:24 pm
261 views
0
Private answer

The same exact method used to change background colors of custom watchlist columns works for charts. So in order to find solutions like this you should also be sure to browse existing solutions in our "Watch Lists" topic.

Using search, I discovered the following posts which are nearly a perfect match for your request:

https://www.hahn-tech.com/ans/hma-and-ema-cross-to-watch-list/

https://www.hahn-tech.com/ans/how-many-days-ago-ema-cross-on-2hr/

The first post linked above actually points to the second one. Each request is slightly different but close enough that I was able to build your solution from the code provided in those previous posts. All I did was to change the moving averages to plot statements, removed the "data" variable which displayed the bar counts and removed two lines of code which performed the bar counts.

You may find it helpful to use the watchlist solution in addition to your charts. Or you may find the watchlist solution is more effective.

input maLengthOne = 9;
input maLengthTwo = 30;
input maTypeOne = AverageType.EXPONENTIAL;
input maTypeTwo = AverageType.HULL;
input priceOne = close;
input priceTwo = close;
plot maOne = MovingAverage(maTypeOne, priceOne, maLengthOne);
plot maTwo = MovingAverage(maTypeTwo, priceTwo, maLengthTwo);
def priceAbove = close > maOne and close > maTwo;
def priceBelow = close < maOne and close < maTwo;
AssignBackgroundColor(if priceAbove then Color.GREEN else if priceBelow then Color.RED else Color.CURRENT);

Marked as spam
Posted by (Questions: 37, Answers: 4087)
Answered on June 7, 2022 8:34 am