Posts Tagged ‘thinkscript_tip’

Thinkscript Tip: Creating Alerts Using Thinkscript in Think or Swim

December 8, 2009

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!

Thinkscript Tip: Dealing With Added Bars On Charts

February 27, 2009

My Formatted Pivot Points Thinkscript was designed to dynamically hide faraway pivot levels based on where the price is currently. If you are down under S1, you don’t want to be looking at R3 all day long. The only problem was that whenever the current bar close would tick above or below the local pivot level, the respective hidden lines would appear and disappear on my chart, making the screen flash like something from YTMND. Easy fix, make the hiding depend on “close[1]” instead of “close”! Not so fast…

If you add extra bars to the right hand side of the chart (which I like to do), then “close[1]” IS NOT the prior bar! Neither is “close” the current bar. “Close” actually refers to the furthest right “bar” on the chart. If you added 5 bars, then “close” is the bar 5 periods into the future. Since this value doesn’t exsist yet, it is assigned the value N/A, and the value returned by calls to the “close” function are the last good value for close, which is the current bar closing price. Most of the time, this is fine since all you care about is the current bar anyway. But if you add bars, and then want to reference bars in the recent past, you won’t get the values you think you will. If I added 5 bars to the right, to get the bar one previous to the current one, I’d have to reference “close[6]”. But you can get around this by using this code:

#
# Define lastclose = last completed bar close:
#
rec lastclose=if isnan(close[-1]) then lastclose[1] else close;

Then you use “lastclose” in your code instead of “close”, and you will get the right values no matter how many extra bars you have on the right. I updated the Formatted Pivot Points code with this change, so no more seizures. You’re welcome.

Thinkscript Tip: Only If Market Is Open

February 13, 2009

When testing trading strategies for Think or Swim, you probably don’t want to enter positions in the pre- or post-market. However, you may have indicators (like the pivot points) that you want calculated on 24 hour charts. Here’s how to get around it:

def isopen=if secondsFromTime(0930)>=0 and secondstillTime(1600)>=0 then 1 else 0;

The function “secondsFromTime” is negative before the time specified, but positive afterthe chosen time. Conversely, “secondsTillTime” is positive before the specified time, and negative after. So if both are positive when you use the market open (0930) and market close times (1600) respectively, you know the market is open. If one or the other is negative, it’s not.

When you define a logic value as either a 0 or a 1, as we have with “isopen” above, then all you have to do to use it in a logic statement is to use:

def test=if isopen then X else Y;

If the value of “isopen” is 1, then “if isopen” returns “1” (i.e. True). If it’s 0, then “if isopen” returns “0” (i.e. False). Easy!