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!
 

tomsk

Well-known member
VIP
Here is an example study

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

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

Hawkeye

New member
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.
 

Hawkeye

New member
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. :)
 

Mike

New member
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”
 

XeoNoX

Well-known member
VIP

XeoNoX

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

XeoNoX

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

Similar threads

Top