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

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

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


  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;

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


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: