Add Chart Label for Pre-Market/Regular Session & Afterhours

BobR

New member
VIP
Hello - I just stumbled onto this forum and it is amazing based on what I've read so far. My only other exposure to coding is some basic formula creation in MetaStock. This is my first attempt at thinkscript and I'm trying to create a chart label to use on intraday and daily charts. To get an idea of the syntax for thinkscripting I started by using Copilot AI, and, although I didn't end up with the result I wanted (after lots of back and forth) it did get me started in the right direction.

You can skip to the end of this post (just prior to the code below) for a summary of what I'm hoping for).

I searched through the forum and found some discussions that touched on what I'm trying to do but didn't seem to contain exactly what I'm looking for. The label I'm trying to create (with some success so far) are two labels. I'd like to reflect Pre-market session then continue on into regular session in the first label showing latest price with net change in price from previous day closing price of regular session along with the associated percentage change. Once the regular session closes I'd like this first label to become static and continue showing the current day closing price of the regular session along with net change in price and percentage from close of previous day regular session.

I'd like the second label to show current afterhours price with net change in price from end of current day regular session closing price along with the associated percentage change (this portion seems to be working well).

During Pre-market and Regular session I'd like to have the afterhours label blacked out. Right now, afterhours label is only blacked out during pre-market session. I would like to also show best bid/ask price regardless of which session is currently open.

I realize that many of these elements are already displayed at the top of the chart by default but I have found that the labels are easier to read.

The labels I came up with work (sort of). The Pre-Market and Regular session label works fine until close of the Regular session then price and net change figures change as aftermarket activity proceeds (I would like Regular session label to become static to continue reflecting values as of market close of current day with net change compared to close of previous day Regular session).

The Afterhours label is correctly reflecting current afterhours price and net change from current day closing price of today's Regular session.

Below is the label as it appeared earlier today during Regular session:

CqbNFYP.png


In summary, what I'm still trying to achieve is as follows:

1. Once regular session closes for current day I'd like to have label on left become static and continue to show regular session closing price for current day along with net price change (and percentage change from previous day regular session closing price)

2. I would like to have afterhours label on right be blacked out until afterhours session begins (right now, it is only blacked out during pre-market session).

3. Regardless of session I'd like to see best bid/ask displayed somewhere in the label (just needed in one location somewhere in label).

My current code is as follows:

# Define the closing price of pre-market and current day standard market session
def closePrice = if GetTime () >=(1600) then close(period = "DAY")[1] else close(period = "DAY");

# Define the current price (which includes pre-market and regular session)
def currentPrice = close;

# Define the closing price of afterhours session
def AHclosePrice = if GetTime () >=(1600) then close(period = "DAY") else close(period = "DAY")[1];

# Calculate the net change of pre-market and current day standard market session
def netChange = currentPrice - closePrice;

# Calculate the net change of afterhours session
def AHnetChange = currentPrice - AHclosePrice;

# Calculate the pre-market and current day standard market session percentage change
def percentChange = (netChange / closePrice) * 100;

# Calculate the percentage change of afterhours session
def AHpercentChange = (AHnetChange / closePrice) * 100;

# Add a label to the chart showing current price, the net change, and percentage change of pre-market and current day standard market session
AddLabel(yes, "Pre-Market or Std Session Current Price: " + AsDollars(currentPrice) + " | Net Change: " + AsDollars(netChange) + " | % Change: " + Round(percentChange, 2) + "%",
if netChange >= 0 then Color.GREEN else Color.LIGHT_RED);

# Add a label to the chart showing current price, the net change, and percentage change of afterhours session
AddLabel(yes, "Afterhours Current Price: " + AsDollars(currentPrice) + " | Net Change: " + AsDollars(AHnetChange) + " | % Change: " + Round(AHpercentChange, 2) + "%",
if AHnetChange >= 0 then Color.GREEN else Color.LIGHT_RED);

This is probably a simple fix for the experts here but I have been struggling with this since the weekend.

Thanks for any help you're able to provide.

Bob
 
Last edited:
Hello - I just stumbled onto this forum and it is amazing based on what I've read so far. My only other exposure to coding is some basic formula creation in MetaStock. This is my first attempt at thinkscript and I'm trying to create a chart label to use on intraday and daily charts. To get an idea of the syntax for thinkscripting I started by using Copilot AI, and, although I didn't end up with the result I wanted (after lots of back and forth) it did get me started in the right direction.

You can skip to the end of this post (just prior to the code below) for a summary of what I'm hoping for).

I searched through the forum and found some discussions that touched on what I'm trying to do but didn't seem to contain exactly what I'm looking for. The label I'm trying to create (with some success so far) are two labels. I'd like to reflect Pre-market session then continue on into regular session in the first label showing latest price with net change in price from previous day closing price of regular session along with the associated percentage change. Once the regular session closes I'd like this first label to become static and continue showing the current day closing price of the regular session along with net change in price and percentage from close of previous day regular session.

I'd like the second label to show current afterhours price with net change in price from end of current day regular session closing price along with the associated percentage change (this portion seems to be working well).

During Pre-market and Regular session I'd like to have the afterhours label blacked out. Right now, afterhours label is only blacked out during pre-market session. I would like to also show best bid/ask price regardless of which session is currently open.

I realize that many of these elements are already displayed at the top of the chart by default but I have found that the labels are easier to read.

The labels I came up with work (sort of). The Pre-Market and Regular session label works fine until close of the Regular session then price and net change figures change as aftermarket activity proceeds (I would like Regular session label to become static to continue reflecting values as of market close of current day with net change compared to close of previous day Regular session).

The Afterhours label is correctly reflecting current afterhours price and net change from current day closing price of today's Regular session.

Below is the label as it appeared earlier today during Regular session:

CqbNFYP.png


In summary, what I'm still trying to achieve is as follows:

1. Once regular session closes for current day I'd like to have label on left become static and continue to show regular session closing price for current day along with net price change (and percentage change from previous day regular session closing price)

2. I would like to have afterhours label on right be blacked out until afterhours session begins (right now, it is only blacked out during pre-market session).

3. Regardless of session I'd like to see best bid/ask displayed somewhere in the label (just needed in one location somewhere in label).

My current code is as follows:

# Define the closing price of pre-market and current day standard market session
def closePrice = if GetTime () >=(1600) then close(period = "DAY")[1] else close(period = "DAY");

# Define the current price (which includes pre-market and regular session)
def currentPrice = close;

# Define the closing price of afterhours session
def AHclosePrice = if GetTime () >=(1600) then close(period = "DAY") else close(period = "DAY")[1];

# Calculate the net change of pre-market and current day standard market session
def netChange = currentPrice - closePrice;

# Calculate the net change of afterhours session
def AHnetChange = currentPrice - AHclosePrice;

# Calculate the pre-market and current day standard market session percentage change
def percentChange = (netChange / closePrice) * 100;

# Calculate the percentage change of afterhours session
def AHpercentChange = (AHnetChange / closePrice) * 100;

# Add a label to the chart showing current price, the net change, and percentage change of pre-market and current day standard market session
AddLabel(yes, "Pre-Market or Std Session Current Price: " + AsDollars(currentPrice) + " | Net Change: " + AsDollars(netChange) + " | % Change: " + Round(percentChange, 2) + "%",
if netChange >= 0 then Color.GREEN else Color.LIGHT_RED);

# Add a label to the chart showing current price, the net change, and percentage change of afterhours session
AddLabel(yes, "Afterhours Current Price: " + AsDollars(currentPrice) + " | Net Change: " + AsDollars(AHnetChange) + " | % Change: " + Round(AHpercentChange, 2) + "%",
if AHnetChange >= 0 then Color.GREEN else Color.LIGHT_RED);

This is probably a simple fix for the experts here but I have been struggling with this since the weekend.

Thanks for any help you're able to provide.

Bob

hi bob, and welcome
keep trying things and you will get there. it took me awhile to understand thinkscript.

you wrote many sentences, and i'm having to read them many times to try to figure out what you want.


learn how to look up functions and see what they do.
. GetTime() >=(1600)
this will always be true.
gettime() , is a huge number, +trillion.
the number of milliseconds since the epoch (January 1, 1970, 00:00:00 GMT).
https://toslc.thinkorswim.com/center/reference/thinkScript/Functions/Date---Time/GetTime

try adding something like this to see variable values. the \n starts a new line
def gt = gettime();
addchartbubble(1, low,
"gettime\n" +
gt
, color.yellow, yes);
i left deveral test codes in the study, at the end.


close(period = "DAY")
this only looks at normal trading hours, not after hours


it's night, so i didn't test this in all time periods. but i did plot data lines and bubbles.

i rewrote your rules and started over.
mine doesn't use any agregation.


Code:
#pre_ah_chg_label_0

#https://usethinkscript.com/threads/add-chart-label-for-pre-market-regular-session-afterhours.19766/
#Add Chart Label for Pre-Market/Regular Session & Afterhours

def na = double.nan;
def bn = barnumber();

def d = getday();
def newday = d != d[1];
def lastbar = (!isnan(close) and isnan(close[-1]));

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

#label1, (left), pre-normal

# Pre-market price then continue on into regular session, 
#  with net change from previous day closing price of regular session,
#  along with the associated percentage change.

# Once the regular session closes I'd like this first label to become static,
#  and continue showing the current day closing price of the regular session, along with net change in price, and
#  percentage from close of previous day regular session.

#..premarket - close , chg prev day close, % chg
#..normal hours - close , chg prev day close, % chg
#..after hours - retain prev values

def daystart = 930;
def dayend = 1600;
def daytime = if secondsfromTime(daystart) >= 0 and secondstillTime(dayend) > 0 then 1 else 0;
def pre = secondstillTime(daystart) > 0;
def after = secondsfromTime(dayend) >= 0;

def pre_and_normal_close = if pre or daytime then close else pre_and_normal_close[1];
def prev_normal_close = if newday then pre_and_normal_close[1] else prev_normal_close[1];
def diff_from_prev = pre_and_normal_close - prev_normal_close;
def per_diff_prev = 100 * diff_from_prev / prev_normal_close;


addlabel(1, " " , color.black);
addlabel(1, 
"Pre/Std: " + AsDollars(pre_and_normal_close) + " | Net Change: " + AsDollars(diff_from_prev) + " | % Change: " + Round( per_diff_prev, 2) + "%"
, (if diff_from_prev >= 0 then Color.GREEN else Color.LIGHT_RED));


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


#label2, (right) afterhours

# show current afterhours price,
#  with net change from end of current day regular session,
#  with the associated percentage change

# During Pre-market and Regular session I'd like to have the afterhours label blacked out. 

#..premarket - blacked out
#..normal hours - blacked out
#..after hours - current afterhours price, $ chg from today close, % chg

def after_close = if after then close else after_close[1];
def diff_after_today = after_close - pre_and_normal_close;
def per_diff_after_today = 100 * diff_after_today/pre_and_normal_close;

addlabel(1, " " , color.black);
AddLabel(yes,
 "AH: " + AsDollars(after_close ) + " | Net Change: " + AsDollars(diff_after_today) + " | % Change: " + Round(per_diff_after_today, 2) + "%"
, (if pre or daytime then color.dark_gray else if diff_after_today >= 0 then Color.GREEN else Color.LIGHT_RED));
addlabel(1, " " , color.black);


#------------------------------
# test stuff

input test2_lines = no;
plot z1 = if test2_lines then pre_and_normal_close else na;
plot z2 = if test2_lines and prev_normal_close > 0 then prev_normal_close else na;
z2.SetStyle(Curve.MEDIUM_DASH);

def line_end1 = (!isnan(pre_and_normal_close) and isnan(pre_and_normal_close[-1]));
addchartbubble(test2_lines and line_end1, pre_and_normal_close , "today close", color.yellow, yes);

def line_end2 = (!isnan(prev_normal_close) and isnan(prev_normal_close[-1]));
addchartbubble(test2_lines and line_end2, prev_normal_close , "prev day close", color.yellow, yes);


input test1_bubble = no;
addchartbubble(test1_bubble, low,
 pre_and_normal_close + "\n" +
 prev_normal_close + " p\n" +
 diff_from_prev + " $\n" +
 per_diff_prev + " %"
, color.yellow, no);


input test2_bubble = no;
addchartbubble(test2_bubble, low,
 pre_and_normal_close + "\n" +
 after_close + "\n" +
 per_diff_after_today
, color.yellow, no);


# test the period times
input test3_vert = no;
addverticalline(test3_vert and daytime, "day", color.green);
addverticalline(test3_vert and pre, "pre", color.cyan); 
addverticalline(test3_vert and after, "after", color.magenta); 
#
 
hi bob, and welcome
keep trying things and you will get there. it took me awhile to understand thinkscript.

you wrote many sentences, and i'm having to read them many times to try to figure out what you want.


learn how to look up functions and see what they do.
. GetTime() >=(1600)
this will always be true.
gettime() , is a huge number, +trillion.
the number of milliseconds since the epoch (January 1, 1970, 00:00:00 GMT).
https://toslc.thinkorswim.com/center/reference/thinkScript/Functions/Date---Time/GetTime

try adding something like this to see variable values. the \n starts a new line
def gt = gettime();
addchartbubble(1, low,
"gettime\n" +
gt
, color.yellow, yes);
i left deveral test codes in the study, at the end.


close(period = "DAY")
this only looks at normal trading hours, not after hours


it's night, so i didn't test this in all time periods. but i did plot data lines and bubbles.

i rewrote your rules and started over.
mine doesn't use any agregation.


Code:
#pre_ah_chg_label_0

#https://usethinkscript.com/threads/add-chart-label-for-pre-market-regular-session-afterhours.19766/
#Add Chart Label for Pre-Market/Regular Session & Afterhours

def na = double.nan;
def bn = barnumber();

def d = getday();
def newday = d != d[1];
def lastbar = (!isnan(close) and isnan(close[-1]));

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

#label1, (left), pre-normal

# Pre-market price then continue on into regular session,
#  with net change from previous day closing price of regular session,
#  along with the associated percentage change.

# Once the regular session closes I'd like this first label to become static,
#  and continue showing the current day closing price of the regular session, along with net change in price, and
#  percentage from close of previous day regular session.

#..premarket - close , chg prev day close, % chg
#..normal hours - close , chg prev day close, % chg
#..after hours - retain prev values

def daystart = 930;
def dayend = 1600;
def daytime = if secondsfromTime(daystart) >= 0 and secondstillTime(dayend) > 0 then 1 else 0;
def pre = secondstillTime(daystart) > 0;
def after = secondsfromTime(dayend) >= 0;

def pre_and_normal_close = if pre or daytime then close else pre_and_normal_close[1];
def prev_normal_close = if newday then pre_and_normal_close[1] else prev_normal_close[1];
def diff_from_prev = pre_and_normal_close - prev_normal_close;
def per_diff_prev = 100 * diff_from_prev / prev_normal_close;


addlabel(1, " " , color.black);
addlabel(1,
"Pre/Std: " + AsDollars(pre_and_normal_close) + " | Net Change: " + AsDollars(diff_from_prev) + " | % Change: " + Round( per_diff_prev, 2) + "%"
, (if diff_from_prev >= 0 then Color.GREEN else Color.LIGHT_RED));


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


#label2, (right) afterhours

# show current afterhours price,
#  with net change from end of current day regular session,
#  with the associated percentage change

# During Pre-market and Regular session I'd like to have the afterhours label blacked out.

#..premarket - blacked out
#..normal hours - blacked out
#..after hours - current afterhours price, $ chg from today close, % chg

def after_close = if after then close else after_close[1];
def diff_after_today = after_close - pre_and_normal_close;
def per_diff_after_today = 100 * diff_after_today/pre_and_normal_close;

addlabel(1, " " , color.black);
AddLabel(yes,
 "AH: " + AsDollars(after_close ) + " | Net Change: " + AsDollars(diff_after_today) + " | % Change: " + Round(per_diff_after_today, 2) + "%"
, (if pre or daytime then color.dark_gray else if diff_after_today >= 0 then Color.GREEN else Color.LIGHT_RED));
addlabel(1, " " , color.black);


#------------------------------
# test stuff

input test2_lines = no;
plot z1 = if test2_lines then pre_and_normal_close else na;
plot z2 = if test2_lines and prev_normal_close > 0 then prev_normal_close else na;
z2.SetStyle(Curve.MEDIUM_DASH);

def line_end1 = (!isnan(pre_and_normal_close) and isnan(pre_and_normal_close[-1]));
addchartbubble(test2_lines and line_end1, pre_and_normal_close , "today close", color.yellow, yes);

def line_end2 = (!isnan(prev_normal_close) and isnan(prev_normal_close[-1]));
addchartbubble(test2_lines and line_end2, prev_normal_close , "prev day close", color.yellow, yes);


input test1_bubble = no;
addchartbubble(test1_bubble, low,
 pre_and_normal_close + "\n" +
 prev_normal_close + " p\n" +
 diff_from_prev + " $\n" +
 per_diff_prev + " %"
, color.yellow, no);


input test2_bubble = no;
addchartbubble(test2_bubble, low,
 pre_and_normal_close + "\n" +
 after_close + "\n" +
 per_diff_after_today
, color.yellow, no);


# test the period times
input test3_vert = no;
addverticalline(test3_vert and daytime, "day", color.green);
addverticalline(test3_vert and pre, "pre", color.cyan);
addverticalline(test3_vert and after, "after", color.magenta);
#
Thanks for the encouragement and for taking a crack at it halcyonguy! I'm sorry that I went into too much detail in my initial post. I just didn't want to leave anything important out. I tried also placing a brief summary after my verbose explanation in the original post in case I had provided too much info.

I felt that the code I provided in my initial post definitely had too much in it. What got me started was input from Copilot. Copilot didn't get me to exactly where I wanted to be (but it gave me a good start) so I tried tweaking it to provide more differentiation in the definitions of the different sessions.

From reviewing your code I can tell that you are on track with what I'm trying to get to. Your code looks much cleaner than what I came up with. I can see why you felt it would be better to start over.

I have been using the OnDemand feature in thinkorswim to significantly speed up testing all three sessions. I used 9/25/2024 as the previous day and 9/26/2024 as the current day. I used NVDA stock for the testing.

********************************************

This is what I see from testing the code that you so kindly put together where I used the 9/25 & 9/26 dates for testing:


9/25/2024 normal hours close is 123.51

9/26/2024 pre-market random snapshot - label1 correctly reflects 126.67

when pre-market price is 126.67 on 9/26/2024 net chg should reflect +3.16 (+2.56%) based on 9/25/2024 normal hours close of 123.51 but revised label1 reflects +3.11 (+2.52%)

when normal hours random snapshot price (while normal session is open) on 9/26/2024 price on revised label1 accurately reflects 125.72. However, it should be reflecting a net change of +2.21 (+1.79%) but it reflects net change of +2.16 (+1.75%).

The closing normal hours price on 9/26/2024 was 124.04. However, the revised label1 reflects a closing price of 123.99. The net change from previous day close should be +0.53 (+0.43%) but is reflecting net change of +0.43 (+0.35%).

In after hours on 9/26/2024 after hours price is correctly reflected on label2 as 123.54. However, the change from 124.04 close of normal hours session on 9/26/2024 should be reflecting a net change of -0.50 (-0.40%) when it is reflecting a net change of -0.45 (-0.36%).

********************************************

In every case except one what appears to me to be the desired answer is always different by $0.05. When comparing the current day (9/26/2024) normal hours closing price with the previous day (9/25/2024) normal hours closing price it was different by $0.05 but the net change amount was off by an additional $0.05 resulting in the net change of that particular value to be off by a total of $0.10. It seems that what you've done is extremely close to what I am hoping for. Your code seems very well laid out. Do you have any thoughts on what would cause the differences?

Thanks again for your input on this.
Bob

EDIT: In my comments above I was only viewing intraday charts. Today (10/7/2024) I am looking at a daily chart of NVDA and see that label1 (during normal hours) Price is reflecting $0.00, net change is reflecting $0.00, and net change is reflecting "N/A%".

Label2 (afterhours) is currently reflecting Price of $129.23, Net Change of $129.23, and % Change that exceeds 100% by a significant amount.

Is there a way to address this in the same script or would a separate label script be needed for daily chart?
 
Last edited:
hi bob, and welcome
keep trying things and you will get there. it took me awhile to understand thinkscript.

you wrote many sentences, and i'm having to read them many times to try to figure out what you want.


learn how to look up functions and see what they do.
. GetTime() >=(1600)
this will always be true.
gettime() , is a huge number, +trillion.
the number of milliseconds since the epoch (January 1, 1970, 00:00:00 GMT).
https://toslc.thinkorswim.com/center/reference/thinkScript/Functions/Date---Time/GetTime

try adding something like this to see variable values. the \n starts a new line
def gt = gettime();
addchartbubble(1, low,
"gettime\n" +
gt
, color.yellow, yes);
i left deveral test codes in the study, at the end.


close(period = "DAY")
this only looks at normal trading hours, not after hours


it's night, so i didn't test this in all time periods. but i did plot data lines and bubbles.

i rewrote your rules and started over.
mine doesn't use any agregation.


Code:
#pre_ah_chg_label_0

#https://usethinkscript.com/threads/add-chart-label-for-pre-market-regular-session-afterhours.19766/
#Add Chart Label for Pre-Market/Regular Session & Afterhours

def na = double.nan;
def bn = barnumber();

def d = getday();
def newday = d != d[1];
def lastbar = (!isnan(close) and isnan(close[-1]));

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

#label1, (left), pre-normal

# Pre-market price then continue on into regular session,
#  with net change from previous day closing price of regular session,
#  along with the associated percentage change.

# Once the regular session closes I'd like this first label to become static,
#  and continue showing the current day closing price of the regular session, along with net change in price, and
#  percentage from close of previous day regular session.

#..premarket - close , chg prev day close, % chg
#..normal hours - close , chg prev day close, % chg
#..after hours - retain prev values

def daystart = 930;
def dayend = 1600;
def daytime = if secondsfromTime(daystart) >= 0 and secondstillTime(dayend) > 0 then 1 else 0;
def pre = secondstillTime(daystart) > 0;
def after = secondsfromTime(dayend) >= 0;

def pre_and_normal_close = if pre or daytime then close else pre_and_normal_close[1];
def prev_normal_close = if newday then pre_and_normal_close[1] else prev_normal_close[1];
def diff_from_prev = pre_and_normal_close - prev_normal_close;
def per_diff_prev = 100 * diff_from_prev / prev_normal_close;


addlabel(1, " " , color.black);
addlabel(1,
"Pre/Std: " + AsDollars(pre_and_normal_close) + " | Net Change: " + AsDollars(diff_from_prev) + " | % Change: " + Round( per_diff_prev, 2) + "%"
, (if diff_from_prev >= 0 then Color.GREEN else Color.LIGHT_RED));


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


#label2, (right) afterhours

# show current afterhours price,
#  with net change from end of current day regular session,
#  with the associated percentage change

# During Pre-market and Regular session I'd like to have the afterhours label blacked out.

#..premarket - blacked out
#..normal hours - blacked out
#..after hours - current afterhours price, $ chg from today close, % chg

def after_close = if after then close else after_close[1];
def diff_after_today = after_close - pre_and_normal_close;
def per_diff_after_today = 100 * diff_after_today/pre_and_normal_close;

addlabel(1, " " , color.black);
AddLabel(yes,
 "AH: " + AsDollars(after_close ) + " | Net Change: " + AsDollars(diff_after_today) + " | % Change: " + Round(per_diff_after_today, 2) + "%"
, (if pre or daytime then color.dark_gray else if diff_after_today >= 0 then Color.GREEN else Color.LIGHT_RED));
addlabel(1, " " , color.black);


#------------------------------
# test stuff

input test2_lines = no;
plot z1 = if test2_lines then pre_and_normal_close else na;
plot z2 = if test2_lines and prev_normal_close > 0 then prev_normal_close else na;
z2.SetStyle(Curve.MEDIUM_DASH);

def line_end1 = (!isnan(pre_and_normal_close) and isnan(pre_and_normal_close[-1]));
addchartbubble(test2_lines and line_end1, pre_and_normal_close , "today close", color.yellow, yes);

def line_end2 = (!isnan(prev_normal_close) and isnan(prev_normal_close[-1]));
addchartbubble(test2_lines and line_end2, prev_normal_close , "prev day close", color.yellow, yes);


input test1_bubble = no;
addchartbubble(test1_bubble, low,
 pre_and_normal_close + "\n" +
 prev_normal_close + " p\n" +
 diff_from_prev + " $\n" +
 per_diff_prev + " %"
, color.yellow, no);


input test2_bubble = no;
addchartbubble(test2_bubble, low,
 pre_and_normal_close + "\n" +
 after_close + "\n" +
 per_diff_after_today
, color.yellow, no);


# test the period times
input test3_vert = no;
addverticalline(test3_vert and daytime, "day", color.green);
addverticalline(test3_vert and pre, "pre", color.cyan);
addverticalline(test3_vert and after, "after", color.magenta);
#
I've been giving this a lot of thought. I like the way the chart label you've created looks and it functions infinitely better than what I created. I just can't seem to figure out why it reflects the closing price just a few cents off from the actual closing price (LLY, META, NVDA). The issue only seems to exist for stocks that trade especially high volume because I see the labels resulting from the script reflecting exactly correct for closing price for many/most stocks.

I don't understand because I can see that your code very precisely states the time.

1) Is there a way to express a more precise time than 1600 (like 1600.000 for example)? My guess is that this wouldn't help in any way even if it were possible to be more specific since 1600.000 is probably, from a functional standpoint, the same thing as specifying 1600.

2) Have you seen this occur before?

I would be interested in any thoughts that you have on this.

Thanks,
Bob
 
I've been giving this a lot of thought. I like the way the chart label you've created looks and it functions infinitely better than what I created. I just can't seem to figure out why it reflects the closing price just a few cents off from the actual closing price (LLY, META, NVDA). The issue only seems to exist for stocks that trade especially high volume because I see the labels resulting from the script reflecting exactly correct for closing price for many/most stocks.

I don't understand because I can see that your code very precisely states the time.

1) Is there a way to express a more precise time than 1600 (like 1600.000 for example)? My guess is that this wouldn't help in any way even if it were possible to be more specific since 1600.000 is probably, from a functional standpoint, the same thing as specifying 1600.

2) Have you seen this occur before?

I would be interested in any thoughts that you have on this.

Thanks,
Bob

Where are you getting your "actual closing price"
The price at the top of the chart shows Market Price (mid point of bid/ask)
and the chart right-axis and @halcyonguy's script shows Last price (last transacted price).

You can try adjusting your settings > equities> price type dropdown to get your "actual closing price".
 
Where are you getting your "actual closing price"
The price at the top of the chart shows Market Price (mid point of bid/ask)
and the chart right-axis and @halcyonguy's script shows Last price (last transacted price).

You can try adjusting your settings > equities> price type dropdown to get your "actual closing price".
Thanks for your response.

I'm after two chart labels (you can see an example of them on my 10/2 post above). I'd like the left chart label to reflect either current pre-market most recent trade price or most recent regular session trade price (for whichever of those two sessions are currently open). I'd like that same label to then reflect the last trade price for the regular session once the regular session closes for that day.

The price reflected at the top of the chart does reflect most recent trade price while regular session is open then once regular session has closed for the day it continues to reflect the last trade price for that regular session (along with the net change from previous day's close of the regular trading session). I would like the label on the left to reflect this same information except that I'd like that left label to INSTEAD reflect Pre-Market trade prices up until the regular trading session opens for the day.

For example, a few minutes ago NVDA was already trading in Pre-Market this morning and the label was correctly reflecting most recent Pre-Market trade price of $135.59 (while price at top of chart is correctly reflecting 134.80 from close of Friday's regular session. The label on the left is currently reflecting the Pre-market net change from Friday's regular session closing price to be +0.84, change of 0.62%. However, since NVDA closed Friday's regular session at 134.80 the net change in the left label should be reflecting as 0.79 (134.80 - 135.59 = 0.79) and not reflect 0.84. It's reflecting the correct last Pre-Market trade price of 135.59 but why is it calculating the net change as 0.84 when the correct net change amount is 0.79?

The other reason I'd like Regular session info to be reflected in the label is because the net change and net change percentages are MUCH easier to read in the label than they are at the top of the chart.

Similarly, when the Regular Session closes the left label is reflecting a Regular session closing price that is usually $0.05 less than the correct regular session closing price reflected at the top of the chart (that you referenced). This difference also causes the chart label's net change to be off by that $0.05 difference.

The second chart label (to the right of the left chart label) is to be blacked out until the regular session closes for that day at 4:00 PM ET. Once the regular trading session closes I'd like the Afterhours last trade price (along with net change from that day's regular trading session close) to be reflected. Then, at a glance, I can quickly determine how significant the afterhours market is from where the Regular session is. Apparently the Afterhours net change is being calculated using the same data that is being used to come up with the Regular session closing price causing the Afterhours net change to also be different than if it would be when using the correct Regular session closing price.

Halcyon guy has done some great work! It would be great if you and he could determine why the desired results are off just slightly.

Thanks Much,
Bob
 
hi bob, and welcome
keep trying things and you will get there. it took me awhile to understand thinkscript.

you wrote many sentences, and i'm having to read them many times to try to figure out what you want.


learn how to look up functions and see what they do.
. GetTime() >=(1600)
this will always be true.
gettime() , is a huge number, +trillion.
the number of milliseconds since the epoch (January 1, 1970, 00:00:00 GMT).
https://toslc.thinkorswim.com/center/reference/thinkScript/Functions/Date---Time/GetTime

try adding something like this to see variable values. the \n starts a new line
def gt = gettime();
addchartbubble(1, low,
"gettime\n" +
gt
, color.yellow, yes);
i left deveral test codes in the study, at the end.


close(period = "DAY")
this only looks at normal trading hours, not after hours


it's night, so i didn't test this in all time periods. but i did plot data lines and bubbles.

i rewrote your rules and started over.
mine doesn't use any agregation.


Code:
#pre_ah_chg_label_0

#https://usethinkscript.com/threads/add-chart-label-for-pre-market-regular-session-afterhours.19766/
#Add Chart Label for Pre-Market/Regular Session & Afterhours

def na = double.nan;
def bn = barnumber();

def d = getday();
def newday = d != d[1];
def lastbar = (!isnan(close) and isnan(close[-1]));

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

#label1, (left), pre-normal

# Pre-market price then continue on into regular session,
#  with net change from previous day closing price of regular session,
#  along with the associated percentage change.

# Once the regular session closes I'd like this first label to become static,
#  and continue showing the current day closing price of the regular session, along with net change in price, and
#  percentage from close of previous day regular session.

#..premarket - close , chg prev day close, % chg
#..normal hours - close , chg prev day close, % chg
#..after hours - retain prev values

def daystart = 930;
def dayend = 1600;
def daytime = if secondsfromTime(daystart) >= 0 and secondstillTime(dayend) > 0 then 1 else 0;
def pre = secondstillTime(daystart) > 0;
def after = secondsfromTime(dayend) >= 0;

def pre_and_normal_close = if pre or daytime then close else pre_and_normal_close[1];
def prev_normal_close = if newday then pre_and_normal_close[1] else prev_normal_close[1];
def diff_from_prev = pre_and_normal_close - prev_normal_close;
def per_diff_prev = 100 * diff_from_prev / prev_normal_close;


addlabel(1, " " , color.black);
addlabel(1,
"Pre/Std: " + AsDollars(pre_and_normal_close) + " | Net Change: " + AsDollars(diff_from_prev) + " | % Change: " + Round( per_diff_prev, 2) + "%"
, (if diff_from_prev >= 0 then Color.GREEN else Color.LIGHT_RED));


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


#label2, (right) afterhours

# show current afterhours price,
#  with net change from end of current day regular session,
#  with the associated percentage change

# During Pre-market and Regular session I'd like to have the afterhours label blacked out.

#..premarket - blacked out
#..normal hours - blacked out
#..after hours - current afterhours price, $ chg from today close, % chg

def after_close = if after then close else after_close[1];
def diff_after_today = after_close - pre_and_normal_close;
def per_diff_after_today = 100 * diff_after_today/pre_and_normal_close;

addlabel(1, " " , color.black);
AddLabel(yes,
 "AH: " + AsDollars(after_close ) + " | Net Change: " + AsDollars(diff_after_today) + " | % Change: " + Round(per_diff_after_today, 2) + "%"
, (if pre or daytime then color.dark_gray else if diff_after_today >= 0 then Color.GREEN else Color.LIGHT_RED));
addlabel(1, " " , color.black);


#------------------------------
# test stuff

input test2_lines = no;
plot z1 = if test2_lines then pre_and_normal_close else na;
plot z2 = if test2_lines and prev_normal_close > 0 then prev_normal_close else na;
z2.SetStyle(Curve.MEDIUM_DASH);

def line_end1 = (!isnan(pre_and_normal_close) and isnan(pre_and_normal_close[-1]));
addchartbubble(test2_lines and line_end1, pre_and_normal_close , "today close", color.yellow, yes);

def line_end2 = (!isnan(prev_normal_close) and isnan(prev_normal_close[-1]));
addchartbubble(test2_lines and line_end2, prev_normal_close , "prev day close", color.yellow, yes);


input test1_bubble = no;
addchartbubble(test1_bubble, low,
 pre_and_normal_close + "\n" +
 prev_normal_close + " p\n" +
 diff_from_prev + " $\n" +
 per_diff_prev + " %"
, color.yellow, no);


input test2_bubble = no;
addchartbubble(test2_bubble, low,
 pre_and_normal_close + "\n" +
 after_close + "\n" +
 per_diff_after_today
, color.yellow, no);


# test the period times
input test3_vert = no;
addverticalline(test3_vert and daytime, "day", color.green);
addverticalline(test3_vert and pre, "pre", color.cyan);
addverticalline(test3_vert and after, "after", color.magenta);
#
Hello @halcyonguy ,

I was just wondering if you could provide your thoughts on the chart labels displaying inconsistent results (mainly around closing price and net change reflected on chart label being different than last regular session price and net change reflected at top of chart)
hi bob, and welcome
keep trying things and you will get there. it took me awhile to understand thinkscript.

you wrote many sentences, and i'm having to read them many times to try to figure out what you want.


learn how to look up functions and see what they do.
. GetTime() >=(1600)
this will always be true.
gettime() , is a huge number, +trillion.
the number of milliseconds since the epoch (January 1, 1970, 00:00:00 GMT).
https://toslc.thinkorswim.com/center/reference/thinkScript/Functions/Date---Time/GetTime

try adding something like this to see variable values. the \n starts a new line
def gt = gettime();
addchartbubble(1, low,
"gettime\n" +
gt
, color.yellow, yes);
i left deveral test codes in the study, at the end.


close(period = "DAY")
this only looks at normal trading hours, not after hours


it's night, so i didn't test this in all time periods. but i did plot data lines and bubbles.

i rewrote your rules and started over.
mine doesn't use any agregation.


Code:
#pre_ah_chg_label_0

#https://usethinkscript.com/threads/add-chart-label-for-pre-market-regular-session-afterhours.19766/
#Add Chart Label for Pre-Market/Regular Session & Afterhours

def na = double.nan;
def bn = barnumber();

def d = getday();
def newday = d != d[1];
def lastbar = (!isnan(close) and isnan(close[-1]));

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

#label1, (left), pre-normal

# Pre-market price then continue on into regular session,
#  with net change from previous day closing price of regular session,
#  along with the associated percentage change.

# Once the regular session closes I'd like this first label to become static,
#  and continue showing the current day closing price of the regular session, along with net change in price, and
#  percentage from close of previous day regular session.

#..premarket - close , chg prev day close, % chg
#..normal hours - close , chg prev day close, % chg
#..after hours - retain prev values

def daystart = 930;
def dayend = 1600;
def daytime = if secondsfromTime(daystart) >= 0 and secondstillTime(dayend) > 0 then 1 else 0;
def pre = secondstillTime(daystart) > 0;
def after = secondsfromTime(dayend) >= 0;

def pre_and_normal_close = if pre or daytime then close else pre_and_normal_close[1];
def prev_normal_close = if newday then pre_and_normal_close[1] else prev_normal_close[1];
def diff_from_prev = pre_and_normal_close - prev_normal_close;
def per_diff_prev = 100 * diff_from_prev / prev_normal_close;


addlabel(1, " " , color.black);
addlabel(1,
"Pre/Std: " + AsDollars(pre_and_normal_close) + " | Net Change: " + AsDollars(diff_from_prev) + " | % Change: " + Round( per_diff_prev, 2) + "%"
, (if diff_from_prev >= 0 then Color.GREEN else Color.LIGHT_RED));


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


#label2, (right) afterhours

# show current afterhours price,
#  with net change from end of current day regular session,
#  with the associated percentage change

# During Pre-market and Regular session I'd like to have the afterhours label blacked out.

#..premarket - blacked out
#..normal hours - blacked out
#..after hours - current afterhours price, $ chg from today close, % chg

def after_close = if after then close else after_close[1];
def diff_after_today = after_close - pre_and_normal_close;
def per_diff_after_today = 100 * diff_after_today/pre_and_normal_close;

addlabel(1, " " , color.black);
AddLabel(yes,
 "AH: " + AsDollars(after_close ) + " | Net Change: " + AsDollars(diff_after_today) + " | % Change: " + Round(per_diff_after_today, 2) + "%"
, (if pre or daytime then color.dark_gray else if diff_after_today >= 0 then Color.GREEN else Color.LIGHT_RED));
addlabel(1, " " , color.black);


#------------------------------
# test stuff

input test2_lines = no;
plot z1 = if test2_lines then pre_and_normal_close else na;
plot z2 = if test2_lines and prev_normal_close > 0 then prev_normal_close else na;
z2.SetStyle(Curve.MEDIUM_DASH);

def line_end1 = (!isnan(pre_and_normal_close) and isnan(pre_and_normal_close[-1]));
addchartbubble(test2_lines and line_end1, pre_and_normal_close , "today close", color.yellow, yes);

def line_end2 = (!isnan(prev_normal_close) and isnan(prev_normal_close[-1]));
addchartbubble(test2_lines and line_end2, prev_normal_close , "prev day close", color.yellow, yes);


input test1_bubble = no;
addchartbubble(test1_bubble, low,
 pre_and_normal_close + "\n" +
 prev_normal_close + " p\n" +
 diff_from_prev + " $\n" +
 per_diff_prev + " %"
, color.yellow, no);


input test2_bubble = no;
addchartbubble(test2_bubble, low,
 pre_and_normal_close + "\n" +
 after_close + "\n" +
 per_diff_after_today
, color.yellow, no);


# test the period times
input test3_vert = no;
addverticalline(test3_vert and daytime, "day", color.green);
addverticalline(test3_vert and pre, "pre", color.cyan);
addverticalline(test3_vert and after, "after", color.magenta);
#
hi bob, and welcome
keep trying things and you will get there. it took me awhile to understand thinkscript.

you wrote many sentences, and i'm having to read them many times to try to figure out what you want.


learn how to look up functions and see what they do.
. GetTime() >=(1600)
this will always be true.
gettime() , is a huge number, +trillion.
the number of milliseconds since the epoch (January 1, 1970, 00:00:00 GMT).
https://toslc.thinkorswim.com/center/reference/thinkScript/Functions/Date---Time/GetTime

try adding something like this to see variable values. the \n starts a new line
def gt = gettime();
addchartbubble(1, low,
"gettime\n" +
gt
, color.yellow, yes);
i left deveral test codes in the study, at the end.


close(period = "DAY")
this only looks at normal trading hours, not after hours


it's night, so i didn't test this in all time periods. but i did plot data lines and bubbles.

i rewrote your rules and started over.
mine doesn't use any agregation.


Code:
#pre_ah_chg_label_0

#https://usethinkscript.com/threads/add-chart-label-for-pre-market-regular-session-afterhours.19766/
#Add Chart Label for Pre-Market/Regular Session & Afterhours

def na = double.nan;
def bn = barnumber();

def d = getday();
def newday = d != d[1];
def lastbar = (!isnan(close) and isnan(close[-1]));

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

#label1, (left), pre-normal

# Pre-market price then continue on into regular session,
#  with net change from previous day closing price of regular session,
#  along with the associated percentage change.

# Once the regular session closes I'd like this first label to become static,
#  and continue showing the current day closing price of the regular session, along with net change in price, and
#  percentage from close of previous day regular session.

#..premarket - close , chg prev day close, % chg
#..normal hours - close , chg prev day close, % chg
#..after hours - retain prev values

def daystart = 930;
def dayend = 1600;
def daytime = if secondsfromTime(daystart) >= 0 and secondstillTime(dayend) > 0 then 1 else 0;
def pre = secondstillTime(daystart) > 0;
def after = secondsfromTime(dayend) >= 0;

def pre_and_normal_close = if pre or daytime then close else pre_and_normal_close[1];
def prev_normal_close = if newday then pre_and_normal_close[1] else prev_normal_close[1];
def diff_from_prev = pre_and_normal_close - prev_normal_close;
def per_diff_prev = 100 * diff_from_prev / prev_normal_close;


addlabel(1, " " , color.black);
addlabel(1,
"Pre/Std: " + AsDollars(pre_and_normal_close) + " | Net Change: " + AsDollars(diff_from_prev) + " | % Change: " + Round( per_diff_prev, 2) + "%"
, (if diff_from_prev >= 0 then Color.GREEN else Color.LIGHT_RED));


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


#label2, (right) afterhours

# show current afterhours price,
#  with net change from end of current day regular session,
#  with the associated percentage change

# During Pre-market and Regular session I'd like to have the afterhours label blacked out.

#..premarket - blacked out
#..normal hours - blacked out
#..after hours - current afterhours price, $ chg from today close, % chg

def after_close = if after then close else after_close[1];
def diff_after_today = after_close - pre_and_normal_close;
def per_diff_after_today = 100 * diff_after_today/pre_and_normal_close;

addlabel(1, " " , color.black);
AddLabel(yes,
 "AH: " + AsDollars(after_close ) + " | Net Change: " + AsDollars(diff_after_today) + " | % Change: " + Round(per_diff_after_today, 2) + "%"
, (if pre or daytime then color.dark_gray else if diff_after_today >= 0 then Color.GREEN else Color.LIGHT_RED));
addlabel(1, " " , color.black);


#------------------------------
# test stuff

input test2_lines = no;
plot z1 = if test2_lines then pre_and_normal_close else na;
plot z2 = if test2_lines and prev_normal_close > 0 then prev_normal_close else na;
z2.SetStyle(Curve.MEDIUM_DASH);

def line_end1 = (!isnan(pre_and_normal_close) and isnan(pre_and_normal_close[-1]));
addchartbubble(test2_lines and line_end1, pre_and_normal_close , "today close", color.yellow, yes);

def line_end2 = (!isnan(prev_normal_close) and isnan(prev_normal_close[-1]));
addchartbubble(test2_lines and line_end2, prev_normal_close , "prev day close", color.yellow, yes);


input test1_bubble = no;
addchartbubble(test1_bubble, low,
 pre_and_normal_close + "\n" +
 prev_normal_close + " p\n" +
 diff_from_prev + " $\n" +
 per_diff_prev + " %"
, color.yellow, no);


input test2_bubble = no;
addchartbubble(test2_bubble, low,
 pre_and_normal_close + "\n" +
 after_close + "\n" +
 per_diff_after_today
, color.yellow, no);


# test the period times
input test3_vert = no;
addverticalline(test3_vert and daytime, "day", color.green);
addverticalline(test3_vert and pre, "pre", color.cyan);
addverticalline(test3_vert and after, "after", color.magenta);
#

@halcyonguy - thanks for what you were able to do on this effort! I was able to get the issue resolved and it is now working perfectly! No further effort needed.

Bob

 

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
163 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