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