Add # days to date to get new date

mark.917

Member
Trying to take today's date and add either 1 or 3 days to it to get the resulting date. Possible?

Code:
def date = getYYYYMMDD();
def year = Round(date/10000, 0);
def month = Round((date % 10000) / 100, 0);
def day = (date % 100);
def dayofweek = getDayOfWeek(getYYYYMMDD());
def DTE = if dayofweek == 5 then 3 else 1;
def expiration_date = date + dte;   <=====
 
Solution
That particular function is only available when you're operating from an actual options price chart. The parameter requires the close of the underlying security. It doesn't work in reverse, it will always return NaN when called from the underlying.

Thinkscript can not access the implied volatility of an individual contract either. The associated function returns an average of implied volatilities over a series of options. The result is always just a weird rough approximation that's not accurate to the chain. The other options functions are then built upon this. In simple terms, none of them work correctly.

In regard to the price of the underlying at the time of the signal, there's no intrabarpersist declaration equivalent in...
There is no function to increment the date, if that's what you're asking. You basically have to script a little calendar of sorts and roll the date yourself. Then you can recompile the date, with leading zeros intact, using something like (Year * 10000) + (Month * 100) + Day.

You're doing something with SPX weeklies, if I am not mistaken. This type of script can get really messy and there's a lack of debug output in many areas. You might want to explain your overall end goal so I can give you a list of things to watch out for ahead of time.
 
Trying to take today's date and add either 1 or 3 days to it to get the resulting date. Possible?

Code:
def date = getYYYYMMDD();
def year = Round(date/10000, 0);
def month = Round((date % 10000) / 100, 0);
def day = (date % 100);
def dayofweek = getDayOfWeek(getYYYYMMDD());
def DTE = if dayofweek == 5 then 3 else 1;
def expiration_date = date + dte;   <=====


here is one way to find a future date.
enter how many days in future. draws a bubble on current day, with future date.
needs to have the future date visible on chart.
might have an issue if on 1 min chart and looking many days in future.

Code:
# date_in_x_days

#https://usethinkscript.com/threads/add-days-to-date-to-get-new-date.16553/
#Add # days to date to get new date
#Trying to take today's date and add either 1 or 3 days to it to get the resulting date. Possible?
#-----------------------------

def bn = BarNumber();
def na = Double.NaN;

def diffday = GetDay() != GetDay()[1];
def lastday = GetDay() == GetlastDay();

input days = 3;
def n = 1000;

def t;
if bn == 1 then {
 t = 0;
} else if lastday and diffday then {
 t = fold e = 1 to n
  with p
  while p < days
  do if getvalue(diffday, -e) and p + 1 == days then e
   else if getvalue(diffday, -e) then p + 1
   else p;
} else {
 t = t[1];
}

def m = getvalue(getmonth(), -t);
def dnum = getvalue(getday(), -t);
def d = dnum - getday() + 1;

input test1 = no;
addchartbubble(test1 and lastday and diffday, low,
t + " t\n" +
m + " m\n" +
dnum + " d#\n" + 
getday() + " d\n" +
d
, color.yellow, no);


input test2 = yes;
addchartbubble(test2 and lastday and diffday, low,
" in " + days + " days\n" +
"it will be\n" + 
m + "/" + d
, color.yellow, no);
#


this will find future fridays
https://usethinkscript.com/threads/...n-chain-data-via-thinkscript.3871/#post-42708
 
There is no function to increment the date, if that's what you're asking. You basically have to script a little calendar of sorts and roll the date yourself. Then you can recompile the date, with leading zeros intact, using something like (Year * 10000) + (Month * 100) + Day.

You're doing something with SPX weeklies, if I am not mistaken. This type of script can get really messy and there's a lack of debug output in many areas. You might want to explain your overall end goal so I can give you a list of things to watch out for ahead of time.
I always trade 1dte options so the option expiration date will always be 1 day (tomorrow) or 3 days (when trading on a Friday). The end goal is compile the option name using the correct date, ".SPY230905C450".
 
And once you have that, where does it go from there?

Like, for example, I once made a right side expansion area profile for 0DTE SPX option premium to visualize it against the price action. Other's have made similar profiles to mark price levels with high amounts of open interest. That sort of thing. Paint me the big picture.

Or, is this just a small exercise to learn the syntax? Perhaps I am over complicating it.
 
And once you have that, where does it go from there?

Like, for example, I once made a right side expansion area profile for 0DTE SPX option premium to visualize it against the price action. Other's have made similar profiles to mark price levels with high amounts of open interest. That sort of thing. Paint me the big picture.

Or, is this just a small exercise to learn the syntax? Perhaps I am over complicating it.
When the study (which runs on the underlying security) can determine the option that I am buying, (always a 1dte or 3dte that is one strike OOM and a .15 stop and .25 target) I hope to calculate, the relative prices for target and stop on the underlying.

Example, SPY option purchased at .82, stop is .67 and target is 1.07. On the underlying chart, these values may be to 452.60, stop 452.10, and target 453.15.

ToS already puts the stop and target levels on the chart so it is doing a calculation to convert from the option to those values but that part of the equation I am still working on as well.

https://usethinkscript.com/threads/option-target-and-stop-converted-to-underlying-price.16549/
 
I could be wrong, of course, but I believe the platform generates those lines with a full understanding of the valuation model, just like how it generates the risk graph on the analyze tab. Then it adjusts those lines relative to changes in all factors that contribute to option pricing, at least theoretically.

I highly doubt that this can be recreated from scratch in thinkscript, primarily due to inaccessible root data. Thinkscript has nowhere near the level of data access afforded to the base platform. It's not just a matter of understanding the mathematics, which is no easy task either.
 
Thanks for all the feedback Joshua.

I could be wrong but I think the option metric that I need is the Delta of the option which is available via a function call, but you need the complete option name in the call, which is what I am trying to build with the correct date. From there it is a matter of reverse engineering the option price from the price of the underlying at the time of the signal. Any option experts out there, feel free to tell me I'm crazy, wrong or both. ;)

None if this is critical for me but if it can be done then it would be a benefit.
 
That particular function is only available when you're operating from an actual options price chart. The parameter requires the close of the underlying security. It doesn't work in reverse, it will always return NaN when called from the underlying.

Thinkscript can not access the implied volatility of an individual contract either. The associated function returns an average of implied volatilities over a series of options. The result is always just a weird rough approximation that's not accurate to the chain. The other options functions are then built upon this. In simple terms, none of them work correctly.

In regard to the price of the underlying at the time of the signal, there's no intrabarpersist declaration equivalent in thinkscript. You can only retrieve the current price of the underlying in any given moment. This may or may not be a problem, kind of racking my brain, but I'll move on.

Here's how you increment the date anyway.
Code:
declare lower;
script Inc {
    input Date = 0;
    def Year = Floor(Date / 10000);
    def Month = Floor(Date % 10000 / 100);
    def Day = Floor(Date % 100);
    def LeapYear = # i think is right...?
        ((Year % 4 == 0) and (Year % 100 != 0)) or (Year % 400 == 0);
    def Days =
        if Month == 1 or Month == 3
        or Month == 5 or Month == 7
        or Month == 8 or Month == 10
        or Month == 12
        then 31
        else if Month == 2 then (if LeapYear then 29 else 28)
        else 30;
    def iDay; def iMonth; def iYear;
        if Month == 12 and Day >= 31 {
            iDay = 1;
            iMonth = 1;
            iYear = Year + 1;
        } else if Day >= Days {
            iDay = 1;
            iMonth = Month + 1;
            iYear = Year;
        } else {
            iDay = Day + 1;
            iMonth = Month;
            iYear = Year;
        }
    ;
    plot Inc = (iYear * 10000) + (iMonth * 100) + iDay;
}

AddLabel(Yes," Today: " + asprice( 
    GetYYYYMMDD()                  
) + " " ,color.blue);

AddLabel(Yes," next day: " + asprice(
    inc(GetYYYYMMDD())
) + " " ,color.blue);

AddLabel(Yes," two days: " + asprice(
    inc(inc(GetYYYYMMDD()))
) + " " ,color.blue);

AddLabel(Yes," three days: " + asprice(
    inc(inc(inc(GetYYYYMMDD())))
) + " " ,color.blue);
 
Solution
Thank you for sharing that! Such clean and neat coding, very impressed.

I will continue on my option journey keeping in mind everything you just said. It may be a dead end but I know I will gain a lot for exploring it.

Thanks again Joshua!
 

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