never_noob
New member
I am trying to code up a strategy that breaks the days of the month into trading days (e.g. first trading day is TD1, second is TD2, etc) but I also need to know which day is the last day of the month for strategies that require closing at end of month. For whatever reason, I am finding this impossible to do in ThinkScript; it seems like there is some weird way that TS handles the date math that is not letting me find the last trading day. I can find the current trading day very easy, and my plan for finding the last trading day was to calculate the first day of the following month (e.g. next month is Nov 1, 2022, which is 20221101) and then set a variable "lastday" as true whenever CountTradingDays between the current day and the first day of the next month is 0 (or 1, depending on whether it counts inclusive or not). Before that, I need to get the count of trading days until the first day of the next month. It seems easy enough, but it's not working no matter what I try.
What is bizarre is I can write code to calculate the first day of the next month in YYYYMMDD format. So, for instance, it's easy to get TS to calculate the first day of next month as 20221101. That works fine. If I use CountTradingDays to count the days between today and the calculation to get that number, it doesn't work. But if I hardcode in that number, it works just fine. This makes no sense, as I've printed both the output of the calculation and the number itself to a label and both are identical. I've been banging my head against the wall for hours. Anyone have any ideas?
However, if I use the same line of code, but hardcode in "Startnextmonth", it works perfectly fine and correctly identifies the number of remaining trade days
Then I make a label to test the output of "startnextmonth" using the code from the first block of code, I show that it displays 20221101 just fine - so its the exact same value as the one I'm hardcoding.
I am at a complete loss here.
What is bizarre is I can write code to calculate the first day of the next month in YYYYMMDD format. So, for instance, it's easy to get TS to calculate the first day of next month as 20221101. That works fine. If I use CountTradingDays to count the days between today and the calculation to get that number, it doesn't work. But if I hardcode in that number, it works just fine. This makes no sense, as I've printed both the output of the calculation and the number itself to a label and both are identical. I've been banging my head against the wall for hours. Anyone have any ideas?
def currentday = GetYYYYMMDD();
def thismonth = GetMonth();
def nextmonth = if thismonth ==12 then 1 else thismonth+1;
def thisyear = GetYear();
def startofmonth = ((10000 * thisyear) + (100 * thismonth) + 1);
def startnextmonth = ((10000 * thisyear) + (100 * nextmonth) + 1);
def daysleft = CountTradingDays(GetYYYYMMDD(), startnextmonth);
AddLabel (yes, daysleft, color.GREEN);
#Trying to display this ^^^^ as a label just to even test the output of 'daysleft' throws an error!
However, if I use the same line of code, but hardcode in "Startnextmonth", it works perfectly fine and correctly identifies the number of remaining trade days
def daysleft = CountTradingDays(GetYYYYMMDD(), 20221101);
#this works fine!
Then I make a label to test the output of "startnextmonth" using the code from the first block of code, I show that it displays 20221101 just fine - so its the exact same value as the one I'm hardcoding.
I am at a complete loss here.