Adding Historical Open Prices to a Chart Label

rnephosCloudMam

New member
VIP
I track Year Open, Month Open & Week Open on symbols for trading purposes. What I would like to have is a label at the top left of the chart that lists Year Open price, Month Open price & Week Open price for the symbol. I don't want "x" number of bars back. Ideally, I would like to type in the date (ie. year/month/week) and have the label return the info. For example (tracking the E-mini ... /ES) the 2024 year open was 4818. I've looked around to see how to specify a date (like I do in AVWAP) to pull an Open, High, Low, Close figure. Any help would be appreciated.
 
Solution
I track Year Open, Month Open & Week Open on symbols for trading purposes. What I would like to have is a label at the top left of the chart that lists Year Open price, Month Open price & Week Open price for the symbol. I don't want "x" number of bars back. Ideally, I would like to type in the date (ie. year/month/week) and have the label return the info. For example (tracking the E-mini ... /ES) the 2024 year open was 4818. I've looked around to see how to specify a date (like I do in AVWAP) to pull an Open, High, Low, Close figure. Any help would be appreciated.
1727753817702.png


Code:
input Price = {default "open", "high", "low", "close"};
input Year  = 20240102;
input Month = 20240603;
input Day   = 20240903;

def PriceY = if...
Here is some script from the help in ToS on ThinkScript. I've modified slightly to try to pull the info I want. It is not working correctly but I believe I am heading in the right direction.

input endDate = 20240102;
def cond = GetYYYYMMDD() > endDate;
def price = if cond then open else double.NaN;
addLabel (1,price);

As I understand the script, this should identify the endDate (first trading day of the year manually entered). Next it uses GetYYYYMMDD in order to pull today's date info and defines "cond" as the date using today's date if it is greater than endDate. It defines "price" as the open price that meats the "cond" parameters. The it creates a label with that open price.

As the formula is now, it pulls the open from whatever aggregation period in on the chart. It this case I have it set on 30 min chart. So the open price in my label is the open price for the current bar.

I need to define my aggregation period to be daily and for it to pull that open from the date I specify. Any help getting me to that point would be greatly appreciated. The number at the top left in red is the correct number for the open of the current bar.

1727743276327.png
 

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

I track Year Open, Month Open & Week Open on symbols for trading purposes. What I would like to have is a label at the top left of the chart that lists Year Open price, Month Open price & Week Open price for the symbol. I don't want "x" number of bars back. Ideally, I would like to type in the date (ie. year/month/week) and have the label return the info. For example (tracking the E-mini ... /ES) the 2024 year open was 4818. I've looked around to see how to specify a date (like I do in AVWAP) to pull an Open, High, Low, Close figure. Any help would be appreciated.
1727753817702.png


Code:
input Price = {default "open", "high", "low", "close"};
input Year  = 20240102;
input Month = 20240603;
input Day   = 20240903;

def PriceY = if GetYYYYMMDD() == Year then open else PriceY[1];
AddLabel(Yes, AsPrice(Year) + " " + PriceY, COLOR.CYAN);

def PriceM = if GetYYYYMMDD() == Month then open else PriceM[1];
AddLabel(Yes, AsPrice(Month) + " " + PriceM, COLOR.YELLOW);

def PriceD = if GetYYYYMMDD() == Day then open else PriceD[1];
AddLabel(Yes, AsPrice(Day) + " " + PriceD, COLOR.LIGHT_ORANGE);
 
Solution
View attachment 23041

Code:
input Price = {default "open", "high", "low", "close"};
input Year  = 20240102;
input Month = 20240603;
input Day   = 20240903;

def PriceY = if GetYYYYMMDD() == Year then open else PriceY[1];
AddLabel(Yes, AsPrice(Year) + " " + PriceY, COLOR.CYAN);

def PriceM = if GetYYYYMMDD() == Month then open else PriceM[1];
AddLabel(Yes, AsPrice(Month) + " " + PriceM, COLOR.YELLOW);

def PriceD = if GetYYYYMMDD() == Day then open else PriceD[1];
AddLabel(Yes, AsPrice(Day) + " " + PriceD, COLOR.LIGHT_ORANGE);
Thanks, Carl-not-Karl. It is working well. I assume if I change PriceD to reflect the data for the week rather than the day it will still work just fine.

I can do more research but if you know or can point me in the right direction your help would be appreciated. Right now the script uses the aggregation time reflected on the chart. Can you "hard code" the aggregation time to be static but vary the times on the chart? While I tracking Yearly, Monthly, & Weekly opens, I usually trade and chart with the 30 min chart. Using 30 min time frame on the chart negatively affects the Cyan/Yellow/Orange boxes.
 
Thanks, Carl-not-Karl. It is working well. I assume if I change PriceD to reflect the data for the week rather than the day it will still work just fine.

I can do more research but if you know or can point me in the right direction your help would be appreciated. Right now the script uses the aggregation time reflected on the chart. Can you "hard code" the aggregation time to be static but vary the times on the chart? While I tracking Yearly, Monthly, & Weekly opens, I usually trade and chart with the 30 min chart. Using 30 min time frame on the chart negatively affects the Cyan/Yellow/Orange boxes.
Yes, you may change the Day/PriceD to Week/PriceW but is not necessary as the result does not appear in the label.
I kept the Y M D to conform to the GetYYYYMMDD code format: try modifying it to conform to your style.

I will be out most of today.
If another uTS member has a quick solution(s) for your additional requests please assist.
 
Yes, you may change the Day/PriceD to Week/PriceW but is not necessary as the result does not appear in the label.
I kept the Y M D to conform to the GetYYYYMMDD code format: try modifying it to conform to your style.

I will be out most of today.
If another uTS member has a quick solution(s) for your additional requests please assist.
Sorry to be a pest but I'm still having trouble coding the aggregation period on the labels but using intraday period on the chart. As long as I use day aggregation (and at least one year) the code works perfectly. After I lower the chart aggregation under 1 year whatever result I want in GetYYYYMMDD over that period returns a zero. (Makes sense to me because I've told the chart only to look back a certain number of bars). I've played with adding "period = aggregationperiod.DAY" to PriceY (for example) but still no joy in getting the correct response when I have the chart set lower than day.

So, clearly the script is still taking the chart setting as the controlling setting (intraday) even though I attempted to override the chart setting in the script by using aggregationperiod. Maybe this involves something I read (but don't understand) about a secondary aggregation.

Screenshots show it working perfectly with 1Y (or greater)/D chart setting; only partially working on a lesser time frame 6M/D (all of these on AAPL). I want the answers I get in the boxes you provided for the 1Y/D but for my chart setting to be 2D/30m. If I understand secondary aggregation, I should be able to have day results on a 30 min chart but not 30 min results on a day chart. Where the breakdown appears to be is can I get 1Y/D results in the boxes on a 2D/30 min chart?

I've searched the site and the internet and have not yet found what I need. Maybe my thinking is wrong here? I would appreciate any help.

1728140337543.png


1728140368772.png
 
Sorry to be a pest but I'm still having trouble coding the aggregation period on the labels but using intraday period on the chart. As long as I use day aggregation (and at least one year) the code works perfectly. After I lower the chart aggregation under 1 year whatever result I want in GetYYYYMMDD over that period returns a zero. (Makes sense to me because I've told the chart only to look back a certain number of bars). I've played with adding "period = aggregationperiod.DAY" to PriceY (for example) but still no joy in getting the correct response when I have the chart set lower than day.

So, clearly the script is still taking the chart setting as the controlling setting (intraday) even though I attempted to override the chart setting in the script by using aggregationperiod. Maybe this involves something I read (but don't understand) about a secondary aggregation.

Screenshots show it working perfectly with 1Y (or greater)/D chart setting; only partially working on a lesser time frame 6M/D (all of these on AAPL). I want the answers I get in the boxes you provided for the 1Y/D but for my chart setting to be 2D/30m. If I understand secondary aggregation, I should be able to have day results on a 30 min chart but not 30 min results on a day chart. Where the breakdown appears to be is can I get 1Y/D results in the boxes on a 2D/30 min chart?

I've searched the site and the internet and have not yet found what I need. Maybe my thinking is wrong here? I would appreciate any help.

View attachment 23077

View attachment 23078
After experimenting and then reaching the same conclusions as you, it appears whatever Time Interval you have set on the chart is the restriction of data returned for that period overriding what is in the Study.

Not knowing your monitor/system setup a non-optimal solution would be to open a grid-over-grid or side-by-side arrangement.
In the image below is a 1 year chart over a 6 month chart and would leave the lower chart available for different studies, or none:

1728143635578.png


1728144286987.png


Another uTS member may have a better solution for you.
 
Sorry to be a pest but I'm still having trouble coding the aggregation period on the labels but using intraday period on the chart. As long as I use day aggregation (and at least one year) the code works perfectly. After I lower the chart aggregation under 1 year whatever result I want in GetYYYYMMDD over that period returns a zero. (Makes sense to me because I've told the chart only to look back a certain number of bars). I've played with adding "period = aggregationperiod.DAY" to PriceY (for example) but still no joy in getting the correct response when I have the chart set lower than day.

So, clearly the script is still taking the chart setting as the controlling setting (intraday) even though I attempted to override the chart setting in the script by using aggregationperiod. Maybe this involves something I read (but don't understand) about a secondary aggregation.

Screenshots show it working perfectly with 1Y (or greater)/D chart setting; only partially working on a lesser time frame 6M/D (all of these on AAPL). I want the answers I get in the boxes you provided for the 1Y/D but for my chart setting to be 2D/30m. If I understand secondary aggregation, I should be able to have day results on a 30 min chart but not 30 min results on a day chart. Where the breakdown appears to be is can I get 1Y/D results in the boxes on a 2D/30 min chart?

I've searched the site and the internet and have not yet found what I need. Maybe my thinking is wrong here? I would appreciate any help.

if you want data from a year ago, you need that data on the chart.
a 2 day chart just doesn't have enough historical data to pull year or month data

also, post#3 code won't always show the correct open. if you have a 30 minute chart, there are 13 bars for the day. for the day open, it will read and display the open from each one, then keep the open from the last bar of the day.

this will read the open from the first bar in the period

Code:
#open_prices_yrmoday
#https://usethinkscript.com/threads/adding-historical-open-prices-to-a-chart-label.19758/#post-146643

def na = double.nan;
def bn = barnumber();
def dat = GetYYYYMMDD();
input Year_date  = 20240102;
input Month_date = 20240801;
input week_date  = 20240826;
input Day_date   = 20240917;

# change code to retain the 1st bar in period
def firsty = dat[1] != Year_date and dat == Year_date;
def PriceY = if firsty then round(open,2) else PriceY[1];

def firstm = dat[1] != Month_date and dat == month_date;
def PriceM = if firstm then round(open,2) else PriceM[1];

def firstw = dat[1] != week_date and dat == week_date;
def Pricew = if firstw then round(open,2) else Pricew[1];

def firstd = dat[1] != Day_date and dat == day_date;
def PriceD = if firstd then round(open,2) else PriceD[1];

input labels = yes;
AddLabel(labels, "Year " + AsPrice(Year_date) + " " + PriceY, COLOR.CYAN);
AddLabel(labels, "Month " + AsPrice(Month_date) + " " + PriceM, COLOR.YELLOW);
AddLabel(labels, "Week " + AsPrice(week_date) + " " + Pricew, COLOR.magenta);
AddLabel(labels, "Day " + AsPrice(Day_date) + " " + PriceD, COLOR.LIGHT_ORANGE);

input bubbles = yes;
addchartbubble(bubbles and firsty, high*1.001, "Year\n"+pricey, color.gray, yes);
addchartbubble(bubbles and firstm, high*1.001, "Month\n"+pricem, color.gray, yes);
addchartbubble(bubbles and firstw, high*1.001, "Week\n"+pricew, color.gray, yes);
addchartbubble(bubbles and firstd, high*1.001, "Day\n"+priced, color.gray, yes);
#
 
Thanks, Carl-not-Karl. I'm familiar and use grid over grid (and grid by grid). My system is sufficiently robust and I have a 4 monitor setup. The workaround will suffice,

Halcyonguy, I understand what you are saying. I'll try out your suggestion and see how it works. These thinkscript tweaks are helping me get all of my relevant trading info down to one or two screens. I was always an Excel guy so a lot of what I've done has been using RTD links back into an Excel sheet. This is helping me think more strategically about the data.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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