condition counter results

LetsMakeMoney

New member
VIP
I want to know how many times a stock has had a high that was 2% or more greater then the open. Here's my code:

def condition = if ((high - open)/open)*100 >= 2 then 1 else 0;
def counter = if condition == 1 then counter[1] + 1 else counter[1];

When I run this script on ANF, for example, I get a count of 163. But my chart is on a Monthly timeframe, the range is 8 years, so there's only 96 bars on the chart. Not understanding this counter result.
 
Solution
how did you know what count you had? you didn't post any output code.
next time, post the whole code. don't post partial code.

good catch on setting counter to 0 at bar #1. i like to always do that.
i don't have an answer on why you see different numbers.

condition is 1 or 0, so you can use if condition then, don't need ==1

below is a better way, just in case a condition can exist on bar #1
this includes a bubble that shows the % and counter. if changes color when a condition exists.
if you list all the 'else if' lines separate, then it is easier to disable one of them and try something else.


Code:
def na = double.nan;
def bn = barnumber();

def per = round(((high - open)/open)*100,2);
input per_level = 2.0;
def condition = if...
I want to know how many times a stock has had a high that was 2% or more greater then the open. Here's my code:

def condition = if ((high - open)/open)*100 >= 2 then 1 else 0;
def counter = if condition == 1 then counter[1] + 1 else counter[1];

When I run this script on ANF, for example, I get a count of 163. But my chart is on a Monthly timeframe, the range is 8 years, so there's only 96 bars on the chart. Not understanding this counter result.
Running BarNumber() gives me the right number of bars (96). Since there's only one monthly high per month, how can there be a result that's greater then 96? Do I have to code something to make sure the counter starts off at 0?
 

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

Running BarNumber() gives me the right number of bars (96). Since there's only one monthly high per month, how can there be a result that's greater then 96? Do I have to code something to make sure the counter starts off at 0?
This worked, hard setting the counter to 0. Now the result is 86.
def condition = if ((high - open)/open)*100 >= 2 then 1 else 0;
def counter = if BarNumber() == 1 then 0 else if condition == 1 then counter[1] + 1 else counter[1];
 
how did you know what count you had? you didn't post any output code.
next time, post the whole code. don't post partial code.

good catch on setting counter to 0 at bar #1. i like to always do that.
i don't have an answer on why you see different numbers.

condition is 1 or 0, so you can use if condition then, don't need ==1

below is a better way, just in case a condition can exist on bar #1
this includes a bubble that shows the % and counter. if changes color when a condition exists.
if you list all the 'else if' lines separate, then it is easier to disable one of them and try something else.


Code:
def na = double.nan;
def bn = barnumber();

def per = round(((high - open)/open)*100,2);
input per_level = 2.0;
def condition = if per >= per_level then 1 else 0;

def counter =
 if bn == 1 and !condition then 0
 else if bn == 1 and condition then 1
 else if condition then counter[1] + 1
 else counter[1];

addlabel(1, counter, color.yellow);

addchartbubble(1, low, 
per + " %\n" +
counter
, (if condition then color.yellow else color.gray), no);
#
 
Solution

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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