Count trading days from the current bar to today?

Hawkeye

New member
I want to use thinkscript to count the number of trading days from the current bar till today but could not figure out how. Any suggestions? Thank you very much!
 
Solution
I agree the above code works...The code doesn't work when I use X_date and Y_date ;basically calculating the number of days from high to low.
def num_trading_days = absValue(CountTradingDays(X_Date, Y_Date));
def num_calendar_days = absvalue(DaysFromDate(X_Date) - DaysFromDate(Y_Date) + 1);

Ok, I see what you were asking. Here is a revision that I think does the original and the new request. I have reorganized it to put anchor info first, then high/low. The minimum date of high/low will appear before the latest.

Same minimum chart days have to at least cover the trading days, but if that does not work try covering the calendar days .

Capture.jpg
Ruby:
input anchor_date     = 20210801;
input to_date         = 20210831;
input...
See if this helps
Thank you my friend it is great help almost close what I am looking for the following :
  1. Start_month=20210801
  2. End_moth=20210831
  3. X= high value between start_month to end month
  4. Y= low value between start_month to end month
  5. X_Date=date at which high occurred between start_month to end month
  6. Y_Date=date at which low occurred between start_month to end month
 

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

Thank you my friend it is great help almost close what I am looking for the following :
  1. Start_month=20210801
  2. End_moth=20210831
  3. X= high value between start_month to end month
  4. Y= low value between start_month to end month
  5. X_Date=date at which high occurred between start_month to end month
  6. Y_Date=date at which low occurred between start_month to end month

Try this

Ruby:
input anchor_date     = 20210801;
input to_date         = 20210831;
input show_label      = yes;
def Xamt    = if GetYYYYMMDD()[1] < anchor_date and GetYYYYMMDD() >= anchor_date
              then high
              else if Between(GetYYYYMMDD(), anchor_date, to_date)
              then Max(high, Xamt[1])
              else double.nan;
def X       = highestall(Xamt);
def X_Date  = if high == X then GetYYYYMMDD() else X_Date[1];

AddLabel(show_label, "X: " + astext(X) + "  X_Date: " + AsPrice(X_Date), Color.WHITE);

def Yamt    = if GetYYYYMMDD()[1] < anchor_date and GetYYYYMMDD() >= anchor_date
              then low
              else if Between(GetYYYYMMDD(), anchor_date, to_date)
              then Min(low, Yamt[1])
              else double.nan;
def Y       = lowestall(Yamt);
def Y_Date  = if low == Y then GetYYYYMMDD() else Y_Date[1];

AddLabel(show_label, "Y: " + astext(Y) + "  Y_Date: " + AsPrice(Y_Date), Color.WHITE);
 
Mate found that your not in discord :( ....But the question is why am not able to calucalte the difference between X-date and Y-date using the following code. And when I calculate the difference can keep the absolute difference...I mean low - high sometimes will be negative I want to keep it positive.

input anchor_date = 20210801;
input to_date = 20210813;
def num_trading_days = CountTradingDays(anchor_date, to_date);
def num_calendar_days = DaysFromDate(anchor_date) - DaysFromDate(to_date) + 1;
AddLabel(yes, "TDays: " + num_trading_days +
" | CDays: " + num_calendar_days +
" - from & including ... " + AsPrice(anchor_date) + " to " + AsPrice(to_date), color.white );
 
Mate found that your not in discord :( ....But the question is why am not able to calucalte the difference between X-date and Y-date using the following code. And when I calculate the difference can keep the absolute difference...I mean low - high sometimes will be negative I want to keep it positive.

input anchor_date = 20210801;
input to_date = 20210813;
def num_trading_days = CountTradingDays(anchor_date, to_date);
def num_calendar_days = DaysFromDate(anchor_date) - DaysFromDate(to_date) + 1;
AddLabel(yes, "TDays: " + num_trading_days +
" | CDays: " + num_calendar_days +
" - from & including ... " + AsPrice(anchor_date) + " to " + AsPrice(to_date), color.white );
The following always has to be countradingdays(earliest date, latest date); format
def num_trading_days = CountTradingDays(anchor_date, to_date);

The following can be forced to be positive by use of absvalue as shown no matter which is put first
def num_calendar_days = absvalue(DaysFromDate(anchor_date) - DaysFromDate(to_date) + 1);
 
The following always has to be countradingdays(earliest date, latest date); format
def num_trading_days = CountTradingDays(anchor_date, to_date);

The following can be forced to be positive by use of absvalue as shown no matter which is put first
def num_calendar_days = absvalue(DaysFromDate(anchor_date) - DaysFromDate(to_date) + 1);
Thank you Sleepz...Absvalue is helpful....The error I am getting is from the previous code where we calculated the X_date and Y_date. When I apply the same step from above it is showing an error;
 
Thank you Sleepz...Absvalue is helpful....The error I am getting is from the previous code where we calculated the X_date and Y_date. When I apply the same step from above it is showing an error;
You will need to give my more information. I need something that shows the error on a chart with the code you used and the data you expected to help you.
 
You will need to give my more information. I need something that shows the error on a chart with the code you used and the data you expected to help you.

input anchor_date = 20210801;
input to_date = 20210831;
input show_label = yes;
def Xamt = if GetYYYYMMDD()[1] < anchor_date and GetYYYYMMDD() >= anchor_date
then high
else if Between(GetYYYYMMDD(), anchor_date, to_date)
then Max(high, Xamt[1])
else double.nan;
def X = highestall(Xamt);
def Yamt = if GetYYYYMMDD()[1] < anchor_date and GetYYYYMMDD() >= anchor_date
then low
else if Between(GetYYYYMMDD(), anchor_date, to_date)
then Min(low, Yamt[1])
else double.nan;
def Y = lowestall(Yamt);

def X_Date = if high == X then GetYYYYMMDD() else X_Date[1];
def Y_Date = if low == Y then GetYYYYMMDD() else Y_Date[1];

/THIS DIDNT WORK As it doesn't produce the last label***/
def num_trading_days = absvalue(CountTradingDays(X_date, Y_date));
def num_calendar_days = absvalue(DaysFromDate(X_date) - DaysFromDate(Y_date) + 1);
/***************************************/
AddLabel(show_label, "X: " + astext(X) + " X_Date: " + AsPrice(X_Date), Color.WHITE);
AddLabel(show_label, "Y: " + astext(Y) + " Y_Date: " + AsPrice(Y_Date), Color.WHITE);

/THIS IS NOT popping up*/
AddLabel(yes, "TDays: " + num_trading_days +
" | CDays: " + num_calendar_days +
" - from & including ... " + AsPrice(anchor_date) + " to " + AsPrice(to_date), color.white );
 
input anchor_date = 20210801;
input to_date = 20210831;
input show_label = yes;
def Xamt = if GetYYYYMMDD()[1] < anchor_date and GetYYYYMMDD() >= anchor_date
then high
else if Between(GetYYYYMMDD(), anchor_date, to_date)
then Max(high, Xamt[1])
else double.nan;
def X = highestall(Xamt);
def Yamt = if GetYYYYMMDD()[1] < anchor_date and GetYYYYMMDD() >= anchor_date
then low
else if Between(GetYYYYMMDD(), anchor_date, to_date)
then Min(low, Yamt[1])
else double.nan;
def Y = lowestall(Yamt);

def X_Date = if high == X then GetYYYYMMDD() else X_Date[1];
def Y_Date = if low == Y then GetYYYYMMDD() else Y_Date[1];

/THIS DIDNT WORK As it doesn't produce the last label***/
def num_trading_days = absvalue(CountTradingDays(X_date, Y_date));
def num_calendar_days = absvalue(DaysFromDate(X_date) - DaysFromDate(Y_date) + 1);
/***************************************/
AddLabel(show_label, "X: " + astext(X) + " X_Date: " + AsPrice(X_Date), Color.WHITE);
AddLabel(show_label, "Y: " + astext(Y) + " Y_Date: " + AsPrice(Y_Date), Color.WHITE);

/THIS IS NOT popping up*/
AddLabel(yes, "TDays: " + num_trading_days +
" | CDays: " + num_calendar_days +
" - from & including ... " + AsPrice(anchor_date) + " to " + AsPrice(to_date), color.white );
I do not know what symbol or timeframe you are having problems with the above code. I combined the code I provided you and it seems to be working.

If you are still having problems, it could be because you must be using at least a chart of the number of trading days to get the "X/Y" code to work. TOS, other than with most built-in functions (eg: counttradingdays, daysfromdate) seems to only work on custom codes (eg: "X, X_Date, Y, Y_Date) if the data required for these is present in chart being displayed (eg: 22 trading days will require a chart likely set at least 30 days of data)..

Here is a chart of JBLU set at 15min, 30 days, and everything appears ok

Capture.jpg


Here is the same chart of JBLU set at 15min, 10 days, and there are problems with the custom code (Y and Y-Date), but not the built-in functions

Capture.jpg


Here is the combined code used in both of the above.

Ruby:
input anchor_date     = 20210801;
input to_date         = 20210831;
input show_label      = yes;
def Xamt    = if GetYYYYMMDD()[1] < anchor_date and GetYYYYMMDD() >= anchor_date
              then high
              else if Between(GetYYYYMMDD(), anchor_date, to_date)
              then Max(high, Xamt[1])
              else double.nan;
def X       = highestall(Xamt);
def X_Date  = if high == X then GetYYYYMMDD() else X_Date[1];

AddLabel(show_label, "X: " + astext(X) + "  X_Date: " + AsPrice(X_Date), Color.WHITE);

def Yamt    = if GetYYYYMMDD()[1] < anchor_date and GetYYYYMMDD() >= anchor_date
              then low
              else if Between(GetYYYYMMDD(), anchor_date, to_date)
              then Min(low, Yamt[1])
              else double.nan;
def Y       = lowestall(Yamt);
def Y_Date  = if low == Y then GetYYYYMMDD() else Y_Date[1];

AddLabel(show_label, "Y: " + astext(Y) + "  Y_Date: " + AsPrice(Y_Date), Color.WHITE);


def num_trading_days  = absValue(CountTradingDays(anchor_date, to_date));
def num_calendar_days = absvalue(DaysFromDate(anchor_date) - DaysFromDate(to_date) + 1);
AddLabel(show_label, "TDays: " + num_trading_days  +
              " | CDays: " + num_calendar_days +
              " - from & including ... " + AsPrice(anchor_date) + " to " + AsPrice(to_date), color.white );
 
I do not know what symbol or timeframe you are having problems with the above code. I combined the code I provided you and it seems to be working.

If you are still having problems, it could be because you must be using at least a chart of the number of trading days to get the "X/Y" code to work. TOS, other than with most built-in functions (eg: counttradingdays, daysfromdate) seems to only work on custom codes (eg: "X, X_Date, Y, Y_Date) if the data required for these is present in chart being displayed (eg: 22 trading days will require a chart likely set at least 30 days of data)..

Here is a chart of JBLU set at 15min, 30 days, and everything appears ok

Capture.jpg


Here is the same chart of JBLU set at 15min, 10 days, and there are problems with the custom code (Y and Y-Date), but not the built-in functions

Capture.jpg


Here is the combined code used in both of the above.
I agree the above code works...The code doesn't work when I use X_date and Y_date ;basically calculating the number of days from high to low.
def num_trading_days = absValue(CountTradingDays(X_Date, Y_Date));
def num_calendar_days = absvalue(DaysFromDate(X_Date) - DaysFromDate(Y_Date) + 1);
 
I agree the above code works...The code doesn't work when I use X_date and Y_date ;basically calculating the number of days from high to low.
def num_trading_days = absValue(CountTradingDays(X_Date, Y_Date));
def num_calendar_days = absvalue(DaysFromDate(X_Date) - DaysFromDate(Y_Date) + 1);

Ok, I see what you were asking. Here is a revision that I think does the original and the new request. I have reorganized it to put anchor info first, then high/low. The minimum date of high/low will appear before the latest.

Same minimum chart days have to at least cover the trading days, but if that does not work try covering the calendar days .

Capture.jpg
Ruby:
input anchor_date     = 20210801;
input to_date         = 20210831;
input show_label      = yes;

AddLabel(show_label, "Anchor", Color.YELLOW);
AddLabel(show_label, AsPrice(anchor_date) + " to " + AsPrice(to_date), Color.YELLOW);

def num_trading_days  = AbsValue(CountTradingDays(anchor_date, to_date));
def num_calendar_days = AbsValue(DaysFromDate(anchor_date) - DaysFromDate(to_date) + 1);

AddLabel(show_label, "TDays: " + num_trading_days  +
              " | CDays: " + num_calendar_days
              , Color.YELLOW );

#X/Y (High/Low)-----------------------------------------
AddLabel(show_label, "H/L", Color.WHITE);
def Xamt    = if GetYYYYMMDD()[1] < anchor_date and GetYYYYMMDD() >= anchor_date
              then high(period=aggregationPeriod.DAY)
              else if Between(GetYYYYMMDD(), anchor_date, to_date)
              then Max(high(period=aggregationPeriod.DAY), Xamt[1])
              else Double.NaN;
def X       = HighestAll(Xamt);
def X_Date  = HighestAll(if high(period=aggregationPeriod.DAY) == X then GetYYYYMMDD() else 0);

def Yamt    = if GetYYYYMMDD()[1] < anchor_date and GetYYYYMMDD() >= anchor_date
              then low(period=aggregationPeriod.DAY)
              else if Between(GetYYYYMMDD(), anchor_date, to_date)
              then Min(low(period=aggregationPeriod.DAY), Yamt[1])
              else Double.NaN;
def Y       = LowestAll(Yamt);
def Y_Date  = highestAll(if low(period=aggregationPeriod.DAY)  == Y then GetYYYYMMDD() else double.nan);

def min_date =  X_date==Min(Y_Date, X_Date);
AddLabel(show_label, if min_date==0 then "L: " + AsText(Y) + "  Y_Date: " + AsPrice(Y_Date) else "H: " + AsText(X) + "  H_Date: " + AsPrice(X_Date), Color.WHITE);
AddLabel(show_label, if min_date==0 then "H: " + AsText(X) + "  X_Date: " + AsPrice(X_Date) else "L: " + AsText(Y) + "  L_Date: " + AsPrice(Y_Date), Color.WHITE);

def num_trading_days1  = AbsValue(CountTradingDays(Min(Y_Date, X_Date), Max(Y_Date, X_Date)));
def num_calendar_days1 = AbsValue(DaysFromDate(Min(Y_Date, X_Date)) - DaysFromDate(Max(Y_Date, X_Date)) + 1);

AddLabel(show_label, "TDays: " + num_trading_days1  +
              " | CDays: " + num_calendar_days1
              , Color.WHITE );
 
Solution
I am not finding exactly what I was looking for here. I am a complete moron when it comes to scripting and have been trying to learn. What I am trying to do is get an exact count of how many bars (candles) there are from the Previous Day's open to just before the Current Day's open and include all pre and post market candles as well. I need this number for a calculation, but since the number of bars in the after hours trading session is changing I need to get a counter that would work on any chart so that it can be referenced for the calculations.
 
I am not finding exactly what I was looking for here. I am a complete moron when it comes to scripting and have been trying to learn. What I am trying to do is get an exact count of how many bars (candles) there are from the Previous Day's open to just before the Current Day's open and include all pre and post market candles as well. I need this number for a calculation, but since the number of bars in the after hours trading session is changing I need to get a counter that would work on any chart so that it can be referenced for the calculations.

The following should provide the count you want as highestall(count).

Ruby:
def rthstart = GetTime() crosses above RegularTradingStart(GetYYYYMMDD());

def prevopen = if GetDay() == GetLastDay() - 1 and rthstart
               then open
               else prevopen[1];
def xcount   = if GetDay() == GetLastDay() - 1 and rthstart
               then 1
               else if GetDay() == GetLastDay() and
                       GetTime() >= RegularTradingStart(GetYYYYMMDD())
               then 0
               else xcount[1] + 1;
plot count   = xcount;
count.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);
AddLabel(1, "Prev Open: " + prevopen + " | Count to Today's Open: " + highestall(count) + " bars", color.yellow);
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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