Formatting dates in chart labels


Category:
0
0

Hi, Pete: is there a way to change how date labels appear?  here’s a bit of code:

# Prior date of indicator change
def prior_X_Up_Date = if X_Status == 1 AND X_Status[1] == 0 then GetYYYYMMDD() else prior_X_Up_Date[1];
def prior_X_Down_Date = if X_Status == 0 AND X_Status[1] == 1 then GetYYYYMMDD() else prior_X_Down_Date[1];

The output from this is, for example: 20211216.00.  It’d be much easier for my wee brain to sort it out if it read: 2021_12_16.  Or even better: 12_16_2021.  Or similar.

Possible?

Thanks.  Keep up the awesome work!

Marked as spam
Posted by (Questions: 6, Answers: 3)
Asked on January 11, 2021 8:10 am
609 views
1
Private answer

The following solution has been submitted to me via email. The person presenting the solution was not able to log in and post the solution so I am doing it on their behalf.

def labelDate = GetYYYYMMDD();
def yy = Round((labelDate / 10000 - 2000), 0); # yy = 21
def yyyy = Round(labelDate / 10000, 0); # yyyy = 2021
def mmdd = labelDate - 10000 * yyyy;
def mm = Round(mmdd / 100, 0);
def dd = mmdd - mm * 100;
AddLabel(1, "Label Date = " + mm + "/" + dd + "/20" + yy, Color.CYAN);

Marked as spam
Posted by (Questions: 37, Answers: 4086)
Answered on May 4, 2022 4:04 pm
0
Private answer

There was a typo in your question title so I had to correct that. While making the correction I also decided to replace the title with one that more closes describes your request. These changes also break the original URL of the question you posted. So everyone that has subscribed to receive notifications of new posts will not be able to view your new question. Please be very careful when forming your question titles. They are probably the most important detail to include because it impacts the ability of the rest of our viewers to search for and locate your post.

Now on to your question. This is yet one more variation of a question that has been asked repeatedly in this forum. How to display a user friendly date value in a chart label or a watchlist column. There are no easy solutions. The Thinkorswim language lacks any of the functions or data objects available in other languages to work with strings and dates. We cannot parse strings,  nor can we format date objects.

The following link provides the full list of functions available for working with dates on Thinkorswim:

https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Date---Time

You may find that you can concatenate several different data functions to assemble something that loosely resembles what you have requested. But it's not simple and would me much longer than the brief 15 minutes I allow for free solutions in the Q&A forum.

Here is one example of concatenating the year with the month:

AddLabel(yes, Concat(GetYear(), Concat("-", GetMonth())), Color.WHITE);

It is far from perfect, the year will include a comma. Once again, we cannot do anything about that in Thinkorswim because it lacks any string parsing functions. You can apply a function named "AsText()", however this will only remove the comma and add a decimal point and 2 zeros to the end of the year.

This is "as good as it gets". Until the developers of Thinkorswim are sufficiently funded to finish building out their scripting language. And that will never happen if none of the users of Thinkorswim take the time to submit feature requests and demand important functionality such as parsing strings and formatting date/time values.

Marked as spam
Posted by (Questions: 37, Answers: 4086)
Answered on January 11, 2021 8:49 am