Adjust as necessary. I've set it to look at Previous Close vs This Close to determine whether it went up. You might prefer Open vs Close. As you see fit.
Code:
def lookback = 5;
def up_days = 3;
def is_up = if close > close[1] then 1 else 0;
plot eureka = if sum(is_up, lookback) == up_days then 1 else double.nan;
Note that this finds symbols which are up EXACTLY 3 of the last 5 days. You can replace == with >= if you want 3, 4, or 5 of the last 5 days. All we're really doing is creating an array with 1s and 0s for each period (Day?): a 1 if the day is up and a 0 if the day is not up. Then we sum the array over the last n days (lookback) and decide if it is equal to the number of up days you're looking for.
Thanks BenTen. So you can't think of any script that will allow you to search in the last X periods for symbols that are up Y times (where X would be 5 and Y would be 3 in the example I gave)?
I'm pretty sure I saw some code like that. If I find it, I will post it here.
Adjust as necessary. I've set it to look at Previous Close vs This Close to determine whether it went up. You might prefer Open vs Close. As you see fit.
Code:
def lookback = 5;
def up_days = 3;
def is_up = if close > close[1] then 1 else 0;
plot eureka = if sum(is_up, lookback) == up_days then 1 else double.nan;
Note that this finds symbols which are up EXACTLY 3 of the last 5 days. You can replace == with >= if you want 3, 4, or 5 of the last 5 days. All we're really doing is creating an array with 1s and 0s for each period (Day?): a 1 if the day is up and a 0 if the day is not up. Then we sum the array over the last n days (lookback) and decide if it is equal to the number of up days you're looking for.
Question: if today's close is below yesterday's, but the previous 4 days were up, will the symbol be plotted, or does today's close need to be up?
Wondering if I would I need to change the is_up variable assignment as follows which would check that at least 1 of the last 3 days were up (which is a requirement for 3 of the last 5 days to be up):
def is_up = if close > close[1] or close[1] > close[2] or close[2] > close[3] then 1 else 0;
Question: if today's close is below yesterday's, but the previous 4 days were up, will the symbol be plotted, or does today's close need to be up?
Wondering if I would I need to change the is_up variable assignment as follows which would check that at least 1 of the last 3 days were up (which is a requirement for 3 of the last 5 days to be up):
Also, is there a difference in output between these statements?
All the code looks at is whether x of the last n days were up days (close > previous close). It does not look at order or anything else. You don't need to do the long string of OR conditions. The sum does that for you pretty well.
If you want to add a condition that at least one of the last 3 days is up, I would do it this way:
Code:
def eureka = if sum(is_up, lookback) == up_days AND sum(is_up, 3) >= 1 then 1 else double.nan;
As always, adjust the logic to suit what you need. I chose >= for the second condition.
While your logic statement is essentially equivalent, it is a bit harder to understand at a glance and certainly more difficult to debug. I can't comment on whether it is faster or not.
You can even get into fun things like "Are there three up days in the 5 days preceding the most recent five days (not counting them)?" like this:
Code:
def eureka = if sum(is_up[5], lookback) == up_days then 1 else double.nan;
and other such fun things with sums of arrays. Note that it may actually be is_up[6]... I'm just typing at the browser not in the ThinkScript editor.
As to the difference between the two statements at the end of your post, if you are using it in a scanner, I prefer to have eureka return no value if there is not a match and a value if there is -- that's the logic behind the 1 vs double.nan. I really don't do scans very often and don't fully understand whether any numeric result would cause the inclusion of a symbol in the returns list or not. I do know that 1 vs NaN works as I expect it to.
Happy Trading, and if you have more questions do come back. We'll see what we can figure out
-mashume
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.
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.