Specifying time on ThinkOrSwim Scan

V

Variety_Jones

New member
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;
 
T

tomsk

Active member
VIP
Warehouse
@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
 
V

Variety_Jones

New member
@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.
 
T

tomsk

Active member
VIP
Warehouse
@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


# 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
 

Top