Moving Average Cross Alert Study for Think or Swim

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

44 Responses to “Moving Average Cross Alert Study for Think or Swim”

  1. tim Says:

    Thank you! This is so helpful, especially since I am just learning TS ~ CHeers

  2. Phillip Says:

    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

  3. Prospectus Says:

    You change average() to expmovingavg() basically.

  4. Ken Says:

    I love the idea of triggering an alert. Would it be possible to make it text my cell phone too?

  5. Prospectus Says:

    If you use the marketwatch tab and have it email on the alert triggering, then yes it can text your phone.

  6. Simon Says:

    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.

  7. ben Says:

    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.

  8. Taylor Says:

    How do you import these?? Have no clue here!

  9. Prospectus Says:

    See this post:

    The Complete Beginner’s Guide to Using Custom Thinkscripts

  10. brydochi@gmail.com Says:

    “and” I also want volume greater than volumeavg. how do I add this to this alert?

  11. Frank Says:

    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)

  12. Rick Hadlock Says:

    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?

  13. Patrick from Ireland Says:

    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

  14. Christian Moriarty Says:

    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 ?

  15. carie Says:

    Hello I tried to open the file you posted in 2009 can’t open it about Moving average cross overs. Can you help

  16. Prospectus Says:

    Open it in any text editor like notepad. It’s a text file. You can import it directly into thinkorswim too.

    >

  17. Phil Says:

    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

  18. Prospectus Says:

    You go to the chart, click the edit studies icon. Next to the study name is a parchment scroll icon. That opens the editor.

    >

  19. Phil Says:

    I see it in the editor, but not sure how to change from 2 SMA to 3 EMA

  20. Prospectus Says:

    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?

    >

  21. Phil Says:

    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…

  22. Prospectus Says:

    How about an alert when it crosses either one?

  23. Phil Says:

    Yes that would be great

  24. Prospectus Says:

    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);

  25. Phil Says:

    Awesome ! Thank You!!!!

  26. Phil Says:

    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

  27. Prospectus Says:

    Yes I messed that up. Those should be ema1 == ema2[1] and ema1 == ema3[1]

  28. Phil Says:

    that was it! Works perfectly….Again , Thanks for your help!

  29. jodshi Says:

    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

  30. Ben Says:

    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.

  31. Prospectus Says:

    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);

  32. Ben Fallah Says:

    Look like same code as before! Can’t identify the change! Any changes?

  33. Prospectus Says:

    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;

  34. Ben Fallah Says:

    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!

  35. Ben Fallah Says:

    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.

  36. Ben Fallah Says:

    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!

  37. Gary West Says:

    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.

  38. Gary West Says:

    Multiple Hull crosses provide trend info, and when a strong trend should occur, and price targets, that i cannot get from anything else.

  39. Ben Fallah Says:

    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?

  40. Ben Fallah Says:

    Just search the web with “Modify the Hull Moving Average indicator” and you’ll see many code examples.

  41. Prospectus Says:

    WordPress hates html in the comments. I think you could type it out like www dot thedomain dot com slash thepage

    >

  42. Hamid Esnaashari Says:

    Thank you for your script, still I don’t know how can I change time frame , and basically in what time frame it works?

  43. Akshay Says:

    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.

  44. Lotus Says:

    Hi, is there a way I can add this to a column in a watchlist and a scan list?

Leave a comment