Building an Advanced Gap Scanner for ThinkorSwim?

D

DonDelMuerte

New member
@RobertPayne I appreciate your help with thinkscript on this thread! I have a question regarding building a robust pre-market scanner. Here is the premise of what I'm trying to achieve:
  • Returning premarket gap ups during the premarket hours
  • Returning close[1] to open gaps during all other hours
  • Write a script for the "quotes" column that displays this gap as well
I've run into several challenges while trying to code this. I'll lay them out here:

1. Determining Current Time
Right now I have several ways of doing this.
Code:
def isopen=if secondsFromTime(0930)>=0 and econdstillTime(1600)>=0 then 1 else 0;
def ispre=if secondsFromTime(0400)>=0 and secondstillTime(0930)>0 then 1 else 0;
Code:
def trading_duration = 6.5*60*60;
def seconds_market = secondsFromTime(0930);
def pre_duration = 5.5*60*60;
def seconds_pre = secondsFromTime(0400);
def isopen=seconds_market>=0 and seconds_market <= trading_duration;
def ispre=seconds_pre>=0 and seconds_pre <= pre_duration;
These methods are great, but they only work on aggregation periods less than or equal to 1 day - as per link. I have to use a recursive variable to store the previous day's close because ToS will not let me reference close(period=AggregationPeriod.DAY)[1] while using an overall period of less than 1 D in the stock hacker study scan setup. I'd really like to find a way to check current time vs open/pre market hours while using the daily timeframe because it would make the rest of the code much cleaner.

2. Storing Previous day Close
Code:
input marketClose = 1600;
def closeCounter = SecondsTillTime(marketClose);
def regSessEnd = closeCounter[-1] == 0;
rec priorDayClose = if regSessEnd then close else priorDayClose[1];
Again, not sure if there is a cleaner way to do this.

3. Determining the Gap
This is where the real problem arises for me - and it comes down to the difference between scripts for "custom quotes" and scripts for the scanner. The difference being that for the custom quotes I can specify an alternate aggregation period for the gap calculation as such:
Code:
def o = if isopen then open(period=AggregationPeriod.DAY) else if ispre then close else open(period=AggregationPeriod.Day);
def c = if isopen then close(period=AggregationPeriod.DAY)[1] else if ispre then priorDayClose else close(period=AggregationPeriod.DAY)[1];
plot x = 100*(l-c)/c;
*** This only works in the custom quotes script where I'm allowed a secondary aggregation period. If I use the same code in the scanner, the error is thrown:
Secondary period not allowed: Day.

Tl;DR
For the study scanner I must use the daily timeframe (or so I believe) for which I do not know how to determine current time vs open/pre market hours or even current premarket prices. Using a lower timeframe (1M for instance) allows me to determine the current time/market hours but forces me to use a secondary aggregation period - which is not allowed in the study scanner (or so I believe).

** Another Issue: I have no way of verifying my results unless I'm in the premarket. So I can only code and do trial and error for a limited time every day. Weird to me that we can't use these types of scanners in the OnDemand mode - makes the trial and error process a bit tougher. Is there a way to circumvent this? I'm relatively new to ToS!
 
RobertPayne

RobertPayne

Member
1. Determining Current Time
I'd really like to find a way to check current time vs open/pre market hours while using the daily timeframe because it would make the rest of the code much cleaner.
Impossible. The daily aggregation does not have access to pre / after market hours data---not on the charts and not in the scanner. You will have to use an intraday scan.

2. Storing Previous day Close
Again, not sure if there is a cleaner way to do this.
This is cleaner.
Ruby:
def yClose = if secondsfromTime(1600) == 0 then close else yclose[1]; #yesterday's close
3. Determining the Gap
*** This only works in the custom quotes script where I'm allowed a secondary aggregation period. If I use the same code in the scanner, the error is thrown:
Secondary period not allowed: Day.
See this post for a method of accessing yesterday's daily OHLC data on an intraday scan.
 
Top