Last Bar Close

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()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def afterEnd = GetTime() > RegularTradingEnd(GetYYYYMMDD());

def firstBar = if (beforeStart[1] == 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;

AddChartBubble(
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 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
https://usethinkscript.com/threads/finding-the-first-and-last-bar-of-the-day-in-thinkorswim.526/
 
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()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def afterEnd = GetTime() > RegularTradingEnd(GetYYYYMMDD());

def firstBar = if (beforeStart[1] == 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;

AddChartBubble(
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()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def afterEnd = GetTime() > RegularTradingEnd(GetYYYYMMDD());

def firstBar = if (beforeStart[1] == 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;

AddChartBubble(
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[1];

Code:
# prevdaylastclose

#https://usethinkscript.com/threads/last-bar-close.11779/
#My last line has a syntax error. can someone help?

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

def firstBar = if (beforeStart[1] == 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[1];

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

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

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

AddChartBubble( 1
, (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));




AddChartBubble( 0 and 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.
#
 
Solution
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
https://usethinkscript.com/threads/...rence-from-close-of-last-bar-yesterday.11784/
 
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[1];

Code:
# prevdaylastclose

#https://usethinkscript.com/threads/last-bar-close.11779/
#My last line has a syntax error. can someone help?

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

def firstBar = if (beforeStart[1] == 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[1];

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

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

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

AddChartBubble( 1
, (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));




AddChartBubble( 0 and 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.
#
Hi,

I just check these codes you modified :

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

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

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

def firstBar = if (beforeStart[1] == 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[1];

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

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
289 Online
Create Post

Similar threads

Similar threads

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.
Back
Top