How to round values decimal places in chart labels?


Category:
0
0

Hello Pete,

 

I had a question how do we round values 2 decimal places in the display labels. For example in the volume code below .

 

 

def Pre = if SecondsFromTime(100) > 0 and
SecondsTillTime(0930) >= 0
then 1
else 0;
def isRollover = GetYYYYMMDD() != GetYYYYMMDD()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def vol = if isRollover and beforeStart then volume else if beforeStart then vol[1] + volume else Double.NaN;
def PreMarketVol = if IsNaN(vol) then PreMarketVol[1] else vol ;
def volumeforecast= (preMarketVol)*4;

AddLabel(yes, “Premarket Volume: ” + PreMarketVol, if PreMarketVol > 2510000 then Color.RED else Color.white);
AddLabel(yes, “Volume Forecast: ” + volumeforecast, if volumeforecast > 25100000 then Color.RED else Color.white);

Marked as spam
Posted by (Questions: 22, Answers: 63)
Asked on September 18, 2019 6:13 am
97 views
0
Private answer

Rounding values in a chart label. Already been asked and answered here:

https://www.hahn-tech.com/ans/output-value-only-to-2-decimal-places/

Edit: After receiving feedback from the author of this post in the comment section below I have some additional assistance:

The AddLabel() function takes three parameters. Get the full details here: https://toslc.thinkorswim.com/center/reference/thinkScript/Functions/Look---Feel/AddLabel.html

ALWAYS begin with the most basic code. Then add to it once you get the most basic version working. From the previous post I linked above, we have this as the example:

addlabel(yes, ” PP ” + astext(PP, Numberformat.TWO_DECIMAL_PLACES),color.MAGENTA);

Ok, PP is the variable from that previous code that displays the value in the label. Your code does not have this variable so you cannot include that in yours (that is one of two reasons your attempt produces an error). In place of PP, your code uses a variable named volumeforecast for the value. So your first functional version of the code should look like this:

AddLabel(yes, ”Volume Forecast: ” + AsText(volumeForecast, NumberFormat.TWO_DECIMAL_PLACES), Color.MAGENTA);

Once you get this working you can then add the if/then/else statement to dynamically set the color of the label. Do this by replacing "Color.MAGENTA" with your if/then/else statement.

From your original code, that color assignment is done using the following snippet:

if volumeforecast > 25100000 then Color.RED else Color.white

After we use that to replace Color.MAGENTA we end up with the following:

AddLabel(yes, ”Volume Forecast: ” + AsText(volumeForecast, NumberFormat.TWO_DECIMAL_PLACES), if volumeForecast > 25100000 then Color.RED else Color.WHITE);

Now that you see how this is done, I'll let you complete the other label in your code, which contains the exact same errors as this one.

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on September 18, 2019 9:18 am
0
How did I find this? By entering the term "decimal" in the search box.
( at September 18, 2019 9:19 am)
0
Currently getting an error with code def Pre = if SecondsFromTime(100) > 0 and SecondsTillTime(0930) >= 0 then 1 else 0; def isRollover = GetYYYYMMDD() != GetYYYYMMDD()[1]; def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD()); def vol = if isRollover and beforeStart then volume else if beforeStart then vol[1] + volume else Double.NaN; def PreMarketVol = if IsNaN(vol) then PreMarketVol[1] else vol ; def volumeforecast= (preMarketVol)*4; AddLabel(yes, “Premarket Volume: ” + PreMarketVol, if PreMarketVol > 2510000 then astext(PP, Numberformat.TWO_DECIMAL_PLACES) Color.RED else Color.white); AddLabel(yes, “Volume Forecast: ” + volumeforecast, if volumeforecast > 25100000 then astext(PP, Numberformat.TWO_DECIMAL_PLACES) Color.RED else Color.white);
( at September 18, 2019 1:50 pm)
0
I have updated my answer to this post to include an explanation of where you went wrong and how to correct one of the two labels.
( at September 18, 2019 2:39 pm)
0
Oh, I think I made a mistake. What I meant to is if its possible translate the volume as text, for example, if the volume calculation is 1,000,000, how can we display with the volume label as 1M or 1 million. If 1,500,000 is the volume then the label should say 1.5M. Sorry for the confusion, i should have thought this through more
( at September 18, 2019 10:39 pm)
0
In order to accommodate that you have to set rules to handle every single possible number of digits. So you would need nested if statements to cover 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000.... etc. Far beyond anything we would cover here in the Q&A forum.
( at September 19, 2019 9:13 am)