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!


One Response to “Thinkscript Tip: Only If Market Is Open”

  1. Forex Steam Review Says:

    Your opinions on investing are really impressive, I’m going to send some
    of my readers to your web site.

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: