How to specify time period in stock scan filter?

Mamba10

New member
I was wondering if it's possible to make a scan limited to a certain time period in the day. Specifically the first 15 mins after open. I want the scan to only output stocks that are exhibiting the filters I've set only at that exact time period in the day.

So I have a MTF MACD indicator that Hahn Tech publicly released. The histogram turns red or green if the Current, Mid, and Long term time frames are in conjunction. It took me some time to make a scan for it, but I got around TOS's "second aggregation not allowed". But I only want the scan to be limited to the first 15 mins of market open.

Could this thinkScript work, if I entered it correctly to the scan code? Or is there something better/easier?:

Code:
input OpenTime = 930;
input DurationMinutes = 15;
def durationSec = DurationMinutes * 60;
def secondsPassed = SecondsFromTime(OpenTime);
def within = if secondsPassed >= 0 and secondsPassed <= durationSec then close else  Double.NaN;
 

tomsk

Well-known member
VIP
Here is a GENERIC scan with a time bracket for the first 15 minutes of the trading day
Describe your scan condition via the variable "condition"
Here's the snippet which evaluates a boolean value

Code:
def condition = <DESCRIBE_YOUR_SCAN_CONDITION_HERE>

def Active = secondsTillTime(0930) > 0 and secondsFromTime(0945) > 0;

plot scan = condition and Active;
 

Mamba10

New member
Here is a GENERIC scan with a time bracket for the first 15 minutes of the trading day
Describe your scan condition via the variable "condition"
Here's the snippet which evaluates a boolean value

def condition = <DESCRIBE_YOUR_SCAN_CONDITION_HERE>
def Active = secondsTillTime(0930) > 0 and secondsFromTime(0945) > 0;
plot scan = condition and Active;

Thanks!!
 

msammons

Member
I tried the above code but there were no executions. I am trying to get trades to execute only from 6:00 am EST to 8:00 pm EST (regular day + normal extended hours). Lots of executions with this code but it does not eliminate the executions outside the extended hours (during backtesting). And when trading SPY thinkorswim extended hours are apparently 24/5.

Can anyone help? where is my code wrong? (or perhaps there is an easier way to restrict trades to only reg hours plus normal extended hours (8 am - 8 pm EST).

Code:
def x = ExpAverage(close, 30);
def y = ExpAverage(close,10);

input DayTimeStart=0600; # 8AM EST
input NightTimeEnd=1959; # 8PM EST

def CountfromStart=SecondsfromTime(DayTimeStart);
def CounttilEnd= Secondstilltime(NightTimeEnd);

def dayperiod=If(CountfromStart >0 && CounttilEnd >0,1,-1); # then 1 else 0;

def long = y crosses above x and dayperiod==1;
def short = y crosses below x and dayperiod == 1;

plot a = long;
plot b = short;

AddOrder(OrderType.BUY_TO_OPEN, long, open[-1], 100);
AddOrder(OrderType.SELL_TO_OPEN, short, open[-1], 100);
 

rovo

Member
VIP
Is it possible to scan for a specific time range, ie 3-4pm?

Currently I know how to setup my scanner to scan on the hourly timeframe, and I can designate how many bars back to include, but this isn't reliable as there are sometime varying number of bars depending on the stock.

I'd like to be able to scan the previous 3-4pm block of time whether it's 8pm or 11pm, and not worry about how many bars to include.
 

Benjaminsabe

New member
Is it possible to scan for a specific time range, ie 3-4pm?

Currently I know how to setup my scanner to scan on the hourly timeframe, and I can designate how many bars back to include, but this isn't reliable as there are sometime varying number of bars depending on the stock.

I'd like to be able to scan the previous 3-4pm block of time whether it's 8pm or 11pm, and not worry about how many bars to include.
Hey! Did you figure out how to do it? Been searching for it but havent been able to find anything
 

Similar threads

Top