Help with Counter script

Hello! I have been searching the forum today in search of a script that will count the number of times something has occurred during a specified time period.

After reading through dozens of threads, I could not find anything that parallels what I am looking for. I did play with a few scripts, but was unsuccessful in getting it do return what I need.

For example, I want to know how many times SPY had a volume candle above 500k at any point from pre-market open to 7:59pm est. Then from there, just have that number in a watchlist column.

Hope this makes sense, and any help would be appreciated!

Thanks!
 
Solution
you can do that something like this: (I'm typing this here in useToS, not in the code editor so there may be typos)
Code:
def start = 0000;
def end = 0800;
def volume_threshold = 500000;

def in_time_window = if secondsFromTime(start) > 0 and secondsTillTime(end) > 0 then 1 else 0;
def volume_above_threshold = if volume > volume_threshold then 1 else 0;

def counter = if in_time_window == 1 and volume_above_threshold == 1 then 1 else 0;
plot condition_met_count = sum(counter, 50);

This is all much easier if you don't need the time window, and I don't know how many bars would exist in your chart or column for the pre-market (this would be necessary for the length of the sum counter... if it is too short, you'll clip the front end of...
you can do that something like this: (I'm typing this here in useToS, not in the code editor so there may be typos)
Code:
def start = 0000;
def end = 0800;
def volume_threshold = 500000;

def in_time_window = if secondsFromTime(start) > 0 and secondsTillTime(end) > 0 then 1 else 0;
def volume_above_threshold = if volume > volume_threshold then 1 else 0;

def counter = if in_time_window == 1 and volume_above_threshold == 1 then 1 else 0;
plot condition_met_count = sum(counter, 50);

This is all much easier if you don't need the time window, and I don't know how many bars would exist in your chart or column for the pre-market (this would be necessary for the length of the sum counter... if it is too short, you'll clip the front end of the counter, and you'll see the number decrease over the day as less and less of the window is counted (as the chart moves forward)).

Perhaps this gets you going in the right direction. It's always better coding to be explicit (that's why I use 1 and 0 not just assumed false if not true kinds of things) and I always try to define my conditions as separate statements.

-mashume
 
Solution

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