# Reference Bar Number by Time

#### C4men

##### Member
Using a 5min chart:

How can I call out Bar #1 (930 to 935) for use in my study?

To keep it simple, I'd love to be able to do something like this:

• Define the first bar
• Have the ability to reference that first bar later in the study

So I could code "If first bar is bullish (close > open) and second bar is bullish (close > open) AND close of second bar > close first bar then...

I'm sure this is possible, but I am not having luck with BarNumber( )

Thanks!

Using a 5min chart:

How can I call out Bar #1 (930 to 935) for use in my study?

To keep it simple, I'd love to be able to do something like this:

• Define the first bar
• Have the ability to reference that first bar later in the study

So I could code "If first bar is bullish (close > open) and second bar is bullish (close > open) AND close of second bar > close first bar then...

I'm sure this is possible, but I am not having luck with BarNumber( )

Thanks!
See if this helps. For example, in the code below, if bullcond is true (1) it will have a plot of 1 above the bars. Then while it is true, if xbull is true it will plot a 1 below the bar that is true.

Ruby:
``````def bn       = if SecondsFromTime(0930) == 0 then 1 else bn[1] + 1;
def bullish  = close > open;
def bull1    = if bn == 1 and bullish then close else bull1[1];
def bull2    = if bn == 2 and bullish then close else bull2[1];
def bullcond = if bull1 and bull2 and bull2 > bull1 then 1 else 0;
plot xbull1  = bullcond;
xbull1.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE);

plot ema1    = ExpAverage(close, 5);
plot ema2    = ExpAverage(close, 13);
plot xbull   = if bullcond == 1 and ema1 crosses above ema2 then 1 else 0;
xbull.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);``````

See if this helps. For example, in the code below, if bullcond is true (1) it will have a plot of 1 above the bars. Then while it is true, if xbull is true it will plot a 1 below the bar that is true.

Thanks for the quick reply. Wondering if you might be able to help me tweak that just a bit.

The most important thing here is being able to "define" the first bar, so that I can reference the open/close/high/low values.

Something like:

def FirstBar_Open = <insert logic>;
def FirstBar_Close = <insert logic>;
def FirstBar_High = <insert logic>;
def FirstBar_Low = <insert logic>;

So that I can call that specific bar later, like:

def Bullish_Move = If close > FirstBar_Close then...

Almost like an ORB maybe?

Thanks for the quick reply. Wondering if you might be able to help me tweak that just a bit.

The most important thing here is being able to "define" the first bar, so that I can reference the open/close/high/low values.

Something like:

def FirstBar_Open = <insert logic>;
def FirstBar_Close = <insert logic>;
def FirstBar_High = <insert logic>;
def FirstBar_Low = <insert logic>;

So that I can call that specific bar later, like:

def Bullish_Move = If close > FirstBar_Close then...

Almost like an ORB maybe?

Using the code that I provided you

Ruby:
``````def bn       = if SecondsFromTime(0930) == 0 then 1 else bn[1] + 1;
def b1open   = if bn==1 then open  else b1open[1];
def b1close  = if bn==1 then close else b1close[1];
def b1high   = if bn==1 then high  else b1high[1];
def b1low    = if bn==1 then low   else b1low[1];

addlabel(1,"O: "+ b1open + " C: " + b1close + " H: " + b1high + " L: " + b1low,color.white);``````

Using a 5min chart:

How can I call out Bar #1 (930 to 935) for use in my study?

To keep it simple, I'd love to be able to do something like this:

• Define the first bar
• Have the ability to reference that first bar later in the study

So I could code "If first bar is bullish (close > open) and second bar is bullish (close > open) AND close of second bar > close first bar then...

I'm sure this is possible, but I am not having luck with BarNumber( )

Thanks!
just to clarify and help you reference things more accurately,
you say bar #1 but mention 9:30. bar #1 is the first bar on the chart ( far left). i assume you want the 'first bar of the day'

i was going to say , use sleepyz code , just use bn as an offset to get back to the first bar of the day.
but this doesn't work. cant use variables derived from time formulas , as offset variables.
def first = close[bn-1];

*******
nevermind , use his example in post#4

here is a link to a study that finds the first and last bar of the day
https://usethinkscript.com/threads/finding-the-first-and-last-bar-of-the-day-in-thinkorswim.526/

Using the code that I provided you
That's exactly what I was trying to do. Thank you!!!!!

One more question - could I do the same thing for the second bar (5m chart), i.e. 9:35 to 9:40?

That's exactly what I was trying to do. Thank you!!!!!

One more question - could I do the same thing for the second bar (5m chart), i.e. 9:35 to 9:40?

here is a mod to korygill's code
you enter an offset ( like 2) and it finds the 2nd bar of the day.
it draws a light blue arrow and displays the bar number of the day.

when this is true, then it is x bars away from the first bar of the day
if firstBarOfDay[firstbaroffset] then ....

Ruby:
``````# firstlast_barofday_mod01

# mod - find a bar after the first bar

# 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));
#

# --------------------------------------------
# find a bar after the first bar, with an offset

def na = double.nan;
input find_x_bar_of_day = 2;
def fxb = (find_x_bar_of_day - 1);

def chartagg = getAggregationPeriod();
def chartmin = (chartagg/1000)/60;
# 390 minutes in a day
def daybars = 390/chartmin;

addlabel(1, "chart min " + chartmin, color.cyan);
addlabel(1, "day bars " + daybars, color.cyan);

def fxberr = (fxb >= daybars) or (fxb < 0);
def firstbaroffset = if fxb > daybars then (daybars-1) else if fxb < 0 then 0 else fxb;
# # wrong, display a message
addlabel(fxberr, "x bar number " + fxb + ", is out of range. (0to" + (daybars -1) + ") pick another", color.cyan);

# find desired bar
plot z = if !fxberr and firstBarOfDay[firstbaroffset] then low else na;
z.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
z.SetDefaultColor(Color.cyan);
z.setlineweight(3);
z.hidebubble();

# plot bar #
plot y = if !fxberr and firstBarOfDay[firstbaroffset] then (firstbaroffset + 1) else na;
y.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE);
y.SetDefaultColor(Color.light_gray);
#``````

That's exactly what I was trying to do. Thank you!!!!!

One more question - could I do the same thing for the second bar (5m chart), i.e. 9:35 to 9:40?
Sorry, I missed your 'one more question'. This should work

Ruby:
``````def bn       = if SecondsFromTime(0930) == 0 then 1 else bn[1] + 1;
def b1open   = if bn==1 then open  else b1open[1];
def b1close  = if bn==1 then close else b1close[1];
def b1high   = if bn==1 then high  else b1high[1];
def b1low    = if bn==1 then low   else b1low[1];

addlabel(1,"930 O: "+ b1open + " C: " + b1close + " H: " + b1high + " L: " + b1low,color.white);

def b2open   = if bn==2 then open  else b2open[1];
def b2close  = if bn==2 then close else b2close[1];
def b2high   = if bn==2 then high  else b2high[1];
def b2low    = if bn==2 then low   else b2low[1];

addlabel(1,"935 O: "+ b2open + " C: " + b2close + " H: " + b2high + " L: " + b2low,color.white);``````

### Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
302 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• ‘Buy The Dip’ signal alerts
• Exclusive members-only content
• Add-ons and resources
• 1 full year of unlimited support

### Frequently Asked Questions

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?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.