# Last Bar Close

#### DanielManahan

##### Member
I want to return the price value of the previous day's close on the last minute of the 1 minute aggregation.
is there a tutorial or reference that I can read to understand how to do this? or simple line of code that I can learn from? any examples already done on this?

Solution
My last line has a syntax error. can someone help?

def isRollover = GetYYYYMMDD() != GetYYYYMMDD();
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def afterEnd = GetTime() > RegularTradingEnd(GetYYYYMMDD());

def firstBar = if (beforeStart == 1 and beforeStart == 0) or (isRollover and beforeStart == 0) then 1 else 0;
def lastBar = if (afterEnd[-1] == 1 and afterEnd == 0) or (isRollover[-1] and firstBar[-1]) then 1 else 0;

lastBar,
close,
"Last Bar",
Color.GREEN,
no);

plot x1 = lastbar; # this shows a boolean value either on or off, one or zero.
plot x2 = close lastbar; #this gives a syntax error. I want to plot the close of the last bar. the label correctly shows the...
I will check it out. looks like a common problem, and I see it is well explained in that link.

thanks

I have created the following script which is time consuming for the user, each day to input the previous day's close price from the close of the last minute candle

I want to replace the input price line with some code that retrieves the previous day's close (last minute candle). it is important that the aggregation period remains on the one minute chart.

so replace input ClosePrice1 = 1.11; with the code that looks at the previous day's last minute close price. seems simple enough. so that the user doesn't have to day by day have to reset it.

declare lower;

input getTicker1 = "";

input ClosePrice1 = 1.11; # please replace this line of code with something that automatically retrieves the previous day's close of the last one minute candle

input getTicker2 = "";
input ClosePrice2 = 1.11;
input getTicker3 = "";
input ClosePrice3 = 1.11;
input getTicker4 = "";

plot XLY = close(GetTicker1) + (0 - ClosePrice1);
plot XLK = close(GetTicker2) + (0 - ClosePrice2);
plot XLV = close(GetTicker3) + (0 - ClosePrice3);

plot Line = 0;

Last edited:
I have created the following script which is time consuming for the user, each day to input the previous day's close price from the close of the last minute candle

I want to replace the input price line with some code that retrieves the previous day's close (last minute candle). it is important that the aggregation period remains on the one minute chart.

so replace input ClosePrice1 = 1.11; with the code that looks at the previous day's last minute close price. seems simple enough. so that the user doesn't have to day by day have to reset it.

declare lower;

input getTicker1 = "";

input ClosePrice1 = 1.11; # please replace this line of code with something that automatically retrieves the previous day's close of the last one minute candle

input getTicker2 = "";
input ClosePrice2 = 1.11;
input getTicker3 = "";
input ClosePrice3 = 1.11;
input getTicker4 = "";

plot XLY = close(GetTicker1) + (0 - ClosePrice1);
plot XLK = close(GetTicker2) + (0 - ClosePrice2);
plot XLV = close(GetTicker3) + (0 - ClosePrice3);

plot Line = 0;

This will find a value on the last bar

• DanielManahan
I spend at least 10 hours dissecting that code from the last time I asked and this was answered with this link.
I really can't find any way of getting this to work.
that code is getting a label. what I need is for it to plot a line
I can't figure out the syntax

I am trying my best. can you help?

My last line has a syntax error. can someone help?

def isRollover = GetYYYYMMDD() != GetYYYYMMDD();
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def afterEnd = GetTime() > RegularTradingEnd(GetYYYYMMDD());

def firstBar = if (beforeStart == 1 and beforeStart == 0) or (isRollover and beforeStart == 0) then 1 else 0;
def lastBar = if (afterEnd[-1] == 1 and afterEnd == 0) or (isRollover[-1] and firstBar[-1]) then 1 else 0;

lastBar,
close,
"Last Bar",
Color.GREEN,
no);

plot x1 = lastbar; # this shows a boolean value either on or off, one or zero.
plot x2 = close lastbar; #this gives a syntax error. I want to plot the close of the last bar. the label correctly shows the last bar close. but not when I try to plot it.

My last line has a syntax error. can someone help?

def isRollover = GetYYYYMMDD() != GetYYYYMMDD();
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def afterEnd = GetTime() > RegularTradingEnd(GetYYYYMMDD());

def firstBar = if (beforeStart == 1 and beforeStart == 0) or (isRollover and beforeStart == 0) then 1 else 0;
def lastBar = if (afterEnd[-1] == 1 and afterEnd == 0) or (isRollover[-1] and firstBar[-1]) then 1 else 0;

lastBar,
close,
"Last Bar",
Color.GREEN,
no);

plot x1 = lastbar; # this shows a boolean value either on or off, one or zero.
plot x2 = close lastbar; #this gives a syntax error. I want to plot the close of the last bar. the label correctly shows the last bar close. but not when I try to plot it.

that code line fails because , close lastbar , isn't a valid formula .

here is a modified version of your code. i added a bubble to display ,
the close of the last bar of the day
and the day close and previous day close
sometimes they match, sometimes they don't. maybe someone else can explain why

on the last bar of the day, this formula updates the variable to be the close of the last bar, and keeps it until the next last bar.
def lastclose = if lastbar then close else lastclose;

Code:
``````# prevdaylastclose

#My last line has a syntax error. can someone help?

def isRollover = GetYYYYMMDD() != GetYYYYMMDD();
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def afterEnd = GetTime() > RegularTradingEnd(GetYYYYMMDD());

def firstBar = if (beforeStart == 1 and beforeStart == 0) or (isRollover and beforeStart == 0) then 1 else 0;
def lastBar = if (afterEnd[-1] == 1 and afterEnd == 0) or (isRollover[-1] and firstBar[-1]) then 1 else 0;

def lastclose = if lastbar then close else lastclose;

#-----------------------

def agg = aggregationPeriod.day;
def close_agg = close(period = agg);
def prevdayclose = close_agg;

#------------------------

, (if lastbar then high*1.01 else low*0.99),
lastclose + " last bar\n" +
"\n" +
close_agg + " day close\n" +
prevdayclose + " prev day close\n"
, (if lastbar then Color.yellow else color.gray)
, (if lastbar then yes else no));

"Last Bar",
Color.GREEN, no);

#plot x1 = lastbar;
# this shows a boolean value either on or off, one or zero.

#plot x2 = close lastbar;
#this gives a syntax error. I want to plot the close of the last bar. the label correctly shows the last bar close. but not when I try to plot it.
#``````

• SleepyZ
I have created the following script which is time consuming for the user, each day to input the previous day's close price from the close of the last minute candle

I want to replace the input price line with some code that retrieves the previous day's close (last minute candle). it is important that the aggregation period remains on the one minute chart.

so replace input ClosePrice1 = 1.11; with the code that looks at the previous day's last minute close price. seems simple enough. so that the user doesn't have to day by day have to reset it.

declare lower;

input getTicker1 = "";

input ClosePrice1 = 1.11; # please replace this line of code with something that automatically retrieves the previous day's close of the last one minute candle

input getTicker2 = "";
input ClosePrice2 = 1.11;
input getTicker3 = "";
input ClosePrice3 = 1.11;
input getTicker4 = "";

plot XLY = close(GetTicker1) + (0 - ClosePrice1);
plot XLK = close(GetTicker2) + (0 - ClosePrice2);
plot XLV = close(GetTicker3) + (0 - ClosePrice3);

plot Line = 0;

take a look at this and see if it is what you are trying to do

okay great sorry for the late reply, didn't see a notification in my inbox. glad I checked in and i will check it out.

Yes I think that works and now I can simply subtract the lastclose value to the current close to get a change of day. i want to learn from this excellent example.

thanks

that code line fails because , close lastbar , isn't a valid formula .

here is a modified version of your code. i added a bubble to display ,
the close of the last bar of the day
and the day close and previous day close
sometimes they match, sometimes they don't. maybe someone else can explain why

on the last bar of the day, this formula updates the variable to be the close of the last bar, and keeps it until the next last bar.
def lastclose = if lastbar then close else lastclose;

Code:
``````# prevdaylastclose

#My last line has a syntax error. can someone help?

def isRollover = GetYYYYMMDD() != GetYYYYMMDD();
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def afterEnd = GetTime() > RegularTradingEnd(GetYYYYMMDD());

def firstBar = if (beforeStart == 1 and beforeStart == 0) or (isRollover and beforeStart == 0) then 1 else 0;
def lastBar = if (afterEnd[-1] == 1 and afterEnd == 0) or (isRollover[-1] and firstBar[-1]) then 1 else 0;

def lastclose = if lastbar then close else lastclose;

#-----------------------

def agg = aggregationPeriod.day;
def close_agg = close(period = agg);
def prevdayclose = close_agg;

#------------------------

, (if lastbar then high*1.01 else low*0.99),
lastclose + " last bar\n" +
"\n" +
close_agg + " day close\n" +
prevdayclose + " prev day close\n"
, (if lastbar then Color.yellow else color.gray)
, (if lastbar then yes else no));

"Last Bar",
Color.GREEN, no);

#plot x1 = lastbar;
# this shows a boolean value either on or off, one or zero.

#plot x2 = close lastbar;
#this gives a syntax error. I want to plot the close of the last bar. the label correctly shows the last bar close. but not when I try to plot it.
#``````
Hi,

I just check these codes you modified :

def isRollover = GetYYYYMMDD() != GetYYYYMMDD();

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

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

def firstBar = if (beforeStart == 1 and beforeStart == 0) or (isRollover and beforeStart == 0) then 1 else 0;

def lastBar = if (afterEnd[-1] == 1 and afterEnd == 0) or (isRollover[-1] and firstBar[-1]) then 1 else 0;

def lastclose = if lastbar then close else lastclose;

I look up different symbols, it doesn't give the correct close for last bar (I select 1 minute timeframe with extended session ticked)

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

87k+ Posts
322 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?