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 d0 = CountTradingDays(endDate, GetYYYYMMDD());
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!
 

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

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

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.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
336 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