Date Math and display the date on the chart

gravity2726

Member
VIP
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 =...

SleepyZ

Well-known member
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);
 

SleepyZ

Well-known member
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:

SleepyZ

Well-known member
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.
 

bmn

Member
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?
 

gravity2726

Member
VIP
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.
 

gravity2726

Member
VIP
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);
 

gravity2726

Member
VIP
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
 

bmn

Member
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)?
 

SleepyZ

Well-known member
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);
 

SleepyZ

Well-known member
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

Top