## Thinkscript Tip: Only If Market Is Open

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!

Tags: