INVITE-ONLY SCRIPT

Worldclassedge [Patrick nill]

53
VWAP
anchor = input.string("Session", title="Anchor Period")
MILLIS_IN_DAY = 86400000
dwmBarTime = timeframe.isdwm ? time : request.security(syminfo.tickerid, "D", time)
dwmBarTime := na(dwmBarTime) ? nz(dwmBarTime[1]) : dwmBarTime
var periodStart = time - time

makeMondayZero(dayOfWeek) => (dayOfWeek + 5) % 7
isMidnight(t) => hour(t) == 0 and minute(t) == 0
isSameDay(t1, t2) => dayofmonth(t1) == dayofmonth(t2) and month(t1) == month(t2) and year(t1) == year(t2)
isOvernight() => not (isMidnight(dwmBarTime) or request.security(syminfo.tickerid, "D", isSameDay(time, time_close), lookahead=barmerge.lookahead_on))

tradingDayStart(t) => timestamp(year(t), month(t), dayofmonth(t), 0, 0)
numDaysBetween(t1, t2) =>
diff = math.abs(tradingDayStart(t1) - tradingDayStart(t2))
diff / MILLIS_IN_DAY

tradingDay = isOvernight() ? tradingDayStart(dwmBarTime + MILLIS_IN_DAY) : tradingDayStart(dwmBarTime)
isNewPeriod() =>
var isNew = false
if tradingDay != nz(tradingDay[1])
isNew := switch anchor
"Session" => na(tradingDay[1]) or tradingDay > tradingDay[1]
"Week" => makeMondayZero(dayofweek(periodStart)) + numDaysBetween(periodStart, tradingDay) >= 7
"Month" => month(periodStart) != month(tradingDay) or year(periodStart) != year(tradingDay)
"Year" => year(periodStart) != year(tradingDay)
=> false
isNew

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.