Lookback Date for label

np1234

New member
Hi,
I am new to TOS scripts. I am looking for date in lable for - from todays to 5th/7th day for week, 28/29/30/31 for month, 252/253(leap year) for year. For ex: if today is 11/30/21 (Tue) than for last 7th day or 5th trading day(Tue) day 11/23/21, for 30thday or 20th trading day -11/01/21, for 365day or 252th tradingday - 11-30-20.
Does anyone have idea how to calculate and display this as label?
 
Solution
Hi,
I am new to TOS scripts. I am looking for date in lable for - from todays to 5th/7th day for week, 28/29/30/31 for month, 252/253(leap year) for year. For ex: if today is 11/30/21 (Tue) than for last 7th day or 5th trading day(Tue) day 11/23/21, for 30thday or 20th trading day -11/01/21, for 365day or 252th tradingday - 11-30-20.
Does anyone have idea how to calculate and display this as label?

This will only work on a chart with enough days, in this case a 1yr or greater Daily Chart, are displayed on the chart you are viewing.

Also, I did 2 formats, one with yyyymmdd and the other mm/dd/yyyy that you can choose at the input format. This way you can see how this can be done.

Lastly, since the code uses trading days, I...
Hi,
I am new to TOS scripts. I am looking for date in lable for - from todays to 5th/7th day for week, 28/29/30/31 for month, 252/253(leap year) for year. For ex: if today is 11/30/21 (Tue) than for last 7th day or 5th trading day(Tue) day 11/23/21, for 30thday or 20th trading day -11/01/21, for 365day or 252th tradingday - 11-30-20.
Does anyone have idea how to calculate and display this as label?

This will only work on a chart with enough days, in this case a 1yr or greater Daily Chart, are displayed on the chart you are viewing.

Also, I did 2 formats, one with yyyymmdd and the other mm/dd/yyyy that you can choose at the input format. This way you can see how this can be done.

Lastly, since the code uses trading days, I included a script that counts the bars in reverse order so you can see where the trading days back are. You need to subtract 1 to exclude today as you explained in your examples.

Capture.jpg
Ruby:
input format = {default yyyymmdd, "mm/dd/yyyy"};
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 tradingdaysback  = 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 == tradingdaysback then GetYYYYMMDD() else TD_daysback[1];

#Format yyyyymmdd -------------------------------------------------------------
AddLabel(format == format.yyyymmdd, "Last Trading Day: " + AsPrice(last_trading_day) + " Trading Date " + tradingdaysback + " ago: " + AsPrice(TD_daysback), Color.WHITE);
AddLabel(format == format.yyyymmdd, "Last Trading Date " + tradingdaysback + " ago: " + AsPrice(TD_daysback), Color.WHITE);

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

#Format mm/dd/yyyy-------------------------------------------------------------
AddLabel(format == format."mm/dd/yyyy", "Last Trading Day: " + Month + "/" + Day + "/" + AsPrice(year), Color.YELLOW);

def year1 = (Round(TD_daysback / 10000, 0));
def Month1 = AbsValue(year1 - Round(TD_daysback / 100, 0) / 100) * 100;
def Day1 = GetDayOfMonth(HighestAll(TD_daysback));
#Format mm/dd/yyyy-------------------------------------------------------------
AddLabel(format == format."mm/dd/yyyy", "Last Trading Date " + tradingdaysback + " ago: " +  Month1 + "/" + Day1 + "/" + AsPrice(year1), Color.YELLOW);

input tradingdaysback2  = 20;
def TD_daysback2 = if thisDay == tradingdaysback2 then GetYYYYMMDD() else TD_daysback2[1];

#Format yyyyymmdd -------------------------------------------------------------
AddLabel(format == format.yyyymmdd, " Trading Date " + tradingdaysback2 + " ago: " + AsPrice(TD_daysback2), Color.WHITE);

def year2 = (Round(TD_daysback2 / 10000, 0));
def Month2 = AbsValue(year2 - Round(TD_daysback2 / 100, 0) / 100) * 100;
def Day2 = GetDayOfMonth(HighestAll(TD_daysback2));

#Format mm/dd/yyyy-------------------------------------------------------------
AddLabel(format == format."mm/dd/yyyy", "Last Trading Date " + tradingdaysback2 + " ago: " +  Month2 + "/" + Day2 + "/" + AsPrice(year2), Color.YELLOW);

input tradingdaysback3  = 252;
def TD_daysback3 = if thisDay == tradingdaysback3 then GetYYYYMMDD() else TD_daysback3[1];

#Format yyyyymmdd -------------------------------------------------------------
AddLabel(format == format.yyyymmdd, " Trading Date " + tradingdaysback3 + " ago: " + AsPrice(TD_daysback3), Color.WHITE);

def year3 = (Round(TD_daysback3 / 10000, 0));
def Month3 = AbsValue(year3 - Round(TD_daysback3 / 100, 0) / 100) * 100;
def Day3 = GetDayOfMonth(HighestAll(TD_daysback3));

#Format mm/dd/yyyy-------------------------------------------------------------
AddLabel(format == format."mm/dd/yyyy", "Last Trading Date " + tradingdaysback3 + " ago: " +  Month3 + "/" + Day3 + "/" + AsPrice(year3), Color.YELLOW);

Ruby:
def hbn = if isnan(close[-1]) and !isnan(close) then barnumber() else double.nan;
addlabel(1,hbn);
def counter = compoundValue(1,if barnumber()==1 then highestall(hbn) else if counter[1]>1 then counter[1]-1 else 0, highestall(hbn));
plot bn = counter;
bn.setpaintingStrategy(paintingStrategy.VALUES_BELOW);
 
Last edited:
Solution
This will only work on a chart with enough days, in this case a 1yr or greater Daily Chart, are displayed on the chart you are viewing.

Also, I did 2 formats, one with yyyymmdd and the other mm/dd/yyyy that you can choose at the input format. This way you can see how this can be done.

Lastly, since the code uses trading days, I included a script that counts the bars in reverse order so you can see where the trading days back are. You need to subtract 1 to exclude today as you explained in your examples.
thanks for posting, will add to my collection of date formulas.

here is a different way to do a reverse counter

Code:
def hbn = if isnan(close[-1]) and !isnan(close) then barnumber() else double.nan;
def revcounter = highestall(hbn) - barnumber() + 1;
#
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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