# Checking for today's date in a multi-day study

#### Ginu09

##### Member
Simple and straightforward.

The GetYYYYMMDD() function returns the day within the multi-day chart that TOS is analyzing. For example, plot the following on a 5 day chart:

def RTHopen = if getTime() crosses above RegularTradingStart(getYYYYMMDD()) then open else RTHopen[1];
plot checkOpen = RTHopen;

And the plot will post the open on each of the 5 days.

Is there a way to store (or plot) the open from today ONLY (i.e. day 0) regardless of the number of days in the chart so it is a single horizontal line with the same value?

I was thinking something like:

def currentOpen = if (getYYYYMMDD() == today's date) then open(period = AggregationPeriod.DAY) else currentOpen[1];
plot checkOpen = currentOpen;

Thanks!

Code:
``def isLastBar = !IsNaN(close) and IsNaN(close[-1]);``

Thanks. I learned something I didn't know. On a multi-day chart, Thinkscript executes days left to right (rather than today first) meaning that currentOpen below is zero until the last day is evaluated.

def isLastBar = !IsNaN(close) and IsNaN(close[-1]);
def currentOpen = if (isLastBar) then open(period = AggregationPeriod.DAY) else currentOpen[1];

Is there a way to grab the open of the day where isLastBar = true? Probably not

@Ginu09 I am not quite sure what you mean. It looks to me like the code you posted does what you are asking. Could you please clarify?

I have a multi-day chart. I'm trying to compare the day high of each day against the open of today. If I plot currentOpen with the code above, the value is 0 until the last day where it then has the value of that day's open. So what will happen is each day except today will be comparing the day high against 0 because historic values of currentOpen are 0.

That's very useful but it finds the day open and day close of each day. I simply want to compare the day highs on a multi-day chart against today's open ONLY (day 0). I'm finding it impossible to store that in a fixed value to compare against. "open" does not work on a multi-day chart.
See if this helps
Code:
``````def isLastBar = !IsNaN(close) and IsNaN(close[-1]);
def currentOpen = if (isLastBar) then open(period = AggregationPeriod.DAY) else currentOpen[1];
plot copen = HighestAll(currentOpen);

plot dailyhigh = high(period = AggregationPeriod.DAY);
dailyhigh.SetDefaultColor(Color.YELLOW);
dailyhigh.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);``````

OK. Still thinking about this but I did come up with a hack that only works on a 1 year daily chart.

Code:
``````# Check to see if leap year and get number of trading days in year

def numTradingDaysInYear = if getyear()%4 == 0 then 253 else 252;

# Get open of last bar by going forward n bars where n is number of
# bars in the year - this bar number

# Add chart bubble to test results

Yes, thanks to both of you. LastBar worked. I was using dates but if your chart has multiple years worth of data then GetDay returns multiple numbers and highestAll might be wrong.

Code:
``````declare upper;

def today_open = if DaysTillDate(getYYYYMMDD()) <= 1 then
open(period = AggregationPeriod.DAY)
else 0
;

plot daily_open = highestall(today_open);``````
maybe

-mashume

I'm getting a strange error. On some tickers, on the daily, my calculated day high is "N/A" but it works on the intraday chart. And all aggregation periods on the intraday are "DAY" per the code below

def dailyHigh = if (GetValue(high(period = AggregationPeriod.DAY),1,0) < dailyHigh[1] and dailyHigh[1] != 0 and GetValue(high(period = AggregationPeriod.DAY),1,0) > currentOpen) or (dailyHigh[1] == 0 and GetValue(high(period = AggregationPeriod.DAY),1,0) > currentOpen)) then GetValue(high(period = AggregationPeriod.DAY),1,0) else dailyHigh[1];
plot recordHigh = dailyHigh;

If I replace dailyHigh[1] != 0 with !isNaN(dailyHigh[1]) the code breaks. Has anyone encountered this?

EDIT: It appears to be from the use of the GetValue function. Should I not be using this outside of a fold?

Last edited:
@Ginu09 would you please post your full code so I can install it and run it on my ToS so I can try to debug?

@Ginu09 would you please post your full code so I can install it and run it on my ToS so I can try to debug?

def isLastBar = !IsNaN(close) and IsNaN(close[-1]);
def currentOpen = HighestAll(if (isLastBar) then open(period = AggregationPeriod.DAY) else 0);

def dailyHigh = if ((GetValue(vwap(period = AggregationPeriod.DAY),1,0) < dailyHigh[1] and dailyHigh[1] != 0 and GetValue(vwap(period = AggregationPeriod.DAY),1,0) > currentOpen) or (dailyHigh[1] == 0 and GetValue(vwap(period = AggregationPeriod.DAY),1,0) > currentOpen)) then GetValue(vwap(period = AggregationPeriod.DAY),1,0) else dailyHigh[1];

plot recordHigh = dailyHigh;

I replaced high with vwap here to do some tests, same result. Can you try debugging with the VWAP? Not sure why it's not working.

Whether I use
GetValue(vwap(period = AggregationPeriod.DAY),1,0)
vwap(period = AggregationPeriod.DAY)[1]
reference VWAP(-2,2,"DAY")[1]

I get the same N/A.

FYI I was backtesting \$BNSO from January 4, 2021 in this test.

EDIT: I emailed TOS and they told me it may be due to liquidity where not enough data is available on some tickers on some days and not others. I tried using !isNan(GetValue(vwap(period = AggregationPeriod.DAY),1,0)) to skip those days but that didn't help either.

I figured it out. I added a new condition up front for if (isNaN(currentVWAP[1])) then dailyHigh[1]. That fixed the VWAP case.

Last edited:
FYI to @_Merch_Man_ and others, your code for leap year is off (but not broken for 79 years).

A leap year is exactly divisible by 4 except for century years (years ending with 00). The century year is a leap year only if it is perfectly divisible by 400.

See this to follow the logic. Exercise left to the reader to convert from Python to thinkscript.

Does anyone know how to get the daily VWAP value on an intraday chart? Let's say I wanted to get the VWAP value from the daily from yesterday.

FYI to @_Merch_Man_ and others, your code for leap year is off (but not broken for 79 years).

A leap year is exactly divisible by 4 except for century years (years ending with 00). The century year is a leap year only if it is perfectly divisible by 400.

See this to follow the logic. Exercise left to the reader to convert from Python to thinkscript.
Good catch @korygill ! Updated logic below:

Code:
``````# Check to see if leap year and get number of trading days in year

def numTradingDaysInYear = if getyear()%400 == 0 then 253 else if (getyear()%100 != 0 and getyear()%4 == 0) then 253 else 252;

# Get open of last bar by going forward n bars where n is number of
# bars in the year - this bar number

# Add chart bubble to test results

Does anyone know how to get the daily VWAP value on an intraday chart? Let's say I wanted to get the VWAP value from the daily from yesterday.
@Ginu09 Doesn't your code from above work?
Code:
``Def yesterdayVWAP = GetValue(vwap(period = AggregationPeriod.DAY),1);``

@Ginu09 Doesn't your code from above work?
Code:
``Def yesterdayVWAP = GetValue(vwap(period = AggregationPeriod.DAY),1);``
I was trying to avoid using GetValue as it's normally only used for fold statements. Plus, I wanted to use the VWAP study rather than vwap as they're two different things.

87k+ Posts
395 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

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?