Date Math and display the date on the chart

For example :

to_date=20211008
X=10 days
Need to add 10 days to to_date and display the date on the chart;
If possible ; Even if the date is in the future
 
Solution
If I wanted to get get the date X-Trading Days in the past from today (excluding today), how can I go about doing that? I tried messing around with +/- etc, but no luck.

e.g. for today (20210910), I want to get the trading date 5 trading days ago (20210902 - due to 3 non-trading days in between), how would I achieve that?

This looks for the last trading day for the days you input the prior trading date.

Capture.jpg
Ruby:
def ymd      = GetYYYYMMDD();
def candles  = !isnan(close);
def capture  = candles and ymd != ymd[1];
def dayCount = CompoundValue(1, if capture then dayCount[1] + 1 else dayCount[1], 0);
def thisDay  = (HighestAll(dayCount) - dayCount) ;

input daysback  = 5;
def today = 1;
def last_trading_day =...

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

For example :

to_date=20211008
X=10 days
Need to add 10 days to to_date and display the date on the chart;
If possible ; Even if the date is in the future
This should help
Capture.jpg

Ruby:
input date = 20210909;

#Projection Date from date input to number of days in Projection-------------------
input Days = 60;

def dbn  = if GetYYYYMMDD() == date then BarNumber() else dbn[1];
def ndte = if BarNumber() == HighestAll(dbn) + Days then GetYYYYMMDD() else Double.NaN;
AddLabel(yes, "Date Input: " + AsPrice(date) + " + " + Days + " Days", Color.WHITE);

#Date Label Formatting--------------------------------------------------------------

def yyyymmdd = HighestAll(ndte);
def year = (Round(yyyymmdd / 10000, 0));
def Month = AbsValue(year - Round(yyyymmdd / 100, 0) / 100) * 100;
def Day = GetDayOfMonth(yyyymmdd);

AddLabel(yes, "Projected Trading Days Date: ", Color.YELLOW);
AddLabel(yes, Concat("Unformatted: ", AsPrice(yyyymmdd)), Color.WHITE);
AddLabel(yes, Concat("Formatted: " + Month + "/", Day + "/" + AsPrice(year)) , Color.WHITE);
 
No label is being shown on the chart. do I need to change anything?
Make sure your time axis in chart settings is set at least wide enough to display the future date. In this case that required at least a time axis setting of 90 on a Daily Chart,
 
Last edited:
Thank you my friend by being patient.....When you add the days it is adding the trading days..Is it possible to add calendar days. (I did 30 mins of research but cannot able to come close)
TOS is not friendly/functional with dates/times. The label I provided showed that it was a trading day computation. I do not know of a way to do it otherwise. Perhaps someone else might.
 
This should help
If I wanted to get get the date X-Trading Days in the past from today (excluding today), how can I go about doing that? I tried messing around with +/- etc, but no luck.

e.g. for today (20210910), I want to get the trading date 5 trading days ago (20210902 - due to 3 non-trading days in between), how would I achieve that?
 
If I wanted to get get the date X-Trading Days in the past from today (excluding today), how can I go about doing that? I tried messing around with +/- etc, but no luck.

e.g. for today (20210910), I want to get the trading date 5 trading days ago (20210902 - due to 3 non-trading days in between), how would I achieve that?
Use #4 post from @SleepyZ ...Change 60 to -5.....I am still looking for calendar days but no luck ; stuck with my project.
 
That is my post. It worked for me. Only change I need that code to do is calculate the calendar days instead of trading days.
input date = 20210910;
#Projection Date from date input to number of days in Projection-------------------
input Days = -5;

def dbn = if GetYYYYMMDD() == date then BarNumber() else dbn[1];
def ndte = if BarNumber() == HighestAll(dbn) + Days then GetYYYYMMDD() else Double.NaN;
AddLabel(yes, "Date Input: " + AsPrice(date) + " + " + Days + " Days", Color.WHITE);

#Date Label Formatting--------------------------------------------------------------

def yyyymmdd = HighestAll(ndte);
def year = (Round(yyyymmdd / 10000, 0));
def Month = AbsValue(year - Round(yyyymmdd / 100, 0) / 100) * 100;
def Day = GetDayOfMonth(yyyymmdd);

AddLabel(yes, "Projected Trading Days Date: ", Color.YELLOW);
AddLabel(yes, Concat("Unformatted: ", AsPrice(yyyymmdd))+"......", Color.WHITE);
AddLabel(yes, Concat("Formatted: " + Month + "/", Day + "/" + AsPrice(year)) +"......", Color.WHITE);
 
input date = 20210910;
#Projection Date from date input to number of days in Projection-------------------
input Days = -5;

def dbn = if GetYYYYMMDD() == date then BarNumber() else dbn[1];
def ndte = if BarNumber() == HighestAll(dbn) + Days then GetYYYYMMDD() else Double.NaN;
AddLabel(yes, "Date Input: " + AsPrice(date) + " + " + Days + " Days", Color.WHITE);

#Date Label Formatting--------------------------------------------------------------

def yyyymmdd = HighestAll(ndte);
def year = (Round(yyyymmdd / 10000, 0));
def Month = AbsValue(year - Round(yyyymmdd / 100, 0) / 100) * 100;
def Day = GetDayOfMonth(yyyymmdd);

AddLabel(yes, "Projected Trading Days Date: ", Color.YELLOW);
AddLabel(yes, Concat("Unformatted: ", AsPrice(yyyymmdd))+"......", Color.WHITE);
AddLabel(yes, Concat("Formatted: " + Month + "/", Day + "/" + AsPrice(year)) +"......", Color.WHITE);
This code should work ; Only change is make it today all the time
 
If I wanted to get get the date X-Trading Days in the past from today (excluding today), how can I go about doing that? I tried messing around with +/- etc, but no luck.

e.g. for today (20210910), I want to get the trading date 5 trading days ago (20210902 - due to 3 non-trading days in between), how would I achieve that?
@SleepyZ , would you be able to assist with this (calculate past Real Trading Date instead of future based on user input)?
 
Thank you my friend by being patient.....When you add the days it is adding the trading days..Is it possible to add calendar days. (I did 30 mins of research but cannot able to come close)
@gravity2726
See if this works in computing calendar days from a date. I have done some testing and it seems close.

Capture.jpg
Ruby:
# Calendar Days From Date Input
input days = 19;
input date = 20210912;
def yymmdd = date;
def year   = Round(date / 10000, 0);
def Month  = AbsValue(year - Round(date / 100, 0) / 100) * 100;
def Day    = GetDayOfMonth(date);

def daysinmonth  = if Month == 2
                   then 28
                   else if Month == 4 or Month == 6 or Month == 9 or Month == 11
                   then 30
                   else 31;

def newdays      = if Month then (Day + days) else newdays[1];

def month_       = if Between(newdays, 1, daysinmonth)
                   then Month
                   else if Between(newdays, 1, daysinmonth * 2)
                   then Month + 1                  
                   else if Between(newdays, 1, daysinmonth * 3)
                   then Month + 2
                   else if Between(newdays, 1, daysinmonth * 4)
                   then Month + 3
                   else month_[1];

def newmonth     = if month_ <= 13
                   then month_
                   else if month_ <= 25
                   then month_ - 12
                   else if month_ > 25
                   then month_ - 24
                   else newmonth[1];

def daysinmonth_ = if month_ == 2
                   then 28
                   else if month_ == 4 or month_ == 6 or month_ == 9 or month_ == 11
                   then 30
                   else 31;

def newyear      = if month_ < 13
                   then year
                   else if month_ < 24
                   then year + 1
                   else if month_ > 24
                   then year + 2
                   else newyear[1];
def newday       = if (newdays - daysinmonth) == 1
                   then 1
                   else if (newdays - daysinmonth) <= 0
                   then newdays
                   else if (newdays - daysinmonth * 2) <= 0
                   then newdays - (daysinmonth * 1)
                   else if (newdays - daysinmonth * 3) <= 0
                   then newdays - (daysinmonth * 2)
                   else if (newdays - daysinmonth * 4) <= 0
                   then newdays - (daysinmonth * 3)
                   else newday[1];


AddLabel(1, "Date Input: " + Month + "/" + Day + "/" + AsPrice(year), Color.YELLOW);
AddLabel(1, "Calendar Days Added: " + days, Color.WHITE);
AddLabel(1, "New Calendar Date: " +  (if daysinmonth_ == 2 and newday[1] == 29
                                      then 31 else newmonth) + "/"
                                    + newday + "/"
                                    + AsPrice(newyear), Color.YELLOW);

input debug = no;
AddLabel(debug, " " + month_ + " " + newdays + " " + daysinmonth + " " + newyear, Color.WHITE);
 
If I wanted to get get the date X-Trading Days in the past from today (excluding today), how can I go about doing that? I tried messing around with +/- etc, but no luck.

e.g. for today (20210910), I want to get the trading date 5 trading days ago (20210902 - due to 3 non-trading days in between), how would I achieve that?

This looks for the last trading day for the days you input the prior trading date.

Capture.jpg
Ruby:
def ymd      = GetYYYYMMDD();
def candles  = !isnan(close);
def capture  = candles and ymd != ymd[1];
def dayCount = CompoundValue(1, if capture then dayCount[1] + 1 else dayCount[1], 0);
def thisDay  = (HighestAll(dayCount) - dayCount) ;

input daysback  = 5;
def today = 1;
def last_trading_day = HighestAll(if !IsNaN(close) and IsNaN(close[-1]) then GetYYYYMMDD() else Double.NaN);
def TD_daysback = if thisday == daysback then getyyyYMMDD() else TD_daysback[1];
addlabel(1, "Last Trading Day: " + asprice(last_trading_day) + " Trading Date " + daysback + " ago: " + asprice(TD_daysback), color.white);
 
  • Like
Reactions: bmn
Solution

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
362 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