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!
 

Ginu09

Member
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

Member
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.
 

Ginu09

Member

SleepyZ

Active member
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);
 

_Merch_Man_

Member
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

def lastBarOpen = getvalue(open,-1*(numTradingDaysInYear-barnumber())) ;

# Add chart bubble to test results

AddChartBubble(1,1,lastBarOpen);

pNm4K4w.jpg
 

mashume

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

Ginu09

Member
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

Member
@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:

korygill

Active member
VIP
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.
 

_Merch_Man_

Member
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

def lastBarOpen = getvalue(open,-1*(numTradingDaysInYear-barnumber())) ;

# Add chart bubble to test results

AddChartBubble(1,1,lastBarOpen);
 

Similar threads

Top