# Date arithmetic / calculation

#### H.G.

##### New member
Hi I would like to run a scanner with some time window (e.g. from 30 days ago to 20 days ago).

Usually I write it like this:

def endDay = 20;
def periodDays = 10;
def rsi = RSI()."RSI";
def isHigh = Sum(rsi crosses above 70, periodDays)[endDay] > 0;
plot Signal = isHigh;

Which basically start checking if RSI crosses above 70 starting from 20 days ago and going back to 10 days before it (i.e. from 30 days ago to 20 days ago from today).

However, I want to do something that is based on date, something like:

def endDay = (07/04/2021 - today); ---> which gives me roughly 20 days.

Can anyone help how to write this properly?
Thanks!

For example, this is another simpler scanner that I plan to write:

input endDate = 20210510;
def isHigh = close[d0] > open[d0];
plot Signal = isHigh;

But it throws an error: "Only constants expected here": d0

Basically, I'd like to see how many stocks had a positive day when SPY had a pullback on May 10th 2021.

I know another way to just go to google and ask how many work days have passed since that date. But I'm just wondering if there's any built-in script for this purpose.

Thank you!

@H.G. You cannot use a variable for an index... You must use a constant... close[1] works... close[var] is invalid...

Is there a way to convert an integer in a variable to a constant?
Thanks.

No, a constant is just that, constant... A variable cannot be converted to a constant because it is a variable...

You must remember that Thinkscript is more or less a rudimentary scripting language intended for paint charts without its primary focus being the manipulation of data... What little manipulation it can do is very basic at best... Thinkscript itself will never be as verbose as what some other trading platforms have and much of the reasoning behind that most likely is due to the parser itself... I have written parsers and they take a lot of time and effort... Time, effort, and money that TD-Ameritrade/Charles Schwab probably isn't interested in investing...

Thank you! I appreciate the confirmation that ToS cannot do this. Alas

Thank you! I appreciate the confirmation that ToS cannot do this. Alas

Both of your examples are solvable. Just not directly the way you're approaching them

You'd have to test it but I believe the rsi example can be written as:
Ruby:
``plot Signal = rsi[20] crosses above 70 within 10 bars;``
That's a lot less code than you had anyway.

For the May 10 pullback you just have to count the trading days yourself and hard code the index. Or you can use an input for the index. Inputs are constants.

87k+ Posts
350 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

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?