Color chart lable if close above or below moving average


Category:
0
0

I apologize if this has been previously asked and answered. I need a study that plots a specific ema or sma on the chart and the chart label is red when the stock price closes below the sma and green when it closes above the sma. Needs to work with current chart time frame. Ideally I need to be able to change the input and chart label text. The end goal is to have a 3 different studies for the 5,9,34 sma’s, each with their own label and plot on the chart. This would allow quick reference to see if a stock is above each of these three sma’s. Thank you for your help

Marked as spam
Posted by (Questions: 1, Answers: 0)
Asked on May 17, 2021 8:30 am
160 views
0
Private answer

There are many posts here in the forum one could use to mash this together but none that specifically provide all of the items you have included in your request.

The following includes user inputs to set the text of the label, the length and type of moving average. Also included are some Global Color settings so users can adjust the color of the label without modifying the code.

input labelText = "5 SMA";
input maLengthOne = 5;
input maTypeOne = AverageType.SIMPLE;
input maPriceOne = close;
DefineGlobalColor("above ma", Color.GREEN);
DefineGlobalColor("below ma", Color.RED);
plot maOne = MovingAverage(maTypeOne, maPriceOne, maLengthOne);
AddLabel(yes, labelText, if close > maOne then GlobalColor("above ma") else GlobalColor("below ma"));

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on May 17, 2021 11:17 am