 # Count how many crossovers within a session?

#### we29125

##### Member
Say i do a MovingAvg5 and MovingAvg10 on a 1min chart.

How do i calculate how many MA cross-up and MA cross-down in the whole trading session (0930 - 1600hr) ?

Addlabel on the chart - Num of Cross-up and Num of Cross-down

Last edited:

##### Well-known member
VIP
You would need to set up a counter... There are examples here in the forums... Pretty sure I just saw one while catching up on posts since last night over the past hour or so... Gotta do the legwork rather than just sitting back and asking for answers...

#### we29125

##### Member
Code:
``````input STime = 0930 ; #hint STime: Start of normal trading hours
input ETime = 1600 ; #hint ETime: End of normal trading hours

def ema = ExpAverage(close, 5);
def ema2 = ExpAverage(close, 10);

def above_ema = low > ema and ema > ema2 ;
def below_ema = high < ema and ema < ema2 ;

def IsActive = if secondsTillTime(ETime) > 0 and
secondsFromTime(STime) >= 0
then 1
else 0;

def above_ema_count= CompoundValue(1, if IsActive and above_ema then above_ema_count + 1 else 0, 0);
def above_ema_count= CompoundValue(1, if IsActive and below_ema then below_ema_count + 1 else 0, 0);

addlabel(yes, concat("above_ema_count: " + above_ema_count , "."));
addlabel(yes, concat("below_ema_count: " + below_ema_count , "."));``````

Can't work. Anyone can help ?

#### RickAns

##### Active member
On a quick glance your Addlabel above is missing a needed second '+' on the other side of your 'above_ema_count' variable. Should be like '+ above_ema_count +' . Same as with your below_ema_count variable.

#### chaser2009

##### New member
Hello everyone,

I was wondering if anyone could help me write a ThinkScript code that displays the number of up/down days when the 20 day SMA crosses up or down. For example, when the 20 day SMA cross up, I want the script to count how many down days there were before it. When the 20 day SMA crosses down, I want the script to count how many up days there were before it.

I hope my description wasn't too hard to get. I am a little new at this so any help is appreciated!

Thank you!

#### XeoNoX

##### Well-known member
VIP
Hello everyone,

I was wondering if anyone could help me write a ThinkScript code that displays the number of up/down days when the 20 day SMA crosses up or down. For example, when the 20 day SMA cross up, I want the script to count how many down days there were before it. When the 20 day SMA crosses down, I want the script to count how many up days there were before it.

I hope my description wasn't too hard to get. I am a little new at this so any help is appreciated!

Thank you!
crosses up and down from what? in order to cross anything you have to specify what it crosses.. define "crosses up"

#### chaser2009

##### New member
crosses up and down from what? in order to cross anything you have to specify what it crosses.. define "crosses up"
Thanks for getting back to me! When I say cross up or down, I mean when the 20days SMA changes direction. So say the SMA is positive for 10 days, then it switches to a down trend. When that switch happens, I want my code to count how many days passed before the SMA switched. So in this example, it will say 10 days. I hope this is clearer. If not maybe I can try to provide a picture?

#### XeoNoX

##### Well-known member
VIP
describe 20days SMA changes direction or is positive

as far as i know this is no negative or positive moving average in price unless the price goes negative as in the case of crude last year which is a very very very rare occurrence ... therefore prices moving average is 99.999999999% of times always positive.

#### XeoNoX

##### Well-known member
VIP
if you mean the amount of bars that the crossing is bullish or bearish here is the code for the count

Counts the number of bars since the specified moving averages last crossed
Code:
``````input EMA = 20;
input EMA2 = 50;
input price = close;
input displace = 0;
Plot AvgExp = ExpAverage(price[-displace], EMA);
Plot AvgExp2 = ExpAverage(price[-displace], EMA2);
def above =   (avgexp > avgexp2);
def below =   (avgexp < avgexp2);
def aboveCount =  if above then aboveCount + 1 else 0;
def belowcount =   if below then belowcount + 1 else 0;
def FinalCount = if above==1 then abovecount  else if below==1 then belowcount else 0;
def Both = avgexp > avgexp2 or avgexp < avgexp2;
def Total = CompoundValue(1, if both then total + 1 else 0, 0);
def prior = total-finalcount;
AddLabel (yes, "Bars Since  " + ema + "/" + ema2 + " Cross: " + FinalCount );
AddLabel (yes, "Prior Bars before " + ema + "/" + ema2 + " Cross: " + prior );``````

#### chaser2009

##### New member
if you mean the amount of bars that the crossing is bullish or bearish here is the code for the count

Counts the number of bars since the specified moving averages last crossed
Code:
``````input EMA = 20;
input EMA2 = 50;
input price = close;
input displace = 0;
Plot AvgExp = ExpAverage(price[-displace], EMA);
Plot AvgExp2 = ExpAverage(price[-displace], EMA2);
def above =   (avgexp > avgexp2);
def below =   (avgexp < avgexp2);
def aboveCount =  if above then aboveCount + 1 else 0;
def belowcount =   if below then belowcount + 1 else 0;
def FinalCount = if above==1 then abovecount  else if below==1 then belowcount else 0;
def Both = avgexp > avgexp2 or avgexp < avgexp2;
def Total = CompoundValue(1, if both then total + 1 else 0, 0);
def prior = total-finalcount;
AddLabel (yes, "Bars Since  " + ema + "/" + ema2 + " Cross: " + FinalCount );
AddLabel (yes, "Prior Bars before " + ema + "/" + ema2 + " Cross: " + prior );``````
It's what I want, Thank you very much!

#### XeoNoX

##### Well-known member
VIP
Say i do a MovingAvg5 and MovingAvg10 on a 1min chart.

How do i calculate how many MA cross-up and MA cross-down in the whole trading session (0930 - 1600hr) ?

Addlabel on the chart - Num of Cross-up and Num of Cross-down

heres what you requested.
if you want to to cound the down then just change
def var =avgexp crosses above avgexp2;
to
def var =avgexp crosses below avgexp2;

COUNT OF THE TOTAL NUMBER OF TIMES THE Moving Average Crosses above the Other Specified Moving Average on the SAME DAY
Code:
``````#( CUMULATIVE ) COUNT OF THE TOTAL NUMBER OF Moving Average Cross above the Other Specified Moving Average
# ON THE Same Day
# By XeoNoX via Usethinkscript.com
input EMA = 20;
input EMA2 = 50;
input price = close;
input displace = 0;
def AvgExp = ExpAverage(price[-displace], EMA);
def AvgExp2 = ExpAverage(price[-displace], EMA2);
def Active = GetDay() == GetLastDay(); #Today
def var =avgexp crosses above avgexp2;
def cumulative = if Active and !Active then var else if Active then cumulative + var else cumulative;
def scan  = cumulative;
AddLabel (yes, EMA +  "MA Crossed Above " + ema2 +  "MA  " +  (scan) + " Times Today" );``````

• ##### New member
heres what you requested.
if you want to to cound the down then just change
def var =avgexp crosses above avgexp2;
to
def var =avgexp crosses below avgexp2;

COUNT OF THE TOTAL NUMBER OF TIMES THE Moving Average Crosses above the Other Specified Moving Average on the SAME DAY
Code:
``````#( CUMULATIVE ) COUNT OF THE TOTAL NUMBER OF Moving Average Cross above the Other Specified Moving Average
# ON THE Same Day
# By XeoNoX via Usethinkscript.com
input EMA = 20;
input EMA2 = 50;
input price = close;
input displace = 0;
def AvgExp = ExpAverage(price[-displace], EMA);
def AvgExp2 = ExpAverage(price[-displace], EMA2);
def Active = GetDay() == GetLastDay(); #Today
def var =avgexp crosses above avgexp2;
def cumulative = if Active and !Active then var else if Active then cumulative + var else cumulative;
def scan  = cumulative;
AddLabel (yes, EMA +  "MA Crossed Above " + ema2 +  "MA  " +  (scan) + " Times Today" );``````
How would I get it so instead of the day it just goes to the first day on the chart? I want to use something like this to be apart of a bigger script, but I would want it to apply to any timeframe I look at, and just start the count at the beginning of it. I have a gap stats script that counts whether you do 1 year or 5. If you could just tell me what needs to be done to this script I would greatly appreciate it.