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...
Here is an example study

input Start = 20180609;
def Days = CountTradingDays(Start, GetYYYYMMDD());

AddLabel(yes, Days + " Trading days since " + AsPrice(Start), Color.Light_Green);
 
Thanks!

What I need is something like

def Days = CountTradingDays(GetYYYYMMDD(), * TODAY * );

But I don't know how to get "TODAY" or the day of the last bar on the chart.
 
Thanks for helping. In thinkscript, current bar and last bar are different.

def yearstart = GetYear() * 10000 + 101;
plot count = CountTradingDays(yearStart, GetYYYYMMDD());

You will see GetYYYYMMDD() refers to the current bar that thinkscript is processing, not the last bar (as of today).

Anyway, I changed my approach to bypass the need on this for now. :)
 
Hello. Can anyone in the community help me modify the thinkscript below to return the current number of trading days (vs calendar days) or the number of bars “after” a predefined low, and assign a desired color to the label?

declare upper;
input anchor_date = 20201030;
def num_days = DaysFromDate(anchor_date);
AddLabel(yes, (DaysFromDate(anchor_date) + " TD from Low"));

Given the above, today (11/19/20) is 14 trading days or bars “after” the 10/30/20 low.
Thanks very much!
 
What is the script (or command) for counting consecutive bars?

I have a custom study I’m trying to find a way to make a chart label that shows how many consecutive red/green bars it has on an intraday chart. “Green 3, Red 0”
 
your original questions was
"What is the script (or command) for counting consecutive bars?"
i sent you an example of the code

now you are requesting a different request and requesting:
a chart label. Ultimately looking for a watchlist column with a red/green box with the consecutive bar count for the zscore volume study posted above.
im not familiar with zscore and i dont see any prior mentions on this thread stating the word "zscore" nor "volume" prior to your post#11.
 
your original questions was
"What is the script (or command) for counting consecutive bars?"
i sent you an example of the code

now you are requesting a different request and requesting:
a chart label. Ultimately looking for a watchlist column with a red/green box with the consecutive bar count for the zscore volume study posted above.
im not familiar with zscore and i dont see any prior mentions on this thread stating the word "zscore" nor "volume" prior to your post#11.
That's my fault. I believe I had the original code posted on another thread. Here is what I was referencing. Sorry for the confusion.

Code:
#Computes and plots the Zscore
#Provided courtesy of ThetaTrend.com
#Feel free to share the code with a link back to thetatrend.com

declare lower;

input price = close;
input length = 20;
input ZavgLength = 20;

#Initialize values
def oneSD = StDev(price, length);
def avgClose = SimpleMovingAvg(price, length);
def ofoneSD = oneSD * price[1];
def Zscorevalue = ((price - avgClose) / oneSD);
def avgZv = Average(Zscorevalue, 20);

#Compute and plot Z-Score
plot Zscore = ((price - avgClose) / oneSD);
Zscore.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Zscore.SetLineWeight(2);
Zscore.AssignValueColor(if Zscore > 0 then Color.GREEN else Color.RED);

plot avgZscore = Average(Zscorevalue, ZavgLength);
avgZscore.SetPaintingStrategy(PaintingStrategy.LINE);

#This is an optional plot that will display the momentum of the Z-Score average
#plot momZAvg = (avgZv-avgZv[5]);

#Plot zero line and extreme bands
plot zero = 0;
plot two = 2;
plot negtwo = -2;
plot three = 3;
plot negthree = -3;

zero.SetDefaultColor(Color.BLACK);
 
@ProfessorAR15 You can count the bars and have them red or green with this code, just add it to the bottom of your zscore script. If you found this post useful be sure to give a thumbs up.

Code:
def nan = double.nan;
def barUp = Zscore > 0;
def barDown = Zscore < 0;
def barUpCount = CompoundValue(1, if barUp then barUpCount[1] + 1 else 0, 0);
def pBarUpCount = if barUpCount > 0 then barUpCount else nan;
def barDownCount = CompoundValue(1, if barDown then barDownCount[1] - 1 else 0, 0);
def pBarDownCount = if barDownCount < 0 then barDownCount else nan;
AddLabel(yes, "Consecutive Bar Count: " +(" "+(Round(barDownCount + barUpCount, 1))), if barUpCount >= 1 then color.green else if barDownCount <= -1 then color.red else Color.gray);
 
Last edited:
@XeoNoX Another hero on this site. I have had a lot of people help me here, thank you again for your time. I tried working the code out myself, and I had a lot of these constraints, I just struggle with brackets, parenthesis, etc
 
Hello. Can anyone in the community help me modify the thinkscript below to return the current number of trading days (vs calendar days) or the number of bars “after” a predefined low, and assign a desired color to the label?

declare upper;
input anchor_date = 20201030;
def num_days = DaysFromDate(anchor_date);
AddLabel(yes, (DaysFromDate(anchor_date) + " TD from Low"));

Given the above, today (11/19/20) is 14 trading days or bars “after” the 10/30/20 low.
Thanks very much!
DID you go the response for this mate ? I am looking for the same
 
DID you go the response for this mate ? I am looking for the same

This uses counttradingdays function, which counts trading days from and including the dates in it. You can adjust that computation if you want to exclude either the from or to dates from the computation.

Ruby:
input anchor_date    = 20201030;
def last_trading_day = HighestAll(if !IsNaN(close) and IsNaN(close[-1]) then GetYYYYMMDD() else Double.NaN);
def num_days         = CountTradingDays(anchor_date, last_trading_day);
AddLabel(yes, (num_days + " TD from & including " + asprice(anchor_date)));
 
This uses counttradingdays function, which counts trading days from and including the dates in it. You can adjust that computation if you want to exclude either the from or to dates from the computation.
Thank you Bud......My actual question in the forum if you figure out let me know.
Dear friends,

From previous month I want to calculate two things No of trading days from High to low (or low to high whichever comes first) and same no. of calendar days. I am working on a project I want to finish this weekend.

For example : In August Spy have low of 300 on august 1st and high of 400 august 13th . Then the trading days will be 10 days and calendar days = 13 days..
 
Thank you Bud......My actual question in the forum if you figure out let me know.
Dear friends,

From previous month I want to calculate two things No of trading days from High to low (or low to high whichever comes first) and same no. of calendar days. I am working on a project I want to finish this weekend.

For example : In August Spy have low of 300 on august 1st and high of 400 august 13th . Then the trading days will be 10 days and calendar days = 13 days..

See if this helps

Capture.jpg
Ruby:
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 );
 

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
417 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