Here’s a script that shows how to trigger an alert on a moving average crossover, but only after the bar that caused a crossover has completed. I have an arrow plotted intrabar if the averages cross, and it disappears if they uncross. This same thing can happen to alerts based on the crossover, giving you an alert that is later invalidated. The way around it is to have the alert look one bar back, so it won’t trigger until the first tick after a crossover bar is finished.
The script is called “MACrossover_w_AlertsSTUDY.ts”, and is free at my Google site under
“Released Thinkscript Studies”.
Tags: alerts, crossover, moving-averages, Thinkscript
February 17, 2010 at 1:25 pm
Thank you! This is so helpful, especially since I am just learning TS ~ CHeers
June 18, 2010 at 2:16 pm
hey great alert..i really appreciate the work you put in.. i was wondering if there was any way to change the sma to a ema..i use 2 emas for my crossover alert. im not a techy person so i would have no idea to do that. thx alot ,Phill
June 18, 2010 at 2:19 pm
You change average() to expmovingavg() basically.
June 20, 2010 at 5:18 am
I love the idea of triggering an alert. Would it be possible to make it text my cell phone too?
June 20, 2010 at 11:10 am
If you use the marketwatch tab and have it email on the alert triggering, then yes it can text your phone.
July 21, 2010 at 7:42 am
Nice work here. Thanks.
The email alert does not work on this alert. Has anyone tried it?
I understand the system is not setup for this functionality (custom indicator email alerts yet, well so TOS tell me.
September 21, 2010 at 1:44 pm
Hey is it possible to do the same thing but only wiht MACD. I would like to get a cell phone alert when the MACD goes bellow the zero line.
February 28, 2012 at 12:37 pm
How do you import these?? Have no clue here!
February 28, 2012 at 2:03 pm
See this post:
May 10, 2012 at 8:14 pm
“and” I also want volume greater than volumeavg. how do I add this to this alert?
November 18, 2012 at 10:57 pm
Very Nice Study with alert! I have the alert selected however I am not sure what parameters to set (Plot fastema or plot slowema or both)
February 25, 2015 at 10:58 am
Nice. How do I set the Alert? I want to be alerted when price crosses above its 10-day SMA. How are the Alerts given? Can I set this to come to cell phone?
November 13, 2015 at 12:20 pm
Hi Thank you got yr MA crossover study working except for notification to my email. The study is working fine in “Messages” located btw “Home screen” and “support chat” and sound also working perfectly
The email notification function is working fine in the create alert function e g the drop down function one gets while in charts to say create alert for price movement above or below a specific price.
So at a loss as to how to get email notification working within your study. Would appreciate help and TIA
Patrick
February 17, 2018 at 12:18 pm
hi awesome script!! but i use 2 simple moving average lines how do i change the ema line to simple ? Anyone know how to do this ?
December 10, 2018 at 1:41 pm
Hello I tried to open the file you posted in 2009 can’t open it about Moving average cross overs. Can you help
December 10, 2018 at 3:18 pm
Open it in any text editor like notepad. It’s a text file. You can import it directly into thinkorswim too.
>
January 2, 2019 at 7:54 pm
Hello, I installed the .ts file into TOS and its exactly what I wanted…..except I have 3 EMA’s (10,25,50) and need an alert for the 10 crossing the other two in either direction…..Can you tell me how to modify the .TS file…Thanks
January 2, 2019 at 7:59 pm
You go to the chart, click the edit studies icon. Next to the study name is a parchment scroll icon. That opens the editor.
>
January 2, 2019 at 8:53 pm
I see it in the editor, but not sure how to change from 2 SMA to 3 EMA
January 2, 2019 at 9:03 pm
This is super easy, but I need clarification on what you’re trying to alert to. It’s highly unlikely that the ema10 will cross over both the ema25 and the ema50 on the same bar, so you won’t get any signals. Do you mean that you want an alert when the ema10 crosses over either of the other two, or that you want an alert when it crosses over the second of the two?
>
January 2, 2019 at 9:11 pm
I see what you mean…..I need it to do both but you are correct it wont be at the same time, but also depending on whats going on…the 10 may cross either the 25 or 50 first…
January 2, 2019 at 9:16 pm
How about an alert when it crosses either one?
January 2, 2019 at 9:18 pm
Yes that would be great
January 2, 2019 at 10:04 pm
input EMAPeriod1 = 10;
input EMAPeriod2 = 25;
input EMAPeriod3 = 50;
input price = close;
def na = double.nan;
plot ema1 = ExpAverage(price, EMAPeriod1);
plot ema2 = ExpAverage(price, EMAPeriod2);
plot ema3 = ExpAverage(price, EMAPeriod3);
def crossover_1_2 = if ema1 > ema2 AND ema1[1] <= ema2[1] then 1 else 0;
def crossunder_1_2 = if ema1 = ema2[1] then 1 else 0;
def crossover_1_3 = if ema1 > ema3 AND ema1[1] <= ema3[1] then 1 else 0;
def crossunder_1_3 = if ema1 = ema3[1] then 1 else 0;
#Plot arrows
Plot up = if (crossover_1_2 or crossover_1_3) then low – tickSize() else na;
Plot down = if (crossunder_1_2 or crossunder_1_3) then high + tickSize() else na;
up.SetPaintingStrategy(paintingStrategy.ARROW_UP);
down.SetPaintingStrategy(paintingStrategy.ARROW_DOWN);
#Trigger alerts
alert(crossover_1_2[1], “Crossover_1_2”, Alert.Bar, Sound.Ding);
alert(crossunder_1_2[1], “Crossunder_1_2”, Alert.Bar, Sound.Ding);
alert(crossover_1_3[1], “Crossover_1_3”, Alert.Bar, Sound.Ding);
alert(crossunder_1_3[1], “Crossunder_1_3”, Alert.Bar, Sound.Ding);
January 2, 2019 at 10:10 pm
Awesome ! Thank You!!!!
January 3, 2019 at 10:46 am
Sorry to be a pain but…
it s saying Line 12 and 14 has a syntax error…2 equal signs?
Syntax error: A double equal sign == expected at 12:30
Invalid statement: def at 12:1
Syntax error: A double equal sign == expected at 14:30
Invalid statement: def at 14:1
def crossunder_1_2 = if ema1 = ema2[1] then 1 else 0;
def crossunder_1_3 = if ema1 = ema3[1] then 1 else 0;
Thanks for your help
January 3, 2019 at 10:48 am
Yes I messed that up. Those should be ema1 == ema2[1] and ema1 == ema3[1]
January 3, 2019 at 11:01 am
that was it! Works perfectly….Again , Thanks for your help!
February 7, 2019 at 10:57 am
Hello Prospectus, thank you for all you do for us. Are you able to help me make the script below thinkorswim compliant? Baiscally am trying to fetch stocks whose MA 9 is crossing the EMA 20 ie
show stocks where the ma(9) 3 days ago was below the ma(20)
show stocks where the ma(9) 1 days ago is above ma(20)
average volume above 1000000
Price is ABOVE 50
Thanks again.
Josh
May 30, 2020 at 12:26 am
I tried this, but it looks like I don’t get alert for cross_unders, if I use if ema1 == ema2[1] and ema1 == ema3[1]
If I put em1 < ema2[1], then I get alerts for every sell in each bar or tick!
I know this is old post, maybe someone will get notified.
May 30, 2020 at 8:37 am
Try this, let me know if it doesn’t work like you wanted:
input EMAPeriod1 = 10; input EMAPeriod2 = 25; input price = close; def na = double.nan;
plot ema1 = ExpAverage(price, EMAPeriod1); plot ema2 = ExpAverage(price, EMAPeriod2);
def crossover_1_2 = if ema1 > ema2 AND ema1[1] <= ema2[1] then 1 else 0; def crossunder_1_2 = if ema1 < ema2 AND ema1[1] >= ema2[1] then 1 else 0;
#Plot arrows Plot up = if (crossover_1_2) then low – tickSize() else na; Plot down = if (crossunder_1_2) then high + tickSize() else na; up.SetPaintingStrategy(paintingStrategy.ARROW_UP); down.SetPaintingStrategy(paintingStrategy.ARROW_DOWN);
#Trigger alerts alert(crossover_1_2[1], “Crossover_1_2”, Alert.Bar, Sound.Ding); alert(crossunder_1_2[1], “Crossunder_1_2”, Alert.Bar, Sound.Ding);
June 3, 2020 at 12:16 am
Look like same code as before! Can’t identify the change! Any changes?
June 3, 2020 at 5:38 am
WordPress ate some of my code. It thought the less than symbol was html code. I’ve fixed the previous comment, but these were the lines that changed:
def crossover_1_2 = if ema1 > ema2 AND ema1[1] <= ema2[1] then 1 else 0; def crossunder_1_2 = if ema1 < ema2 AND ema1[1] >= ema2[1] then 1 else 0;
June 10, 2020 at 9:51 pm
Thanks, It might work..I need to find where is the plot down arrow color, so I can match the alert. I let u know how it comes. fyi, I have 24×5 day ToS machine that has many charts/studies/scripts/filters/scanners that basically runs many operation/ code and I mixed few other codes together and basically sends me alerts,but 5d5m charts is too fast since I have tons of stocks ,amazing , like all of dow and nasdaq and many covidindex others, so now @ at 5d15m and alerts are well spaced, p/l working good and from memory I finally left them for good day trading and now using 4 and 8 days and in another one 5 and 10 days and 15 and 20day ema/sma mixed studies, but they are well synchornized and I verified them. This code here is great cause It gives me two more ema crossover tests/alerts and matches my sma alerts! I need to make sure my 2 cross_under alers are working well! I provide update in market action or 24hrs equities!
June 11, 2020 at 8:54 pm
Looks great..Now I get DeathX/sell strongsell1 strongsell2 and I have GoldenX/buy strongbuy1 and 2 alerts..thank you!
How do I check for volume or volality and insure an uptrend move? Sometimes I get buy signal, but they keep them down on purpose specially if they are due for earning afterhours..that way I don’t waste my time till later before market close I can pick them up! What’s the best way to make sure there is movement up on the stock while it crosses a buy signal. I think vol increase filter in studies ToS. Thanks for your help.
June 20, 2020 at 5:43 pm
I have RSI study at bottom of my chart..I like to add RSI filter to my alerts..I just want alerts for xover if RSI still in good range. How do I add that to the code? Thanks in advance!
August 4, 2020 at 10:50 am
Can this be used for Multiple Hull MA Crosses, As i use Hull Crosses for my trading strategy but would love to have something that provided Alerts also when i get certain Hull Crosses Like a 15hull crosses the 34hull. and a 34hull crosses and 89Hull, and there are more each providing different Info to me.
August 4, 2020 at 10:58 am
Multiple Hull crosses provide trend info, and when a strong trend should occur, and price targets, that i cannot get from anything else.
August 5, 2020 at 7:11 pm
Gary, if you are comfortable with coding and understanding and modifying them..then check this link, it has Hull MA indicator and alerts and perhaps alerts for X , i think I saw lower in the thread!
I can’t paste link here?
August 5, 2020 at 7:15 pm
Just search the web with “Modify the Hull Moving Average indicator” and you’ll see many code examples.
August 5, 2020 at 7:55 pm
WordPress hates html in the comments. I think you could type it out like www dot thedomain dot com slash thepage
>
December 16, 2020 at 11:59 am
Thank you for your script, still I don’t know how can I change time frame , and basically in what time frame it works?
December 23, 2020 at 4:25 am
Hello Prospectus, thank you for all you do for us. Are you able to help me make the script in thinkorswim. Baiscally am trying to fetch stocks whose SMA 9 is crossing the SMA 20,SMA 50 and SMA 200.
March 3, 2022 at 9:49 am
Hi, is there a way I can add this to a column in a watchlist and a scan list?