• Get $40 off VIP by signing up for a free account! Sign Up

EMA & MACD crossovers

mii

Member
VIP
need a study that does the following
# the following conditions should be true for both the time periods
if aggregation period is 1 min and 5 min
then check for
if (EMA 12 cross down 20 ) and (MACD crossover down 0 line)
alert with sell label
if (EMA 12 cross above 20 ) and (MACD crossover above buy line)
alert with buy label

Ema 12 and Ema 20 crossovers
Macd zero line not buy line
 
Last edited by a moderator:
Solution
please not that crosses will just plot exact moment when it crosses and nothing after, so i will make the following two assumptions that instead of

crosses above, you meant crosses above or is above.
crosses below, you meant crosses below or is below

but if im wrong on my assumption then you can just take that section of the code out.

i also made the assumption you wanted no label if it was neither buy or sell.

Code:
declare lower;
input show_label = yes;
input price1 = close;
input length1 = 12;
input displace1 = 0;

def AvgExp1 = ExpAverage(price1[-displace1], length1);

input price2 = close;
input length2 = 20;
input displace2 = 0;
def AvgExp2 = ExpAverage(price2[-displace2], length2);

def emacross_below = if  AvgExp1 crosses below...
please not that crosses will just plot exact moment when it crosses and nothing after, so i will make the following two assumptions that instead of

crosses above, you meant crosses above or is above.
crosses below, you meant crosses below or is below

but if im wrong on my assumption then you can just take that section of the code out.

i also made the assumption you wanted no label if it was neither buy or sell.

Code:
declare lower;
input show_label = yes;
input price1 = close;
input length1 = 12;
input displace1 = 0;

def AvgExp1 = ExpAverage(price1[-displace1], length1);

input price2 = close;
input length2 = 20;
input displace2 = 0;
def AvgExp2 = ExpAverage(price2[-displace2], length2);

def emacross_below = if  AvgExp1 crosses below avgexp2 or AvgExp1 < avgexp2 then 1 else 0;
def emacross_above = if AvgExp1 crosses above avgexp2 or AvgExp1 >avgexp2 then 1 else 0;

input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;
input showBreakoutSignals = no;
def Value = MovingAverage(averageType, close, fastLength) - MovingAverage(averageType, close, slowLength);
def Avg = MovingAverage(averageType, Value, MACDLength);
def Diff = Value - Avg;
def ZeroLine = 0;
def UpSignal = if Diff crosses above ZeroLine or  Diff > ZeroLine then 1 else 0;
def DownSignal = if Diff crosses below ZeroLine or  Diff < ZeroLine then 1 else 0;

def sell = if emacross_below and DownSignal then 1 else 0 ;
def buy = if emacross_above and upsignal then 1 else 0;

AddLabel(sell,  (if show_label==1 then "Sell" else ""), Color.red);
AddLabel(buy,  (if show_label==1 then "Buy" else ""), Color.GREEN);
 
Solution

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

please not that crosses will just plot exact moment when it crosses and nothing after, so i will make the following two assumptions that instead of

crosses above, you meant crosses above or is above.
crosses below, you meant crosses below or is below

but if im wrong on my assumption then you can just take that section of the code out.

i also made the assumption you wanted no label if it was neither buy or sell.

Code:
declare lower;
input show_label = yes;
input price1 = close;
input length1 = 12;
input displace1 = 0;

def AvgExp1 = ExpAverage(price1[-displace1], length1);

input price2 = close;
input length2 = 20;
input displace2 = 0;
def AvgExp2 = ExpAverage(price2[-displace2], length2);

def emacross_below = if  AvgExp1 crosses below avgexp2 or AvgExp1 < avgexp2 then 1 else 0;
def emacross_above = if AvgExp1 crosses above avgexp2 or AvgExp1 >avgexp2 then 1 else 0;

input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;
input showBreakoutSignals = no;
def Value = MovingAverage(averageType, close, fastLength) - MovingAverage(averageType, close, slowLength);
def Avg = MovingAverage(averageType, Value, MACDLength);
def Diff = Value - Avg;
def ZeroLine = 0;
def UpSignal = if Diff crosses above ZeroLine or  Diff > ZeroLine then 1 else 0;
def DownSignal = if Diff crosses below ZeroLine or  Diff < ZeroLine then 1 else 0;

def sell = if emacross_below and DownSignal then 1 else 0 ;
def buy = if emacross_above and upsignal then 1 else 0;

AddLabel(sell,  (if show_label==1 then "Sell" else ""), Color.red);
AddLabel(buy,  (if show_label==1 then "Buy" else ""), Color.GREEN);
Thank you so much.. will test it out.
 
I modified the above code and testing it. i am also working on a scanner for this.
not able to convert to scanner, could anyone please help. thank you



declare lower.
input show_label = yes;
input price1 = close;
input length1 = 5;
input displace1 = 0;

def AvgExp1 = ExpAverage(price1[-displace1], length1);
input price2 = close;
input length2 = 10;
input displace2 = 0;
def AvgExp2 = ExpAverage(price2[-displace2], length2);

def emacross_below = if AvgExp1 crosses below avgexp2
# or AvgExp1 < avgexp2
then 1 else 0;

def emacross_above = if AvgExp1 crosses above avgexp2
# or AvgExp1 >avgexp2
then 1 else 0;



input fastLength = 12;

input slowLength = 26;

input MACDLength = 9;

input averageType = AverageType.EXPONENTIAL;

input showBreakoutSignals = no;

def Value = MovingAverage(averageType, close, fastLength) - MovingAverage(averageType, close, slowLength);

def Avg = MovingAverage(averageType, Value, MACDLength);

def Diff = Value - Avg;

def ZeroLine = 0;

def UpSignal = if value crosses above avg
# or Diff > ZeroLine
then 1 else 0;

def DownSignal = if value crosses below avg
#or Diff < ZeroLine
then 1 else 0;


AssignPriceColor(if
close crosses below ExpAverage(close, 50 ) and DownSignal==1 and avgExp1 < avgExp2
then Color.WHITE else Color.CURRENT);
AssignPriceColor(if
close crosses above ExpAverage(close, 50 ) and UpSignal==1 and avgExp1 > avgExp2
then Color.YELLOW else Color.CURRENT);
 
Caveat: I did not check for errors throughout your whole script.

But this error jumped out:
You can only have ONE AssignPriceColor statement per script
Change this:
AssignPriceColor(if
close crosses below ExpAverage(close, 50 ) and DownSignal==1 and avgExp1 < avgExp2
then Color.WHITE else Color.CURRENT);
AssignPriceColor(if
close crosses above ExpAverage(close, 50 ) and UpSignal==1 and avgExp1 > avgExp2
then Color.YELLOW else Color.CURRENT);

To this:
AssignPriceColor(
if close crosses below ExpAverage(close, 50 ) and DownSignal==1 and avgExp1 < avgExp2
then Color.WHITE else
if close crosses above ExpAverage(close, 50 ) and UpSignal==1 and avgExp1 > avgExp2
then Color.YELLOW else Color.CURRENT);

To create a scanner, add this code to the bottom of your study:
plot signal1 = close crosses below ExpAverage(close, 50 ) and DownSignal==1 and avgExp1 < avgExp2;
signal1.Hide();
plot signal2 = close crosses above ExpAverage(close, 50 ) and UpSignal==1 and avgExp1 > avgExp2;
signal2.Hide();

To set up the scan hacker to filter on the above conditions:
https://usethinkscript.com/threads/how-to-use-thinkorswim-stock-hacker-scans.284/
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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