MACD Help with Custom Value & Avg labels.

BOOMERMANG

New member
VIP
Hi everyone,

I'm trying to add two labels on a MACD lower study for the plots Value and Avg.

I cant figure out how to get the labels to work with the plots for the value line(Cyan) and avg line(Yellow):

They work on some tickers, on some time frames, but not all. Seemingly from what I've tested it only appears to happen with some negative plots.
eIc08o7.png


This one isn't even close (2Y 1D)
EoD5FHP.png


This one seems to work (3Y M):
uxYKQ54.png


This one just shows -0 (180D 15M):
BJ8vyTx.png


I've searched up and down the site, and I'm just stumped!

Preferably, I'm looking for a solution that incorporates 100 thousandths on the label, right now it'll produce a label sometimes up to 10 thousandths.

The full code I'm using is:
Code:
declare lower;

input price = close;
input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input AverageType = {SMA, default EMA};
input MACDLevel = 0.0;

def fastEMA = ExpAverage(price, fastLength);
def slowEMA = ExpAverage(price, slowLength);

plot Value;
plot Avg;
switch (AverageType) {
case SMA:
    Value = Average(price, fastLength) - Average(price, slowLength);
    Avg = Average(Value, MACDLength);
case EMA:
    Value = fastEMA - slowEMA;
    Avg = ExpAverage(Value, MACDLength);
}

plot Diff = Value - Avg;
plot Level = MACDLevel;



Value.SetDefaultColor(GetColor(1));
Avg.SetDefaultColor(GetColor(8));
Diff.DefineColor("Positive and Up", Color.GREEN);
Diff.DefineColor("Positive and Down", Color.DARK_GREEN);
Diff.DefineColor("Negative and Down", Color.RED);
Diff.DefineColor("Negative and Up", Color.DARK_RED);
Diff.AssignValueColor(if Diff >= 0
        then if Diff > Diff[1] then Diff.color("Positive and Up") else Diff.color("Positive and Down")
          else if Diff < Diff[1] then Diff.color("Negative and Down") else Diff.color("Negative and Up"));
Diff.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Diff.SetLineWeight(3);
Level.SetDefaultColor(GetColor(3));

addLabel(yes, " Value = "  + Value + "  ", if Value > Avg then Color.GREEN else Color.RED);

addLabel(yes, " Avg = "  + avg + "  ", if Avg > Value then Color.RED else Color.GREEN);

I've tried using different lines of code to get some consistency, but have been unsuccessful:
Code:
addLabel(yes, " Value = "  + absValue(Value) + "  ", if Value > Avg then Color.GREEN else Color.RED);

addLabel(yes, " Avg = "  + avg + "  ", if Avg > Value then Color.RED else Color.GREEN);
 
Last edited by a moderator:
Solution
The only thing I can think of is to add a multiplier to your Value in the label:
Code:
addLabel(yes, " Value = "  + absValue(Value * 1000) + "  <Thousandths>. ", if Value > Avg then Color.GREEN else Color.RED);

addLabel(yes, " Avg = "  + avg * 1000+ "  <Thousandths>. ", if Avg > Value then Color.RED else Color.GREEN);

and go from there. There is no way I know of to enforce precision increases. You can use round() if you want to limit precision but there is no way to format a number to that many decimal places.

Per the docs, AsText() allows you to format a number to to or three places
https://tlc.thinkorswim.com/center/reference/thinkScript/Constants/NumberFormat

mashume
The only thing I can think of is to add a multiplier to your Value in the label:
Code:
addLabel(yes, " Value = "  + absValue(Value * 1000) + "  <Thousandths>. ", if Value > Avg then Color.GREEN else Color.RED);

addLabel(yes, " Avg = "  + avg * 1000+ "  <Thousandths>. ", if Avg > Value then Color.RED else Color.GREEN);

and go from there. There is no way I know of to enforce precision increases. You can use round() if you want to limit precision but there is no way to format a number to that many decimal places.

Per the docs, AsText() allows you to format a number to to or three places
https://tlc.thinkorswim.com/center/reference/thinkScript/Constants/NumberFormat

mashume
 
Solution
The only thing I can think of is to add a multiplier to your Value in the label:
Code:
addLabel(yes, " Value = "  + absValue(Value * 1000) + "  <Thousandths>. ", if Value > Avg then Color.GREEN else Color.RED);

addLabel(yes, " Avg = "  + avg * 1000+ "  <Thousandths>. ", if Avg > Value then Color.RED else Color.GREEN);

and go from there. There is no way I know of to enforce precision increases. You can use round() if you want to limit precision but there is no way to format a number to that many decimal places.

Per the docs, AsText() allows you to format a number to to or three places
https://tlc.thinkorswim.com/center/reference/thinkScript/Constants/NumberFormat

mashume
Thank you, I appreciate the help. Unfortunately this did not solve my problem. I understand what your saying about the precision increase. I may have been confusing with how I wrote that part. What I was getting at was, I just didn't want the values to get rounded off on the label that's all, and that if possible at least give a value capable of up to 100 thousandths. . Im pretty green when it comes to coding, but I know that there are certain ways to write the code that will give you a value only displaying up to the hundredths, and I was just trying to prevent someone providing me with a code that would do that.

My main problem that I'm finding, and really trying to solve is that the plot values aren't equalling the label values in some cases... like not even close to the value. I've noticed that the title block at the top of the indicator is doing the same, so Im wondering if it has something to do with the plot code itself. I tried to use your code, and it seemed to fix some, but not all, and some cases moves the decimal place over to far the other way. I understand that multiplying by (1000) is moving the decimal place over. Unfortunately the values that were labeling correctly are now adding those extra zeros as well, so they're now incorrect.

When I say title block for lack of not knowing the actual name I'm referring to the area circled in red
DFgGoVw.png


On this ticker and time frame my code works and yours does not. Title block matches my labels and plot value.
qF8d4Zr.png


On this ticker and time frame your label for AVG works and the rest are incorrect, and title block doesn't match.
j8bxnNu.png


So I figured out that using
Code:
absValue(Value * 1000)
for the value label was giving me positive number's even if the value is negative. So now I've got that sorted out, and replaced it with just
Code:
Value * 1000
just like I did with the AVG label.

Interesting though now looking at the same ticker and same time frame, the plot values have changed through the day, but the concept shouldn't change. The formula is producing an incorrect label again. Title block is off too.
0qEbPVv.png


3Y M mine works your's does not. Title block matches plot values.
iIX1NWc.png


360D 4H Mine does not work, your's does. My labels match the title block but are rounded to hundred thousandths.
7vpnhaO.png


This is where I'm at, no clue what's going on.

Code:
declare lower;

input price = close;
input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input AverageType = {SMA, default EMA};
input MACDLevel = 0.0;
input DrawCloud = no;
input displayBullBearLabels = yes;
input displayAbbreviatedPriceLabels = yes;
input displayVerbosePriceLabels = no;
input displayValueAvgLabels = yes;

def fastEMA = ExpAverage(price, fastLength);
def slowEMA = ExpAverage(price, slowLength);

plot Value;
plot Avg;
switch (AverageType) {
case SMA:
    Value = Average(price, fastLength) - Average(price, slowLength);
    Avg = Average(Value, MACDLength);
case EMA:
    Value = fastEMA - slowEMA;
    Avg = ExpAverage(Value, MACDLength);
}

plot Diff = Value - Avg;
plot Level = MACDLevel;

def PMACDeq = reference ReverseEngineeringMACD(price, fastLength, slowLength, MACDLength, AverageType, MACDLevel).PMACDeq;
def PMACDlevel = reference ReverseEngineeringMACD(price, fastLength, slowLength, MACDLength, AverageType, MACDLevel).PMACDlevel;
def PMACDsignal = reference ReverseEngineeringMACD(price, fastLength, slowLength, MACDLength, AverageType, MACDLevel).PMACDsignal;


Value.SetDefaultColor(GetColor(1));
Avg.SetDefaultColor(GetColor(8));
Diff.DefineColor("Positive and Up", Color.GREEN);
Diff.DefineColor("Positive and Down", Color.DARK_GREEN);
Diff.DefineColor("Negative and Down", Color.RED);
Diff.DefineColor("Negative and Up", Color.DARK_RED);
Diff.AssignValueColor(if Diff >= 0
        then if Diff > Diff[1] then Diff.Color("Positive and Up") else Diff.Color("Positive and Down")
          else if Diff < Diff[1] then Diff.Color("Negative and Down") else Diff.Color("Negative and Up"));
Diff.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Diff.SetLineWeight(3);
Level.SetDefaultColor(GetColor(3));



Value.AssignValueColor(if Value > Avg and DrawCloud == yes
                              then Color.GREEN
                            else if Value < Avg and DrawCloud == yes
                              then Color.RED
                          else Color.CURRENT);

addLabel(displayValueAvgLabels, " Value = "  + Value + "  ", if Value > Avg then Color.GREEN else Color.RED);

addLabel(displayValueAvgLabels, " Avg = "  + avg + "  ", if Avg > Value then Color.RED else Color.GREEN);

addLabel(displayValueAvgLabels, " Value = "  + Value * 1000 + "  <Thousandths>. ", if Value > Avg then Color.GREEN else Color.RED);

addLabel(displayValueAvgLabels, " Avg = "  + avg * 1000+ "  <Thousandths>. ", if Avg > Value then Color.RED else Color.GREEN);
 
Last edited by a moderator:

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
390 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top