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:
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...
 

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

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[1] > ema[1] and ema[1] > ema2[1] ;
def below_ema = high[1] < ema[1] and ema[1] < ema2[1] ;

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] + 1 else 0, 0);
def above_ema_count= CompoundValue(1, if IsActive and below_ema then below_ema_count[1] + 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 ?
 
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.
 
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!
 
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"
 
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?
 
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.
 
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] + 1 else 0;
def belowcount =   if below then belowcount[1] + 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] + 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 );
 
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] + 1 else 0;
def belowcount =   if below then belowcount[1] + 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] + 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!
 
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[1] then var else if Active then cumulative[1] + var else cumulative[1];
def scan  = cumulative;
AddLabel (yes, EMA +  "MA Crossed Above " + ema2 +  "MA  " +  (scan) + " Times Today" );
 
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[1] then var else if Active then cumulative[1] + var else cumulative[1];
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.
 
@Quinefine Trader The code you posted wouldn't be as easy to modify as the original code posted earlier in the topic... Using that code you would remove references to IsActive which is the limiting condition... That may or may not be fully functional code...
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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