Specifying time on ThinkOrSwim Scan

Variety_Jones

New member
VIP
I'm trying to set up a ORB Scan that is linked to a watchlist that will alert me. I only want to be alerted for breakouts or breakdowns that happen between 10am and 11am, however. Can anyone offer a helping hand into what I would need enter into the ThinkScript so that it only scans for crossovers (breakouts/breakdowns) between a certain timeframe?

Much appreciated.

This is what I'm working with right now that I'm so grateful to have gotten from this site.
Code:
script ORB_Mobile {
# ORB for TOS Mobile App
# Mobius
# V01.08.2018

def o = open;
def h = high;
def l = low;
def c = close;
def OpeningBell = getTime()[1] < RegularTradingStart(getYYYYMMDD()) and
                  getTime() > RegularTradingStart(getYYYYMMDD());
def RTH = getTime() >= RegularTradingStart(getYYYYMMDD()) and
          getTime() <= RegularTradingEnd(getYYYYMMDD());
def ORActive = getTime() >= OpeningBell and
               getTime() <= RegularTradingStart(getYYYYMMDD()) + 1800000;
def ORH = if OpeningBell
          then h
          else if ORActive and
                  h > ORH[1]
               then h
               else ORH[1];
def ORL = if OpeningBell
          then l
          else if ORActive and
                  l < ORL[1]
               then l
               else ORL[1];
plot ORhigh = if !ORActive and RTH
              then ORH
              else Double.NaN;
ORhigh.SetStyle(Curve.LONG_DASH);
ORhigh.SetLineWeight(3);
ORhigh.SetDefaultColor(Color.GREEN);
plot ORlow = if !ORActive and RTH
             then ORL
             else Double.NaN;
ORlow.SetStyle(Curve.LONG_DASH);
ORlow.SetLineWeight(3);
ORlow.SetDefaultColor(Color.RED);
def ORmeanActive = getTime() >= OpeningBell and
                   getTime() <= RegularTradingStart(getYYYYMMDD()) + 300000;
def ORmeanH = if OpeningBell
              then h
              else if ORmeanActive and h > ORmeanH[1]
                   then h
                   else ORmeanH[1];
def ORmeanL = if OpeningBell
              then l
              else if ORmeanActive and l < ORmeanL[1]
                   then l
                   else ORmeanL[1];
plot ORmean = if !ORmeanActive and RTH
              then Round(((ORmeanH + ORmeanL) / 2) / TickSize(), 0) * TickSize()
              else Double.NaN;
ORmean.SetStyle(Curve.LONG_DASH);
ORmean.SetLineWeight(3);
ORmean.SetDefaultColor(Color.YELLOW);
# End Code ORB for Mobile App
}

def ema = movavgexponential(close, 21);

#bullish
#plot breakthrough = close crosses above ORB_Mobile()."ORhigh" within 2 bars and (ORB_Mobile()."ORhigh" - ORB_Mobile()."ORlow")/ema < .025;

#bearish
plot breakdown = close crosses below ORB_Mobile()."ORlow" within 2 bars and (ORB_Mobile()."ORhigh" - ORB_Mobile()."ORlow")/ema < .025;
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

@Variety_Jones, I have cleaned up your code and converted that to a scan between the hours you specified. Remember to comment out one of the scan plot statements as the scanner only expects a single plot. I generally would not encapsulate a scan within a script() function and keep things simple.

Code:
# ORB for TOS Mobile App
# Mobius
# V01.08.2018

def o = open;
def h = high;
def l = low;
def c = close;
def OpeningBell = getTime()[1] < RegularTradingStart(getYYYYMMDD()) and
                  getTime() > RegularTradingStart(getYYYYMMDD());
def RTH = getTime() >= RegularTradingStart(getYYYYMMDD()) and
          getTime() <= RegularTradingEnd(getYYYYMMDD());
def ORActive = getTime() >= OpeningBell and
               getTime() <= RegularTradingStart(getYYYYMMDD()) + 1800000;
def ORH = if OpeningBell
          then h
          else if ORActive and
                  h > ORH[1]
               then h
               else ORH[1];
def ORL = if OpeningBell
          then l
          else if ORActive and
                  l < ORL[1]
               then l
               else ORL[1];
def ORhigh = if !ORActive and RTH
              then ORH
              else Double.NaN;
def ORlow = if !ORActive and RTH
             then ORL
             else Double.NaN;
def ORmeanActive = getTime() >= OpeningBell and
                   getTime() <= RegularTradingStart(getYYYYMMDD()) + 300000;
def ORmeanH = if OpeningBell
              then h
              else if ORmeanActive and h > ORmeanH[1]
                   then h
                   else ORmeanH[1];
def ORmeanL = if OpeningBell
              then l
              else if ORmeanActive and l < ORmeanL[1]
                   then l
                   else ORmeanL[1];
def ORmean = if !ORmeanActive and RTH
              then Round(((ORmeanH + ORmeanL) / 2) / TickSize(), 0) * TickSize()
              else Double.NaN;
def Active = secondsTillTime(1000) > 0 and secondsFromTime(1100) > 0;
def ema = ExpAverage(c, 21);
#
# Select ONLY one of the following scans and comment out the other one
#
plot BullBreakThrough = active and c crosses above ORhigh within 2 bars and (ORhigh - ORlow)/ema < .025;
#plot BearBreakDown =    active and c crosses below ORlow  within 2 bars and (ORhigh - ORlow)/ema < .025;
# End Code ORB for Mobile App

#09:37 Mobius: Since the introduction of GetTime(), watchlists and mobile applications seem to work more reliably using it rather than SecondsTillTime() or SecondsFromTime().

Code:
def Post = getTime() > RegularTradingEnd(getYYYYMMDD());
def Pre = getTime() < RegularTradingStart(getYYYYMMDD());
def Closed  = Post or Pre;
def DayClose = if getTime() crosses RegularTradingEnd(getYYYYMMDD())
               then close
               else DayClose[1];


def Change = Round((close - DayClose), 2);
def Percent = Round(((close - DayClose) / DayClose) * 100, 2);

plot Xtended = If(Closed, Percent, 0);

AssignBackgroundColor(Color.BLACK);
Xtended.AssignValueColor(if !Closed then Color.YELLOW else if Percent > 0 then Color.GREEN else if Percent < 0 then Color.RED else color.gray);
 
@tomsk Hmm it doesn't seem to get any results. I tried to also edit the script for the current timeframe " def Active = secondsTillTime(1230) > 0 and secondsFromTime(1530) " but still doesn't get anything and surely there are tickers that currently match the intended criteria.

This scan's primary purpose would be to find stocks who had 2 consecutive 5m candles close above or below the indicated ORB levels (high/low) between 10am and 11:00am EST. The main purpose would be to monitor a watchlist (be alerted) for when a ticker is added, but ideally, one could also search, later in the day, for stocks that matched the criteria between 10 and 11:00.

Is that possible? Or would a better solution be signifying which range of 5m candles should be scanned? I'm only aware of how to refer to candles based on the current candle i.e. how many candles ago, not how many candles from market open.

All help so far is greatly appreciated as well as any further insight. Definitely learning as I go.
 
@Variety_Jones, it appears that Mobius ORB for TOS Mobile App may not be designed for a scan.

Here's an alternative approach. Essentially capture the high during the time bracket you're interested in. In this case it is between 1000 - 1100.

Then once this time period passes, just scan whenever the last price > the identified high. I just ran this against the S&P500 on a 5 min aggregation and obtained 78 hits. You can construct a similar code structure for a breakdown scenario


Code:
# START CODE
def Active = SecondsFromTime(1000) > 0 and SecondsTillTime(1100) >= 0;
def BracketedHigh = if Active and !Active[1]
                 then high
                 else if Active and
                         high > BracketedHigh[1]
                       then high
                       else BracketedHigh[1];
plot Breakout = !Active and close > BracketedHigh;
# END CODE
 
Thank you. I notice that the SecondsFromTime and SecondsTillTime functions always return 1 on a daily chart. Is there an alternative set of functions that can monitor time on the daily timeframe?
 
Thank you. I notice that the SecondsFromTime and SecondsTillTime functions always return 1 on a daily chart. Is there an alternative set of functions that can monitor time on the daily timeframe?

No, there is no intraday time for the Daily timeframe... That is why there are intraday timeframes... You need to use the right tool for the job... And remember, you can display higher aggregation periods/timeframes on lower timeframe charts but not lower on higher... Therein lies the limitation that negates your request...
 
No, there is no intraday time for the Daily timeframe... That is why there are intraday timeframes... You need to use the right tool for the job... And remember, you can display higher aggregation periods/timeframes on lower timeframe charts but not lower on higher... Therein lies the limitation that negates your request...

Not for scans. You can’t reference the day aggregation within an intraday scan.
 
@tomsk Hmm it doesn't seem to get any results. I tried to also edit the script for the current timeframe " def Active = secondsTillTime(1230) > 0 and secondsFromTime(1530) " but still doesn't get anything and surely there are tickers that currently match the intended criteria.

This scan's primary purpose would be to find stocks who had 2 consecutive 5m candles close above or below the indicated ORB levels (high/low) between 10am and 11:00am EST. The main purpose would be to monitor a watchlist (be alerted) for when a ticker is added, but ideally, one could also search, later in the day, for stocks that matched the criteria between 10 and 11:00.

Is that possible? Or would a better solution be signifying which range of 5m candles should be scanned? I'm only aware of how to refer to candles based on the current candle i.e. how many candles ago, not how many candles from market open.

All help so far is greatly appreciated as well as any further insight. Definitely learning as I go.
The comparison for the ORB should be >= to 10AM (1800...)

def ORActive = getTime() >= OpeningBell and
getTime() >= RegularTradingStart(getYYYYMMDD()) + 1800000;
 
But when we use SecondsFromTime(930) on intraday, it never update until current bar close, so it's useless to use SecondsFromTime on any strategy. If I want to measure volume per seconds. for example... def VPS = TotalSum(volume)/SecondsFromTime(930); It never changes until bar close, useless! Is there other way to measure 'seconds' in real time?? Using GetTime() ???
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
445 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top