GetDaysToExpiration() returns the WRONG number

KohPhiPhi

New member
I have created a very simple custom script that should display the number of days to expiration as a column in the option chain. I use the GetDaysToExpiration() function. The platform shows that as of today (Dec 15th 2023), there are 280 days left to the expiration Sep 20th 2024). Therefore, I was expecting the script to display "280" for all the strikes within that expiration date.

THE PROBLEM IS... the function is displaying "282" for most strikes, and sometimes it displays other RANDOM numbers for different strikes within the SAME expiration date!!! It's like the function GetDaysToExpiration() itself was broken.

Please see the two screenshots attached.

Any idea how I can get the accurate number of days to expiration? Thanks!

1702625804520.png

1702625814906.png
 
Solution
It's giving you the days to expiration from the last time the option actually traded, which is not necessarily today's date, and some options have never traded yet at all. The blank option data going forward, at least relative to the underlying, takes some coercion to be evaluated. Bear in mind, the solutions to these issues with custom options columns are generally unstable, and prone to periodic failure, N/A, and Loading...

TcgUuMW.png

Code:
def Under =
    Close(getunderlyingSymbol());
def Extra; def Fetch; def Days;
if !isNaN(close) and isNaN(close[-1]) {
    Extra =
        fold index = 0 to 49
        with data = 0
        while !isNaN(data)
        do
        if !IsNaN(GetValue(Under,-index)) then...
It's giving you the days to expiration from the last time the option actually traded, which is not necessarily today's date, and some options have never traded yet at all. The blank option data going forward, at least relative to the underlying, takes some coercion to be evaluated. Bear in mind, the solutions to these issues with custom options columns are generally unstable, and prone to periodic failure, N/A, and Loading...

TcgUuMW.png

Code:
def Under =
    Close(getunderlyingSymbol());
def Extra; def Fetch; def Days;
if !isNaN(close) and isNaN(close[-1]) {
    Extra =
        fold index = 0 to 49
        with data = 0
        while !isNaN(data)
        do
        if !IsNaN(GetValue(Under,-index)) then index
        else data;
    Fetch =
        GetValue(GetyYYYMMDD(),-Extra);
    Days =
        getdaysToExpiration() - daysTillDate(Fetch) - 1;  
} else {
    Extra = Extra[1];
    Fetch = Fetch[1];
    Days = Days[1];
}
plot Column =
    if Days > 10000 then Days - 19725 else days;
 
Last edited:
Solution

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

It's giving you the days to expiration from the last time the option actually traded, which is not necessarily today's date, and some options have never traded yet at all. The blank option data going forward, at least relative to the underlying, takes some coercion to be evaluated. Bear in mind, the solutions to these issues with custom options columns are generally unstable, and prone to periodic failure, N/A, and Loading...

Very useful post, thank you Joshua.

It's VERY strange that ThinkOrSwim doesn't have a simple function to retrieve the number of days left to expiration, forcing us to use patchy work arounds. I wonder why?

At any rate, thank you again!
 

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
396 Online
Create Post

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