# Count Close xOvers

#### stickPC

##### New member
Guys,
could you find what exactly going wrong with my simple code. All I want is to count the number of times the close crossingOver 9ma.
For some reason the count not getting incremented with SUM() function and if i use TOTALSum() it counting all the crossovers in the chart.

here is the code:

def tt = ( close crosses above MovAvgExponential()."AvgExp" );
def ctr = if isNaN(tt) then ctr[1] else tt;
def TotCross = Sum(ctr) ;
AddChartBubble((tt ), high, ""+TotCross , Color.green, yes)

With Sum here is the chart:

with TOTAL SUM:

Last edited by a moderator:
Solution
yes but if u observe the first pic, after 1 again the count started at 1. But after earnings bubble, it did made correct increaments as 1,2,3,4.
This is my issue. But incase of TotalSUM(), it did make right increments (2nd pic) from 123 to 129.
Could you point me where i'm going wrong in case of SUM()!! ?
thx

you seem to be missing what joshua explained.
a sum( x , 12) will look back and add up the previous 12 values of a variable, not all the bars on a chart.
if you count the bars between the green bubbles, there are 13 bars, with a value of 0 for ctr.
that is why the count started over with 1, a couple of times.

Ruby:
``````#observations,
# the tt formula is referencing a study instead of a funtion.
#   the MovAvgExponential()...``````
Guys,
could you find what exactly going wrong with my simple code. All I want is to count the number of times the close crossingOver 9ma.
For some reason the count not getting incremented with SUM() function and if i use TOTALSum() it counting all the crossovers in the chart.
Sum, in this case, is defaulting to a length of 12, and only counting from 12 bars ago.

Last edited by a moderator:
Sum, in this case, is defaulting to a length of 12, and only counting from 12 bars ago.
yes but if u observe the first pic, after 1 again the count started at 1. But after earnings bubble, it did made correct increaments as 1,2,3,4.
This is my issue. But incase of TotalSUM(), it did make right increments (2nd pic) from 123 to 129.
Could you point me where i'm going wrong in case of SUM()!! ?
thx

yes but if u observe the first pic, after 1 again the count started at 1. But after earnings bubble, it did made correct increaments as 1,2,3,4.
This is my issue. But incase of TotalSUM(), it did make right increments (2nd pic) from 123 to 129.
Could you point me where i'm going wrong in case of SUM()!! ?
thx

you seem to be missing what joshua explained.
a sum( x , 12) will look back and add up the previous 12 values of a variable, not all the bars on a chart.
if you count the bars between the green bubbles, there are 13 bars, with a value of 0 for ctr.
that is why the count started over with 1, a couple of times.

Ruby:
``````#observations,
# the tt formula is referencing a study instead of a funtion.
#   the MovAvgExponential() study doesn't have any parameters, not sure what the default length is.
#     i didn't see a default listed on info page.
#   a study has commands to draw things on the chart.
#   a function calculates a number.
#   in your study, all you need is a number, so a function will work.

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

# i use this function when i want an average value
#  if i am going to use an average, i set it to a variable, so it can be used in multiple formulas.
input avg1_len = 9;
input avg1_type =  AverageType.EXPONENTIAL;
def ma1 = MovingAverage(avg1_type, close, avg1_len);

input show_average_line = yes;
plot avg = if show_average_line then ma1 else na;

# tt = 1 or 0, 1 when ma1 is crossed
def tt = ( close crosses above ma1 );

# increment ctr when tt = 1 , if no cross then it = the prev bar
# if you want to count occurances, you want to add a number to a variable.
def ctr = if bn == 1 then 0 else if tt then ctr[1]+1 else ctr[1];
#  without this check , if barnumber() == 1 then 0,
#   when the bar is #1 and it looks back 1 bar, it may cause an error that will cascade through all the bars.

AddChartBubble(tt, high, ctr , Color.green, yes);

addlabel(1, "total count of crossings: " + ctr, color.yellow);``````

https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Math---Trig/Sum
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Tech-Analysis/MovingAverage
https://tlc.thinkorswim.com/center/reference/thinkScript/Constants/AverageType

You don't seem to be satisfied with your results, but I don't understand what you actually want to be happening instead. Like, for example, are you just trying to count the number of crosses per day? What is your actual goal?

Thnks guys. halcyonguy thx for the code. I did see what i was doing wrong and fixed it. now its working.
Joshua.. No not intraday but on daily or weekly. The issue for me is I migrated from NinjaTrader which is more
"programmer friendly" than TOS. Just got my feet ***.

### Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
298 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• 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?