How to count the number of EMA crossovers in the past days?

thomas2004ch

New member
Hi,

I want to get the number of the EMA5 crosses above EMA20 in the past 10 days for example. Here is my script. But it outputs 0. There must be something wrong.

declare lower;

def MyEMA1 = expAverage(5);
def MyEMA2 = expAverage(15);

def CrossOver = MyEMA1 crosses above MyEMA2;

def MyCount = Sum(CrossOver, 10);

plot scan = MyCount;
 

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

Hi,

I want to get the number of the EMA5 crosses above EMA20 in the past 10 days for example. Here is my script. But it outputs 0. There must be something wrong.

your expAverage( ) code is wrong
you only put in 1 parameter, but the length is the 2nd parameter. your length numbers are being used as the first parameter, price levels. they are always the same, so no crosses.

if not using all the parameters in a function, should use the parameter names

def MyEMA1 = expAverage( data = close, length = avg_len1 );

https://toslc.thinkorswim.com/center/reference/thinkScript/Functions/Tech-Analysis/ExpAverage

side note, use more variables. this will allow you to change things . my first thought was the length was too short so i made it 50. nothing.
then i added average lines to the chart to verify what is happening.


Code:
declare lower;
input bars_back = 10;
input avg_len1 = 5;
input avg_len2 = 15;

def MyEMA1 = expAverage(close, avg_len1);
def MyEMA2 = expAverage(close, avg_len2);

def Crossup = MyEMA1 crosses above MyEMA2;
def upCount = Sum(Crossup, bars_back);
plot z = upCount;
#


---------------------------------------
average lines

Code:
# avgs3

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

#https://toslc.thinkorswim.com/center/reference/thinkScript/Constants/AverageType
#input avg1_type = AverageType.Simple;
#input avg1_type = AverageType.exponential;
#input avg1_type = AverageType.hull;
#input avg1_type = AverageType.weighted;
#input avg1_type = AverageType.wilders;

def data = close;
input avg1_type = AverageType.Simple;
input avg1_length = 22;
def avg1 = MovingAverage(avg1_type, data, avg1_length );

input avg2_type = AverageType.Simple;
input avg2_length = 55;
def avg2 = MovingAverage(avg2_type, data, avg2_length );

input avg3_type = AverageType.Simple;
input avg3_length = 99;
def avg3 = MovingAverage(avg3_type, data, avg3_length );

input show_avg1_line = yes;
input show_avg2_line = no;
input show_avg3_line = no;
plot zavg1 = if show_avg1_line then avg1 else na;
plot zavg2 = if show_avg2_line then avg2 else na;
plot zavg3 = if show_avg3_line then avg3 else na;

zavg1.SetDefaultColor(Color.cyan);
zavg1.setlineweight(1);
zavg1.hidebubble();

zavg2.SetDefaultColor(Color.yellow);
zavg2.setlineweight(1);
zavg2.hidebubble();

zavg3.SetDefaultColor(Color.magenta);
zavg3.setlineweight(1);
zavg3.hidebubble();
#
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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