Finding the First and Last Bar of the day in ThinkorSwim

korygill

korygill

Member
VIP
Warehouse
One would think finding the first and last bar of the day in thinkscript would be easy.

I struggled to identify the first and last bars WITH extended session on and extended session off in the same study. I wrote this study, GetDayValues, to help me debug how to identify these so I could track PreviousClose and DayOpen bars in Magic Fibonacci 1.272 Indicator for ThinkorSwim v2.

I ran into issues around GetTime(), RegularTradingStart(), and RegularTradingEnd() and sometimes thinkscript would go a little bonkers and break my study if I wrote code in a certain way, which seemed valid at the time.

Sidebar: I will submit a feature request to the thinkscript to add a few built-in boolean values like IsFirstRegularBar, IsLastRegularBar, IsFirstExtendedBar, IsLastExtendedBar, IsRegularSession, IsExtendedSession, etc.

So what did I finally use to determine this?

Image of the GetDayValues study in action



Discussion of the chart and numbed highlights

(1) Notice the cursor is on the first bar of the day
(2) Notice the colors of the labels and how they match the indicators in the lower studies
(3) Notice the values of the labels that are applicable on the highlighted first bar of the day
(4) Notice how the indicators differ on extended and non-extended charts

Once I plotted these values (some by trial and error), I was able to finally define FirstBarOfDay and LastBarOfDay and subsequently use those to track other values like previous close, opening bar, and for other studies like Opening Range, and others, these calculations could be very useful.

The secret sauce is in the following logic in the code:
Code:
#

# logic

#

def nan = Double.NaN;

def isRollover = GetYYYYMMDD() != GetYYYYMMDD()[1];

def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());

def afterEnd = GetTime() > RegularTradingEnd(GetYYYYMMDD());

def firstBarOfDay = if (beforeStart[1] == 1 and beforeStart == 0) or (isRollover and beforeStart == 0) then 1 else 0;

def lastBarOfDay = if

    (afterEnd[-1] == 1 and afterEnd == 0) or

    (isRollover[-1] and firstBarOfDay[-1])

    then 1

    else 0;
Once you see it, maybe it makes perfect sense. As previously discussed, it may not be that obvious to others, and was not obvious to me. I used things like GetDate() != GetDate()[1] and other things that hard-coded start of trading to 9:30 or 6:30 which varies based on your timezone.

So I am now hopeful that this pattern for finding first/last bars can be used as the de-facto standard for how to do this. If there is a better way that you know of, please feel free to share. I could not find one with a quick search of the internet, thinkscript docs, etc.

Link to GetDayValues Flex Grid

http://tos.mx/EXWUIK

Source Code for GetDayValues

Code:
# GetDayValues
# Author: Kory Gill, @korygill
#
# VERSION HISTORY (sortable date and time (your local time is fine), and your initials
# 20190823-1400-KG    - Created.
# ...
# ...
declare hide_on_daily;
declare once_per_bar;

input onUpper = yes;

#
# logic
#
def nan = Double.NaN;
def isRollover = GetYYYYMMDD() != GetYYYYMMDD()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def afterEnd = GetTime() > RegularTradingEnd(GetYYYYMMDD());
def firstBarOfDay = if (beforeStart[1] == 1 and beforeStart == 0) or (isRollover and beforeStart == 0) then 1 else 0;
def lastBarOfDay = if
    (afterEnd[-1] == 1 and afterEnd == 0) or
    (isRollover[-1] and firstBarOfDay[-1])
    then 1
    else 0;

#
# Identify first bar of day and last bar of day on chart
#
AddChartBubble(
    firstBarOfDay and onUpper,
    close,
    "First Bar of Day",
    Color.GREEN,
    yes);

AddChartBubble(
    lastBarOfDay and onUpper,
    close,
    "Last Bar of Day",
    Color.GREEN,
    no);

#
# plots
#
plot p1 = if !onUpper then isRollover else nan;
plot p2 = if !onUpper then beforeStart else nan;
plot p3 = if !onUpper then afterEnd else nan;
plot p4 = if !onUpper then firstBarOfDay else nan;
plot p5 = if !onUpper then lastBarOfDay else nan;

p1.SetDefaultColor(GetColor(1));
p2.SetDefaultColor(GetColor(2));
p3.SetDefaultColor(GetColor(3));
p4.SetDefaultColor(GetColor(4));
p5.SetDefaultColor(GetColor(5));

AddLabel(!onUpper, "isRollOver", GetColor(1));
AddLabel(!onUpper, "beforeStart", GetColor(2));
AddLabel(!onUpper, "afterEnd", GetColor(3));
AddLabel(!onUpper, "firstBarOfDay", GetColor(4));
AddLabel(!onUpper, "lastBarOfDay", GetColor(5));
Disclaimer and Notices

You are free to use this code for personal use, and make derivative works from it. You are also GRANTED permission to use this code (or derivative works) for commercial purposes which includes and is not limited to selling, reselling, or packaging with other commercial indicators. Headers and attribution in this code should remain as provided, and any derivative works should extend the existing headers.
 
S

skynetgen

Member
Warehouse
Nice work. Also for some cases there is one trick for finding last bar. One advantage of that method is that TOS doesnt flag it as "too complex" (which happens whenever highestall is used):
Code:
def isLastBar = !IsNaN(close) and IsNaN(close[-1]);
 

Top