Thinkscript Tip: Creating Alerts Using Thinkscript in Think or Swim

The newest Think or Swim release brings us the ability to create alerts from Thinkscript code! Here’s how to use them.

The syntax of the “alert” function is as below:

alert(condition, text, alert type, sound);

The required arguments are Condition and Text. The other two are optional, but have defaults if you don’t supply them:

alert type default value: Alert.ONCE
sound default value: Sound.NoSound

Condition is the logical value that will trigger the alert, and should be calculated to either 1 or 0 (true or false).
Text is the text string that will appear in the alert window when it is triggered.
Alert Type tells the alert how often it can trigger. The different ‘alert type’ parameters are:

Alert.ONCE – alert can be triggered only once after adding study
Alert.BAR – alert can be triggered only once per bar
Alert.TICK – alert can be triggered after each tick

Sound tells the alert what sound to play (if any). The different ‘sound’ parameters are:

Sound.Bell,
Sound.Chimes,
Sound.Ding,
Sound.NoSound,
Sound.Ring

Now some examples! This code will call an alert once on every single bar:

alert(1,”hello”,alert.BAR);

If you wanted an alert on RSI(2)>80 for example, you could use

def condition=if RSIWilder(2)>80 then 1 else 0;
alert(condition,”text”,alert.BAR);

If you had a plot of the NYSE TICK and wanted an alert every single time that a tick comes across as above 1000 or below -1000 (and didn’t value your sanity), you could use

alert(close>1000,”High TICK!”,alert.TICK,sound.DING);
alert(close<-1000,"Low TICK!",alert.TICK,sound.DING);

A slightly less ADD version would be to alert only once per bar, like so:

alert(close>1000,”High TICK!”,alert.BAR,sound.DING);
alert(close<-1000,"Low TICK!",alert.BAR,sound.DING);

And finally, if you only wanted the first +1000 tick of the day, you could use

alert(close>1000,”High TICK!”,alert.ONCE,sound.DING);

If you have any cool uses of Thinkscript alerts, feel free to share in the comments!

Tags: , ,

80 Responses to “Thinkscript Tip: Creating Alerts Using Thinkscript in Think or Swim”

  1. M B Says:

    Is there a way to set up alarm for PPS study?
    reference pps.”BuySignal” does not return anything…
    Thanks!

  2. Prospectus Says:

    I tried and I couldn’t access the signal for PPS either. They’ve got it locked up tight. Sorry!

  3. Dan Robbins Says:

    The revised PPS study seems to be using the 4sma (using H/L/C for price type) and the 7sma (close) (the original was 6 and 8).

    I’ve got code to paint arrows for the crossovers, but it paints every crossover, not just the first one in each direction. Prospectus – can you code it to only paint for the initial crossover?

  4. Prospectus Says:

    Dan, I sure can. Send me an email.

  5. Dan Robbins Says:

    Arrggg. I went to review my code and compare it against the revised PPS but it looks like they did some more tweaking to it, so my code isn’t replicating it as closely as they did before. They did mention a few weeks back that PPS would be an available option in the new Prodigio, so lets see how that rollout goes…

    I like the PPS on the hourly and 4 hour FX charts, and on the 1600tick ES charts. Oh, and heck, it’s pretty nice on the daily charts with pivots set to Opt Exp (instead of monthly). Have fun!

  6. Kirill Says:

    Instead of
    def condition=if RSIWilder(2)>80 then 1 else 0;
    alert(condition,”text”,alert.BAR);
    you can just use
    def condition = RSIWilder(2) > 80;
    alert(yes, “text”, alert.BAR);

  7. Kirill Says:

    Sorry, correct code is
    def condition = RSIWilder(2) > 80;
    alert(condition, “text”, alert.BAR);

  8. Prospectus Says:

    Thanks, that’s a cool shortcut! If you really wanted to compress it, you could even write it in one line: alert(RSIWilder(2) > 80, “text”, alert.BAR);

  9. Kirill Says:

    I just want to notice, that there is no sense to use “if” for “condition” def, because it only produces redundant calculations.
    And there is sense to define this variable if you want to reuse this def with other functions (to reduce number of calculations):
    def condition = RSIWilder(2) > 80;
    alert(condition, “text”, alert.BAR);
    assignBackgroundColor(if condition then Color.Red else Color.CURRENT);
    Otherwise, it’s better to use your version in the last comment

  10. Kirill Says:

    I wanted to notice in my last comment, that there is no sense to use “if” for “condition” def, because it only produces redundant calculations.

    Also there is a case, when it would be better not to compress it. It’s reusing this def with other functions (to reduce number of calculations):
    def condition = RSIWilder(2) > 80;
    alert(condition, “text”, alert.BAR);
    assignBackgroundColor(if condition then Color.Red else Color.CURRENT);

    BTW, thanks for your useful posts.

  11. Prospectus Says:

    Exactly! Thanks for the comments.

  12. KumoBob Says:

    declare lower;
    def condition = RSIWilder(2) > 80;
    alert(condition, “text”, alert.BAR);
    plot data = close;

    What am I doing wrong?

    I get this error
    Invalid statement: alert at 3:1

  13. Prospectus Says:

    The problem is the quotation marks on “text”. You need to delete them and actually retype them inside of your thinkscript editor.

    This happens when you copy and paste from some WordPress blogs. That open quote is not really a quotation mark. It’s some other ASCII character that is visually rendered as a double quote in thinkscript editor, but the compiler doesn’t recognize it. This is why I release all my indicators in plain text files instead of in a blog post.

  14. HR Says:

    Great expertise and thanks for sharing it.

  15. HR Says:

    I would appreciate any input on how to ADD different sounds to TOS that can then be accessed on the ALERTS page instead of the default set provided by TOS. _____

  16. HR Says:

    I figured out how to add alternate sounds for TOS Alerts that are accessed at the TOS Alerts page by pointing to other personal “.wav” files. No further response needed. Thanks.

  17. shaheen Says:

    does any one know how to create an alert for a list of symbols…
    i created a custom colums and i want to be alerted if the value of the colums reach the condition
    .. called td cs but they dont really know much ??

  18. Prospectus Says:

    I’ll make a tutorial for it

  19. TickRick Says:

    Hey how did you add custom sound for TOS alert.sound???

  20. J Swanson Says:

    I was wondering how custom sounds are put in, as well (if possible). Any ideas?

  21. max Says:

    Prospectus, is there a way to add “% change from the opening price” when having a stock watch list. They have some options like “%change from previous days close”. I really appreciate it. Just found your blog and you are doing a ton of great work here. thank you

  22. oz Says:

    Hello,

    I would like to write a code for when 2 moving averages crossing each other ( fast one and slow One) and want to add an alert to this.

    Great Job with all these posts. Any help much appreciated.

    Thank you,

  23. Eric Says:

    Thanks for the instructions! They work like a charm.
    Problem, though. Can alerts from a strategy appear on ToS blackberry/ipod apps?

    When I go to the marketwatch tab and manually create an alert, when it gets triggered – this appears on my ipod/blackberry

    But when I have a strategy running on my ToS workstation and an alert is triggered, it does not appear on my blackberry/ipod.

    Is there a way to change this so I can receive alerts on my ipod/blackberry? Secondly, is it possible to have alerts sms’ed to a cell phone?

  24. Prospectus Says:

    I’m working on this.

  25. Tom Says:

    I too am trying to get alerts triggered in my studies to generate an email. Is that possible? Thanks.

  26. Prospectus Says:

    I’ve been in contact with the TOS developers, and they know we want the email alert. It’s a matter of time now, as they are understaffed on thinkscript devs.

  27. Clint Says:

    I would also love to get an sms when an alert triggers!!!!!

  28. Popeious Says:

    I saw that some figured out how to reference the ‘custom’ sounds… it’d be great if that information was shared here..

  29. wonderberger Says:

    I would appreciate seeing a real example of how to use local “.wav” files.
    this works:
    alert(close>1000,”High TICK!”,alert.TICK,sound.DING);

    this does not:
    alert(close>1000,”High TICK!”,alert.TICK,c:/aasound/ringin.wav);

  30. jcardo Says:

    I have created this study, it plots and sounds buy and sell PPS signals.

    # pps buy signal
    plot ppsBuy = pps().buySignal;
    ppsBuy.SetPaintingStrategy(PaintingStrategy.arrow_up);
    ppsBuy.AssignValueColor(color.DARK_GREEN);
    alert(ppsBuy, “PPS Buy Signal”,alert.BAR, sound.Ring);

    # pps sell signal
    plot ppsSell = pps().sellSignal;
    ppsSell.SetPaintingStrategy(PaintingStrategy.arrow_down);
    ppsSell.AssignValueColor(color.LIGHT_ORANGE);
    alert(ppsSell, “PPS Sell Signal”,alert.BAR, sound.Ring);

  31. KumoBob Says:

    # PPS_R1V1
    # R1V0 2010.11.17:11:27 jcardo
    # Thinkscript Tip: Creating Alerts Using Thinkscript in Think or Swim
    # https://readtheprospectus.wordpress.com/2009/12/08/thinkscript-tip-creating-alerts-using-thinkscript-in-think-or-swim/
    # R1V1 2010.11.17:11:30 KumoBob
    # Nice jacardo!
    # Added a space between arrows and candle
    # Added option to plot only today
    # Added Option to turn alerts On/Off
    # * Note * I was not able to create an AssignPriceColor option.

    Input AlertsOn = yes;
    Input ShowTodayOnly = yes;

    Def Today = if !ShowTodayOnly then 1 else if getday() == getLastDay() then 1 else 0;
    def space = average(high-low)/3;

    # pps buy signal
    plot ppsBuy = If !Today then Double.NaN else pps().buySignal-space;
    ppsBuy.SetPaintingStrategy(PaintingStrategy.arrow_up);
    ppsBuy.AssignValueColor(color.WHITE);
    alert(AlertsOn && ppsBuy, “PPS Buy Signal”,alert.BAR, sound.Ring);

    # pps sell signal
    plot ppsSell = If !Today then Double.NaN else pps().sellSignal+space;
    ppsSell.SetPaintingStrategy(PaintingStrategy.arrow_down);
    ppsSell.AssignValueColor(color.WHITE);
    alert(AlertsOn && ppsSell, “PPS Sell Signal”,alert.BAR, sound.Ring);

  32. KumoBob Says:

    jcardo,

    PPS is not listed in the ThinkOrSwim Resource Center.

    How did you figure out the Parameter names?

  33. Dan Robbins Says:

    Also, this bit on scanning using PPS is at the thinkscript group at yahoo:

    http://finance.dir.groups.yahoo.com/group/TOS_thinkscript/message/4829?threaded=1&p=8

  34. jcardo Says:

    I got the idea from:

    http://finance.dir.groups.yahoo.com/group/TOS_thinkscript/message/4829?threaded=1&p=8

  35. Joe Says:

    Here’s a way to get TOS to send you a text message when an alert is triggered. Have them send an e-mail to your phone number! Check it:
    http://20somethingfinance.com/how-to-send-text-messages-sms-via-email-for-free/

  36. Prospectus Says:

    Yes, that works for alerts from the alert tab. But there’s still no way to do it from a thinkscript chart. Not yet, anyway. I’ve told the TOS devs that we want that.

  37. cybertrader Says:

    Hi Prospectus,

    How to apply the alert created by thinkscript to a watchlist and send out email in real time?

    Thanks.

  38. Paul Says:

    Re this: would the alert show up on the alerts tab on the web version? I would be satisfied with that.
    Then I could run the web version in the background at work, and get an alert there. I can’t run the desktop because we have a firewall, but this way I could still see the alert even though I’m not home on the desktop version.
    Ditto, by the way, for the cell phone app. That might actually be better.

  39. dhabud Says:

    Any updates on alert. Can some one tell me how to create conditions like pps in prodigio? So far I am using persons pivot sma of 5 days to create pps like signal. But like prospectus said pps is little be more tweaked.

    Any suggestions.

    Any update on getting email or web alert for pps.

    Regards
    Dhabud

  40. Prospectus Says:

    No updates on email alerts yet

  41. Eric Says:

    any way to add an alert with a sound wav. with a recording that will let you know when “the market is open in 2 minutes” or “London is closing in 2 minutes” or “inventory announcement in 2 minutes” or “the trading day is over turn the computer off” at 2:30 for example and is not tied to a study. Thanks Eric

  42. Brian Says:

    Does TOS allow the use of custom sounds via the “alert” function, or are we limited to just the 5 “Sound” constants? I know that you can do custom alert sounds through the “Alerts” tab of the “MarketWatch” tab, but those custom sounds aren’t valid parameters in the “alert” function. Thanks for your help, Brian

  43. keyword2 Says:

    keyword2…

    […]Thinkscript Tip: Creating Alerts Using Thinkscript in Think or Swim « Read the Prospectus[…]…

  44. retrobeast Says:

    I have the same question Brian had.
    How can I create a custom alert for a custom study such as a MA crossover? I know you can set custom alerts under Market Watch but I do not think these work within a study added to a chart.
    I want more than Ding, Chime, Ring. I want to add the sound I want.
    Thanks
    p.s. please email if there is an answer
    sweatyburrito yahoo

  45. Jody Porter Says:

    Does anyone know how to write an alert for a new low?

  46. Prospectus Says:

    A new low in what context? New low for a bar, for the day, etc?

  47. فرفشة Says:

    it’s nice
    thank u

  48. roger456 Says:

    I’m using the Pro_MACDDivergencev3 indicator in TOS and would like to add a sound alert each time the arrow prints on my charts. Have tried to figure this out on my own but I need help. Thanks!

  49. Michael Says:

    I have a custom scan I run on TOS intraday, and I view the scan in TOS in realtime as a watchlist. However, I cannot figure out how to get an alert or sort when a stock on the list hits a new intraday high or intraday low. Theres about 30 stocks so its a pain to keep scrolling through throughout the day. Is there a way to do this via alerts?

  50. Alan Says:

    I have been trying to implement the syntax (alert.TICK) because I don’t want to set the alert again and again. Once I selected my studies under MarketWatch > Study Alert > Condition Wizard, I typed the words alert.Tick in the thinkScript Editior box but I always got an error. Could someone please show me the correct steps and what buttons / boxes to enter the syntax alert(condition, text, alert type, sound) ?

    I am currently trying the PaperMoney account (build 1824.13)

  51. Prospectus Says:

    What does the error say specifically?

  52. Alan Says:

    The error reads: “Invalid statement: .at 1:94”

    Is the built-in Alert under MarketWatch different from the Alert we are talking here? If it is, do I just copy and paste the syntax alert(condition, text, alert type, sound); at the end of my customs study to make the alert work? Thanks a lot!

  53. Prospectus Says:

    Email me your code so I can see what is on that line. Readtheprospectus at gmail.

    The other question, I don’t follow what you are asking. The alert functionality on the market watch tab is limited compared to that you can program in a Thinkscript chart.

  54. Alan Says:

    I finally figured it out: I put the syntax inside my custom studies instead of the MarketWatch Alert. It worked! Thanks prospectus for the help =)

  55. Stan Says:

    Is there a way to create an Alert just once and apply it to a watch list, rather than creating it over and over again for every single stock in the watch list? So each time I update my watch list, the alert will be automatically applied to the updated watch list.

  56. gsrogers1 Says:

    I second the question Stan asked, but looks like this forum is dead since no new posts since 10/19/12. Anyone out there?

  57. Prospectus Says:

    I’m here. I haven’t looked at alerts in a while. I need to go back and see if they’ve changed things to allow this.

    On Apr 4, 2013, at 11:27 AM, Read the Prospectus

  58. gsrogers1 Says:

    I want to set an alert in the implied volatility column I’ve put in the watch list. Seems like TOS would allow an alert that applies to every stock on the watch list without having to individually set each one. Thanks for looking into this.

  59. fpinero Says:

    Hello, running TOS in linux we have no sound when an order is filled. Does anyone know how can I use “alert” to play a sound when my order is filled?

    Thanks in advance.

  60. Mike Says:

    No but in return my question is why they cannot make custom sound choices available in the studies? All you get is ding, ring, beep, or bell. I want to add my own sound (in the study).

  61. Toby Says:

    Steps for generating Custom Audio Alerts, Text Messages, etc. (Mac users) from Alerts triggered in TOS.

    1. Create/Paste custom code with embedded Alert function that are conditional to your custom function.

    2. Activate TOS Email notifications via Email.

    3. Go to your Mail App settings on your Mac.

    4. Generate new Rule to run an “AppleScript” when you recieve an email from TOS alerts (alerts@thinkorswim.com)

    5. Select customized AppleScript to generate text message, activate select Song from your iTunes Library, activate Siri and make her tell you that an Alert has been triggered within TOS, etc. If your creating custom thinkscript criteria for generating alerts, then you should be able to handle AppleScript. It’s beyond easy with the syntax reading like a letter to your Grandmother than a comptuer programming language.

    6. Share your success stories.

  62. pema Says:

    hellooo just wanted ask …

    how to set up alert whenever arrow indicator shows up ???????

  63. pema Says:

    hellooo just wanted ask …

    how to set up alert whenever arrow indicator shows up ?????

  64. geodepe Says:

    Any email alert support yet?

  65. Cee Says:

    Hello group,

    Wondering if someone can help with this; I Need code to perform the TOS Push notification that is sent to TOS Mobile phone app, through the ThinkorSwim System. So I can place the script in my scan have that push notification as the signal sent to my phone. This notification has the push sigh logo, sound and it vibrates. It’s the same one sent when you create an alert through the Market Watch tab in thinkorswim; desktop

    I would just copy it but I can’t find the script on the system

    I’m willing to donate, If anyone can complete a working Script for this signal
    I’m at (the1stoven@yahoo.com)

    Thanks,
    Cee

  66. Rodrigo Says:

    Hi thinkscripters! Maybe you can help me with this… I have some experience creating my own studies and strategies in TOS. I try to add an alert to my new strategy but i figure that it only reproduces the sound the first time… There ir a way that the sound plays each time?

    Alert(diff crosses BELOW 0 AND DIFF2<0, ”Long!”, Alert.tick, Sound.Ring);

  67. Manny Says:

    Does anyone know how to set an alert to Ehlers simple decycler?
    I was looking to be alerted once the high is 6 pips or closer to the upperband and the opposite, 6 pips from the lower band.

  68. Prospectus Says:

    You could use the “referencen” keyword to get the Ehlers decycler high and low and then set your alert up from there. See here: http://tlc.thinkorswim.com/center/charting/thinkscript/reference/Reserved-Words/reference.html

  69. Lee Says:

    Can custom alert code be saved somewhere in TOS? I have a few different alerts but the window that pops up when you select Study for an alert only gives me 6 choices for custom alerts. I have a Word file where I copy and paste the alert code from then into the alert code window and go from there. Works but can be cumbersome. Would rather save the alert code as a file in TOS.

  70. Prospectus Says:

    I haven’t seen anywhere to do that unfortunately.

    >

  71. Joe Gonzalvez Says:

    does anyone know to write code for allowing me to:

    Condition 1. paint chart bar GREEN if macd(12,26,9) is >=50 AND Slow Stochastics (14,3) %D is >=50 AND RSI(7) is >= 50

    Condition 2.. paint chart bar RED if MACD (12,26,9) is <=50 AND Slow Stochastics (14,3) % D is <= 50 and RSI(7) is <=50

    Condition 3, paint chart bar black if none of the conditions 1 and 2 apply

  72. Joe Gonzalvez Says:

    correction: for condition 1…..paint chart bar GREEN if macd(12,26,9) fast line crosses above slow line

    correction for condition 2: paint chart bar RED if macd(12,26,9) fast line crosses below slow line

  73. Vish Says:

    I have quite a few scans i use on TOS platform. I save the scans and load them as watchlists on TOS platform or on my iPhone App to view dynamic results. What i am looking for is get these dynamic results emailed or text to my phone, since i m not in front of my computer all the time or not constantly checking my phone. So essentially if the scan is running in the background and if a ticker meets the scan criteria and pops up as a result, i need a text or email message to alert me instantly.

    Is this possible ?

  74. June Says:

    I have a few auto scans like Vish, and I looking for a way to accumulate the symbols in watchlists.

    How insert a Symbol (using thinkscript) to an existing watchlist in TOS.

    I guess if the symbols can be accumulated in respective watchlist, I should be able to review them on the phone when I login to the TOS app.

  75. ebizvision Says:

    Hi, I have the same request Brian and retrobeast has:
    How can I create a custom alert for a custom study such as a MA crossover? I know you can set custom alerts under Market Watch but I do not think these work within a study added to a chart.
    I want more than Ding, Chime, Ring. I want to add the sound I want.

    I have already uploaded 6 custom sounds to TOS, Which I could use for Alerts in MarketWatch. So how do I reference those sounds in my study code. Meaning instead of DING what should I put in this line
    Alert(pLow, “Stock Low Alert”, Alert.BAR, Sound.DING);

    Thanks

  76. Bart Says:

    Really cool feature, Thanks for explaining it!

    Since in the example you posted,
    alert(1,”hello”,alert.BAR);
    an alert is generated everytime a new bar paints, I would think that in the following example:
    alert(1,”hello”,alert.TICK);
    an alert would be generated every time a bar updated, so I would expect multiple alerts per bar, if the price moves several times per bar.

    But that’s not what I’m seeing. The two examples (alert.BAR, and alert.TICK) return exactly the same result: An alert is generated only when a new bar appears, and only once per bar (exactly when the clock advances to the next minute).

    Is my expectation of the TICK option behavior incorrect?
    Thank you in advance!

  77. Prospectus Says:

    That’s not what I would expect. Are you running one chart with both alert styles? I can see that the alert.bar might silence all other alerts for that bar. Try taking that out and just use alert.tick and see what happens.

    >

  78. Bart Says:

    I was running one chart with one alert style, but what I was doing wrong (apparently), was that I saved the alert signal inside of a strategy, not a study. Once I saved it inside of a study, it started acting exactly as I would expect it to. Thanks for helping me, I would have never thought of doing this if I wasn’t thinking about your suggestion. Cheers! Fantastic blog!

  79. Hilario Dsouza Says:

    Instead of a sound can the occurrence of the alert save it to the local drive (directly or indirectly). If someone knows how to write this thinkscript code, I don’t mind paying for it.

  80. Jeff Stroup Says:

    Doesn’t the chart have to be open for the study alert to trigger though? Also, the alert script can only be applied to one symbol at a time, right?

Leave a comment