Aggregation code works for some tickers and not for others

Zuper

New member
Hi Folks...

1) I'm new to thinkscript and wrote the following script (excerpt) to perform some calculations on after hours 'last trade price' and the 'end of day (EOD) closing price'. The code is only executed during the 4 PM to 8PM EST 'after hours' time frame.

2) The code is executed in a Watchlist Custom Column

3) The code to get the last trade price works fine and isn't included. Nor is the calculations code. That's because I'm trying to focus my question only on the code for getting the EOD closing price which works fine for some tickers and not for others. Weird.

4) So here is the code for the Watchlist custom column:

# START
# Use 1 minute time frame
# Include after hours = Yes
declare lower;
input aggregationPeriodMIN = AggregationPeriod.MIN;
input aggregationPeriodDAY = AggregationPeriod.DAY;

# last price and EOD close price
def lastPrice = close(period = aggregationPeriodMIN);
def myDailyClose = close(period = aggregationPeriodDay);

# Plot the daily close
plot theDailyClose = myDailyClose;

#END

5) Here is sample output for some random stock symbols:

1703031659772.png


6) So my question is... why is the 'Plot' statement displaying 'Not a Number' (NaN) for some stocks and in the other cases it is getting the correct EOD closing price for other stock symbols? This was run on 12/19/23 at 7:45 PM EST.

Thanks :)
 
Hi Folks...

1) I'm new to thinkscript and wrote the following script (excerpt) to perform some calculations on after hours 'last trade price' and the 'end of day (EOD) closing price'. The code is only executed during the 4 PM to 8PM EST 'after hours' time frame.

2) The code is executed in a Watchlist Custom Column

3) The code to get the last trade price works fine and isn't included. Nor is the calculations code. That's because I'm trying to focus my question only on the code for getting the EOD closing price which works fine for some tickers and not for others. Weird.

4) So here is the code for the Watchlist custom column:

# START
# Use 1 minute time frame
# Include after hours = Yes
declare lower;
input aggregationPeriodMIN = AggregationPeriod.MIN;
input aggregationPeriodDAY = AggregationPeriod.DAY;

# last price and EOD close price
def lastPrice = close(period = aggregationPeriodMIN);
def myDailyClose = close(period = aggregationPeriodDay);

# Plot the daily close
plot theDailyClose = myDailyClose;

#END

5) Here is sample output for some random stock symbols:

View attachment 20462

6) So my question is... why is the 'Plot' statement displaying 'Not a Number' (NaN) for some stocks and in the other cases it is getting the correct EOD closing price for other stock symbols? This was run on 12/19/23 at 7:45 PM EST.

Thanks :)

When using a 1m agg to display a Day agg, if there are any bars missing (usually no trades in 1m occur on lightly traded stock) then the Day agg will not plot (N/A). To fix the missing bars values we use a recursive (use last actual value) .
Code:
def c = if IsNaN(close(period = aggregationPeriodDAY)) then c[1] else close(period = aggregationPeriodDAY);

Then we reference def c in def myDailyClose = c; as used in your code.
Code:
# START
# Use 1 minute time frame
# Include after hours = Yes
declare lower;
input aggregationPeriodMIN = AggregationPeriod.MIN;
input aggregationPeriodDAY = AggregationPeriod.DAY;
def c = if IsNaN(close(period = aggregationPeriodDAY)) then c[1] else close(period = aggregationPeriodDAY);
# last price and EOD close price
def lastPrice = close(period = aggregationPeriodMIN);
def myDailyClose = c;

# Plot the daily close
plot theDailyClose = myDailyClose;
AddLabel(1, lastPrice + " " + myDailyClose);
#END

By referencing the previous day as c[1] in the above code, we get the missing values in your example on today's watchlist.
 
Hi Folks...

1) I'm new to thinkscript and wrote the following script (excerpt) to perform some calculations on after hours 'last trade price' and the 'end of day (EOD) closing price'. The code is only executed during the 4 PM to 8PM EST 'after hours' time frame.

2) The code is executed in a Watchlist Custom Column

3) The code to get the last trade price works fine and isn't included. Nor is the calculations code. That's because I'm trying to focus my question only on the code for getting the EOD closing price which works fine for some tickers and not for others. Weird.

4) So here is the code for the Watchlist custom column:

# START
# Use 1 minute time frame
# Include after hours = Yes
declare lower;
input aggregationPeriodMIN = AggregationPeriod.MIN;
input aggregationPeriodDAY = AggregationPeriod.DAY;

# last price and EOD close price
def lastPrice = close(period = aggregationPeriodMIN);
def myDailyClose = close(period = aggregationPeriodDay);

# Plot the daily close
plot theDailyClose = myDailyClose;

#END

5) Here is sample output for some random stock symbols:

View attachment 20462

6) So my question is... why is the 'Plot' statement displaying 'Not a Number' (NaN) for some stocks and in the other cases it is getting the correct EOD closing price for other stock symbols? This was run on 12/19/23 at 7:45 PM EST.

Thanks :)
When using a 1m agg to display a Day agg, if there are any bars missing (usually no trades in 1m occur on lightly traded stock) then the Day agg will not plot (N/A). To fix the missing bars values we use a recursive (use last actual value) .


Then we reference def c in def myDailyClose = c; as used in your code.


By referencing the previous day as c[1] in the above code, we get the missing values in your example on today's watchlist.
Gentlemen, if you wish to automate the changing of aggregations so that your script always uses the time you have set on your chart,

https://usethinkscript.com/threads/automating-aggregation-selection-for-thinkorswim.3183/#post-29405
 

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