Condition counter

SJP07

Member
I'd like a code that counts the number of times a condition is met. For example: How many times close crosses above the 9ema. Thank you in advance!
 
counters are fairly straight-forward:
Code:
input count_window_length = 100;
def condition = if close crosses above MovAvgExponential(length = 9) then 1 else 0;
def counter = sum(condition, count_window_length);
That is, they are straight forward if you set the condition to be 1 and 0 (for true and false). Then a simple sum over a range will tell you the count.
counts can also be done by incrementing a variable:
Code:
def condition = if close crosses above MovAvgExponential(length = 9) then 1 else 0;
def counter = if condition == 1 then counter[1] + 1 else counter[1];
This is, when the condition is 1 (true) we increment the last value of counter (counter[1]) by 1 otherwise we maintain the last value of counter.
Each approach has advantages and disadvantages. The first is very, very fast, and operates within a given window. The second is slower, as it runs over the length of your entire chart, and requires a comparison to be made at every bar. While performance is not always an issue with ThinkScript, you should be aware of the impacts of how you implement code.

Hope that helps,
-mashume
 

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

How would you count the number of times a conditon is met across timeframes? Example: Count the number of times MACD Crosses zero (only the most recent crosses) on the Daily, 3 Day and Weekly aggregations? Is this possible? Thanks.
 
Input lookbacklength = 30;

Def condition = x;

def cross_lookback = fold index1 = 1 to lookbacklength with x1 do x1 + condition[index1];

Will store the number of times condition was met within the length.
 
How would you count the number of times a conditon is met across timeframes? Example: Count the number of times MACD Crosses zero (only the most recent crosses) on the Daily, 3 Day and Weekly aggregations? Is this possible? Thanks.

here is another way to count true signals

Input lookbacklength = 30;
Def condition = x;
def cross_lookback = sum(condition, lookbacklength);


in order to count data from different aggregations, price data from different aggregations has to be used.
def z = close(period = AggregationPeriod.HOUR);
macd() doesn't have a price input parameter, so the original code would have to be copied and pasted 3 times, ( or use script() like sleepyz did) and modified to use 2nd agg price data.

https://tlc.thinkorswim.com/center/...ants/AggregationPeriod/AggregationPeriod-HOUR
 
Last edited:
I'm really only looking to count the most recent occurence of, say the MACD Crossing across aggregations. I want to know how many aggregations had a MACD crossing within an editable window of 2 or 3 (or more) bars. So I have 4 aggregations: 4 Hour, Daily, 3 Day and Weekly. How many MACD crossings occurred across the time frames (but only looking at the most recent crossing on each. I only need to count 1 event per aggregation. Is that doable? Thanks!
 
I'm really only looking to count the most recent occurence of, say the MACD Crossing across aggregations. I want to know how many aggregations had a MACD crossing within an editable window of 2 or 3 (or more) bars. So I have 4 aggregations: 4 Hour, Daily, 3 Day and Weekly. How many MACD crossings occurred across the time frames (but only looking at the most recent crossing on each. I only need to count 1 event per aggregation. Is that doable? Thanks!

This uses a script function to define the macd diff crossover.
Labels are added for each agg you requested with the agg changed within the script in the label.
An input for the lookbaclength is added which you can change as shown in the image.
The lookbacklength was set at 6 in the image to test the count to the arrows plotted by the macd crossover indicator.
Make sure that the main chart timeframe has more than 5 days displayed to count the weekly data.

Screenshot 2023-10-29 053407.png
Code:
script m {
    input aggregationperiod = AggregationPeriod.TWO_MIN;
    input lookbacklength = 3;
    input fastLength = 12;
    input slowLength = 26;
    input MACDLength = 9;

    input averageType = AverageType.EXPONENTIAL;

    def Value = MovingAverage(averageType, close(period = aggregationperiod), fastLength) - MovingAverage(averageType, close(period = aggregationperiod), slowLength);
    def Avg = MovingAverage(averageType, Value, MACDLength);

    def Diff = Value - Avg;

    def condition = Crosses(Diff, 0, CrossingDirection.ANY);
    plot cross_lookback = Sum(condition, lookbacklength);

}
input lookbacklength = 6;
AddLabel(1, "4hr: " + m(aggregationperiod = "FOUR_HOURS", lookbacklength = lookbacklength),color.white);
AddLabel(1, "1D: " + m(aggregationperiod = "DAY", lookbacklength = lookbacklength),color.white);
AddLabel(1, "3D: " + m(aggregationperiod = "THREE_DAYS", lookbacklength = lookbacklength),color.white);
AddLabel(1, "W: " + m(aggregationperiod = "WEEK", lookbacklength = lookbacklength),color.white);

input showvertical = yes;
def lastbar = highestall(if isnan(close[-1]) then barnumber() else 0);
addverticalLine(showvertical and barnumber() == lastbar - lookbacklength + 1);

#
 
This is great! Thank you @SleepyZ . My set up has the aggs horizontally, and I need to get a total of the MACD Crossings in the watchlist column. Can you suggest how I can count them using what you've built? Maybe keep a running count, using +1 for an Up crossing and -1 for a down crossing, then placing the total in the watchlist column? Then I can sort by that column to see potential calls/puts in order of probability. I don't rely on any one indicator but this will be useful.

There's a method to my madness. When there are a higher number of up (or down) crossings that happen near-simultaneously, the move lasts longer (I do swing trading). This is due to more support at the beginning of the price move.

I attached my chart so you can see what I mean. Notice the confluence of indicators across the 4 aggs. This is CAT on June 1st. It showed a great setup for a Call that day.

Thanks again for your help.

 

Attachments

  • CAT June 1.png
    CAT June 1.png
    811.5 KB · Views: 148
This is great! Thank you @SleepyZ . My set up has the aggs horizontally, and I need to get a total of the MACD Crossings in the watchlist column. Can you suggest how I can count them using what you've built? Maybe keep a running count, using +1 for an Up crossing and -1 for a down crossing, then placing the total in the watchlist column? Then I can sort by that column to see potential calls/puts in order of probability. I don't rely on any one indicator but this will be useful.

There's a method to my madness. When there are a higher number of up (or down) crossings that happen near-simultaneously, the move lasts longer (I do swing trading). This is due to more support at the beginning of the price move.

I attached my chart so you can see what I mean. Notice the confluence of indicators across the 4 aggs. This is CAT on June 1st. It showed a great setup for a Call that day.

Thanks again for your help.


Try this link that may have a watchlist setup with columns how you might want
https://tos.mx/JjANJiR

Screenshot 2023-10-29 092357.png
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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